| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | public interface ISliderComponentView |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// Event that will be triggered when the slider value changes. |
| | 10 | | /// </summary> |
| | 11 | | Slider.SliderEvent onSliderChange { get; } |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Event that will be triggered when the increment button is clicked. |
| | 15 | | /// </summary> |
| | 16 | | Button.ButtonClickedEvent onIncrement { get; } |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Event that will be triggered when the decrement button is clicked. |
| | 20 | | /// </summary> |
| | 21 | | Button.ButtonClickedEvent onDecrement { get; } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Set the slider text. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="newText">New text.</param> |
| | 27 | | void SetText(string newText); |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Set increment/decrement buttons active |
| | 31 | | /// </summary> |
| | 32 | | void SetButtonsActive(bool isActive); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Set slider text active |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="isActive">Is active</param> |
| | 38 | | void SetTextActive(bool isActive); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Set slider background |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="image">Background image</param> |
| | 44 | | void SetBackground(Sprite image); |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Set the slider interactable or not. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="isInteractable">Clickable or not</param> |
| | 50 | | void SetInteractable(bool isInteractable); |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Returns true if the slider is Interactable |
| | 54 | | /// </summary> |
| | 55 | | bool IsInteractable(); |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public class SliderComponentView : BaseComponentView, ISliderComponentView, IComponentModelConfig<SliderComponentModel> |
| | 59 | | { |
| | 60 | |
|
| | 61 | | [Header("Prefab References")] |
| | 62 | | [SerializeField] public Slider slider; |
| | 63 | | [SerializeField] internal Image background; |
| | 64 | | [SerializeField] internal TMP_Text text; |
| | 65 | | [SerializeField] public Button incrementButton; |
| | 66 | | [SerializeField] public Button decrementButton; |
| | 67 | |
|
| | 68 | | [Header("Configuration")] |
| | 69 | | [SerializeField] internal SliderComponentModel model; |
| | 70 | |
|
| 105 | 71 | | public Slider.SliderEvent onSliderChange => slider?.onValueChanged; |
| 105 | 72 | | public Button.ButtonClickedEvent onIncrement => incrementButton?.onClick; |
| 105 | 73 | | public Button.ButtonClickedEvent onDecrement => decrementButton?.onClick; |
| | 74 | |
|
| | 75 | | public void AddValueToSlider(float value) |
| | 76 | | { |
| 0 | 77 | | slider.value += value; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Configure(SliderComponentModel newModel) |
| | 81 | | { |
| 0 | 82 | | model = newModel; |
| 0 | 83 | | RefreshControl(); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | public override void RefreshControl() |
| | 87 | | { |
| 0 | 88 | | if (model == null) |
| 0 | 89 | | return; |
| | 90 | |
|
| 0 | 91 | | SetButtonsActive(model.areButtonsActive); |
| 0 | 92 | | SetBackground(model.background); |
| 0 | 93 | | SetTextActive(model.isTextActive); |
| 0 | 94 | | SetText(model.text); |
| 0 | 95 | | } |
| | 96 | |
|
| 0 | 97 | | public bool IsInteractable() { return slider.interactable; } |
| | 98 | |
|
| 0 | 99 | | public void SetInteractable(bool isActive) { slider.interactable = isActive; } |
| | 100 | |
|
| | 101 | | public void SetTextActive(bool isActive) |
| | 102 | | { |
| 0 | 103 | | model.isTextActive = isActive; |
| 0 | 104 | | text.gameObject.SetActive(isActive); |
| 0 | 105 | | } |
| | 106 | |
|
| | 107 | | public void SetButtonsActive(bool isActive) |
| | 108 | | { |
| 0 | 109 | | model.areButtonsActive = isActive; |
| 0 | 110 | | incrementButton.gameObject.SetActive(isActive); |
| 0 | 111 | | decrementButton.gameObject.SetActive(isActive); |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | public void SetText(string newText) |
| | 115 | | { |
| 0 | 116 | | model.text = newText; |
| | 117 | |
|
| 0 | 118 | | if (text == null) |
| 0 | 119 | | return; |
| | 120 | |
|
| 0 | 121 | | text.text = newText; |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public void SetBackground(Sprite image) |
| | 125 | | { |
| 0 | 126 | | model.background = image; |
| | 127 | |
|
| 0 | 128 | | if (image == null) |
| 0 | 129 | | return; |
| | 130 | |
|
| 0 | 131 | | background.sprite = image; |
| 0 | 132 | | } |
| | 133 | | } |