AttachPropPane.cs 9.1 KB

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