Slider.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. public float Left { get; set; }
  29. public float Right { get; set; }
  30. public Slider(string label, float left, float right, float value = 0)
  31. {
  32. Label = label;
  33. Left = left;
  34. Right = right;
  35. this.value = Utility.Bound(value, left, right);
  36. }
  37. public Slider(float min, float max, float value = 0) : this("", min, max, value) { }
  38. public override void Draw(params GUILayoutOption[] layoutOptions)
  39. {
  40. if (!Visible) return;
  41. GUIStyle sliderStyle = new GUIStyle(GUI.skin.horizontalSlider);
  42. if (hasLabel)
  43. {
  44. GUILayout.BeginVertical(GUILayout.ExpandWidth(false));
  45. GUIStyle sliderLabelStyle = new GUIStyle(GUI.skin.label);
  46. sliderLabelStyle.padding.bottom = -5;
  47. sliderLabelStyle.margin = new RectOffset(0, 0, 0, 0);
  48. sliderLabelStyle.alignment = TextAnchor.LowerLeft;
  49. sliderLabelStyle.fontSize = 13;
  50. GUILayout.Label(Label, sliderLabelStyle, GUILayout.ExpandWidth(false));
  51. }
  52. else sliderStyle.margin.top = 10;
  53. float value = GUILayout.HorizontalSlider(Value, Left, Right, sliderStyle, GUI.skin.horizontalSliderThumb, layoutOptions);
  54. if (hasLabel) GUILayout.EndVertical();
  55. if (value != Value) Value = value;
  56. }
  57. }
  58. }