MaidSelectorPane.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace MeidoPhotoStudio.Plugin;
  4. public class MaidSelectorPane : BasePane
  5. {
  6. private readonly MeidoManager meidoManager;
  7. private readonly Button clearMaidsButton;
  8. private readonly Button callMaidsButton;
  9. private readonly Toggle activeMeidoListToggle;
  10. private readonly List<Meido> selectedMeidoList = new();
  11. private readonly HashSet<Meido> selectedMeidoSet = new();
  12. private Vector2 maidListScrollPos;
  13. private Vector2 activeMaidListScrollPos;
  14. public MaidSelectorPane(MeidoManager meidoManager)
  15. {
  16. this.meidoManager = meidoManager;
  17. clearMaidsButton = new(Translation.Get("maidCallWindow", "clearButton"));
  18. clearMaidsButton.ControlEvent += (_, _) =>
  19. ClearSelectedMaids();
  20. callMaidsButton = new(Translation.Get("maidCallWindow", "callButton"));
  21. callMaidsButton.ControlEvent += (_, _) =>
  22. this.meidoManager.CallMeidos(selectedMeidoList);
  23. activeMeidoListToggle = new(Translation.Get("maidCallWindow", "activeOnlyToggle"));
  24. this.meidoManager.BeginCallMeidos += (_, _) =>
  25. {
  26. if (selectedMeidoSet.Count is 0)
  27. activeMeidoListToggle.Value = false;
  28. };
  29. }
  30. public override void Activate()
  31. {
  32. base.Activate();
  33. ClearSelectedMaids();
  34. // NOTE: Leaving this mode enabled pretty much softlocks meido selection so disable it on activation
  35. activeMeidoListToggle.Value = false;
  36. }
  37. public override void Draw()
  38. {
  39. GUILayout.BeginHorizontal();
  40. clearMaidsButton.Draw(GUILayout.ExpandWidth(false));
  41. callMaidsButton.Draw();
  42. GUILayout.EndHorizontal();
  43. MpsGui.WhiteLine();
  44. GUI.enabled = meidoManager.HasActiveMeido;
  45. activeMeidoListToggle.Draw();
  46. GUI.enabled = true;
  47. var onlyActiveMeido = activeMeidoListToggle.Value;
  48. IList<Meido> meidoList = onlyActiveMeido
  49. ? meidoManager.ActiveMeidoList
  50. : meidoManager.Meidos;
  51. var labelStyle = new GUIStyle(GUI.skin.label)
  52. {
  53. fontSize = 14,
  54. };
  55. var selectLabelStyle = new GUIStyle(labelStyle)
  56. {
  57. normal = { textColor = Color.black },
  58. alignment = TextAnchor.UpperRight,
  59. };
  60. var labelSelectedStyle = new GUIStyle(labelStyle)
  61. {
  62. normal = { textColor = Color.black },
  63. };
  64. var windowRect = parent.WindowRect;
  65. var windowHeight = windowRect.height;
  66. var buttonWidth = windowRect.width - 30f;
  67. const float buttonHeight = 85f;
  68. const float offsetTop = 130f;
  69. var positionRect = new Rect(5f, offsetTop, windowRect.width - 10f, windowHeight - (offsetTop + 35));
  70. var viewRect = new Rect(0f, 0f, buttonWidth, buttonHeight * meidoList.Count + 5f);
  71. if (onlyActiveMeido)
  72. activeMaidListScrollPos = GUI.BeginScrollView(positionRect, activeMaidListScrollPos, viewRect);
  73. else
  74. maidListScrollPos = GUI.BeginScrollView(positionRect, maidListScrollPos, viewRect);
  75. for (var i = 0; i < meidoList.Count; i++)
  76. {
  77. var meido = meidoList[i];
  78. var y = i * buttonHeight;
  79. var selected = selectedMeidoSet.Contains(meido);
  80. if (GUI.Button(new(0f, y, buttonWidth, buttonHeight), string.Empty))
  81. SelectMaid(meido);
  82. if (selected)
  83. {
  84. var selectedIndex = selectedMeidoList.IndexOf(meido) + 1;
  85. GUI.DrawTexture(new(5f, y + 5f, buttonWidth - 10f, buttonHeight - 10f), Texture2D.whiteTexture);
  86. GUI.Label(new(0f, y + 5f, buttonWidth - 10f, buttonHeight), selectedIndex.ToString(), selectLabelStyle);
  87. }
  88. if (meido.Portrait)
  89. GUI.DrawTexture(new(5f, y, buttonHeight, buttonHeight), meido.Portrait);
  90. GUI.Label(
  91. new(95f, y + 30f, buttonWidth - 80f, buttonHeight),
  92. $"{meido.LastName}\n{meido.FirstName}",
  93. selected ? labelSelectedStyle : labelStyle);
  94. }
  95. GUI.EndScrollView();
  96. }
  97. protected override void ReloadTranslation()
  98. {
  99. clearMaidsButton.Label = Translation.Get("maidCallWindow", "clearButton");
  100. callMaidsButton.Label = Translation.Get("maidCallWindow", "callButton");
  101. activeMeidoListToggle.Label = Translation.Get("maidCallWindow", "activeOnlyToggle");
  102. }
  103. private void SelectMaid(Meido meido)
  104. {
  105. if (selectedMeidoSet.Contains(meido))
  106. {
  107. if (!MeidoPhotoStudio.EditMode || meido != meidoManager.OriginalEditingMeido)
  108. {
  109. selectedMeidoSet.Remove(meido);
  110. selectedMeidoList.Remove(meido);
  111. }
  112. }
  113. else
  114. {
  115. selectedMeidoSet.Add(meido);
  116. selectedMeidoList.Add(meido);
  117. }
  118. }
  119. private void ClearSelectedMaids()
  120. {
  121. selectedMeidoSet.Clear();
  122. selectedMeidoList.Clear();
  123. if (!MeidoPhotoStudio.EditMode)
  124. return;
  125. SelectMaid(meidoManager.OriginalEditingMeido);
  126. }
  127. }