com.wallstop-studios.unity-helpers 2.0.0-rc73.2 → 2.0.0-rc73.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. package/Runtime/Tags/AttributeEffect.cs +1 -0
  2. package/Styles/Elements/Progress/ArcedProgressBar.cs +345 -0
  3. package/Styles/Elements/Progress/ArcedProgressBar.cs.meta +3 -0
  4. package/Styles/Elements/{CircularProgressBar.cs → Progress/CircularProgressBar.cs} +56 -55
  5. package/Styles/Elements/Progress/GlitchProgressBar.cs +416 -0
  6. package/Styles/Elements/Progress/GlitchProgressBar.cs.meta +3 -0
  7. package/Styles/Elements/Progress/LiquidProgressBar.cs +632 -0
  8. package/Styles/Elements/Progress/LiquidProgressBar.cs.meta +3 -0
  9. package/Styles/Elements/Progress/MarchingAntsProgressBar.cs +722 -0
  10. package/Styles/Elements/Progress/MarchingAntsProgressBar.cs.meta +3 -0
  11. package/Styles/Elements/{RegularProgressBar.cs → Progress/RegularProgressBar.cs} +24 -13
  12. package/Styles/Elements/Progress/WigglyProgressBar.cs +837 -0
  13. package/Styles/Elements/Progress/WigglyProgressBar.cs.meta +3 -0
  14. package/Styles/Elements/Progress.meta +3 -0
  15. package/Styles/USS/ArcedProgressBar.uss +19 -0
  16. package/Styles/USS/ArcedProgressBar.uss.meta +3 -0
  17. package/Styles/USS/WigglyProgressBar.uss +17 -0
  18. package/Styles/USS/WigglyProgressBar.uss.meta +3 -0
  19. package/package.json +2 -1
  20. package/Styles/UXML/CircularProgressBar.uxml +0 -11
  21. package/Styles/UXML/CircularProgressBar.uxml.meta +0 -10
  22. package/Styles/UXML/RegularProgressBar.uxml +0 -22
  23. package/Styles/UXML/RegularProgressBar.uxml.meta +0 -10
  24. package/Styles/UXML.meta +0 -3
  25. /package/Styles/Elements/{CircularProgressBar.cs.meta → Progress/CircularProgressBar.cs.meta} +0 -0
  26. /package/Styles/Elements/{RegularProgressBar.cs.meta → Progress/RegularProgressBar.cs.meta} +0 -0
@@ -0,0 +1,416 @@
1
+ namespace WallstopStudios.UnityHelpers.Styles.Elements.Progress
2
+ {
3
+ using Core.Random;
4
+ using UnityEngine;
5
+ using UnityEngine.UIElements;
6
+
7
+ public sealed class GlitchProgressBar : VisualElement
8
+ {
9
+ public const string USSClassName = "glitch-progress-bar";
10
+ public const string USSNormalColorVarName = "--gpb-normal-color";
11
+ public const string USSGlitchColor1VarName = "--gpb-glitch-color1";
12
+ public const string USSGlitchColor2VarName = "--gpb-glitch-color2";
13
+ public const string USSTrackColorVarName = "--gpb-track-color";
14
+
15
+ private float _progress = 0.5f;
16
+ private float _visualProgress;
17
+ public float Progress
18
+ {
19
+ get => _progress;
20
+ set
21
+ {
22
+ if (float.IsNaN(value) || float.IsInfinity(value))
23
+ {
24
+ return;
25
+ }
26
+ _targetProgress = Mathf.Clamp01(value);
27
+ float newTarget = Mathf.Clamp01(value);
28
+ if (Mathf.Approximately(_targetProgress, newTarget))
29
+ {
30
+ return;
31
+ }
32
+
33
+ _targetProgress = newTarget;
34
+ if (panel != null && _animationUpdateItem == null)
35
+ {
36
+ StartAnimationUpdate();
37
+ }
38
+ }
39
+ }
40
+
41
+ private Color _normalColor = Color.green;
42
+ public Color NormalColor
43
+ {
44
+ get => _normalColor;
45
+ set
46
+ {
47
+ _normalColor = value;
48
+ MarkDirtyRepaint();
49
+ }
50
+ }
51
+
52
+ private Color _glitchColor1 = Color.red;
53
+ public Color GlitchColor1
54
+ {
55
+ get => _glitchColor1;
56
+ set
57
+ {
58
+ _glitchColor1 = value;
59
+ MarkDirtyRepaint();
60
+ }
61
+ }
62
+
63
+ private Color _glitchColor2 = Color.blue;
64
+ public Color GlitchColor2
65
+ {
66
+ get => _glitchColor2;
67
+ set
68
+ {
69
+ _glitchColor2 = value;
70
+ MarkDirtyRepaint();
71
+ }
72
+ }
73
+
74
+ private Color _trackColor = Color.black;
75
+ public Color TrackColor
76
+ {
77
+ get => _trackColor;
78
+ set
79
+ {
80
+ _trackColor = value;
81
+ MarkDirtyRepaint();
82
+ }
83
+ }
84
+
85
+ public float glitchIntensity = 0.1f;
86
+ public float glitchFrequency = 0.15f;
87
+ public int glitchDurationFrames = 3;
88
+ public float progressAnimationSpeed = 5f;
89
+
90
+ private bool _isGlitching;
91
+ private int _glitchTimer;
92
+ private float _targetProgress;
93
+ private IVisualElementScheduledItem _animationUpdateItem;
94
+
95
+ private readonly IRandom _random;
96
+
97
+ public new class UxmlFactory : UxmlFactory<GlitchProgressBar, UxmlTraits> { }
98
+
99
+ public new class UxmlTraits : VisualElement.UxmlTraits
100
+ {
101
+ private readonly UxmlFloatAttributeDescription _progressAttribute = new()
102
+ {
103
+ name = "progress",
104
+ defaultValue = 0.5f,
105
+ };
106
+
107
+ private readonly UxmlColorAttributeDescription _normalColorAttribute = new()
108
+ {
109
+ name = "normal-color",
110
+ defaultValue = Color.green,
111
+ };
112
+
113
+ private readonly UxmlColorAttributeDescription _glitchColor1Attribute = new()
114
+ {
115
+ name = "glitch-color1",
116
+ defaultValue = Color.red,
117
+ };
118
+
119
+ private readonly UxmlColorAttributeDescription _glitchColor2Attribute = new()
120
+ {
121
+ name = "glitch-color2",
122
+ defaultValue = Color.blue,
123
+ };
124
+
125
+ private readonly UxmlColorAttributeDescription _trackColorAttribute = new()
126
+ {
127
+ name = "track-color",
128
+ defaultValue = Color.black,
129
+ };
130
+
131
+ private readonly UxmlFloatAttributeDescription _glitchIntensityAttribute = new()
132
+ {
133
+ name = "glitch-intensity",
134
+ defaultValue = 0.1f,
135
+ };
136
+
137
+ private readonly UxmlFloatAttributeDescription _glitchFrequencyAttribute = new()
138
+ {
139
+ name = "glitch-frequency",
140
+ defaultValue = 0.2f,
141
+ };
142
+
143
+ private readonly UxmlIntAttributeDescription _glitchDurationFramesAttribute = new()
144
+ {
145
+ name = "glitch-duration-frames",
146
+ defaultValue = 3,
147
+ };
148
+
149
+ private readonly UxmlFloatAttributeDescription _progressAnimationSpeedAttribute = new()
150
+ {
151
+ name = "progress-animation-speed",
152
+ defaultValue = 5f,
153
+ };
154
+
155
+ public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
156
+ {
157
+ base.Init(ve, bag, cc);
158
+ if (ve is not GlitchProgressBar bar)
159
+ {
160
+ Debug.LogError(
161
+ $"Initialization failed, expected {nameof(GlitchProgressBar)}, found {ve?.GetType()}.)"
162
+ );
163
+ return;
164
+ }
165
+
166
+ bar._targetProgress = _progressAttribute.GetValueFromBag(bag, cc);
167
+ bar._visualProgress = bar._targetProgress;
168
+ bar._progress = bar._targetProgress;
169
+
170
+ bar.NormalColor = _normalColorAttribute.GetValueFromBag(bag, cc);
171
+ bar.GlitchColor1 = _glitchColor1Attribute.GetValueFromBag(bag, cc);
172
+ bar.GlitchColor2 = _glitchColor2Attribute.GetValueFromBag(bag, cc);
173
+ bar.TrackColor = _trackColorAttribute.GetValueFromBag(bag, cc);
174
+ bar.glitchIntensity = _glitchIntensityAttribute.GetValueFromBag(bag, cc);
175
+ bar.glitchFrequency = _glitchFrequencyAttribute.GetValueFromBag(bag, cc);
176
+ bar.glitchDurationFrames = _glitchDurationFramesAttribute.GetValueFromBag(bag, cc);
177
+ bar.progressAnimationSpeed = _progressAnimationSpeedAttribute.GetValueFromBag(
178
+ bag,
179
+ cc
180
+ );
181
+
182
+ if (bar.style.height.keyword == StyleKeyword.Auto || bar.style.height.value == 0)
183
+ {
184
+ bar.style.height = 20;
185
+ }
186
+
187
+ if (bar.style.width.keyword == StyleKeyword.Auto || bar.style.width.value == 0)
188
+ {
189
+ bar.style.width = 200;
190
+ }
191
+ }
192
+ }
193
+
194
+ public GlitchProgressBar()
195
+ : this(PRNG.Instance) { }
196
+
197
+ public GlitchProgressBar(IRandom random)
198
+ {
199
+ _random = random ?? PRNG.Instance;
200
+ AddToClassList(USSClassName);
201
+ generateVisualContent += OnGenerateVisualContent;
202
+ RegisterCallback<CustomStyleResolvedEvent>(OnCustomStyleResolved);
203
+ RegisterCallback<AttachToPanelEvent>(OnAttachToPanel);
204
+ RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
205
+
206
+ _visualProgress = _progress;
207
+ _targetProgress = _progress;
208
+ }
209
+
210
+ private void OnAttachToPanel(AttachToPanelEvent evt)
211
+ {
212
+ if (!Mathf.Approximately(_visualProgress, _targetProgress) || _isGlitching)
213
+ {
214
+ StartAnimationUpdate();
215
+ }
216
+ }
217
+
218
+ private void OnDetachFromPanel(DetachFromPanelEvent evt)
219
+ {
220
+ StopAnimationUpdate();
221
+ }
222
+
223
+ private void StartAnimationUpdate()
224
+ {
225
+ if (panel == null || _animationUpdateItem != null)
226
+ {
227
+ return;
228
+ }
229
+
230
+ _animationUpdateItem = schedule.Execute(UpdateState).Every(16);
231
+ }
232
+
233
+ private void StopAnimationUpdate()
234
+ {
235
+ _animationUpdateItem?.Pause();
236
+ _animationUpdateItem = null;
237
+ }
238
+
239
+ private void UpdateState(TimerState ts)
240
+ {
241
+ bool needsRepaint = false;
242
+
243
+ if (!Mathf.Approximately(_visualProgress, _targetProgress))
244
+ {
245
+ _visualProgress = Mathf.Lerp(
246
+ _visualProgress,
247
+ _targetProgress,
248
+ ts.deltaTime / 1000f * progressAnimationSpeed
249
+ );
250
+ _progress = _visualProgress;
251
+ if (Mathf.Abs(_visualProgress - _targetProgress) < 0.001f)
252
+ {
253
+ _visualProgress = _targetProgress;
254
+ _progress = _targetProgress;
255
+ }
256
+ needsRepaint = true;
257
+ }
258
+
259
+ if (_isGlitching)
260
+ {
261
+ _glitchTimer--;
262
+ if (_glitchTimer <= 0)
263
+ {
264
+ _isGlitching = false;
265
+ }
266
+ needsRepaint = true;
267
+ }
268
+ else
269
+ {
270
+ bool progressIsChanging = !Mathf.Approximately(_visualProgress, _targetProgress);
271
+ if (progressIsChanging && _random.NextFloat() < glitchFrequency)
272
+ {
273
+ _isGlitching = true;
274
+ _glitchTimer = glitchDurationFrames;
275
+ needsRepaint = true;
276
+ }
277
+ }
278
+
279
+ if (needsRepaint)
280
+ {
281
+ MarkDirtyRepaint();
282
+ }
283
+
284
+ if (
285
+ Mathf.Approximately(_visualProgress, _targetProgress)
286
+ && !_isGlitching
287
+ && _animationUpdateItem != null
288
+ ) { }
289
+ }
290
+
291
+ private void OnCustomStyleResolved(CustomStyleResolvedEvent evt)
292
+ {
293
+ if (
294
+ customStyle.TryGetValue(
295
+ new CustomStyleProperty<Color>(USSNormalColorVarName),
296
+ out Color normCol
297
+ )
298
+ )
299
+ {
300
+ NormalColor = normCol;
301
+ }
302
+
303
+ if (
304
+ customStyle.TryGetValue(
305
+ new CustomStyleProperty<Color>(USSGlitchColor1VarName),
306
+ out Color g1Col
307
+ )
308
+ )
309
+ {
310
+ GlitchColor1 = g1Col;
311
+ }
312
+
313
+ if (
314
+ customStyle.TryGetValue(
315
+ new CustomStyleProperty<Color>(USSGlitchColor2VarName),
316
+ out Color g2Col
317
+ )
318
+ )
319
+ {
320
+ GlitchColor2 = g2Col;
321
+ }
322
+
323
+ if (
324
+ customStyle.TryGetValue(
325
+ new CustomStyleProperty<Color>(USSTrackColorVarName),
326
+ out Color trackCol
327
+ )
328
+ )
329
+ {
330
+ TrackColor = trackCol;
331
+ }
332
+
333
+ MarkDirtyRepaint();
334
+ }
335
+
336
+ private void OnGenerateVisualContent(MeshGenerationContext mgc)
337
+ {
338
+ Painter2D painter = mgc.painter2D;
339
+ Rect r = contentRect;
340
+
341
+ if (r.width <= 0 || r.height <= 0)
342
+ {
343
+ return;
344
+ }
345
+
346
+ painter.fillColor = _trackColor;
347
+ painter.BeginPath();
348
+ painter.MoveTo(new Vector2(r.xMin, r.yMin));
349
+ painter.LineTo(new Vector2(r.xMax, r.yMin));
350
+ painter.LineTo(new Vector2(r.xMax, r.yMax));
351
+ painter.LineTo(new Vector2(r.xMin, r.yMax));
352
+ painter.ClosePath();
353
+ painter.Fill();
354
+
355
+ if (Mathf.Approximately(_visualProgress, 0))
356
+ {
357
+ return;
358
+ }
359
+
360
+ float fillWidth = r.width * _visualProgress;
361
+
362
+ if (_isGlitching)
363
+ {
364
+ int numSegments = _random.Next(2, 5);
365
+ float segmentHeight = r.height / numSegments;
366
+
367
+ for (int i = 0; i < numSegments; i++)
368
+ {
369
+ float offsetX = (_random.NextFloat() - 0.5f) * 2f * r.width * glitchIntensity;
370
+ float offsetY =
371
+ (_random.NextFloat() - 0.5f) * 2f * r.height * glitchIntensity * 0.3f;
372
+
373
+ float currentSegmentY = r.yMin + i * segmentHeight;
374
+
375
+ offsetX = Mathf.Clamp(offsetX, -r.width * 0.2f, r.width * 0.2f);
376
+ offsetY = Mathf.Clamp(offsetY, -r.height * 0.1f, r.height * 0.1f);
377
+
378
+ Rect glitchRect = new(
379
+ r.xMin + offsetX,
380
+ currentSegmentY + offsetY,
381
+ fillWidth,
382
+ segmentHeight
383
+ );
384
+
385
+ glitchRect.xMin = Mathf.Max(glitchRect.xMin, r.xMin);
386
+ glitchRect.yMin = Mathf.Max(glitchRect.yMin, r.yMin);
387
+ glitchRect.xMax = Mathf.Min(glitchRect.xMax, r.xMax);
388
+ glitchRect.yMax = Mathf.Min(glitchRect.yMax, r.yMax);
389
+
390
+ if (glitchRect is { width: > 0, height: > 0 })
391
+ {
392
+ painter.fillColor = _random.NextBool() ? _glitchColor1 : _glitchColor2;
393
+ painter.BeginPath();
394
+ painter.MoveTo(new Vector2(glitchRect.xMin, glitchRect.yMin));
395
+ painter.LineTo(new Vector2(glitchRect.xMax, glitchRect.yMin));
396
+ painter.LineTo(new Vector2(glitchRect.xMax, glitchRect.yMax));
397
+ painter.LineTo(new Vector2(glitchRect.xMin, glitchRect.yMax));
398
+ painter.ClosePath();
399
+ painter.Fill();
400
+ }
401
+ }
402
+ }
403
+ else
404
+ {
405
+ painter.fillColor = _normalColor;
406
+ painter.BeginPath();
407
+ painter.MoveTo(new Vector2(r.xMin, r.yMin));
408
+ painter.LineTo(new Vector2(r.xMin + fillWidth, r.yMin));
409
+ painter.LineTo(new Vector2(r.xMin + fillWidth, r.yMax));
410
+ painter.LineTo(new Vector2(r.xMin, r.yMax));
411
+ painter.ClosePath();
412
+ painter.Fill();
413
+ }
414
+ }
415
+ }
416
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 992c1f9192b04f798517b68319f1fd25
3
+ timeCreated: 1746740301