Slider.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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("", min, max, value) { }
  56. public void SetBounds(float left, float right)
  57. {
  58. this.left = left;
  59. this.right = right;
  60. this.Value = this.Value;
  61. }
  62. public override void Draw(params GUILayoutOption[] layoutOptions)
  63. {
  64. if (!Visible) return;
  65. GUIStyle sliderStyle = new GUIStyle(GUI.skin.horizontalSlider);
  66. if (hasLabel)
  67. {
  68. GUILayout.BeginVertical(GUILayout.ExpandWidth(false));
  69. GUIStyle sliderLabelStyle = new GUIStyle(GUI.skin.label);
  70. sliderLabelStyle.padding.bottom = -5;
  71. sliderLabelStyle.margin = new RectOffset(0, 0, 0, 0);
  72. sliderLabelStyle.alignment = TextAnchor.LowerLeft;
  73. sliderLabelStyle.fontSize = 13;
  74. GUILayout.Label(Label, sliderLabelStyle, GUILayout.ExpandWidth(false));
  75. }
  76. else sliderStyle.margin.top = 10;
  77. float value = GUILayout.HorizontalSlider(Value, Left, Right, sliderStyle, GUI.skin.horizontalSliderThumb, layoutOptions);
  78. if (hasLabel) GUILayout.EndVertical();
  79. if (value != Value) Value = value;
  80. }
  81. }
  82. }