Slider.cs 4.9 KB

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