Skip to content

Commit 1078690

Browse files
authored
Add clone handling for clone-able entities
1 parent edca30a commit 1078690

8 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/TSMapEditor/Config/Translations/en/Translation_en.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ WindowController.NoTriggerAttached.Title=No trigger attached
372372
WindowController.NoTriggerAttached.Description=The specified Tag has no attached Trigger!
373373
ListExtensions.TaskForceParseError=Failed to load TaskForce {0}. It might be missing a section or be otherwise invalid.
374374
ListExtensions.TeamTypes.TaskForceNotFound=TeamType {0} has an invalid TaskForce ({1}) specified!
375+
CloneName=\s(Clone)
375376
376377
; *************************************************
377378
; Hint texts

src/TSMapEditor/Config/Translations/zh-Hans/Translation_zh-Hans.ini

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,8 +2385,5 @@ SetMapDifficultySettingsWindow.lblTeamDelayEasy.Text=简单:
23852385
SetMapDifficultySettingsWindow.btnApply.Text=应用
23862386
23872387
TriggersWindow.UnknownSpeech=\s- 未知语音
2388-
Trigger.CloneName=\s(副本)
2389-
TaskForce.CloneName=\s(副本)
2390-
TeamType.CloneName=\s(副本)
2391-
Script.CloneName=\s(副本)
2388+
CloneName=\s(副本)
23922389
TeamTypes.Global=(全局)\s

src/TSMapEditor/Helpers.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,5 +847,29 @@ public static string DifficultyToTranslatedString(Difficulty difficulty)
847847
throw new NotImplementedException(nameof(DifficultyToTranslatedString) + ": Unknown difficulty level " + difficulty);
848848
}
849849
}
850+
851+
/// <summary>
852+
/// Shared helper function used by Triggers, TaskForces, Scripts, TeamTypes, and AI Triggers.
853+
/// Used to generate the name of the instance during the cloning process of those entities.
854+
/// If the provided name contains a number, the number is incremented and the name is returned as is.
855+
/// Otherwise, a localized " (Clone)" suffix is appended to the name.
856+
/// If there are multiple numbers in the name, only the first is incremented.
857+
/// </summary>
858+
public static string GetNameForClone(string name)
859+
{
860+
string[] parts = name.Split(" ");
861+
int numPart = -1;
862+
863+
for (int i = 0; i < parts.Length; i++)
864+
{
865+
if (int.TryParse(parts[i], out numPart) && numPart >= 0)
866+
{
867+
parts[i] = (numPart + 1).ToString(CultureInfo.InvariantCulture);
868+
return string.Join(" ", parts);
869+
}
870+
}
871+
872+
return name + Translate("CloneName", " (Clone)");
873+
}
850874
}
851875
}

src/TSMapEditor/Models/AITriggerType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public AITriggerType(string iniName)
9696
public AITriggerType Clone(string newUniqueId)
9797
{
9898
var clonedAITrigger = (AITriggerType)MemberwiseClone();
99-
clonedAITrigger.Name = Name + " (Clone)";
99+
clonedAITrigger.Name = Helpers.GetNameForClone(Name);
100100
clonedAITrigger.ININame = newUniqueId;
101101
return clonedAITrigger;
102102
}

src/TSMapEditor/Models/Script.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public string EditorColor
9696
public Script Clone(string iniName)
9797
{
9898
var script = new Script(iniName);
99-
script.Name = Name + Translate(this, "CloneName", " (Clone)");
99+
script.Name = Helpers.GetNameForClone(Name);
100100
script.EditorColor = EditorColor;
101101

102102
foreach (var action in Actions)

src/TSMapEditor/Models/TaskForce.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public string GetHintText()
160160
public TaskForce Clone(string iniName)
161161
{
162162
var newTaskForce = new TaskForce(iniName);
163-
newTaskForce.Name = Name + Translate(this, "CloneName", " (Clone)");
163+
newTaskForce.Name = Helpers.GetNameForClone(Name);
164164
newTaskForce.Group = Group;
165165

166166
for (int i = 0; i < TechnoTypes.Length; i++)

src/TSMapEditor/Models/TeamType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public TeamType Clone(string iniName)
132132
{
133133
var clone = MemberwiseClone() as TeamType;
134134
clone.ININame = iniName;
135-
clone.Name = Name + Translate(this, "CloneName", " (Clone)");
135+
clone.Name = Helpers.GetNameForClone(Name);
136136

137137
clone.EnabledTeamTypeFlags = new List<string>(EnabledTeamTypeFlags);
138138

src/TSMapEditor/Models/Trigger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Trigger Clone(string uniqueId)
8787
{
8888
Trigger clone = (Trigger)MemberwiseClone();
8989
clone.ID = uniqueId;
90-
clone.Name = Name + Translate(this, "CloneName", " (Clone)");
90+
clone.Name = Helpers.GetNameForClone(Name);
9191

9292
// Deep clone the events and actions
9393

0 commit comments

Comments
 (0)