| | 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 | |
|
| | 15 | | public override void Initialize(SettingsControlModel controlConfig, SettingsControlController settingsControlCon |
| | 16 | | { |
| | 17 | | //we use spinbox control model and control controller for compatibility |
| 5 | 18 | | SetLabels(((SpinBoxControlModel)controlConfig).spinBoxLabels); |
| | 19 | |
|
| 5 | 20 | | spinBoxController = (SpinBoxSettingsControlController)settingsControlController; |
| 5 | 21 | | spinBoxController.OnSetLabels += SetLabels; |
| 5 | 22 | | spinBoxController.OnCurrentLabelChange += SetOption; |
| | 23 | |
|
| 5 | 24 | | base.Initialize(controlConfig, spinBoxController); |
| 5 | 25 | | spinBoxController.UpdateSetting(dropdown.value); |
| | 26 | |
|
| 5 | 27 | | dropdown.onValueChanged.AddListener(spinBoxValue => |
| | 28 | | { |
| 0 | 29 | | ApplySetting(spinBoxValue); |
| 0 | 30 | | }); |
| 5 | 31 | | } |
| | 32 | | private void SetOption(string option) |
| | 33 | | { |
| 1 | 34 | | dropdown.captionText.text = option; |
| 1 | 35 | | } |
| | 36 | | private void SetLabels(string[] labels) |
| | 37 | | { |
| 7 | 38 | | if (labels.Length == 0) |
| 1 | 39 | | return; |
| | 40 | |
|
| 6 | 41 | | dropdown.ClearOptions(); |
| 30 | 42 | | List<TMP_Dropdown.OptionData> dropdownOptions = labels.Select(l => new TMP_Dropdown.OptionData(l)).ToList(); |
| 60 | 43 | | foreach (TMP_Dropdown.OptionData data in dropdownOptions) |
| | 44 | | { |
| 24 | 45 | | dropdown.options.Add(data); |
| | 46 | | } |
| 6 | 47 | | dropdown.RefreshShownValue(); |
| 6 | 48 | | } |
| | 49 | |
|
| | 50 | | protected override void OnDestroy() |
| | 51 | | { |
| 5 | 52 | | base.OnDestroy(); |
| | 53 | |
|
| 5 | 54 | | if (spinBoxController != null) |
| | 55 | | { |
| 5 | 56 | | spinBoxController.OnSetLabels -= SetLabels; |
| 5 | 57 | | spinBoxController.OnCurrentLabelChange -= SetOption; |
| | 58 | | } |
| 5 | 59 | | } |
| | 60 | |
|
| | 61 | | public override void RefreshControl() |
| | 62 | | { |
| 5 | 63 | | base.RefreshControl(); |
| | 64 | |
|
| 5 | 65 | | int newValue = (int)spinBoxController.GetStoredValue(); |
| 5 | 66 | | if (dropdown.value != newValue) |
| 5 | 67 | | dropdown.value = newValue; |
| 5 | 68 | | } |
| | 69 | |
|
| | 70 | | } |
| | 71 | | } |