forked from potatosalad775/MWC_Localization_Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAdjustment.cs
More file actions
263 lines (223 loc) · 9.11 KB
/
TextAdjustment.cs
File metadata and controls
263 lines (223 loc) · 9.11 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System.Collections.Generic;
using UnityEngine;
namespace MWC_Localization_Core
{
/// <summary>
/// Stores original TextMesh state before adjustment
/// </summary>
public struct OriginalTextMeshState
{
public Vector3 LocalPosition;
public float CharacterSize;
public float LineSpacing;
public float WidthScale;
public OriginalTextMeshState(TextMesh textMesh)
{
LocalPosition = textMesh.transform.localPosition;
CharacterSize = textMesh.characterSize;
LineSpacing = textMesh.lineSpacing;
WidthScale = textMesh.transform.localScale.x;
}
}
/// <summary>
/// Represents a position adjustment rule with path matching conditions
/// Supports Contains, EndsWith, StartsWith, and negation (!Contains)
/// </summary>
public class TextAdjustment
{
public List<PathCondition> Conditions { get; private set; } = new List<PathCondition>();
public Vector3 Offset { get; private set; }
public float? FontSize { get; private set; }
public float? LineSpacing { get; private set; }
public float? WidthScale { get; private set; }
// Track which TextMesh objects have been adjusted to prevent duplicate adjustments
private HashSet<TextMesh> adjustedTextMeshes = new HashSet<TextMesh>();
// Store original state of adjusted TextMesh objects for restoration
private Dictionary<TextMesh, OriginalTextMeshState> originalStates = new Dictionary<TextMesh, OriginalTextMeshState>();
public TextAdjustment(string conditionsString, Vector3 offset, float? fontSize = null, float? lineSpacing = null, float? widthScale = null)
{
Offset = offset;
FontSize = fontSize;
LineSpacing = lineSpacing;
WidthScale = widthScale;
ParseConditions(conditionsString);
}
/// <summary>
/// Apply position adjustment to TextMesh if not already adjusted
/// Uses HashSet to prevent duplicate adjustments
/// </summary>
/// <returns>True if adjustment was applied, false if already adjusted</returns>
public bool ApplyAdjustment(TextMesh textMesh)
{
if (textMesh == null)
return false;
// Skip if already adjusted
if (adjustedTextMeshes.Contains(textMesh))
return false;
// Store original state before applying adjustments (only once, on first application)
if (!originalStates.ContainsKey(textMesh))
originalStates[textMesh] = new OriginalTextMeshState(textMesh);
// Apply the position offset
Vector3 currentPosition = textMesh.transform.localPosition;
textMesh.transform.localPosition = currentPosition + Offset;
// Apply font size if specified
if (FontSize.HasValue)
{
textMesh.characterSize = FontSize.Value;
}
// Apply line spacing if specified
if (LineSpacing.HasValue)
{
textMesh.lineSpacing = LineSpacing.Value;
}
// Apply width scale if specified (scales X axis to make text wider/narrower)
if (WidthScale.HasValue)
{
Vector3 scale = textMesh.transform.localScale;
scale.x = WidthScale.Value;
textMesh.transform.localScale = scale;
}
// Note: Unity 5.x TextMesh supports: characterSize, fontSize, lineSpacing, transform.localScale
// Character width is controlled via transform.localScale.x
// Mark as adjusted
adjustedTextMeshes.Add(textMesh);
return true;
}
/// <summary>
/// Restore TextMesh to its original state before adjustment
/// </summary>
/// <returns>True if restored, false if no original state exists</returns>
public bool RestoreOriginal(TextMesh textMesh)
{
if (textMesh == null || !originalStates.ContainsKey(textMesh))
return false;
OriginalTextMeshState originalState = originalStates[textMesh];
// Restore original values
textMesh.transform.localPosition = originalState.LocalPosition;
textMesh.characterSize = originalState.CharacterSize;
textMesh.lineSpacing = originalState.LineSpacing;
Vector3 scale = textMesh.transform.localScale;
scale.x = originalState.WidthScale;
textMesh.transform.localScale = scale;
// Remove from adjusted set so it can be re-adjusted
adjustedTextMeshes.Remove(textMesh);
return true;
}
/// <summary>
/// Clear the cache of adjusted TextMesh objects and restore all to original state
/// Useful for F8/F9 reload functionality
/// </summary>
public void ClearCache()
{
// Restore all adjusted TextMeshes to their original state before clearing
foreach (var textMesh in new List<TextMesh>(originalStates.Keys))
{
if (textMesh != null)
RestoreOriginal(textMesh);
}
// adjustedTextMeshes is already cleared by RestoreOriginal() calls
originalStates.Clear();
}
/// <summary>
/// Parse condition string (e.g., "Contains(GUI/HUD) & EndsWith(/HUDLabel)")
/// </summary>
private void ParseConditions(string conditionsString)
{
string[] parts = conditionsString.Split('&');
foreach (string part in parts)
{
string trimmed = part.Trim();
if (string.IsNullOrEmpty(trimmed))
continue;
// Check for negation
bool negate = trimmed.StartsWith("!");
if (negate)
trimmed = trimmed.Substring(1).Trim();
// Parse condition type and value
if (trimmed.StartsWith("Contains(") && trimmed.EndsWith(")"))
{
string value = trimmed.Substring(9, trimmed.Length - 10);
Conditions.Add(new PathCondition(ConditionType.Contains, value, negate));
}
else if (trimmed.StartsWith("EndsWith(") && trimmed.EndsWith(")"))
{
string value = trimmed.Substring(9, trimmed.Length - 10);
Conditions.Add(new PathCondition(ConditionType.EndsWith, value, negate));
}
else if (trimmed.StartsWith("StartsWith(") && trimmed.EndsWith(")"))
{
string value = trimmed.Substring(11, trimmed.Length - 12);
Conditions.Add(new PathCondition(ConditionType.StartsWith, value, negate));
}
else if (trimmed.StartsWith("Equals(") && trimmed.EndsWith(")"))
{
string value = trimmed.Substring(7, trimmed.Length - 8);
Conditions.Add(new PathCondition(ConditionType.Equals, value, negate));
}
}
}
/// <summary>
/// Check if the given path matches all conditions
/// </summary>
public bool Matches(string path)
{
if (string.IsNullOrEmpty(path))
return false;
foreach (PathCondition condition in Conditions)
{
if (!condition.Evaluate(path))
return false;
}
return Conditions.Count > 0; // At least one condition must exist
}
}
/// <summary>
/// Represents a single path matching condition
/// </summary>
public class PathCondition
{
public ConditionType Type { get; private set; }
public string Value { get; private set; }
public bool Negate { get; private set; }
public PathCondition(ConditionType type, string value, bool negate = false)
{
Type = type;
Value = value;
Negate = negate;
}
/// <summary>
/// Evaluate if the path satisfies this condition
/// </summary>
public bool Evaluate(string path)
{
bool result = false;
switch (Type)
{
case ConditionType.Contains:
result = path.Contains(Value);
break;
case ConditionType.EndsWith:
result = path.EndsWith(Value);
break;
case ConditionType.StartsWith:
result = path.StartsWith(Value);
break;
case ConditionType.Equals:
result = path == Value;
break;
}
// Apply negation if needed
return Negate ? !result : result;
}
}
/// <summary>
/// Types of path matching conditions
/// </summary>
public enum ConditionType
{
Contains,
EndsWith,
StartsWith,
Equals
}
}