| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | public interface ITagComponentView |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Fill the model and updates the tag with this data. |
| | 9 | | /// </summary> |
| | 10 | | /// <param name="model">Data to configure the tag.</param> |
| | 11 | | void Configure(TagComponentModel model); |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Set the tag text. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="newText">New text.</param> |
| | 17 | | void SetText(string newText); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Set the tag icon. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="newIcon">New Icon. Null for hide the icon.</param> |
| | 23 | | void SetIcon(Sprite newIcon); |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public class TagComponentView : BaseComponentView, ITagComponentView |
| | 27 | | { |
| | 28 | | [Header("Prefab References")] |
| | 29 | | [SerializeField] internal Image icon; |
| | 30 | | [SerializeField] internal TMP_Text text; |
| | 31 | |
|
| | 32 | | [Header("Configuration")] |
| | 33 | | [SerializeField] internal TagComponentModel model; |
| | 34 | |
|
| | 35 | | public override void PostInitialization() |
| | 36 | | { |
| | 37 | | ; |
| 15 | 38 | | Configure(model); |
| 15 | 39 | | } |
| | 40 | |
|
| | 41 | | public void Configure(TagComponentModel model) |
| | 42 | | { |
| 16 | 43 | | this.model = model; |
| 16 | 44 | | RefreshControl(); |
| 16 | 45 | | } |
| | 46 | |
|
| | 47 | | public override void RefreshControl() |
| | 48 | | { |
| 17 | 49 | | if (model == null) |
| 0 | 50 | | return; |
| | 51 | |
|
| 17 | 52 | | SetText(model.text); |
| 17 | 53 | | SetIcon(model.icon); |
| 17 | 54 | | } |
| | 55 | |
|
| | 56 | | public void SetText(string newText) |
| | 57 | | { |
| 18 | 58 | | model.text = newText; |
| | 59 | |
|
| 18 | 60 | | if (text == null) |
| 0 | 61 | | return; |
| | 62 | |
|
| 18 | 63 | | text.text = newText; |
| 18 | 64 | | } |
| | 65 | |
|
| | 66 | | public void SetIcon(Sprite newIcon) |
| | 67 | | { |
| 19 | 68 | | model.icon = newIcon; |
| | 69 | |
|
| 19 | 70 | | if (icon == null) |
| 0 | 71 | | return; |
| | 72 | |
|
| 19 | 73 | | icon.enabled = newIcon != null; |
| 19 | 74 | | icon.sprite = newIcon; |
| 19 | 75 | | } |
| | 76 | | } |