SelectionGrid.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. internal class SelectionGrid : BaseControl
  6. {
  7. private SimpleToggle[] toggles;
  8. private int selectedItemIndex;
  9. public int SelectedItemIndex
  10. {
  11. get => selectedItemIndex;
  12. set
  13. {
  14. this.selectedItemIndex = Mathf.Clamp(value, 0, this.toggles.Length - 1);
  15. foreach (SimpleToggle toggle in toggles)
  16. {
  17. toggle.value = toggle.toggleIndex == this.selectedItemIndex;
  18. }
  19. OnControlEvent(EventArgs.Empty);
  20. }
  21. }
  22. public SelectionGrid(string[] items, int selected = 0)
  23. {
  24. this.selectedItemIndex = Mathf.Clamp(selected, 0, items.Length - 1);
  25. toggles = MakeToggles(items);
  26. }
  27. private SimpleToggle[] MakeToggles(string[] items)
  28. {
  29. SimpleToggle[] toggles = new SimpleToggle[items.Length];
  30. for (int i = 0; i < items.Length; i++)
  31. {
  32. SimpleToggle toggle = new SimpleToggle(items[i], i == SelectedItemIndex);
  33. toggle.toggleIndex = i;
  34. toggle.ControlEvent += (s, a) =>
  35. {
  36. int value = (s as SimpleToggle).toggleIndex;
  37. if (value != this.SelectedItemIndex) this.SelectedItemIndex = value;
  38. };
  39. toggles[i] = toggle;
  40. }
  41. return toggles;
  42. }
  43. public void SetItems(string[] items, int selectedItemIndex = -1)
  44. {
  45. if (selectedItemIndex < 0) selectedItemIndex = this.SelectedItemIndex;
  46. if (items.Length != toggles.Length)
  47. {
  48. this.toggles = MakeToggles(items);
  49. }
  50. else
  51. {
  52. for (int i = 0; i < items.Length; i++)
  53. {
  54. string item = items[i];
  55. this.toggles[i].value = i == SelectedItemIndex;
  56. this.toggles[i].label = item;
  57. }
  58. }
  59. this.SelectedItemIndex = Mathf.Clamp(selectedItemIndex, 0, items.Length - 1);
  60. }
  61. public override void Draw(params GUILayoutOption[] layoutOptions)
  62. {
  63. GUILayout.BeginHorizontal();
  64. foreach (SimpleToggle toggle in toggles)
  65. {
  66. toggle.Draw(layoutOptions);
  67. }
  68. GUILayout.EndHorizontal();
  69. }
  70. private class SimpleToggle
  71. {
  72. public int toggleIndex;
  73. public bool value;
  74. public string label;
  75. public event EventHandler ControlEvent;
  76. public SimpleToggle(string label, bool value = false)
  77. {
  78. this.label = label;
  79. this.value = value;
  80. }
  81. public void Draw(params GUILayoutOption[] layoutOptions)
  82. {
  83. bool value = GUILayout.Toggle(this.value, label, layoutOptions);
  84. if (value != this.value)
  85. {
  86. if (value == false) this.value = true;
  87. else
  88. {
  89. this.value = value;
  90. ControlEvent?.Invoke(this, EventArgs.Empty);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }