MaidPoseSelectorPane.cs 7.5 KB

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