AttachPropPane.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.meidoManager.EndCallMeidos += (s, a) => SetMeidoDropdown();
  51. this.propManager.DoguListChange += (s, a) => SetDoguDropdown();
  52. this.propManager.DoguSelectChange += (s, a) =>
  53. {
  54. this.doguDropdown.SelectedItemIndex = this.propManager.CurrentDoguIndex;
  55. };
  56. this.meidoDropdown = new Dropdown(new[] { Translation.Get("systemMessage", "noMaids") });
  57. this.meidoDropdown.SelectionChange += (s, a) => SwitchMaid();
  58. this.previousMaidButton = new Button("<");
  59. this.previousMaidButton.ControlEvent += (s, a) => this.meidoDropdown.Step(-1);
  60. this.nextMaidButton = new Button(">");
  61. this.nextMaidButton.ControlEvent += (s, a) => this.meidoDropdown.Step(1);
  62. this.doguDropdown = new Dropdown(new[] { Translation.Get("systemMessage", "noProps") });
  63. this.doguDropdown.SelectionChange += (s, a) => SwitchDogu();
  64. this.previousDoguButton = new Button("<");
  65. this.previousDoguButton.ControlEvent += (s, a) => this.doguDropdown.Step(-1);
  66. this.nextDoguButton = new Button(">");
  67. this.nextDoguButton.ControlEvent += (s, a) => this.doguDropdown.Step(1);
  68. this.keepWorldPositionToggle = new Toggle(Translation.Get("attachPropPane", "keepWorldPosition"));
  69. foreach (AttachPoint attachPoint in Enum.GetValues(typeof(AttachPoint)))
  70. {
  71. if (attachPoint == AttachPoint.None) continue;
  72. AttachPoint point = attachPoint;
  73. Toggle toggle = new Toggle(Translation.Get("attachPropPane", toggleTranslation[point]));
  74. toggle.ControlEvent += (s, a) =>
  75. {
  76. if (this.updating) return;
  77. ChangeAttachPoint(point);
  78. };
  79. Toggles[point] = toggle;
  80. }
  81. }
  82. protected override void ReloadTranslation()
  83. {
  84. this.header = Translation.Get("attachPropPane", "header");
  85. this.keepWorldPositionToggle.Label = Translation.Get("attachPropPane", "keepWorldPosition");
  86. foreach (AttachPoint attachPoint in Enum.GetValues(typeof(AttachPoint)))
  87. {
  88. if (attachPoint == AttachPoint.None) continue;
  89. Toggles[attachPoint].Label = Translation.Get("attachPropPane", toggleTranslation[attachPoint]);
  90. }
  91. }
  92. public override void Draw()
  93. {
  94. float arrowButtonSize = 30;
  95. GUILayoutOption[] arrowLayoutOptions = {
  96. GUILayout.Width(arrowButtonSize),
  97. GUILayout.Height(arrowButtonSize)
  98. };
  99. float dropdownButtonHeight = arrowButtonSize;
  100. float dropdownButtonWidth = 153f;
  101. GUILayoutOption[] dropdownLayoutOptions = new GUILayoutOption[] {
  102. GUILayout.Height(dropdownButtonHeight),
  103. GUILayout.Width(dropdownButtonWidth)
  104. };
  105. MiscGUI.Header(this.header);
  106. MiscGUI.WhiteLine();
  107. GUI.enabled = PaneActive;
  108. meidoDropdown.Draw(dropdownLayoutOptions);
  109. GUILayout.BeginHorizontal();
  110. doguDropdown.Draw(dropdownLayoutOptions);
  111. previousDoguButton.Draw(arrowLayoutOptions);
  112. nextDoguButton.Draw(arrowLayoutOptions);
  113. GUILayout.EndHorizontal();
  114. keepWorldPositionToggle.Draw();
  115. DrawToggleGroup(AttachPoint.Head, AttachPoint.Neck);
  116. DrawToggleGroup(AttachPoint.UpperArmR, AttachPoint.UpperArmL);
  117. DrawToggleGroup(AttachPoint.ForearmR, AttachPoint.ForearmL);
  118. DrawToggleGroup(AttachPoint.MuneR, AttachPoint.MuneL);
  119. DrawToggleGroup(AttachPoint.HandR, AttachPoint.Pelvis, AttachPoint.HandL);
  120. DrawToggleGroup(AttachPoint.ThighR, AttachPoint.ThighL);
  121. DrawToggleGroup(AttachPoint.CalfR, AttachPoint.CalfL);
  122. DrawToggleGroup(AttachPoint.FootR, AttachPoint.FootL);
  123. GUI.enabled = true;
  124. }
  125. private void DrawToggleGroup(params AttachPoint[] attachPoints)
  126. {
  127. GUILayout.BeginHorizontal();
  128. GUILayout.FlexibleSpace();
  129. foreach (AttachPoint point in attachPoints)
  130. {
  131. Toggles[point].Draw();
  132. }
  133. GUILayout.FlexibleSpace();
  134. GUILayout.EndHorizontal();
  135. }
  136. private void SetAttachPointToggle(AttachPoint point, bool value)
  137. {
  138. this.updating = true;
  139. foreach (Toggle toggle in Toggles.Values)
  140. {
  141. toggle.Value = false;
  142. }
  143. if (point != AttachPoint.None) Toggles[point].Value = value;
  144. this.updating = false;
  145. }
  146. private void ChangeAttachPoint(AttachPoint point)
  147. {
  148. bool toggleValue = point == AttachPoint.None ? false : Toggles[point].Value;
  149. SetAttachPointToggle(point, toggleValue);
  150. Meido meido = null;
  151. if (point != AttachPoint.None)
  152. {
  153. meido = Toggles[point].Value
  154. ? this.meidoManager.ActiveMeidoList[this.meidoDropdown.SelectedItemIndex]
  155. : null;
  156. }
  157. this.propManager.AttachProp(
  158. this.doguDropdown.SelectedItemIndex, point, meido, this.keepWorldPositionToggle.Value
  159. );
  160. }
  161. private void SwitchMaid()
  162. {
  163. if (updating || selectedMaid == this.meidoDropdown.SelectedItemIndex) return;
  164. selectedMaid = this.meidoDropdown.SelectedItemIndex;
  165. DragPointDogu dragDogu = this.propManager.GetDogu(this.doguDropdown.SelectedItemIndex);
  166. if (dragDogu != null)
  167. {
  168. if (dragDogu.attachPointInfo.AttachPoint == AttachPoint.None) return;
  169. ChangeAttachPoint(dragDogu.attachPointInfo.AttachPoint);
  170. }
  171. }
  172. private void SwitchDogu()
  173. {
  174. if (updating) return;
  175. DragPointDogu dragDogu = this.propManager.GetDogu(this.doguDropdown.SelectedItemIndex);
  176. if (dragDogu != null) SetAttachPointToggle(dragDogu.attachPointInfo.AttachPoint, true);
  177. }
  178. private void SetDoguDropdown()
  179. {
  180. if (this.propManager.DoguCount == 0)
  181. {
  182. SetAttachPointToggle(AttachPoint.Head, false);
  183. }
  184. int index = Mathf.Clamp(this.doguDropdown.SelectedItemIndex, 0, this.propManager.DoguCount);
  185. this.doguDropdown.SetDropdownItems(this.propManager.PropNameList, index);
  186. doguDropdownActive = this.propManager.DoguCount != 0;
  187. }
  188. private void SetMeidoDropdown()
  189. {
  190. if (this.meidoManager.ActiveMeidoList.Count == 0)
  191. {
  192. SetAttachPointToggle(AttachPoint.Head, false);
  193. }
  194. int index = Mathf.Clamp(this.meidoDropdown.SelectedItemIndex, 0, this.meidoManager.ActiveMeidoList.Count);
  195. string[] dropdownList = this.meidoManager.ActiveMeidoList.Count == 0
  196. ? new[] { Translation.Get("systemMessage", "noMaids") }
  197. : this.meidoManager.ActiveMeidoList.Select(
  198. meido => $"{meido.Slot + 1}: {meido.FirstName} {meido.LastName}"
  199. ).ToArray();
  200. this.updating = true;
  201. this.meidoDropdown.SetDropdownItems(dropdownList, index);
  202. this.updating = false;
  203. meidoDropdownActive = this.meidoManager.HasActiveMeido;
  204. }
  205. }
  206. }