| | 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 |
| | 8 | | { |
| | 9 | | [SerializeField] |
| | 10 | | private ColorToggle colorTogglePrefab; |
| | 11 | |
|
| | 12 | | [SerializeField] |
| | 13 | | private RectTransform colorContainer; |
| | 14 | |
|
| | 15 | | public event System.Action<Color> OnColorChanged; |
| | 16 | |
|
| | 17 | | private ColorToggle currentToggle; |
| | 18 | |
|
| 141 | 19 | | private List<ColorToggle> colorToggles = new List<ColorToggle>(); |
| | 20 | |
|
| | 21 | | public void Populate(IReadOnlyList<Color> colors) |
| | 22 | | { |
| 138 | 23 | | int colorNumber = colors.Count; |
| | 24 | |
|
| 3036 | 25 | | for (int i = 0; i < colorNumber; ++i) |
| | 26 | | { |
| 1380 | 27 | | ColorToggle newToggle = Instantiate(colorTogglePrefab, colorContainer); |
| 1380 | 28 | | newToggle.Initialize(colors[i], false); |
| 1380 | 29 | | newToggle.OnClicked += ToggleClicked; |
| 1380 | 30 | | colorToggles.Add(newToggle); |
| | 31 | | } |
| 138 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Select(Color color) |
| | 35 | | { |
| 311 | 36 | | ColorToggle toggle = GetColorToggle(color); |
| 311 | 37 | | if (toggle == currentToggle) |
| 156 | 38 | | return; |
| 155 | 39 | | Select(toggle); |
| 155 | 40 | | } |
| | 41 | |
|
| | 42 | | public void SelectRandom() |
| | 43 | | { |
| 0 | 44 | | if (colorToggles.Count == 0) |
| 0 | 45 | | return; |
| | 46 | |
|
| 0 | 47 | | ColorToggle toggle = colorToggles[Random.Range(0, colorToggles.Count)]; |
| 0 | 48 | | if (toggle == currentToggle) |
| 0 | 49 | | return; |
| | 50 | |
|
| 0 | 51 | | Select(toggle); |
| 0 | 52 | | OnColorChanged?.Invoke(currentToggle.color); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | private void Select(ColorToggle colorToggle) |
| | 56 | | { |
| 155 | 57 | | if (currentToggle != null) |
| | 58 | | { |
| 20 | 59 | | currentToggle.selected = false; |
| | 60 | | } |
| | 61 | |
|
| 155 | 62 | | currentToggle = colorToggle; |
| | 63 | |
|
| 155 | 64 | | if (colorToggle) |
| | 65 | | { |
| 155 | 66 | | colorToggle.selected = true; |
| | 67 | | } |
| 155 | 68 | | } |
| | 69 | |
|
| | 70 | | private void ToggleClicked(ColorToggle toggle) |
| | 71 | | { |
| 0 | 72 | | if (toggle == currentToggle) |
| 0 | 73 | | return; |
| | 74 | |
|
| 0 | 75 | | Select(toggle); |
| 0 | 76 | | OnColorChanged?.Invoke(currentToggle.color); |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | private ColorToggle GetColorToggle(Color color) |
| | 80 | | { |
| 311 | 81 | | int colorTogglesCount = colorToggles.Count; |
| 1420 | 82 | | for (int i = 0; i < colorTogglesCount; i++) |
| | 83 | | { |
| 710 | 84 | | Color current = colorToggles[i].color; |
| 710 | 85 | | if (color.AproxComparison(current)) |
| | 86 | | { |
| 311 | 87 | | return colorToggles[i]; |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | return null; |
| | 92 | | } |
| | 93 | | } |