| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Events; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | namespace DCL.SettingsPanelHUD.Sections |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// MonoBehaviour that represents a template for a menu button. |
| | 11 | | /// It will be instantiated on the left part of the main settings panel and will be associated to a specific SECTION |
| | 12 | | /// </summary> |
| | 13 | | public class SettingsButtonEntry : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler |
| | 14 | | { |
| | 15 | | [SerializeField] |
| | 16 | | private Image icon; |
| | 17 | |
|
| | 18 | | [SerializeField] |
| | 19 | | private TextMeshProUGUI text; |
| | 20 | |
|
| | 21 | | [SerializeField] |
| | 22 | | private Button button; |
| | 23 | |
|
| | 24 | | [SerializeField] |
| | 25 | | private Image backgroundImage; |
| | 26 | |
|
| | 27 | | [SerializeField] |
| | 28 | | private Color textColorOnSelect; |
| | 29 | |
|
| | 30 | | [SerializeField] |
| | 31 | | private Color backgroundColorOnSelect; |
| | 32 | |
|
| | 33 | | private Color originalIconColor; |
| | 34 | | private Color originalTextColor; |
| | 35 | | private Color originalBackgroundColor; |
| | 36 | | private bool isSelected; |
| | 37 | |
|
| 0 | 38 | | public string settingName { get => text.text; } |
| | 39 | |
|
| | 40 | | public void Initialize(Sprite icon, string text) |
| | 41 | | { |
| 3 | 42 | | this.icon.sprite = icon; |
| 3 | 43 | | this.text.text = text; |
| | 44 | |
|
| 3 | 45 | | originalIconColor = this.icon.color; |
| 3 | 46 | | originalTextColor = this.text.color; |
| 3 | 47 | | originalBackgroundColor = backgroundImage.color; |
| 3 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Adds an action to execute when the button is clicked. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="action">Action to execute.</param> |
| 6 | 54 | | public void ConfigureAction(UnityAction action) { button.onClick.AddListener(action); } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Mark this button as selected. It will change the visual aspect of the button. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="isSelected">True for select the button.</param> |
| | 60 | | public void MarkAsSelected(bool isSelected) |
| | 61 | | { |
| 6 | 62 | | icon.color = isSelected ? textColorOnSelect : originalIconColor; |
| 6 | 63 | | text.color = isSelected ? textColorOnSelect : originalTextColor; |
| 6 | 64 | | backgroundImage.color = isSelected ? backgroundColorOnSelect : originalBackgroundColor; |
| 6 | 65 | | this.isSelected = isSelected; |
| 6 | 66 | | } |
| | 67 | |
|
| | 68 | | public void OnPointerEnter(PointerEventData eventData) |
| | 69 | | { |
| 0 | 70 | | if (isSelected) |
| 0 | 71 | | return; |
| | 72 | |
|
| 0 | 73 | | icon.color = textColorOnSelect; |
| 0 | 74 | | backgroundImage.color = backgroundColorOnSelect; |
| 0 | 75 | | text.color = textColorOnSelect; |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public void OnPointerExit(PointerEventData eventData) |
| | 79 | | { |
| 0 | 80 | | if (isSelected) |
| 0 | 81 | | return; |
| | 82 | |
|
| 0 | 83 | | icon.color = originalTextColor; |
| 0 | 84 | | backgroundImage.color = originalBackgroundColor; |
| 0 | 85 | | text.color = originalTextColor; |
| 0 | 86 | | } |
| | 87 | | } |
| | 88 | | } |