MaidSwitcherPane.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using UnityEngine;
  2. namespace MeidoPhotoStudio.Plugin;
  3. public class MaidSwitcherPane : BasePane
  4. {
  5. private readonly MeidoManager meidoManager;
  6. private readonly Button previousButton;
  7. private readonly Button nextButton;
  8. private readonly Toggle editToggle;
  9. public MaidSwitcherPane(MeidoManager meidoManager)
  10. {
  11. this.meidoManager = meidoManager;
  12. this.meidoManager.UpdateMeido += (_, _) =>
  13. UpdatePane();
  14. previousButton = new("<");
  15. previousButton.ControlEvent += (_, _) =>
  16. ChangeMaid(-1);
  17. nextButton = new(">");
  18. nextButton.ControlEvent += (_, _) =>
  19. ChangeMaid(1);
  20. editToggle = new("Edit", true);
  21. editToggle.ControlEvent += (_, _) =>
  22. SetEditMaid();
  23. }
  24. public override void Draw()
  25. {
  26. const float boxSize = 70;
  27. const int margin = (int)(boxSize / 2.8f);
  28. var buttonStyle = new GUIStyle(GUI.skin.button);
  29. buttonStyle.margin.top = margin;
  30. var labelStyle = new GUIStyle(GUI.skin.label);
  31. labelStyle.margin.top = margin;
  32. var boxStyle = new GUIStyle(GUI.skin.box)
  33. {
  34. margin = new RectOffset(0, 0, 0, 0),
  35. };
  36. var horizontalStyle = new GUIStyle
  37. {
  38. padding = new RectOffset(4, 4, 0, 0),
  39. };
  40. var buttonOptions = new[]
  41. {
  42. GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(false),
  43. };
  44. var boxLayoutOptions = new[]
  45. {
  46. GUILayout.Height(boxSize), GUILayout.Width(boxSize),
  47. };
  48. GUI.enabled = meidoManager.HasActiveMeido;
  49. var meido = meidoManager.ActiveMeido;
  50. GUILayout.BeginHorizontal(horizontalStyle, GUILayout.Height(boxSize));
  51. previousButton.Draw(buttonStyle, buttonOptions);
  52. GUILayout.Space(20);
  53. if (meidoManager.HasActiveMeido && meido.Portrait)
  54. MpsGui.DrawTexture(meido.Portrait, boxLayoutOptions);
  55. else
  56. GUILayout.Box(GUIContent.none, boxStyle, boxLayoutOptions);
  57. var label = meidoManager.HasActiveMeido ? $"{meido.LastName}\n{meido.FirstName}" : string.Empty;
  58. GUILayout.Label(label, labelStyle, GUILayout.ExpandWidth(false));
  59. GUILayout.FlexibleSpace();
  60. nextButton.Draw(buttonStyle, buttonOptions);
  61. GUILayout.EndHorizontal();
  62. var previousRect = GUILayoutUtility.GetLastRect();
  63. if (MeidoPhotoStudio.EditMode)
  64. editToggle.Draw(new Rect(previousRect.x + 4f, previousRect.y, 40f, 20f));
  65. var labelRect = new Rect(previousRect.width - 45f, previousRect.y, 40f, 20f);
  66. var slotStyle = new GUIStyle()
  67. {
  68. alignment = TextAnchor.UpperRight,
  69. fontSize = 13,
  70. };
  71. slotStyle.padding.right = 5;
  72. slotStyle.normal.textColor = Color.white;
  73. if (meidoManager.HasActiveMeido)
  74. GUI.Label(labelRect, $"{meidoManager.ActiveMeido.Slot + 1}", slotStyle);
  75. }
  76. public override void UpdatePane()
  77. {
  78. if (!meidoManager.HasActiveMeido)
  79. return;
  80. updating = true;
  81. editToggle.Value = meidoManager.ActiveMeido.IsEditMaid;
  82. updating = false;
  83. }
  84. private void ChangeMaid(int dir)
  85. {
  86. var selected =
  87. Utility.Wrap(meidoManager.SelectedMeido + (int)Mathf.Sign(dir), 0, meidoManager.ActiveMeidoList.Count);
  88. meidoManager.ChangeMaid(selected);
  89. }
  90. private void SetEditMaid()
  91. {
  92. if (updating)
  93. return;
  94. if (!editToggle.Value)
  95. {
  96. updating = true;
  97. editToggle.Value = true;
  98. updating = false;
  99. return;
  100. }
  101. if (meidoManager.HasActiveMeido)
  102. meidoManager.SetEditMaid(meidoManager.ActiveMeido);
  103. }
  104. }