AttachPropPane.cs 9.4 KB

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