HandPresetPane.cs 4.8 KB

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