AttachPropPane.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace COM3D2.MeidoPhotoStudio.Plugin
  6. {
  7. public class AttachPropPane : BasePane
  8. {
  9. private readonly PropManager propManager;
  10. private readonly MeidoManager meidoManager;
  11. private readonly 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 readonly Toggle keepWorldPositionToggle;
  34. private readonly Button previousMaidButton;
  35. private readonly Button nextMaidButton;
  36. private readonly Dropdown meidoDropdown;
  37. private bool meidoDropdownActive;
  38. private bool doguDropdownActive;
  39. private bool PaneActive => meidoDropdownActive && doguDropdownActive;
  40. private string header;
  41. private int selectedMaid;
  42. public AttachPropPane(MeidoManager meidoManager, PropManager propManager)
  43. {
  44. 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. meidoDropdown = new Dropdown(new[] { Translation.Get("systemMessage", "noMaids") });
  51. meidoDropdown.SelectionChange += (s, a) => SwitchMaid();
  52. previousMaidButton = new Button("<");
  53. previousMaidButton.ControlEvent += (s, a) => meidoDropdown.Step(-1);
  54. nextMaidButton = new Button(">");
  55. nextMaidButton.ControlEvent += (s, a) => meidoDropdown.Step(1);
  56. 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 (updating) return;
  65. ChangeAttachPoint(point);
  66. };
  67. Toggles[point] = toggle;
  68. }
  69. }
  70. protected override void ReloadTranslation()
  71. {
  72. header = Translation.Get("attachPropPane", "header");
  73. 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. const float dropdownButtonHeight = 30;
  83. const float dropdownButtonWidth = 153f;
  84. GUILayoutOption[] dropdownLayoutOptions = new GUILayoutOption[] {
  85. GUILayout.Height(dropdownButtonHeight),
  86. GUILayout.Width(dropdownButtonWidth)
  87. };
  88. MpsGui.Header(header);
  89. MpsGui.WhiteLine();
  90. GUI.enabled = PaneActive;
  91. meidoDropdown.Draw(dropdownLayoutOptions);
  92. keepWorldPositionToggle.Draw();
  93. DrawToggleGroup(AttachPoint.Head, AttachPoint.Neck);
  94. DrawToggleGroup(AttachPoint.UpperArmR, AttachPoint.UpperArmL);
  95. DrawToggleGroup(AttachPoint.ForearmR, AttachPoint.ForearmL);
  96. DrawToggleGroup(AttachPoint.MuneR, AttachPoint.MuneL);
  97. DrawToggleGroup(AttachPoint.HandR, AttachPoint.Pelvis, AttachPoint.HandL);
  98. DrawToggleGroup(AttachPoint.ThighR, AttachPoint.ThighL);
  99. DrawToggleGroup(AttachPoint.CalfR, AttachPoint.CalfL);
  100. DrawToggleGroup(AttachPoint.FootR, AttachPoint.FootL);
  101. GUI.enabled = true;
  102. }
  103. private void DrawToggleGroup(params AttachPoint[] attachPoints)
  104. {
  105. GUILayout.BeginHorizontal();
  106. GUILayout.FlexibleSpace();
  107. foreach (AttachPoint point in attachPoints)
  108. {
  109. Toggles[point].Draw();
  110. }
  111. GUILayout.FlexibleSpace();
  112. GUILayout.EndHorizontal();
  113. }
  114. private void SetAttachPointToggle(AttachPoint point, bool value)
  115. {
  116. updating = true;
  117. foreach (Toggle toggle in Toggles.Values)
  118. {
  119. toggle.Value = false;
  120. }
  121. if (point != AttachPoint.None) Toggles[point].Value = value;
  122. updating = false;
  123. }
  124. private void ChangeAttachPoint(AttachPoint point)
  125. {
  126. bool toggleValue = point != AttachPoint.None && Toggles[point].Value;
  127. SetAttachPointToggle(point, toggleValue);
  128. Meido meido = null;
  129. if (point != AttachPoint.None)
  130. {
  131. meido = Toggles[point].Value
  132. ? meidoManager.ActiveMeidoList[meidoDropdown.SelectedItemIndex]
  133. : null;
  134. }
  135. propManager.AttachProp(
  136. propManager.CurrentDoguIndex, point, meido, keepWorldPositionToggle.Value
  137. );
  138. }
  139. private void SwitchMaid()
  140. {
  141. if (updating || selectedMaid == meidoDropdown.SelectedItemIndex) return;
  142. selectedMaid = meidoDropdown.SelectedItemIndex;
  143. DragPointDogu dragDogu = propManager.CurrentDogu;
  144. if (dragDogu != null)
  145. {
  146. if (dragDogu.attachPointInfo.AttachPoint == AttachPoint.None) return;
  147. ChangeAttachPoint(dragDogu.attachPointInfo.AttachPoint);
  148. }
  149. }
  150. private void SwitchDogu()
  151. {
  152. if (updating) return;
  153. DragPointDogu dragDogu = propManager.CurrentDogu;
  154. if (dragDogu != null) SetAttachPointToggle(dragDogu.attachPointInfo.AttachPoint, true);
  155. }
  156. private void SetMeidoDropdown()
  157. {
  158. if (meidoManager.ActiveMeidoList.Count == 0)
  159. {
  160. SetAttachPointToggle(AttachPoint.Head, false);
  161. }
  162. int index = Mathf.Clamp(meidoDropdown.SelectedItemIndex, 0, meidoManager.ActiveMeidoList.Count);
  163. string[] dropdownList = meidoManager.ActiveMeidoList.Count == 0
  164. ? new[] { Translation.Get("systemMessage", "noMaids") }
  165. : meidoManager.ActiveMeidoList.Select(
  166. meido => $"{meido.Slot + 1}: {meido.FirstName} {meido.LastName}"
  167. ).ToArray();
  168. updating = true;
  169. meidoDropdown.SetDropdownItems(dropdownList, index);
  170. updating = false;
  171. meidoDropdownActive = meidoManager.HasActiveMeido;
  172. }
  173. }
  174. }