Slider.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. internal class Slider : BaseControl
  6. {
  7. private bool hasLabel;
  8. private string label;
  9. public string Label
  10. {
  11. get => label;
  12. set
  13. {
  14. label = value;
  15. hasLabel = !string.IsNullOrEmpty(label);
  16. }
  17. }
  18. private float value;
  19. public float Value
  20. {
  21. get => value;
  22. set
  23. {
  24. this.value = Utility.Bound(value, this.Left, this.Right);
  25. OnControlEvent(EventArgs.Empty);
  26. }
  27. }
  28. private float left;
  29. public float Left
  30. {
  31. get => this.left;
  32. set
  33. {
  34. this.left = value;
  35. this.Value = this.value;
  36. }
  37. }
  38. private float right;
  39. public float Right
  40. {
  41. get => this.right;
  42. set
  43. {
  44. this.right = value;
  45. this.Value = this.value;
  46. }
  47. }
  48. public Slider(string label, float left, float right, float value = 0)
  49. {
  50. Label = label;
  51. Left = left;
  52. Right = right;
  53. this.value = Utility.Bound(value, left, right);
  54. }
  55. public Slider(float min, float max, float value = 0f) : this(String.Empty, min, max, value) { }
  56. public Slider(string label, SliderProp prop) : this(label, prop.Left, prop.Right, prop.Initial) { }
  57. public Slider(SliderProp prop) : this(String.Empty, prop.Left, prop.Right, prop.Initial) { }
  58. public void SetBounds(float left, float right)
  59. {
  60. this.left = left;
  61. this.right = right;
  62. this.Value = this.Value;
  63. }
  64. public override void Draw(params GUILayoutOption[] layoutOptions)
  65. {
  66. if (!Visible) return;
  67. GUIStyle sliderStyle = new GUIStyle(GUI.skin.horizontalSlider);
  68. sliderStyle.margin.bottom = 0;
  69. if (hasLabel)
  70. {
  71. GUILayout.BeginVertical(GUILayout.ExpandWidth(false));
  72. GUIStyle sliderLabelStyle = new GUIStyle(GUI.skin.label);
  73. sliderLabelStyle.padding.bottom = -5;
  74. sliderLabelStyle.margin = new RectOffset(0, 0, 0, 0);
  75. sliderLabelStyle.alignment = TextAnchor.LowerLeft;
  76. sliderLabelStyle.fontSize = 13;
  77. GUILayout.Label(Label, sliderLabelStyle, GUILayout.ExpandWidth(false));
  78. }
  79. else sliderStyle.margin.top = 10;
  80. float value = GUILayout.HorizontalSlider(
  81. Value, Left, Right, sliderStyle, GUI.skin.horizontalSliderThumb, layoutOptions
  82. );
  83. if (hasLabel) GUILayout.EndVertical();
  84. if (value != Value) Value = value;
  85. }
  86. }
  87. public struct SliderProp
  88. {
  89. public float Left { get; }
  90. public float Right { get; }
  91. public float Initial { get; }
  92. public SliderProp(float left, float right, float initial = 0f)
  93. {
  94. this.Left = left;
  95. this.Right = right;
  96. this.Initial = Utility.Bound(initial, left, right);
  97. }
  98. }
  99. }