AttachPropPane.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace COM3D2.MeidoPhotoStudio.Plugin
  6. {
  7. using static DragPointManager;
  8. internal class AttachPropPane : BasePane
  9. {
  10. private PropManager propManager;
  11. private MeidoManager meidoManager;
  12. private Dictionary<AttachPoint, Toggle> Toggles = new Dictionary<AttachPoint, Toggle>();
  13. private static readonly Dictionary<AttachPoint, string> toggleTranslation =
  14. new Dictionary<AttachPoint, string>()
  15. {
  16. [AttachPoint.Head] = "head",
  17. [AttachPoint.Neck] = "neck",
  18. [AttachPoint.UpperArmL] = "upperArmL",
  19. [AttachPoint.UpperArmR] = "upperArmR",
  20. [AttachPoint.ForearmL] = "forearmL",
  21. [AttachPoint.ForearmR] = "forearmR",
  22. [AttachPoint.MuneL] = "muneL",
  23. [AttachPoint.MuneR] = "muneR",
  24. [AttachPoint.HandL] = "handL",
  25. [AttachPoint.HandR] = "handR",
  26. [AttachPoint.Pelvis] = "pelvis",
  27. [AttachPoint.ThighL] = "thighL",
  28. [AttachPoint.ThighR] = "thighR",
  29. [AttachPoint.CalfL] = "calfL",
  30. [AttachPoint.CalfR] = "calfR",
  31. [AttachPoint.FootL] = "footL",
  32. [AttachPoint.FootR] = "footR",
  33. };
  34. private Toggle keepWorldPositionToggle;
  35. private Button previousMaidButton;
  36. private Button nextMaidButton;
  37. private Dropdown meidoDropdown;
  38. private Button previousDoguButton;
  39. private Button nextDoguButton;
  40. private Dropdown doguDropdown;
  41. private bool meidoDropdownActive = false;
  42. private bool doguDropdownActive = false;
  43. private bool PaneActive => meidoDropdownActive && doguDropdownActive;
  44. private string header;
  45. private int selectedMaid = 0;
  46. public AttachPropPane(MeidoManager meidoManager, PropManager propManager)
  47. {
  48. this.header = Translation.Get("attachPropPane", "header");
  49. this.propManager = propManager;
  50. this.meidoManager = meidoManager;
  51. this.propManager.DoguListChange += (s, a) => SetDoguDropdown();
  52. this.meidoManager.EndCallMeidos += (s, a) => SetMeidoDropdown();
  53. this.meidoDropdown = new Dropdown(new[] { Translation.Get("systemMessage", "noMaids") });
  54. this.meidoDropdown.SelectionChange += (s, a) => SwitchMaid();
  55. this.previousMaidButton = new Button("<");
  56. this.previousMaidButton.ControlEvent += (s, a) => this.meidoDropdown.Step(-1);
  57. this.nextMaidButton = new Button(">");
  58. this.nextMaidButton.ControlEvent += (s, a) => this.meidoDropdown.Step(1);
  59. this.doguDropdown = new Dropdown(new[] { Translation.Get("systemMessage", "noProps") });
  60. this.doguDropdown.SelectionChange += (s, a) => SwitchDogu();
  61. this.previousDoguButton = new Button("<");
  62. this.previousDoguButton.ControlEvent += (s, a) => this.doguDropdown.Step(-1);
  63. this.nextDoguButton = new Button(">");
  64. this.nextDoguButton.ControlEvent += (s, a) => this.doguDropdown.Step(1);
  65. this.keepWorldPositionToggle = new Toggle(Translation.Get("attachPropPane", "keepWorldPosition"));
  66. foreach (AttachPoint attachPoint in Enum.GetValues(typeof(AttachPoint)))
  67. {
  68. if (attachPoint == AttachPoint.None) continue;
  69. AttachPoint point = attachPoint;
  70. Toggle toggle = new Toggle(Translation.Get("attachPropPane", toggleTranslation[point]));
  71. toggle.ControlEvent += (s, a) =>
  72. {
  73. if (this.updating) return;
  74. ChangeAttachPoint(point);
  75. };
  76. Toggles[point] = toggle;
  77. }
  78. }
  79. protected override void ReloadTranslation()
  80. {
  81. this.header = Translation.Get("attachPropPane", "header");
  82. this.keepWorldPositionToggle.Label = Translation.Get("attachPropPane", "keepWorldPosition");
  83. foreach (AttachPoint attachPoint in Enum.GetValues(typeof(AttachPoint)))
  84. {
  85. if (attachPoint == AttachPoint.None) continue;
  86. Toggles[attachPoint].Label = Translation.Get("attachPropPane", toggleTranslation[attachPoint]);
  87. }
  88. }
  89. public override void Draw()
  90. {
  91. float arrowButtonSize = 30;
  92. GUILayoutOption[] arrowLayoutOptions = {
  93. GUILayout.Width(arrowButtonSize),
  94. GUILayout.Height(arrowButtonSize)
  95. };
  96. float dropdownButtonHeight = arrowButtonSize;
  97. float dropdownButtonWidth = 153f;
  98. GUILayoutOption[] dropdownLayoutOptions = new GUILayoutOption[] {
  99. GUILayout.Height(dropdownButtonHeight),
  100. GUILayout.Width(dropdownButtonWidth)
  101. };
  102. MiscGUI.Header(this.header);
  103. MiscGUI.WhiteLine();
  104. GUI.enabled = PaneActive;
  105. meidoDropdown.Draw(dropdownLayoutOptions);
  106. GUILayout.BeginHorizontal();
  107. doguDropdown.Draw(dropdownLayoutOptions);
  108. previousDoguButton.Draw(arrowLayoutOptions);
  109. nextDoguButton.Draw(arrowLayoutOptions);
  110. GUILayout.EndHorizontal();
  111. keepWorldPositionToggle.Draw();
  112. DrawToggleGroup(AttachPoint.Head, AttachPoint.Neck);
  113. DrawToggleGroup(AttachPoint.UpperArmL, AttachPoint.UpperArmR);
  114. DrawToggleGroup(AttachPoint.ForearmL, AttachPoint.ForearmR);
  115. DrawToggleGroup(AttachPoint.MuneL, AttachPoint.MuneR);
  116. DrawToggleGroup(AttachPoint.HandL, AttachPoint.Pelvis, AttachPoint.HandR);
  117. DrawToggleGroup(AttachPoint.ThighL, AttachPoint.ThighR);
  118. DrawToggleGroup(AttachPoint.CalfL, AttachPoint.CalfR);
  119. DrawToggleGroup(AttachPoint.FootL, AttachPoint.FootR);
  120. GUI.enabled = true;
  121. }
  122. private void DrawToggleGroup(params AttachPoint[] attachPoints)
  123. {
  124. GUILayout.BeginHorizontal();
  125. GUILayout.FlexibleSpace();
  126. foreach (AttachPoint point in attachPoints)
  127. {
  128. Toggles[point].Draw();
  129. }
  130. GUILayout.FlexibleSpace();
  131. GUILayout.EndHorizontal();
  132. }
  133. private void SetAttachPointToggle(AttachPoint point, bool value)
  134. {
  135. this.updating = true;
  136. foreach (KeyValuePair<AttachPoint, Toggle> kvp in Toggles)
  137. {
  138. Toggle toggle = kvp.Value;
  139. toggle.Value = false;
  140. }
  141. if (point != AttachPoint.None) Toggles[point].Value = value;
  142. this.updating = false;
  143. }
  144. private void ChangeAttachPoint(AttachPoint point)
  145. {
  146. bool toggleValue = point == AttachPoint.None ? false : Toggles[point].Value;
  147. SetAttachPointToggle(point, toggleValue);
  148. Meido meido = null;
  149. if (point != AttachPoint.None)
  150. {
  151. meido = Toggles[point].Value
  152. ? this.meidoManager.ActiveMeidoList[this.meidoDropdown.SelectedItemIndex]
  153. : null;
  154. }
  155. this.propManager.AttachProp(
  156. this.doguDropdown.SelectedItemIndex, point, meido, this.keepWorldPositionToggle.Value
  157. );
  158. }
  159. private void SwitchMaid()
  160. {
  161. if (updating || selectedMaid == this.meidoDropdown.SelectedItemIndex) return;
  162. selectedMaid = this.meidoDropdown.SelectedItemIndex;
  163. DragDogu dragDogu = this.propManager.GetDogu(this.doguDropdown.SelectedItemIndex);
  164. if (dragDogu != null)
  165. {
  166. if (dragDogu.attachPointInfo.AttachPoint == AttachPoint.None) return;
  167. ChangeAttachPoint(dragDogu.attachPointInfo.AttachPoint);
  168. }
  169. }
  170. private void SwitchDogu()
  171. {
  172. if (updating) return;
  173. DragDogu dragDogu = this.propManager.GetDogu(this.doguDropdown.SelectedItemIndex);
  174. if (dragDogu != null) SetAttachPointToggle(dragDogu.attachPointInfo.AttachPoint, true);
  175. }
  176. private void SetDoguDropdown()
  177. {
  178. if (this.propManager.DoguCount == 0)
  179. {
  180. SetAttachPointToggle(AttachPoint.Head, false);
  181. }
  182. int index = Mathf.Clamp(this.doguDropdown.SelectedItemIndex, 0, this.propManager.DoguCount);
  183. this.doguDropdown.SetDropdownItems(this.propManager.PropNameList, index);
  184. doguDropdownActive = this.propManager.DoguCount != 0;
  185. }
  186. private void SetMeidoDropdown()
  187. {
  188. if (this.meidoManager.ActiveMeidoList.Count == 0)
  189. {
  190. SetAttachPointToggle(AttachPoint.Head, false);
  191. }
  192. int index = Mathf.Clamp(this.meidoDropdown.SelectedItemIndex, 0, this.meidoManager.ActiveMeidoList.Count);
  193. this.updating = true;
  194. this.meidoDropdown.SetDropdownItems(this.meidoManager.ActiveMeidoNameList, index);
  195. this.updating = false;
  196. meidoDropdownActive = this.meidoManager.HasActiveMeido;
  197. }
  198. }
  199. }