| | 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; } |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Set the button text. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="newText">New text.</param> |
| | 16 | | void SetText(string newText); |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Set the button icon. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="newIcon">New Icon. Null for hide the icon.</param> |
| | 22 | | void SetIcon(Sprite newIcon); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Set the button clickable or not. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="isInteractable">Clickable or not</param> |
| | 28 | | void SetInteractable(bool isInteractable); |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Return if the button is Interactable or not |
| | 32 | | /// </summary> |
| | 33 | | bool IsInteractable(); |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public class ButtonComponentView : BaseComponentView, IButtonComponentView, IComponentModelConfig |
| | 37 | | { |
| | 38 | | [Header("Prefab References")] |
| | 39 | | [SerializeField] internal Button button; |
| | 40 | | [SerializeField] internal TMP_Text text; |
| | 41 | | [SerializeField] internal Image icon; |
| | 42 | |
|
| | 43 | | [Header("Configuration")] |
| | 44 | | [SerializeField] internal ButtonComponentModel model; |
| | 45 | |
|
| 0 | 46 | | public Button.ButtonClickedEvent onClick => button?.onClick; |
| | 47 | |
|
| | 48 | | public void Configure(BaseComponentModel newModel) |
| | 49 | | { |
| 1 | 50 | | model = (ButtonComponentModel)newModel; |
| 1 | 51 | | RefreshControl(); |
| 1 | 52 | | } |
| | 53 | |
|
| | 54 | | public override void RefreshControl() |
| | 55 | | { |
| 1 | 56 | | if (model == null) |
| 0 | 57 | | return; |
| | 58 | |
|
| 1 | 59 | | SetText(model.text); |
| 1 | 60 | | SetIcon(model.icon); |
| 1 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | public bool IsInteractable() { return button.interactable; } |
| | 64 | |
|
| 30 | 65 | | public void SetInteractable(bool isActive) { button.interactable = isActive;} |
| | 66 | |
|
| | 67 | | public void SetText(string newText) |
| | 68 | | { |
| 6 | 69 | | model.text = newText; |
| | 70 | |
|
| 6 | 71 | | if (text == null) |
| 0 | 72 | | return; |
| | 73 | |
|
| 6 | 74 | | text.text = newText; |
| 6 | 75 | | } |
| | 76 | |
|
| | 77 | | public void SetIcon(Sprite newIcon) |
| | 78 | | { |
| 3 | 79 | | model.icon = newIcon; |
| | 80 | |
|
| 3 | 81 | | if (icon == null) |
| 0 | 82 | | return; |
| | 83 | |
|
| 3 | 84 | | icon.gameObject.SetActive(newIcon != null); |
| 3 | 85 | | icon.sprite = newIcon; |
| 3 | 86 | | } |
| | 87 | | } |