Automatic rename UIControl Items

See #213
This commit is contained in:
stefnotch
2021-02-19 12:55:22 +01:00
parent cc980fd70d
commit 3964fc7795
2 changed files with 18 additions and 7 deletions

View File

@@ -405,7 +405,13 @@ namespace FlaxEditor.CustomEditors.Dedicated
for (int i = 0; i < uiControls.Count; i++)
{
var uiControl = (UIControl)uiControls[i];
string previousName = uiControl.Control?.GetType()?.Name ?? typeof(UIControl).Name;
uiControl.Control = (Control)controlType.CreateInstance();
if (uiControl.Name.StartsWith(previousName))
{
string newName = controlType.Name + uiControl.Name.Substring(previousName.Length);
uiControl.Name = StringUtils.IncrementNameNumber(newName, x => uiControl.Parent.GetChild(x) == null);
}
}
}
}
@@ -414,7 +420,13 @@ namespace FlaxEditor.CustomEditors.Dedicated
for (int i = 0; i < uiControls.Count; i++)
{
var uiControl = (UIControl)uiControls[i];
string previousName = uiControl.Control?.GetType()?.Name ?? typeof(UIControl).Name;
uiControl.Control = (Control)controlType.CreateInstance();
if (uiControl.Name.StartsWith(previousName))
{
string newName = controlType.Name + uiControl.Name.Substring(previousName.Length);
uiControl.Name = StringUtils.IncrementNameNumber(newName, x => uiControl.Parent.GetChild(x) == null);
}
}
}

View File

@@ -244,8 +244,8 @@ namespace FlaxEngine
return new string(charArray);
}
private static readonly Regex IncNameRegex1 = new Regex("^(\\d+)");
private static readonly Regex IncNameRegex2 = new Regex("^\\)(\\d+)\\(");
private static readonly Regex IncNameRegex1 = new Regex("(\\d+)$");
private static readonly Regex IncNameRegex2 = new Regex("\\((\\d+)\\)$");
/// <summary>
/// Tries to parse number in the name brackets at the end of the value and then increment it to create a new name.
@@ -264,14 +264,13 @@ namespace FlaxEngine
int index;
int MaxChecks = 10000;
string result;
string reversed = name.Reverse();
// Find '<name><num>' case
var match = IncNameRegex1.Match(reversed);
var match = IncNameRegex1.Match(name);
if (match.Success && match.Groups.Count == 2)
{
// Get result
string num = match.Groups[0].Value.Reverse();
string num = match.Groups[0].Value;
// Parse value
if (int.TryParse(num, out index))
@@ -294,12 +293,12 @@ namespace FlaxEngine
}
// Find '<name> (<num>)' case
match = IncNameRegex2.Match(reversed);
match = IncNameRegex2.Match(name);
if (match.Success && match.Groups.Count == 2)
{
// Get result
string num = match.Groups[0].Value;
num = num.Substring(1, num.Length - 2).Reverse();
num = num.Substring(1, num.Length - 2);
// Parse value
if (int.TryParse(num, out index))