AttachPropPane.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. [AttachPoint.Spine1a] = "spine1a",
  33. [AttachPoint.Spine1] = "spine1",
  34. [AttachPoint.Spine0a] = "spine0a",
  35. [AttachPoint.Spine0] = "spine0"
  36. };
  37. private readonly Toggle keepWorldPositionToggle;
  38. private readonly Dropdown meidoDropdown;
  39. private Toggle activeToggle;
  40. private bool meidoDropdownActive;
  41. private bool doguDropdownActive;
  42. private string header;
  43. private bool PaneActive => meidoDropdownActive && doguDropdownActive;
  44. private Meido SelectedMeido => meidoManager.ActiveMeidoList[meidoDropdown.SelectedItemIndex];
  45. private DragPointProp SelectedProp => propManager.CurrentProp;
  46. private bool KeepWoldPosition => keepWorldPositionToggle.Value;
  47. public AttachPropPane(MeidoManager meidoManager, PropManager propManager)
  48. {
  49. header = Translation.Get("attachPropPane", "header");
  50. this.propManager = propManager;
  51. this.meidoManager = meidoManager;
  52. this.meidoManager.EndCallMeidos += (s, a) => SetMeidoDropdown();
  53. this.propManager.PropSelectionChange += (s, a) => UpdateToggles();
  54. this.propManager.PropListChange += (s, a) =>
  55. {
  56. doguDropdownActive = this.propManager.PropCount > 0;
  57. UpdateToggles();
  58. };
  59. meidoDropdown = new Dropdown(new[] { Translation.Get("systemMessage", "noMaids") });
  60. meidoDropdown.SelectionChange += (s, a) => UpdateToggles();
  61. keepWorldPositionToggle = new Toggle(Translation.Get("attachPropPane", "keepWorldPosition"));
  62. foreach (AttachPoint attachPoint in Enum.GetValues(typeof(AttachPoint)))
  63. {
  64. if (attachPoint == AttachPoint.None) continue;
  65. var point = attachPoint;
  66. var toggle = new Toggle(Translation.Get("attachPropPane", toggleTranslation[point]));
  67. toggle.ControlEvent += (s, a) => OnToggleChange(point);
  68. toggles[point] = toggle;
  69. }
  70. }
  71. protected override void ReloadTranslation()
  72. {
  73. header = Translation.Get("attachPropPane", "header");
  74. keepWorldPositionToggle.Label = Translation.Get("attachPropPane", "keepWorldPosition");
  75. foreach (AttachPoint attachPoint in Enum.GetValues(typeof(AttachPoint)))
  76. {
  77. if (attachPoint == AttachPoint.None) continue;
  78. toggles[attachPoint].Label = Translation.Get("attachPropPane", toggleTranslation[attachPoint]);
  79. }
  80. }
  81. public override void Draw()
  82. {
  83. const float dropdownButtonHeight = 30;
  84. const float dropdownButtonWidth = 153f;
  85. GUILayoutOption[] dropdownLayoutOptions =
  86. {
  87. GUILayout.Height(dropdownButtonHeight), GUILayout.Width(dropdownButtonWidth)
  88. };
  89. MpsGui.Header(header);
  90. MpsGui.WhiteLine();
  91. GUI.enabled = PaneActive;
  92. meidoDropdown.Draw(dropdownLayoutOptions);
  93. keepWorldPositionToggle.Draw();
  94. DrawToggleGroup(AttachPoint.Head, AttachPoint.Neck);
  95. DrawToggleGroup(AttachPoint.UpperArmR, AttachPoint.Spine1a, AttachPoint.UpperArmL);
  96. DrawToggleGroup(AttachPoint.ForearmR, AttachPoint.Spine1, AttachPoint.ForearmL);
  97. DrawToggleGroup(AttachPoint.MuneR, AttachPoint.Spine0a, AttachPoint.MuneL);
  98. DrawToggleGroup(AttachPoint.HandR, AttachPoint.Spine0, AttachPoint.HandL);
  99. DrawToggleGroup(AttachPoint.ThighR, AttachPoint.Pelvis, AttachPoint.ThighL);
  100. DrawToggleGroup(AttachPoint.CalfR, AttachPoint.CalfL);
  101. DrawToggleGroup(AttachPoint.FootR, AttachPoint.FootL);
  102. GUI.enabled = true;
  103. }
  104. private void DrawToggleGroup(params AttachPoint[] attachPoints)
  105. {
  106. GUILayout.BeginHorizontal();
  107. GUILayout.FlexibleSpace();
  108. foreach (var point in attachPoints) toggles[point].Draw();
  109. GUILayout.FlexibleSpace();
  110. GUILayout.EndHorizontal();
  111. }
  112. private void OnToggleChange(AttachPoint point)
  113. {
  114. if (updating) return;
  115. var toggle = toggles[point];
  116. if (toggle.Value)
  117. {
  118. if (activeToggle != null)
  119. {
  120. updating = true;
  121. activeToggle.Value = false;
  122. updating = false;
  123. }
  124. activeToggle = toggle;
  125. SelectedProp.AttachTo(SelectedMeido, point, KeepWoldPosition);
  126. }
  127. else
  128. {
  129. SelectedProp.DetachFrom(KeepWoldPosition);
  130. activeToggle = null;
  131. }
  132. }
  133. private void UpdateToggles()
  134. {
  135. updating = true;
  136. if (activeToggle != null) activeToggle.Value = false;
  137. activeToggle = null;
  138. updating = false;
  139. if (!meidoManager.HasActiveMeido || propManager.PropCount == 0) return;
  140. var info = SelectedProp.AttachPointInfo;
  141. if (SelectedMeido.Maid.status.guid != info.MaidGuid) return;
  142. updating = true;
  143. var toggle = toggles[info.AttachPoint];
  144. toggle.Value = true;
  145. activeToggle = toggle;
  146. updating = false;
  147. }
  148. private void SetMeidoDropdown()
  149. {
  150. meidoDropdownActive = meidoManager.HasActiveMeido;
  151. string[] dropdownList = meidoManager.ActiveMeidoList.Count == 0
  152. ? new[] { Translation.Get("systemMessage", "noMaids") }
  153. : meidoManager.ActiveMeidoList.Select(meido => $"{meido.Slot + 1}: {meido.FirstName} {meido.LastName}")
  154. .ToArray();
  155. meidoDropdown.SetDropdownItems(dropdownList, 0);
  156. }
  157. }
  158. }