Skip to content

Commit 6a60b42

Browse files
authored
Merge pull request #12 from TarasK8/feature/progress-bar
Setters added to Progress Bar components, renaming
2 parents 32985d2 + f700ac4 commit 6a60b42

File tree

8 files changed

+166
-39
lines changed

8 files changed

+166
-39
lines changed

Editor/MultiProgressBarEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private void OnEnable()
2424

2525
_ignoreLenght = serializedObject.FindProperty("_ignoreLenght");
2626
_reversed = serializedObject.FindProperty("_reversed");
27-
_bars = serializedObject.FindProperty("_bars");
27+
_bars = serializedObject.FindProperty("_fillBars");
2828

2929
_minValue = serializedObject.FindProperty("_minValue");
3030
_maxValue = serializedObject.FindProperty("_maxValue");

Editor/ProgressBarEditor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private void OnEnable()
2424
{
2525
_target = (ProgressBar)serializedObject.targetObject;
2626

27-
_bar = serializedObject.FindProperty("_bar");
27+
_bar = serializedObject.FindProperty("_fillBar");
2828
_direction = serializedObject.FindProperty("_direction");
2929
_center = serializedObject.FindProperty("_center");
3030

Runtime/Progress Bar/BarMultiSegmentTrail.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using UnityEngine;
22
using UnityEngine.Pool;
3+
using UnityEngine.Serialization;
34

45
namespace TarasK8.UI
56
{
67
[AddComponentMenu("Optimized UI/Progress Bar/Bar Multi Segment Trail")]
78
[RequireComponent(typeof(RectTransform))]
89
public class BarMultiSegmentTrail : MonoBehaviour
910
{
10-
[SerializeField] private BarSegment _targetSegment;
11+
[SerializeField] private BarSegment _trackingBar;
1112
[SerializeField] private PooledBarSegment _segmentPrefab;
1213
[Header("Active")]
1314
[SerializeField] private bool _increasing = true;
@@ -39,14 +40,14 @@ private void Awake()
3940

4041
private void OnEnable()
4142
{
42-
_targetSegment.OnStartPositionChange += BarSegment_OnStartPositionChange;
43-
_targetSegment.OnEndPositionChange += BarSegment_OnEndPositionChange;
43+
_trackingBar.OnStartPositionChange += BarSegment_OnStartPositionChange;
44+
_trackingBar.OnEndPositionChange += BarSegment_OnEndPositionChange;
4445
}
4546

4647
private void OnDisable()
4748
{
48-
_targetSegment.OnStartPositionChange -= BarSegment_OnStartPositionChange;
49-
_targetSegment.OnEndPositionChange -= BarSegment_OnEndPositionChange;
49+
_trackingBar.OnStartPositionChange -= BarSegment_OnStartPositionChange;
50+
_trackingBar.OnEndPositionChange -= BarSegment_OnEndPositionChange;
5051
}
5152

5253
private void BarSegment_OnStartPositionChange(float oldPosition, float newPosition)

Runtime/Progress Bar/BarSegmentAnimated.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ protected override void SetPositionEnd(float position)
9494

9595
private void PlayIncrease((Animation increase, Animation decrease) animation, float from, float to)
9696
{
97+
if(Mathf.Approximately(from, to))
98+
return;
99+
97100
if (_increaseCompleteBeforePlay)
98101
{
99102
if(animation.increase.IsCompleted == false)
@@ -111,6 +114,9 @@ private void PlayIncrease((Animation increase, Animation decrease) animation, fl
111114

112115
private void PlayDecrease((Animation increase, Animation decrease) animation, float from, float to)
113116
{
117+
if(Mathf.Approximately(from, to))
118+
return;
119+
114120
if (_decreaseCompleteBeforePlay)
115121
{
116122
if(animation.decrease.IsCompleted == false)

Runtime/Progress Bar/MultiProgressBar.cs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Linq;
43
using UnityEngine;
5-
using UnityEngine.Serialization;
64

75
namespace TarasK8.UI
86
{
@@ -12,11 +10,31 @@ public class MultiProgressBar : ProgressBarBase
1210
{
1311
[SerializeField] private bool _ignoreLenght = true;
1412
[SerializeField] private bool _reversed = false;
15-
[SerializeField] private List<ProgressBarBase> _bars;
13+
[SerializeField] private List<ProgressBarBase> _fillBars = new();
14+
15+
public bool IgnoreLenght
16+
{
17+
get => _ignoreLenght;
18+
set
19+
{
20+
_ignoreLenght = value;
21+
base.UpdateValue();
22+
}
23+
}
24+
25+
public bool Reversed
26+
{
27+
get => _reversed;
28+
set
29+
{
30+
_reversed = value;
31+
base.UpdateValue();
32+
}
33+
}
1634

1735
private void OnValidate()
1836
{
19-
if (_bars.Remove(this))
37+
if (_fillBars.Remove(this))
2038
{
2139
Debug.LogWarning("MultiProgressBar removed from list");
2240
}
@@ -30,25 +48,30 @@ private void OnEnable()
3048
#if UNITY_EDITOR
3149
private void Update()
3250
{
33-
if(Application.isPlaying == false && _bars != null)
34-
OnSetValue(0f, Value);
51+
if(Application.isPlaying == false && _fillBars != null)
52+
base.UpdateValue();
3553
}
3654
#endif
3755

3856
protected override void OnSetValue(float oldValue, float newValue)
3957
{
40-
if(_bars.Count <= 0)
58+
if (_fillBars == null || _fillBars.Count <= 0)
59+
{
60+
#if UNITY_EDITOR
61+
Debug.LogWarning($"MultiProgressBar does not contain any Fill Bar", this);
62+
#endif
4163
return;
42-
64+
}
65+
4366
float position = CalculatePosition(newValue);
44-
float allLength = _bars.Sum(GetLength);
67+
float allLength = _fillBars.Sum(GetLength);
4568

4669
float length = 0f;
4770

48-
for (int i = 0; i < _bars.Count; i++)
71+
for (int i = 0; i < _fillBars.Count; i++)
4972
{
50-
int index = _reversed ? -i + _bars.Count - 1 : i;
51-
var bar = _bars[index];
73+
int index = _reversed ? -i + _fillBars.Count - 1 : i;
74+
var bar = _fillBars[index];
5275

5376
var globalPos = position * allLength;
5477
bar.Value = GetValue(globalPos - length, bar);
@@ -75,21 +98,20 @@ private float GetValue(float rawValue, ProgressBarBase bar)
7598
[ContextMenu("Sort Bars List By Position X")]
7699
public void SortListByX()
77100
{
78-
_bars.Sort((a, b) => a.transform.position.x.CompareTo(b.transform.position.x));
101+
_fillBars.Sort((a, b) => a.transform.position.x.CompareTo(b.transform.position.x));
79102
}
80103

81104
[ContextMenu("Sort Bars List By Position Y")]
82105
public void SortListByY()
83106
{
84-
_bars.Sort((a, b) => a.transform.position.y.CompareTo(b.transform.position.y));
107+
_fillBars.Sort((a, b) => a.transform.position.y.CompareTo(b.transform.position.y));
85108
}
86109

87110
[ContextMenu("Reverse Bars List")]
88111
public void ReverseList()
89112
{
90-
_bars.Reverse();
113+
_fillBars.Reverse();
91114
}
92-
93-
#endif
115+
#endif
94116
}
95117
}

Runtime/Progress Bar/ProgressBar.cs

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ public class ProgressBar : ProgressBarBase
1010
{
1111
[SerializeField, HideInInspector] private RectTransform _rectTransform;
1212

13-
[SerializeField] private BarSegment _bar;
13+
[SerializeField] private BarSegment _fillBar;
1414
[SerializeField] private Direction _direction;
1515
[SerializeField, Range(0f, 1f)] private float _center = 0.5f;
1616

1717
[SerializeField] private ValueMode _minValueMode = ValueMode.Custom;
1818
[SerializeField] private ValueMode _maxValueMode = ValueMode.Custom;
1919

20-
private RectTransform RectTransform
20+
public RectTransform RectTransform
2121
{
2222
get
2323
{
@@ -27,9 +27,27 @@ private RectTransform RectTransform
2727
}
2828
}
2929

30-
public override float MinValue => GetValueByMode(_minValueMode, base.MinValue);
31-
public override float MaxValue => GetValueByMode(_maxValueMode, base.MaxValue);
32-
30+
public BarSegment FillBar
31+
{
32+
get => _fillBar;
33+
set
34+
{
35+
_fillBar = value;
36+
if(_fillBar != null)
37+
UpdateVisualProgress(Value);
38+
}
39+
}
40+
public override float MinValue
41+
{
42+
get => GetValueByMode(_minValueMode, base.MinValue);
43+
set => SetValueByMode(_minValueMode, value, (ctx) => base.MinValue = ctx);
44+
}
45+
public override float MaxValue
46+
{
47+
get => GetValueByMode(_maxValueMode, base.MaxValue);
48+
set => SetValueByMode(_maxValueMode, value, (ctx) => base.MaxValue = ctx);
49+
}
50+
3351
public float Center
3452
{
3553
get => _center;
@@ -52,7 +70,7 @@ public Direction direction
5270
#if UNITY_EDITOR
5371
private void Update()
5472
{
55-
if(Application.isPlaying == false && _bar != null)
73+
if(Application.isPlaying == false && _fillBar != null)
5674
UpdateVisualProgress(Value);
5775
}
5876
#endif
@@ -74,25 +92,33 @@ public float CalculateRightCenter(float position)
7492

7593
public void UpdateVisualProgress(float value)
7694
{
95+
#if UNITY_EDITOR
96+
if (_fillBar == null)
97+
{
98+
Debug.LogException(new NullReferenceException("Fill Bar is null"), this);
99+
return;
100+
}
101+
#endif
102+
77103
var position = CalculatePosition(value);
78104

79105
switch (_direction)
80106
{
81107
case Direction.Right:
82108
{
83-
_bar.SetPosition(0f, position);
109+
_fillBar.SetPosition(0f, position);
84110
break;
85111
}
86112
case Direction.Left:
87113
{
88-
_bar.SetPosition(-position + 1f, 1f);
114+
_fillBar.SetPosition(-position + 1f, 1f);
89115
break;
90116
}
91117
case Direction.Center:
92118
{
93119
float left = CalculateLeftCenter(position);
94120
float right = CalculateRightCenter(position);
95-
_bar.SetPosition(left, right);
121+
_fillBar.SetPosition(left, right);
96122
break;
97123
}
98124
default:
@@ -112,16 +138,66 @@ private float GetValueByMode(ValueMode mode, float customValue)
112138
};
113139
}
114140

141+
private void SetValueByMode(ValueMode mode, float value, Action<float> customValue)
142+
{
143+
switch (mode)
144+
{
145+
case ValueMode.Custom:
146+
{
147+
customValue.Invoke(value);
148+
base.MaxValue = value;
149+
UpdateValue();
150+
break;
151+
}
152+
case ValueMode.Width:
153+
{
154+
var size = new Vector2(value, RectTransform.sizeDelta.y);
155+
RectTransform.sizeDelta = size;
156+
break;
157+
}
158+
case ValueMode.Height:
159+
{
160+
var size = new Vector2(RectTransform.sizeDelta.x, value);
161+
RectTransform.sizeDelta = size;
162+
break;
163+
}
164+
case ValueMode.HeightOrWidth:
165+
{
166+
SetHeightOrWidthOption(value);
167+
break;
168+
}
169+
default:
170+
throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
171+
}
172+
}
173+
115174
private float GetHeightOrWidthOption()
116175
{
117-
return _bar.axis switch
176+
return _fillBar.axis switch
118177
{
119178
BarSegment.Axis.Horizontal => RectTransform.rect.width,
120179
BarSegment.Axis.Vertical => RectTransform.rect.height,
121180
_ => throw new ArgumentOutOfRangeException()
122181
};
123182
}
124183

184+
private void SetHeightOrWidthOption(float value)
185+
{
186+
var size = _fillBar.axis switch
187+
{
188+
BarSegment.Axis.Horizontal => new Vector2(value, RectTransform.sizeDelta.y),
189+
BarSegment.Axis.Vertical => new Vector2(RectTransform.sizeDelta.x, value),
190+
_ => throw new ArgumentOutOfRangeException()
191+
};
192+
RectTransform.sizeDelta = size;
193+
base.UpdateValue();
194+
}
195+
196+
protected override bool ShouldFilterSameValues()
197+
{
198+
return true;
199+
}
200+
125201
public enum Direction
126202
{
127203
Right,

Runtime/Progress Bar/ProgressBarBase.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,26 @@ public float Value
1818
}
1919

2020
public float Length => MaxValue - MinValue;
21-
22-
public virtual float MinValue => _minValue;
23-
public virtual float MaxValue => _maxValue;
21+
22+
public virtual float MinValue
23+
{
24+
get => _minValue;
25+
set
26+
{
27+
_minValue = value;
28+
SetValue(Value);
29+
}
30+
}
31+
32+
public virtual float MaxValue
33+
{
34+
get => _maxValue;
35+
set
36+
{
37+
_maxValue = value;
38+
UpdateValue();
39+
}
40+
}
2441

2542
private void OnValidate()
2643
{
@@ -34,7 +51,12 @@ public void AddValue(float value)
3451
{
3552
Value += value;
3653
}
37-
54+
55+
public void UpdateValue()
56+
{
57+
SetValue(Value);
58+
}
59+
3860
public float CalculatePosition(float value)
3961
{
4062
return Mathf.InverseLerp(MinValue, MaxValue, value);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.tarask8.optimized-ui",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"displayName": "Optimized UI",
55
"description": "A package for Unity that is designed to greatly simplify user interface development.",
66
"unity": "2022.3",

0 commit comments

Comments
 (0)