| | 1 | | using System; |
| | 2 | | using System.Text; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | public class PlaceCategoryButton : MonoBehaviour |
| | 8 | | { |
| | 9 | | [SerializeField] internal Button button; |
| | 10 | | [SerializeField] internal Image buttonBackgroundImage; |
| | 11 | | [SerializeField] internal Image iconImage; |
| | 12 | | [SerializeField] internal TMP_Text text; |
| | 13 | | [SerializeField] internal Color selectedBackgroundColor; |
| | 14 | | [SerializeField] internal Color selectedTextColor; |
| | 15 | | [SerializeField] internal Color deselectedBackgroundColor; |
| | 16 | | [SerializeField] internal Color deselectedTextColor; |
| | 17 | |
|
| | 18 | | [SerializeField] internal PlaceCategoryIcons cagtegoryIconsSO; |
| | 19 | |
|
| | 20 | | public event Action<string, bool> OnClick; |
| | 21 | |
|
| 0 | 22 | | public string currenCategory { get; private set; } |
| | 23 | |
|
| | 24 | | private bool isCurrentlySelected; |
| | 25 | |
|
| | 26 | | private void Awake() |
| | 27 | | { |
| 1 | 28 | | if (button == null) |
| 1 | 29 | | return; |
| | 30 | |
|
| 0 | 31 | | button.onClick.AddListener(() => OnClick?.Invoke(currenCategory, !isCurrentlySelected)); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public void SetCategory(string categoryId, string nameToShow) |
| | 35 | | { |
| 0 | 36 | | currenCategory = categoryId; |
| 0 | 37 | | text.text = RemoveNonASCIICharacters(nameToShow) |
| | 38 | | .ToUpper() |
| | 39 | | .Trim(); |
| | 40 | |
|
| 0 | 41 | | var iconFound = false; |
| 0 | 42 | | foreach (PlaceCategoryIcon categoryIcon in cagtegoryIconsSO.categoryIcons) |
| | 43 | | { |
| 0 | 44 | | if (categoryIcon.category != categoryId) |
| | 45 | | continue; |
| | 46 | |
|
| 0 | 47 | | iconImage.sprite = categoryIcon.icon; |
| 0 | 48 | | iconFound = true; |
| 0 | 49 | | break; |
| | 50 | | } |
| | 51 | |
|
| 0 | 52 | | iconImage.gameObject.SetActive(iconFound); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void SetStatus(bool isSelected) |
| | 56 | | { |
| 0 | 57 | | isCurrentlySelected = isSelected; |
| 0 | 58 | | buttonBackgroundImage.color = isSelected ? selectedBackgroundColor : deselectedBackgroundColor; |
| 0 | 59 | | text.color = isSelected ? selectedTextColor : deselectedTextColor; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | private static string RemoveNonASCIICharacters(string text) |
| | 63 | | { |
| 0 | 64 | | StringBuilder sb = new StringBuilder(); |
| | 65 | |
|
| 0 | 66 | | foreach (char c in text) |
| 0 | 67 | | if (c <= 127) sb.Append(c); |
| | 68 | |
|
| 0 | 69 | | return sb.ToString(); |
| | 70 | | } |
| | 71 | | } |