| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL.SettingsCommon.SettingsControllers.BaseControllers; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.SettingsPanelHUD.Controls |
| | 8 | | { |
| | 9 | | public class DropdownControlView : SettingsControlView |
| | 10 | | { |
| | 11 | | [SerializeField] private TMP_Dropdown dropdown; |
| | 12 | |
|
| | 13 | | private SpinBoxSettingsControlController spinBoxController; |
| | 14 | | private PointerClickEventInterceptor pointerClick; |
| | 15 | |
|
| | 16 | | private void Awake() |
| | 17 | | { |
| 8 | 18 | | pointerClick = dropdown.GetComponent<PointerClickEventInterceptor>(); |
| 8 | 19 | | } |
| | 20 | |
|
| | 21 | | public override void Initialize(SettingsControlModel model, SettingsControlController controller) |
| | 22 | | { |
| | 23 | | // we use spinbox control model and control controller for compatibility |
| 8 | 24 | | SetLabels(((SpinBoxControlModel)model).spinBoxLabels); |
| | 25 | |
|
| 8 | 26 | | spinBoxController = (SpinBoxSettingsControlController)controller; |
| 8 | 27 | | spinBoxController.OnSetLabels += SetLabels; |
| 8 | 28 | | spinBoxController.OnCurrentLabelChange += SetOption; |
| | 29 | |
|
| 8 | 30 | | base.Initialize(model, spinBoxController); |
| 8 | 31 | | spinBoxController.UpdateSetting(dropdown.value); |
| | 32 | |
|
| 8 | 33 | | dropdown.onValueChanged.AddListener(spinBoxValue => |
| | 34 | | { |
| 0 | 35 | | ApplySetting(spinBoxValue); |
| 0 | 36 | | }); |
| | 37 | |
|
| 8 | 38 | | pointerClick.PointerClicked += spinBoxController.OnPointerClicked; |
| 8 | 39 | | } |
| | 40 | |
|
| | 41 | | private void SetOption(string option) |
| | 42 | | { |
| 1 | 43 | | dropdown.captionText.text = option; |
| 1 | 44 | | } |
| | 45 | | private void SetLabels(string[] labels) |
| | 46 | | { |
| 10 | 47 | | if (labels.Length == 0) |
| 1 | 48 | | return; |
| | 49 | |
|
| 9 | 50 | | dropdown.ClearOptions(); |
| 38 | 51 | | List<TMP_Dropdown.OptionData> dropdownOptions = labels.Select(l => new TMP_Dropdown.OptionData(l)).ToList(); |
| 76 | 52 | | foreach (TMP_Dropdown.OptionData data in dropdownOptions) |
| | 53 | | { |
| 29 | 54 | | dropdown.options.Add(data); |
| | 55 | | } |
| | 56 | |
|
| 9 | 57 | | dropdown.Hide(); |
| 9 | 58 | | dropdown.RefreshShownValue(); |
| 9 | 59 | | } |
| | 60 | |
|
| | 61 | | protected override void OnDestroy() |
| | 62 | | { |
| 8 | 63 | | base.OnDestroy(); |
| 8 | 64 | | pointerClick.PointerClicked -= spinBoxController.OnPointerClicked; |
| | 65 | |
|
| 8 | 66 | | if (spinBoxController != null) |
| | 67 | | { |
| 8 | 68 | | spinBoxController.OnSetLabels -= SetLabels; |
| 8 | 69 | | spinBoxController.OnCurrentLabelChange -= SetOption; |
| | 70 | | } |
| 8 | 71 | | } |
| | 72 | |
|
| | 73 | | public override void RefreshControl() |
| | 74 | | { |
| 8 | 75 | | base.RefreshControl(); |
| | 76 | |
|
| 8 | 77 | | int newValue = (int)spinBoxController.GetStoredValue(); |
| 8 | 78 | | if (dropdown.value != newValue) |
| 3 | 79 | | dropdown.value = newValue; |
| 8 | 80 | | } |
| | 81 | |
|
| | 82 | | } |
| | 83 | | } |