Slider.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, Left, Right);
  25. OnControlEvent(EventArgs.Empty);
  26. }
  27. }
  28. private float left;
  29. public float Left
  30. {
  31. get => left;
  32. set
  33. {
  34. left = value;
  35. this.value = Utility.Bound(value, left, right);
  36. }
  37. }
  38. private float right;
  39. public float Right
  40. {
  41. get => right;
  42. set
  43. {
  44. right = value;
  45. this.value = Utility.Bound(value, left, right);
  46. }
  47. }
  48. public Slider(string label, float left, float right, float value = 0)
  49. {
  50. Label = label;
  51. this.left = left;
  52. this.right = right;
  53. this.value = Utility.Bound(value, left, right);
  54. }
  55. public Slider(string label, SliderProp prop) : this(label, prop.Left, prop.Right, prop.Initial) { }
  56. public Slider(SliderProp prop) : this(string.Empty, prop.Left, prop.Right, prop.Initial) { }
  57. public void SetBounds(float left, float right)
  58. {
  59. this.left = left;
  60. this.right = right;
  61. value = Utility.Bound(value, left, right);
  62. }
  63. public override void Draw(params GUILayoutOption[] layoutOptions)
  64. {
  65. GUIStyle sliderStyle = new GUIStyle(GUI.skin.horizontalSlider);
  66. sliderStyle.margin.bottom = 0;
  67. if (hasLabel)
  68. {
  69. GUILayout.BeginVertical(GUILayout.ExpandWidth(false));
  70. GUIStyle sliderLabelStyle = new GUIStyle(GUI.skin.label);
  71. sliderLabelStyle.padding.bottom = -5;
  72. sliderLabelStyle.margin = new RectOffset(0, 0, 0, 0);
  73. sliderLabelStyle.alignment = TextAnchor.LowerLeft;
  74. sliderLabelStyle.fontSize = 13;
  75. GUILayout.Label(Label, sliderLabelStyle, GUILayout.ExpandWidth(false));
  76. }
  77. else sliderStyle.margin.top = 10;
  78. float value = GUILayout.HorizontalSlider(
  79. Value, Left, Right, sliderStyle, GUI.skin.horizontalSliderThumb, layoutOptions
  80. );
  81. if (hasLabel) GUILayout.EndVertical();
  82. if (value != Value) Value = value;
  83. }
  84. }
  85. public struct SliderProp
  86. {
  87. public float Left { get; }
  88. public float Right { get; }
  89. public float Initial { get; }
  90. public SliderProp(float left, float right, float initial = 0f)
  91. {
  92. Left = left;
  93. Right = right;
  94. Initial = Utility.Bound(initial, left, right);
  95. }
  96. }
  97. }