Slider.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Globalization;
  3. using UnityEngine;
  4. namespace MeidoPhotoStudio.Plugin;
  5. public class Slider : BaseControl
  6. {
  7. private bool hasLabel;
  8. private string label;
  9. private float value;
  10. private float left;
  11. private float right;
  12. private float defaultValue;
  13. private string textFieldValue;
  14. private bool hasTextField;
  15. public Slider(string label, float left, float right, float value = 0, float defaultValue = 0)
  16. {
  17. Label = label;
  18. this.left = left;
  19. this.right = right;
  20. this.value = Utility.Bound(value, left, right);
  21. textFieldValue = FormatValue(this.value);
  22. DefaultValue = defaultValue;
  23. }
  24. public Slider(string label, SliderProp prop)
  25. : this(label, prop.Left, prop.Right, prop.Initial, prop.Default)
  26. {
  27. }
  28. public Slider(SliderProp prop)
  29. : this(string.Empty, prop.Left, prop.Right, prop.Initial, prop.Default)
  30. {
  31. }
  32. public bool HasReset { get; set; }
  33. public string Label
  34. {
  35. get => label;
  36. set
  37. {
  38. label = value;
  39. hasLabel = !string.IsNullOrEmpty(label);
  40. }
  41. }
  42. public float Value
  43. {
  44. get => value;
  45. set
  46. {
  47. this.value = Utility.Bound(value, Left, Right);
  48. if (hasTextField)
  49. textFieldValue = FormatValue(value);
  50. OnControlEvent(EventArgs.Empty);
  51. }
  52. }
  53. public float Left
  54. {
  55. get => left;
  56. set
  57. {
  58. left = value;
  59. this.value = Utility.Bound(value, left, right);
  60. }
  61. }
  62. public float Right
  63. {
  64. get => right;
  65. set
  66. {
  67. right = value;
  68. this.value = Utility.Bound(value, left, right);
  69. }
  70. }
  71. public float DefaultValue
  72. {
  73. get => defaultValue;
  74. set => defaultValue = Utility.Bound(value, Left, Right);
  75. }
  76. public bool HasTextField
  77. {
  78. get => hasTextField;
  79. set
  80. {
  81. hasTextField = value;
  82. if (hasTextField)
  83. textFieldValue = FormatValue(Value);
  84. }
  85. }
  86. public override void Draw(params GUILayoutOption[] layoutOptions)
  87. {
  88. var hasUpper = hasLabel || HasTextField || HasReset;
  89. var tempText = string.Empty;
  90. if (hasUpper)
  91. {
  92. GUILayout.BeginVertical(GUILayout.ExpandWidth(false));
  93. GUILayout.BeginHorizontal();
  94. if (hasLabel)
  95. {
  96. GUILayout.Label(Label, MpsGui.SliderLabelStyle, GUILayout.ExpandWidth(false));
  97. GUILayout.FlexibleSpace();
  98. }
  99. if (HasTextField)
  100. tempText = GUILayout.TextField(textFieldValue, MpsGui.SliderTextBoxStyle, GUILayout.Width(60f));
  101. if (HasReset && GUILayout.Button("|", MpsGui.SliderResetButtonStyle, GUILayout.Width(15f)))
  102. {
  103. Value = DefaultValue;
  104. tempText = textFieldValue = FormatValue(Value);
  105. }
  106. GUILayout.EndHorizontal();
  107. }
  108. var sliderStyle = hasUpper ? MpsGui.SliderStyle : MpsGui.SliderStyleNoLabel;
  109. var tempValue =
  110. GUILayout.HorizontalSlider(Value, Left, Right, sliderStyle, MpsGui.SliderThumbStyle, layoutOptions);
  111. if (hasUpper)
  112. GUILayout.EndVertical();
  113. if (HasTextField)
  114. {
  115. if (tempValue != Value)
  116. tempText = textFieldValue = FormatValue(tempValue);
  117. if (tempText != textFieldValue)
  118. {
  119. textFieldValue = tempText;
  120. if (float.TryParse(tempText, out var newValue))
  121. tempValue = newValue;
  122. }
  123. }
  124. if (tempValue != Value)
  125. Value = tempValue;
  126. }
  127. public void SetBounds(float left, float right)
  128. {
  129. this.left = left;
  130. this.right = right;
  131. value = Utility.Bound(value, left, right);
  132. }
  133. private static string FormatValue(float value) =>
  134. value.ToString("0.####", CultureInfo.InvariantCulture);
  135. }