MaidPoseSelectorPane.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 MaidPoseSelectorPane : BasePane
  8. {
  9. private static readonly string[] tabTranslations = new[] { "baseTab", "customTab" };
  10. private readonly MeidoManager meidoManager;
  11. private readonly Button poseLeftButton;
  12. private readonly Button poseRightButton;
  13. private readonly Button poseGroupLeftButton;
  14. private readonly Button poseGroupRightButton;
  15. private readonly Dropdown poseGroupDropdown;
  16. private readonly Dropdown poseDropdown;
  17. private readonly SelectionGrid poseModeGrid;
  18. private Dictionary<string, List<string>> CurrentPoseDict
  19. => customPoseMode ? Constants.CustomPoseDict : Constants.PoseDict;
  20. private List<string> CurrentPoseGroupList
  21. => customPoseMode ? Constants.CustomPoseGroupList : Constants.PoseGroupList;
  22. private string SelectedPoseGroup => CurrentPoseGroupList[poseGroupDropdown.SelectedItemIndex];
  23. private List<string> CurrentPoseList => CurrentPoseDict[SelectedPoseGroup];
  24. private int SelectedPoseIndex => poseDropdown.SelectedItemIndex;
  25. private string SelectedPose => CurrentPoseList[SelectedPoseIndex];
  26. private PoseInfo CurrentPoseInfo => new PoseInfo(SelectedPoseGroup, SelectedPose, customPoseMode);
  27. private bool customPoseMode;
  28. private bool poseListEnabled;
  29. private string previousPoseGroup;
  30. public MaidPoseSelectorPane(MeidoManager meidoManager)
  31. {
  32. Constants.CustomPoseChange += OnPresetChange;
  33. this.meidoManager = meidoManager;
  34. poseModeGrid = new SelectionGrid(Translation.GetArray("posePane", tabTranslations));
  35. poseModeGrid.ControlEvent += (s, a) => SetPoseMode();
  36. poseGroupDropdown = new Dropdown(Translation.GetArray("poseGroupDropdown", Constants.PoseGroupList));
  37. poseGroupDropdown.SelectionChange += (s, a) => ChangePoseGroup();
  38. poseDropdown = new Dropdown(UIPoseList());
  39. poseDropdown.SelectionChange += (s, a) => ChangePose();
  40. poseGroupLeftButton = new Button("<");
  41. poseGroupLeftButton.ControlEvent += (s, a) => poseGroupDropdown.Step(-1);
  42. poseGroupRightButton = new Button(">");
  43. poseGroupRightButton.ControlEvent += (s, a) => poseGroupDropdown.Step(1);
  44. poseLeftButton = new Button("<");
  45. poseLeftButton.ControlEvent += (s, a) => poseDropdown.Step(-1);
  46. poseRightButton = new Button(">");
  47. poseRightButton.ControlEvent += (s, a) => poseDropdown.Step(1);
  48. customPoseMode = poseModeGrid.SelectedItemIndex == 1;
  49. previousPoseGroup = SelectedPoseGroup;
  50. poseListEnabled = CurrentPoseList.Count > 0;
  51. }
  52. protected override void ReloadTranslation()
  53. {
  54. updating = true;
  55. poseModeGrid.SetItems(Translation.GetArray("posePane", tabTranslations));
  56. if (!customPoseMode)
  57. {
  58. poseGroupDropdown.SetDropdownItems(
  59. Translation.GetArray("poseGroupDropdown", Constants.PoseGroupList)
  60. );
  61. }
  62. updating = false;
  63. }
  64. public override void Draw()
  65. {
  66. const float buttonHeight = 30f;
  67. GUILayoutOption[] arrowLayoutOptions = {
  68. GUILayout.Width(buttonHeight),
  69. GUILayout.Height(buttonHeight)
  70. };
  71. const float dropdownButtonWidth = 153f;
  72. GUILayoutOption[] dropdownLayoutOptions = new GUILayoutOption[] {
  73. GUILayout.Height(buttonHeight),
  74. GUILayout.Width(dropdownButtonWidth)
  75. };
  76. GUI.enabled = meidoManager.HasActiveMeido && !meidoManager.ActiveMeido.Stop;
  77. poseModeGrid.Draw();
  78. MpsGui.WhiteLine();
  79. GUILayout.BeginHorizontal();
  80. poseGroupLeftButton.Draw(arrowLayoutOptions);
  81. poseGroupDropdown.Draw(dropdownLayoutOptions);
  82. poseGroupRightButton.Draw(arrowLayoutOptions);
  83. GUILayout.EndHorizontal();
  84. GUILayout.BeginHorizontal();
  85. GUI.enabled = GUI.enabled && poseListEnabled;
  86. poseLeftButton.Draw(arrowLayoutOptions);
  87. poseDropdown.Draw(dropdownLayoutOptions);
  88. poseRightButton.Draw(arrowLayoutOptions);
  89. GUILayout.EndHorizontal();
  90. GUI.enabled = true;
  91. }
  92. public override void UpdatePane()
  93. {
  94. updating = true;
  95. PoseInfo poseInfo = meidoManager.ActiveMeido.CachedPose;
  96. bool oldPoseMode = customPoseMode;
  97. poseModeGrid.SelectedItemIndex = poseInfo.CustomPose ? 1 : 0;
  98. int poseGroupIndex = CurrentPoseGroupList.IndexOf(poseInfo.PoseGroup);
  99. if (poseGroupIndex < 0) poseGroupIndex = 0;
  100. int poseIndex = CurrentPoseDict[poseInfo.PoseGroup].IndexOf(poseInfo.Pose);
  101. if (poseIndex < 0) poseIndex = 0;
  102. if (oldPoseMode != customPoseMode)
  103. {
  104. string[] list = customPoseMode
  105. ? CurrentPoseGroupList.ToArray()
  106. : Translation.GetArray("poseGroupDropdown", CurrentPoseGroupList);
  107. poseGroupDropdown.SetDropdownItems(list);
  108. }
  109. poseGroupDropdown.SelectedItemIndex = poseGroupIndex;
  110. poseDropdown.SelectedItemIndex = poseIndex;
  111. updating = false;
  112. }
  113. private void OnPresetChange(object sender, PresetChangeEventArgs args)
  114. {
  115. if (args == PresetChangeEventArgs.Empty)
  116. {
  117. if (poseModeGrid.SelectedItemIndex == 1)
  118. {
  119. updating = true;
  120. poseGroupDropdown.SetDropdownItems(CurrentPoseGroupList.ToArray(), 0);
  121. poseDropdown.SetDropdownItems(UIPoseList(), 0);
  122. updating = false;
  123. }
  124. }
  125. else
  126. {
  127. updating = true;
  128. poseModeGrid.SelectedItemIndex = 1;
  129. poseGroupDropdown.SetDropdownItems(
  130. CurrentPoseGroupList.ToArray(), CurrentPoseGroupList.IndexOf(args.Category)
  131. );
  132. updating = false;
  133. poseDropdown.SetDropdownItems(UIPoseList(), CurrentPoseList.IndexOf(args.Path));
  134. poseListEnabled = true;
  135. }
  136. }
  137. private void SetPoseMode()
  138. {
  139. customPoseMode = poseModeGrid.SelectedItemIndex == 1;
  140. if (updating) return;
  141. string[] list = customPoseMode
  142. ? CurrentPoseGroupList.ToArray()
  143. : Translation.GetArray("poseGroupDropdown", CurrentPoseGroupList);
  144. poseGroupDropdown.SetDropdownItems(list, 0);
  145. }
  146. private void ChangePoseGroup()
  147. {
  148. poseListEnabled = CurrentPoseList.Count > 0;
  149. if (previousPoseGroup == SelectedPoseGroup)
  150. {
  151. poseDropdown.SelectedItemIndex = 0;
  152. }
  153. else
  154. {
  155. previousPoseGroup = SelectedPoseGroup;
  156. poseDropdown.SetDropdownItems(UIPoseList(), 0);
  157. }
  158. }
  159. private void ChangePose()
  160. {
  161. if (!poseListEnabled || updating) return;
  162. meidoManager.ActiveMeido.SetPose(CurrentPoseInfo);
  163. }
  164. private string[] UIPoseList()
  165. {
  166. return CurrentPoseList.Count == 0
  167. ? new[] { "No Poses" }
  168. : CurrentPoseList
  169. .Select((pose, i) => $"{i + 1}:{(customPoseMode ? Path.GetFileNameWithoutExtension(pose) : pose)}")
  170. .ToArray();
  171. }
  172. }
  173. }