Slider.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public 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 = 0) : 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. if (hasLabel)
  69. {
  70. GUILayout.BeginVertical(GUILayout.ExpandWidth(false));
  71. GUIStyle sliderLabelStyle = new GUIStyle(GUI.skin.label);
  72. sliderLabelStyle.padding.bottom = -5;
  73. sliderLabelStyle.margin = new RectOffset(0, 0, 0, 0);
  74. sliderLabelStyle.alignment = TextAnchor.LowerLeft;
  75. sliderLabelStyle.fontSize = 13;
  76. GUILayout.Label(Label, sliderLabelStyle, GUILayout.ExpandWidth(false));
  77. }
  78. else sliderStyle.margin.top = 10;
  79. float value = GUILayout.HorizontalSlider(
  80. Value, Left, Right, sliderStyle, GUI.skin.horizontalSliderThumb, layoutOptions
  81. );
  82. if (hasLabel) GUILayout.EndVertical();
  83. if (value != Value) Value = value;
  84. }
  85. }
  86. public struct SliderProp
  87. {
  88. public float Left { get; private set; }
  89. public float Right { get; private set; }
  90. public float Initial { get; private set; }
  91. public SliderProp(float left, float right, float initial = 0f)
  92. {
  93. this.Left = left;
  94. this.Right = right;
  95. this.Initial = initial;
  96. }
  97. }
  98. }