HandPresetPane.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.IO;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace COM3D2.MeidoPhotoStudio.Plugin
  6. {
  7. public class HandPresetPane : BasePane
  8. {
  9. private readonly MeidoManager meidoManager;
  10. private readonly Dropdown presetCategoryDropdown;
  11. private readonly Button nextCategoryButton;
  12. private readonly Button previousCategoryButton;
  13. private readonly Dropdown presetDropdown;
  14. private readonly Button nextPresetButton;
  15. private readonly Button previousPresetButton;
  16. private readonly Button leftHandButton;
  17. private readonly Button rightHandButton;
  18. private string SelectedCategory => Constants.CustomHandGroupList[presetCategoryDropdown.SelectedItemIndex];
  19. private List<string> CurrentPresetList => Constants.CustomHandDict[SelectedCategory];
  20. private string CurrentPreset => CurrentPresetList[presetDropdown.SelectedItemIndex];
  21. private string previousCategory;
  22. private bool presetListEnabled = true;
  23. public HandPresetPane(MeidoManager meidoManager)
  24. {
  25. Constants.CustomHandChange += OnPresetChange;
  26. this.meidoManager = meidoManager;
  27. presetCategoryDropdown = new Dropdown(Constants.CustomHandGroupList.ToArray());
  28. presetCategoryDropdown.SelectionChange += (s, a) => ChangePresetCategory();
  29. nextCategoryButton = new Button(">");
  30. nextCategoryButton.ControlEvent += (s, a) => presetCategoryDropdown.Step(1);
  31. previousCategoryButton = new Button("<");
  32. previousCategoryButton.ControlEvent += (s, a) => presetCategoryDropdown.Step(-1);
  33. presetDropdown = new Dropdown(UIPresetList());
  34. nextPresetButton = new Button(">");
  35. nextPresetButton.ControlEvent += (s, a) => presetDropdown.Step(1);
  36. previousPresetButton = new Button("<");
  37. previousPresetButton.ControlEvent += (s, a) => presetDropdown.Step(-1);
  38. leftHandButton = new Button(Translation.Get("handPane", "leftHand"));
  39. leftHandButton.ControlEvent += (s, a) => SetHandPreset(right: false);
  40. rightHandButton = new Button(Translation.Get("handPane", "rightHand"));
  41. rightHandButton.ControlEvent += (s, a) => SetHandPreset(right: true);
  42. previousCategory = SelectedCategory;
  43. presetListEnabled = CurrentPresetList.Count > 0;
  44. }
  45. protected override void ReloadTranslation()
  46. {
  47. leftHandButton.Label = Translation.Get("handPane", "leftHand");
  48. rightHandButton.Label = Translation.Get("handPane", "rightHand");
  49. if (CurrentPresetList.Count == 0) presetDropdown.SetDropdownItems(UIPresetList());
  50. }
  51. public override void Draw()
  52. {
  53. GUILayoutOption dropdownWidth = GUILayout.Width(156f);
  54. GUILayoutOption noExpandWidth = GUILayout.ExpandWidth(false);
  55. GUI.enabled = meidoManager.HasActiveMeido;
  56. GUILayout.BeginHorizontal();
  57. presetCategoryDropdown.Draw(dropdownWidth);
  58. previousCategoryButton.Draw(noExpandWidth);
  59. nextCategoryButton.Draw(noExpandWidth);
  60. GUILayout.EndHorizontal();
  61. GUI.enabled = GUI.enabled && presetListEnabled;
  62. GUILayout.BeginHorizontal();
  63. presetDropdown.Draw(dropdownWidth);
  64. previousPresetButton.Draw(noExpandWidth);
  65. nextPresetButton.Draw(noExpandWidth);
  66. GUILayout.EndHorizontal();
  67. GUILayout.BeginHorizontal();
  68. rightHandButton.Draw();
  69. leftHandButton.Draw();
  70. GUILayout.EndHorizontal();
  71. GUI.enabled = true;
  72. }
  73. private void ChangePresetCategory()
  74. {
  75. presetListEnabled = CurrentPresetList.Count > 0;
  76. if (previousCategory == SelectedCategory) presetDropdown.SelectedItemIndex = 0;
  77. else
  78. {
  79. previousCategory = SelectedCategory;
  80. presetDropdown.SetDropdownItems(UIPresetList(), 0);
  81. }
  82. }
  83. private void SetHandPreset(bool right = false)
  84. {
  85. if (!meidoManager.HasActiveMeido) return;
  86. meidoManager.ActiveMeido.SetHandPreset(CurrentPreset, right);
  87. }
  88. private void OnPresetChange(object sender, PresetChangeEventArgs args)
  89. {
  90. if (args == PresetChangeEventArgs.Empty)
  91. {
  92. presetCategoryDropdown.SetDropdownItems(Constants.CustomHandGroupList.ToArray(), 0);
  93. presetDropdown.SetDropdownItems(UIPresetList(), 0);
  94. }
  95. else
  96. {
  97. presetCategoryDropdown.SetDropdownItems(
  98. Constants.CustomHandGroupList.ToArray(), Constants.CustomHandGroupList.IndexOf(args.Category)
  99. );
  100. presetDropdown.SetDropdownItems(UIPresetList(), CurrentPresetList.IndexOf(args.Path));
  101. }
  102. }
  103. private string[] UIPresetList()
  104. {
  105. return CurrentPresetList.Count == 0
  106. ? new[] { Translation.Get("handPane", "noPresetsMessage") }
  107. : CurrentPresetList.Select(Path.GetFileNameWithoutExtension).ToArray();
  108. }
  109. }
  110. }