AttachPropPane.cs 8.2 KB

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