-
Notifications
You must be signed in to change notification settings - Fork 1
/
Renamer.cs
138 lines (118 loc) · 3.08 KB
/
Renamer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
namespace Xarbrough.Renamer
{
using System.Collections.Generic;
using System;
using System.Linq;
using UnityEngine;
using UnityEditor;
using Object = UnityEngine.Object;
[Serializable]
public class Renamer
{
public bool HasOperations => operations.Count > 0;
public IEnumerable<(StringOperation operation, int id)> Operations
{
get
{
for (int i = 0; i < operations.Count; i++)
yield return (operations[i], i);
}
}
[SerializeField]
private List<StringOperation> operations;
public void Initialize()
{
if (operations == null || operations.Any(x => x == null))
{
operations = new List<StringOperation>();
var types = typeof(RenameWindow).Assembly.GetTypes();
for (int i = 0; i < types.Length; i++)
{
if (!types[i].IsAbstract && typeof(StringOperation).IsAssignableFrom(types[i]))
{
var instance = StringOperation.Create(types[i]);
operations.Add(instance);
}
}
operations.Sort(SortByCustomOrder);
}
}
private static int SortByCustomOrder(StringOperation x, StringOperation y)
{
return x.DefaultOrder.CompareTo(y.DefaultOrder);
}
public string CalculateRename(string name, int index, int objectCount)
{
if (name == null)
return null;
for (int i = 0; i < operations.Count; i++)
{
StringOperation operation = operations[i];
if (operation.Active)
{
if (operations[i].Rename(new RenameInput(name, index, objectCount), out string output))
name = output;
}
}
return name;
}
public void Clear()
{
if (operations != null)
{
for (int i = 0; i < operations.Count; i++)
Object.DestroyImmediate(operations[i]);
operations.Clear();
}
}
public void MoveUp(int id)
{
if (id > 0)
{
(operations[id - 1], operations[id]) = (operations[id], operations[id - 1]);
}
}
public void MoveDown(int id)
{
if (id < operations.Count - 1)
{
(operations[id + 1], operations[id]) = (operations[id], operations[id + 1]);
}
}
public void ResetAllOperationSettings()
{
for (int i = 0; i < operations.Count; i++)
{
operations[i].Active = false;
ResetOperationSettings(i);
}
}
public void ResetOperationSettings(int id)
{
Type type = operations[id].GetType();
Object.DestroyImmediate(operations[id]);
operations[id] = StringOperation.Create(type);
}
public void AddOperationHeaderMenu(GenericMenu menu, int targetID, Action onResetCalled)
{
if (targetID > 0)
menu.AddItem(new GUIContent("Move Up"), false, () => MoveUp(targetID));
else
menu.AddDisabledItem(new GUIContent("Move Up"));
if (targetID < operations.Count - 1)
menu.AddItem(new GUIContent("Move Down"), false, () => MoveDown(targetID));
else
menu.AddDisabledItem(new GUIContent("Move Down"));
menu.AddSeparator(string.Empty);
menu.AddItem(new GUIContent("Reset Settings"), false, () =>
{
ResetOperationSettings(targetID);
onResetCalled();
});
}
public void AddWindowOptions(GenericMenu menu)
{
menu.AddItem(new GUIContent("Reset All"), false, ResetAllOperationSettings);
}
}
}