|
| 1 | +/** |
| 2 | + * $File: JCS_DropdownLanguage.cs $ |
| 3 | + * $Date: 2025-04-15 01:14:38 $ |
| 4 | + * $Revision: $ |
| 5 | + * $Creator: Jen-Chieh Shen $ |
| 6 | + * $Notice: See LICENSE.txt for modification and distribution information |
| 7 | + * Copyright © 2025 by Shen, Jen-Chieh $ |
| 8 | + */ |
| 9 | +using System.Collections.Generic; |
| 10 | +using UnityEngine; |
| 11 | +using UnityEngine.UI; |
| 12 | +using TMPro; |
| 13 | +using MyBox; |
| 14 | + |
| 15 | +namespace JCSUnity |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// A dropdown menu let you choose the language. |
| 19 | + /// </summary> |
| 20 | + public class JCS_DropdownLanguage : JCS_DropdownObject |
| 21 | + { |
| 22 | + /* Variables */ |
| 23 | + |
| 24 | + [Separator("Initialize Variables (JCS_DropdownLanguage)")] |
| 25 | + |
| 26 | + [Tooltip("List of supported languages.")] |
| 27 | + [SerializeField] |
| 28 | + private List<SystemLanguage> mOptions = new() |
| 29 | + { |
| 30 | + SystemLanguage.English, |
| 31 | + SystemLanguage.ChineseTraditional, |
| 32 | + }; |
| 33 | + |
| 34 | + [Tooltip("If true, remove all other options at the beginning.")] |
| 35 | + [SerializeField] |
| 36 | + private bool mRemoveAllOptions = true; |
| 37 | + |
| 38 | + /* Setter & Getter */ |
| 39 | + |
| 40 | + public List<SystemLanguage> Options { get { return mOptions; } set { this.mOptions = value; } } |
| 41 | + public bool RemoveAllOptions { get { return mRemoveAllOptions; } set { this.mRemoveAllOptions = value; } } |
| 42 | + |
| 43 | + /* Functions */ |
| 44 | + |
| 45 | + private void Start() |
| 46 | + { |
| 47 | + Refresh(); |
| 48 | + |
| 49 | + AddListener(); |
| 50 | + } |
| 51 | + |
| 52 | + private void AddListener() |
| 53 | + { |
| 54 | + mDropdownLegacy?.onValueChanged.AddListener(delegate |
| 55 | + { |
| 56 | + OnValueChanged_Legacy(mDropdownLegacy); |
| 57 | + }); |
| 58 | + |
| 59 | + mDropdownTMP?.onValueChanged.AddListener(delegate |
| 60 | + { |
| 61 | + OnValueChanged_TMP(mDropdownTMP); |
| 62 | + }); |
| 63 | + |
| 64 | + // Run once. |
| 65 | + OnValueChanged_Legacy(mDropdownLegacy); |
| 66 | + OnValueChanged_TMP(mDropdownTMP); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Refresh all options once. |
| 71 | + /// </summary> |
| 72 | + public void Refresh() |
| 73 | + { |
| 74 | + if (mRemoveAllOptions) |
| 75 | + ClearOptions(); |
| 76 | + |
| 77 | + foreach (SystemLanguage option in mOptions) |
| 78 | + { |
| 79 | + string text = JCS_Locale.SystemLangToString(option); |
| 80 | + |
| 81 | + JCS_UIUtil.Dropdown_AddOption(this, text); |
| 82 | + } |
| 83 | + |
| 84 | + // Default to the current windowed mode. |
| 85 | + { |
| 86 | + string text = JCS_Locale.SystemLangToString(JCS_AppManager.instance.systemLanguage); |
| 87 | + |
| 88 | + JCS_UIUtil.Dropdown_SetSelection(this, text); |
| 89 | + } |
| 90 | + |
| 91 | + JCS_UIUtil.Dropdown_RefreshSelection(this); |
| 92 | + } |
| 93 | + |
| 94 | + private void OnValueChanged_Legacy(Dropdown dropdown) |
| 95 | + { |
| 96 | + if (dropdown == null) |
| 97 | + return; |
| 98 | + |
| 99 | + string text = JCS_UIUtil.Dropdown_GetSelectedValue(dropdown); |
| 100 | + |
| 101 | + OnValueChanged(text); |
| 102 | + } |
| 103 | + private void OnValueChanged_TMP(TMP_Dropdown dropdown) |
| 104 | + { |
| 105 | + if (dropdown == null) |
| 106 | + return; |
| 107 | + |
| 108 | + string text = JCS_UIUtil.Dropdown_GetSelectedValue(dropdown); |
| 109 | + |
| 110 | + OnValueChanged(text); |
| 111 | + } |
| 112 | + private void OnValueChanged(string text) |
| 113 | + { |
| 114 | + SystemLanguage selected = JCS_Locale.StringToSystemLang(text); |
| 115 | + |
| 116 | + JCS_AppManager.instance.systemLanguage = selected; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments