Skip to content

Commit

Permalink
Added layer name regex
Browse files Browse the repository at this point in the history
  • Loading branch information
CPKreu committed Mar 13, 2021
1 parent d152058 commit af0b8aa
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
13 changes: 13 additions & 0 deletions PixiEditor/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Linq;

namespace PixiEditor.Helpers
{
public static class StringExtensions
{
public static string Reverse(this string s)
{
return new string(s.Reverse<char>().ToArray());
}
}
}
57 changes: 56 additions & 1 deletion PixiEditor/Models/DataHolders/Document/Document.Layers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media.Imaging;
using GalaSoft.MvvmLight.Messaging;
using PixiEditor.Helpers;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.Enums;
using PixiEditor.Models.Layers;
Expand Down Expand Up @@ -96,11 +98,16 @@ public void AddNewLayer(string name, bool setAsActive = true)

public void AddNewLayer(string name, int width, int height, bool setAsActive = true)
{
Layers.Add(new Layer(name, width, height)
Layer layer;

Layers.Add(layer = new Layer(name, width, height)
{
MaxHeight = Height,
MaxWidth = Width
});

layer.Name = GetLayerSuffix(layer);

if (setAsActive)
{
SetMainActiveLayer(Layers.Count - 1);
Expand Down Expand Up @@ -437,6 +444,54 @@ private void RemoveLayerProcess(object[] parameters)
}
}

private string GetLayerSuffix(Layer layer)
{
int? highgestValue = null;

Regex reversedRegex = new (@"(?:\)([0-9]+)*\()? *([\s\S]+)", RegexOptions.Compiled);

Match match = reversedRegex.Match(layer.Name.Reverse());

foreach (Layer otherLayer in Layers)
{
if (otherLayer == layer)
{
continue;
}

Match otherMatch = reversedRegex.Match(otherLayer.Name.Reverse());

if (otherMatch.Groups[2].Value == match.Groups[2].Value)
{
bool sucess = int.TryParse(otherMatch.Groups[1].Value.Reverse(), out int number);

if (sucess)
{
if (highgestValue == null || highgestValue < number)
{
highgestValue = number;
}
}
else
{
if (highgestValue == null)
{
highgestValue = 0;
}
}
}
}

string actualName = match.Groups[2].Value.Reverse();

if (highgestValue == null)
{
return actualName;
}

return actualName + $" ({highgestValue + 1})";
}

private void RemoveLayersProcess(object[] parameters)
{
if (parameters != null && parameters.Length > 0 && parameters[0] is IEnumerable<Guid> layerGuids)
Expand Down

0 comments on commit af0b8aa

Please sign in to comment.