AttachPropPane.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace MeidoPhotoStudio.Plugin;
  6. public class AttachPropPane : BasePane
  7. {
  8. private static readonly Dictionary<AttachPoint, string> ToggleTranslation =
  9. new()
  10. {
  11. [AttachPoint.Head] = "head",
  12. [AttachPoint.Neck] = "neck",
  13. [AttachPoint.UpperArmL] = "upperArmL",
  14. [AttachPoint.UpperArmR] = "upperArmR",
  15. [AttachPoint.ForearmL] = "forearmL",
  16. [AttachPoint.ForearmR] = "forearmR",
  17. [AttachPoint.MuneL] = "muneL",
  18. [AttachPoint.MuneR] = "muneR",
  19. [AttachPoint.HandL] = "handL",
  20. [AttachPoint.HandR] = "handR",
  21. [AttachPoint.Pelvis] = "pelvis",
  22. [AttachPoint.ThighL] = "thighL",
  23. [AttachPoint.ThighR] = "thighR",
  24. [AttachPoint.CalfL] = "calfL",
  25. [AttachPoint.CalfR] = "calfR",
  26. [AttachPoint.FootL] = "footL",
  27. [AttachPoint.FootR] = "footR",
  28. [AttachPoint.Spine1a] = "spine1a",
  29. [AttachPoint.Spine1] = "spine1",
  30. [AttachPoint.Spine0a] = "spine0a",
  31. [AttachPoint.Spine0] = "spine0",
  32. };
  33. private readonly PropManager propManager;
  34. private readonly MeidoManager meidoManager;
  35. private readonly Dictionary<AttachPoint, Toggle> toggles = new();
  36. private readonly Toggle keepWorldPositionToggle;
  37. private readonly Dropdown meidoDropdown;
  38. private Toggle activeToggle;
  39. private bool meidoDropdownActive;
  40. private bool doguDropdownActive;
  41. private string header;
  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 += (_, _) =>
  48. SetMeidoDropdown();
  49. this.propManager.PropSelectionChange += (_, _) =>
  50. UpdateToggles();
  51. this.propManager.PropListChange += (_, _) =>
  52. {
  53. doguDropdownActive = this.propManager.PropCount > 0;
  54. UpdateToggles();
  55. };
  56. meidoDropdown = new(new[] { Translation.Get("systemMessage", "noMaids") });
  57. meidoDropdown.SelectionChange += (_, _) =>
  58. UpdateToggles();
  59. keepWorldPositionToggle = new(Translation.Get("attachPropPane", "keepWorldPosition"));
  60. foreach (var attachPoint in Enum.GetValues(typeof(AttachPoint)).Cast<AttachPoint>())
  61. {
  62. if (attachPoint is AttachPoint.None)
  63. continue;
  64. var point = attachPoint;
  65. var toggle = new Toggle(Translation.Get("attachPropPane", ToggleTranslation[point]));
  66. toggle.ControlEvent += (_, _) =>
  67. OnToggleChange(point);
  68. toggles[point] = toggle;
  69. }
  70. }
  71. private bool PaneActive =>
  72. meidoDropdownActive && doguDropdownActive;
  73. private Meido SelectedMeido =>
  74. meidoManager.ActiveMeidoList[meidoDropdown.SelectedItemIndex];
  75. private DragPointProp SelectedProp =>
  76. propManager.CurrentProp;
  77. private bool KeepWoldPosition =>
  78. keepWorldPositionToggle.Value;
  79. public override void Draw()
  80. {
  81. const float dropdownButtonHeight = 30;
  82. const float dropdownButtonWidth = 153f;
  83. var dropdownLayoutOptions = new[]
  84. {
  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.Spine1a, AttachPoint.UpperArmL);
  95. DrawToggleGroup(AttachPoint.ForearmR, AttachPoint.Spine1, AttachPoint.ForearmL);
  96. DrawToggleGroup(AttachPoint.MuneR, AttachPoint.Spine0a, AttachPoint.MuneL);
  97. DrawToggleGroup(AttachPoint.HandR, AttachPoint.Spine0, AttachPoint.HandL);
  98. DrawToggleGroup(AttachPoint.ThighR, AttachPoint.Pelvis, AttachPoint.ThighL);
  99. DrawToggleGroup(AttachPoint.CalfR, AttachPoint.CalfL);
  100. DrawToggleGroup(AttachPoint.FootR, AttachPoint.FootL);
  101. GUI.enabled = true;
  102. }
  103. protected override void ReloadTranslation()
  104. {
  105. header = Translation.Get("attachPropPane", "header");
  106. keepWorldPositionToggle.Label = Translation.Get("attachPropPane", "keepWorldPosition");
  107. foreach (var attachPoint in Enum.GetValues(typeof(AttachPoint)).Cast<AttachPoint>())
  108. {
  109. if (attachPoint is AttachPoint.None)
  110. continue;
  111. toggles[attachPoint].Label = Translation.Get("attachPropPane", ToggleTranslation[attachPoint]);
  112. }
  113. }
  114. private void DrawToggleGroup(params AttachPoint[] attachPoints)
  115. {
  116. GUILayout.BeginHorizontal();
  117. GUILayout.FlexibleSpace();
  118. foreach (var point in attachPoints)
  119. toggles[point].Draw();
  120. GUILayout.FlexibleSpace();
  121. GUILayout.EndHorizontal();
  122. }
  123. private void OnToggleChange(AttachPoint point)
  124. {
  125. if (updating)
  126. return;
  127. var toggle = toggles[point];
  128. if (toggle.Value)
  129. {
  130. if (activeToggle is not null)
  131. {
  132. updating = true;
  133. activeToggle.Value = false;
  134. updating = false;
  135. }
  136. activeToggle = toggle;
  137. SelectedProp.AttachTo(SelectedMeido, point, KeepWoldPosition);
  138. }
  139. else
  140. {
  141. SelectedProp.DetachFrom(KeepWoldPosition);
  142. activeToggle = null;
  143. }
  144. }
  145. private void UpdateToggles()
  146. {
  147. updating = true;
  148. if (activeToggle is not null)
  149. activeToggle.Value = false;
  150. activeToggle = null;
  151. updating = false;
  152. if (!meidoManager.HasActiveMeido || propManager.PropCount is 0)
  153. return;
  154. var info = SelectedProp.AttachPointInfo;
  155. if (SelectedMeido.Maid.status.guid != info.MaidGuid)
  156. return;
  157. updating = true;
  158. var toggle = toggles[info.AttachPoint];
  159. toggle.Value = true;
  160. activeToggle = toggle;
  161. updating = false;
  162. }
  163. private void SetMeidoDropdown()
  164. {
  165. meidoDropdownActive = meidoManager.HasActiveMeido;
  166. var dropdownList = meidoManager.ActiveMeidoList.Count is 0
  167. ? new[] { Translation.Get("systemMessage", "noMaids") }
  168. : meidoManager.ActiveMeidoList.Select(meido => $"{meido.Slot + 1}: {meido.FirstName} {meido.LastName}")
  169. .ToArray();
  170. meidoDropdown.SetDropdownItems(dropdownList, 0);
  171. }
  172. }