| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | public interface IButtonComponentView |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Event that will be triggered when the button is clicked. |
| | 9 | | /// </summary> |
| | 10 | | Button.ButtonClickedEvent onClick { get; set; } |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Fill the model and updates the button with this data. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="model">Data to configure the button.</param> |
| | 16 | | void Configure(ButtonComponentModel model); |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Set the button text. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="newText">New text.</param> |
| | 22 | | void SetText(string newText); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Set the button icon. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="newIcon">New Icon. Null for hide the icon.</param> |
| | 28 | | void SetIcon(Sprite newIcon); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | public class ButtonComponentView : BaseComponentView, IButtonComponentView |
| | 32 | | { |
| | 33 | | [Header("Prefab References")] |
| | 34 | | [SerializeField] internal Button button; |
| | 35 | | [SerializeField] internal TMP_Text text; |
| | 36 | | [SerializeField] internal Image icon; |
| | 37 | |
|
| | 38 | | [Header("Configuration")] |
| | 39 | | [SerializeField] internal ButtonComponentModel model; |
| | 40 | |
|
| | 41 | | public Button.ButtonClickedEvent onClick |
| | 42 | | { |
| | 43 | | get |
| | 44 | | { |
| 177 | 45 | | if (button == null) |
| 0 | 46 | | return null; |
| | 47 | |
|
| 177 | 48 | | return button.onClick; |
| | 49 | | } |
| | 50 | | set |
| | 51 | | { |
| 79 | 52 | | model.onClick = value; |
| | 53 | |
|
| 79 | 54 | | if (button != null) |
| | 55 | | { |
| 79 | 56 | | button.onClick.RemoveAllListeners(); |
| 79 | 57 | | button.onClick.AddListener(() => |
| | 58 | | { |
| 0 | 59 | | value?.Invoke(); |
| 0 | 60 | | }); |
| | 61 | | } |
| 79 | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| 146 | 65 | | public override void PostInitialization() { Configure(model); } |
| | 66 | |
|
| | 67 | | public virtual void Configure(ButtonComponentModel model) |
| | 68 | | { |
| 76 | 69 | | this.model = model; |
| 76 | 70 | | RefreshControl(); |
| 76 | 71 | | } |
| | 72 | |
|
| | 73 | | public override void RefreshControl() |
| | 74 | | { |
| 79 | 75 | | if (model == null) |
| 0 | 76 | | return; |
| | 77 | |
|
| 79 | 78 | | SetText(model.text); |
| 79 | 79 | | SetIcon(model.icon); |
| 79 | 80 | | onClick = model.onClick; |
| 79 | 81 | | } |
| | 82 | |
|
| | 83 | | public override void Dispose() |
| | 84 | | { |
| 153 | 85 | | base.Dispose(); |
| | 86 | |
|
| 153 | 87 | | if (button == null) |
| 0 | 88 | | return; |
| | 89 | |
|
| 153 | 90 | | button.onClick.RemoveAllListeners(); |
| 153 | 91 | | } |
| | 92 | |
|
| | 93 | | public void SetText(string newText) |
| | 94 | | { |
| 82 | 95 | | model.text = newText; |
| | 96 | |
|
| 82 | 97 | | if (text == null) |
| 0 | 98 | | return; |
| | 99 | |
|
| 82 | 100 | | text.text = newText; |
| 82 | 101 | | } |
| | 102 | |
|
| | 103 | | public void SetIcon(Sprite newIcon) |
| | 104 | | { |
| 85 | 105 | | model.icon = newIcon; |
| | 106 | |
|
| 85 | 107 | | if (icon == null) |
| 0 | 108 | | return; |
| | 109 | |
|
| 85 | 110 | | icon.gameObject.SetActive(newIcon != null); |
| 85 | 111 | | icon.sprite = newIcon; |
| 85 | 112 | | } |
| | 113 | | } |