| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | [RequireComponent(typeof(ToggleGroup))] |
| | 7 | | public class ColorSelector : MonoBehaviour, IColorSelector |
| | 8 | | { |
| | 9 | | [SerializeField] private ColorToggle colorTogglePrefab; |
| | 10 | | [SerializeField] private RectTransform colorContainer; |
| | 11 | |
|
| | 12 | | public event System.Action<Color> OnColorSelectorChange; |
| | 13 | |
|
| | 14 | | private ColorToggle currentToggle; |
| 37 | 15 | | private readonly List<ColorToggle> colorToggles = new (); |
| | 16 | |
|
| | 17 | | public void Populate(IReadOnlyList<Color> colors) |
| | 18 | | { |
| 2 | 19 | | int colorNumber = colors.Count; |
| | 20 | |
|
| 48 | 21 | | for (var i = 0; i < colorNumber; ++i) |
| | 22 | | { |
| 22 | 23 | | ColorToggle newToggle = Instantiate(colorTogglePrefab, colorContainer); |
| 22 | 24 | | newToggle.Initialize(colors[i], false); |
| 22 | 25 | | newToggle.OnClicked += ToggleClicked; |
| 22 | 26 | | colorToggles.Add(newToggle); |
| | 27 | | } |
| 2 | 28 | | } |
| | 29 | |
|
| | 30 | | public void Cleanup() |
| | 31 | | { |
| 4 | 32 | | foreach (var colorToggle in colorToggles) |
| | 33 | | { |
| | 34 | | #if UNITY_EDITOR |
| 0 | 35 | | if (!Application.isPlaying) |
| | 36 | | { |
| 0 | 37 | | DestroyImmediate(colorToggle.gameObject); |
| 0 | 38 | | continue; |
| | 39 | | } |
| | 40 | | #endif |
| 0 | 41 | | Destroy(colorToggle.gameObject); |
| | 42 | | } |
| | 43 | |
|
| 2 | 44 | | colorToggles.Clear(); |
| 2 | 45 | | } |
| | 46 | |
|
| | 47 | | public void Select(Color color) |
| | 48 | | { |
| 1 | 49 | | ColorToggle toggle = GetColorToggle(color); |
| | 50 | |
|
| 1 | 51 | | if (toggle == currentToggle) |
| 1 | 52 | | return; |
| | 53 | |
|
| 0 | 54 | | Select(toggle); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public void SelectRandom() |
| | 58 | | { |
| 0 | 59 | | if (colorToggles.Count == 0) |
| 0 | 60 | | return; |
| | 61 | |
|
| 0 | 62 | | ColorToggle toggle = colorToggles[Random.Range(0, colorToggles.Count)]; |
| 0 | 63 | | if (toggle == currentToggle) |
| 0 | 64 | | return; |
| | 65 | |
|
| 0 | 66 | | Select(toggle); |
| 0 | 67 | | OnColorSelectorChange?.Invoke(currentToggle.Color); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | private void Select(ColorToggle colorToggle) |
| | 71 | | { |
| 0 | 72 | | if (currentToggle != null) |
| 0 | 73 | | currentToggle.Selected = false; |
| | 74 | |
|
| 0 | 75 | | currentToggle = colorToggle; |
| | 76 | |
|
| 0 | 77 | | if (colorToggle) |
| 0 | 78 | | colorToggle.Selected = true; |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | private void ToggleClicked(ColorToggle toggle) |
| | 82 | | { |
| 0 | 83 | | if (toggle == currentToggle) |
| 0 | 84 | | return; |
| | 85 | |
|
| 0 | 86 | | Select(toggle); |
| 0 | 87 | | OnColorSelectorChange?.Invoke(currentToggle.Color); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | private ColorToggle GetColorToggle(Color color) |
| | 91 | | { |
| 1 | 92 | | int colorTogglesCount = colorToggles.Count; |
| 2 | 93 | | for (var i = 0; i < colorTogglesCount; i++) |
| | 94 | | { |
| 0 | 95 | | Color current = colorToggles[i].Color; |
| 0 | 96 | | if (color.AproxComparison(current)) |
| 0 | 97 | | return colorToggles[i]; |
| | 98 | | } |
| | 99 | |
|
| 1 | 100 | | return null; |
| | 101 | | } |
| | 102 | | } |