Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SliderWidget.cs
浏览该文件的文档.
1using System.Xml.Linq;
2using Engine;
3using Engine.Media;
4
5namespace Game {
6 public class SliderWidget : CanvasWidget {
8
10
12
14
15 public float m_minValue;
16
17 public float m_maxValue = 1f;
18
19 public float m_granularity = 0.1f;
20
21 public float m_value;
22
24
25 public bool IsSliding { get; set; }
26
27 public LayoutDirection LayoutDirection { get; set; }
28
29 public float MinValue {
30 get => m_minValue;
31 set {
32 if (value != m_minValue) {
33 m_minValue = value;
35 Value = Math.Clamp(Value, MinValue, MaxValue);
36 }
37 }
38 }
39
40 public float MaxValue {
41 get => m_maxValue;
42 set {
43 if (value != m_maxValue) {
44 m_maxValue = value;
46 Value = Math.Clamp(Value, MinValue, MaxValue);
47 }
48 }
49 }
50
51 public float Value {
52 get => m_value;
53 set => m_value = m_granularity > 0f
54 ? MathF.Round(Math.Clamp(value, MinValue, MaxValue) / m_granularity) * m_granularity
55 : Math.Clamp(value, MinValue, MaxValue);
56 }
57
58 public float Granularity {
59 get => m_granularity;
60 set => m_granularity = MathUtils.Max(value, 0f);
61 }
62
63 public string Text {
64 get => m_labelWidget.Text;
65 set => m_labelWidget.Text = value;
66 }
67
69 get => m_labelWidget.Font;
70 set => m_labelWidget.Font = value;
71 }
72
73 public string SoundName { get; set; }
74
75 public bool IsLabelVisible {
76 get => m_labelCanvasWidget.IsVisible;
77 set => m_labelCanvasWidget.IsVisible = value;
78 }
79
80 public float LabelWidth {
81 get => m_labelCanvasWidget.Size.X;
82 set => m_labelCanvasWidget.Size = new Vector2(value, m_labelCanvasWidget.Size.Y);
83 }
84
86 get => m_labelWidget.Color;
87 set => m_labelWidget.Color = value;
88 }
89
90 public bool SlidingCompleted { get; private set; }
91
92 public SliderWidget() {
93 XElement node = ContentManager.Get<XElement>("Widgets/SliderContents");
94 LoadChildren(this, node);
95 m_canvasWidget = Children.Find<CanvasWidget>("Slider.Canvas");
96 m_labelCanvasWidget = Children.Find<CanvasWidget>("Slider.LabelCanvas");
97 m_tabWidget = Children.Find<Widget>("Slider.Tab");
98 m_labelWidget = Children.Find<LabelWidget>("Slider.Label");
99 LoadProperties(this, node);
100 }
101
102 public override void MeasureOverride(Vector2 parentAvailableSize) {
103 base.MeasureOverride(parentAvailableSize);
104 IsDrawRequired = true;
105 }
106
107 public override void ArrangeOverride() {
108 base.ArrangeOverride();
109 float num = LayoutDirection == LayoutDirection.Horizontal ? m_canvasWidget.ActualSize.X : m_canvasWidget.ActualSize.Y;
110 float num2 = LayoutDirection == LayoutDirection.Horizontal ? m_tabWidget.ActualSize.X : m_tabWidget.ActualSize.Y;
111 float num3 = MaxValue > MinValue ? (Value - MinValue) / (MaxValue - MinValue) : 0f;
112 if (LayoutDirection == LayoutDirection.Horizontal) {
113 Vector2 zero = Vector2.Zero;
114 zero.X = num3 * (num - num2);
115 zero.Y = MathUtils.Max((ActualSize.Y - m_tabWidget.ActualSize.Y) / 2f, 0f);
116 m_canvasWidget.SetWidgetPosition(m_tabWidget, zero);
117 }
118 else {
119 Vector2 zero2 = Vector2.Zero;
120 zero2.X = MathUtils.Max(ActualSize.X - m_tabWidget.ActualSize.X, 0f) / 2f;
121 zero2.Y = num3 * (num - num2);
122 m_canvasWidget.SetWidgetPosition(m_tabWidget, zero2);
123 }
124 base.ArrangeOverride();
125 }
126
127 public override void Update() {
128 float num = LayoutDirection == LayoutDirection.Horizontal ? m_canvasWidget.ActualSize.X : m_canvasWidget.ActualSize.Y;
129 float num2 = LayoutDirection == LayoutDirection.Horizontal ? m_tabWidget.ActualSize.X : m_tabWidget.ActualSize.Y;
130 if (Input.Tap.HasValue
131 && HitTestGlobal(Input.Tap.Value) == m_tabWidget) {
133 }
134 if (Input.Press.HasValue) {
135 if (m_dragStartPoint.HasValue) {
136 Vector2 vector = ScreenToWidget(Input.Press.Value);
137 float value = Value;
138 if (LayoutDirection == LayoutDirection.Horizontal) {
139 float f = (vector.X - num2 / 2f) / (num - num2);
141 }
142 else {
143 float f2 = (vector.Y - num2 / 2f) / (num - num2);
145 }
146 if (Value != value
147 && m_granularity > 0f
148 && !string.IsNullOrEmpty(SoundName)) {
149 AudioManager.PlaySound(SoundName, 1f, 0f, 0f);
150 }
151 }
152 }
153 else {
154 m_dragStartPoint = null;
155 }
156 bool flag = m_dragStartPoint.HasValue && IsEnabledGlobal && IsVisibleGlobal;
157 SlidingCompleted = IsSliding && !flag;
158 IsSliding = flag;
159 if (m_dragStartPoint.HasValue) {
160 Input.Clear();
161 }
162 }
163 }
164}
static int Min(int x1, int x2)
static int Max(int x1, int x2)
static float Lerp(float x1, float x2, float f)
static void PlaySound(string name, float volume, float pitch, float pan)
readonly WidgetsList Children
static object Get(Type type, string name)
CanvasWidget m_labelCanvasWidget
override void MeasureOverride(Vector2 parentAvailableSize)
LayoutDirection LayoutDirection
CanvasWidget m_canvasWidget
override void Update()
override void ArrangeOverride()
bool IsVisibleGlobal
virtual Widget HitTestGlobal(Vector2 point, Func< Widget, bool > predicate=null)
WidgetInput Input
bool IsDrawRequired
bool IsEnabledGlobal
virtual void LoadProperties(object eventsTarget, XElement node)
Vector2 ActualSize
virtual Vector2 ScreenToWidget(Vector2 p)
virtual void LoadChildren(object eventsTarget, XElement node)
static readonly Vector2 Zero