Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ValueBarWidget.cs
浏览该文件的文档.
1using Engine;
3
4namespace Game {
5 public class ValueBarWidget : Widget {
6 public float m_value;
7
8 public int m_barsCount = 8;
9
10 public Color m_litBarColor = new(16, 140, 0);
11
13
14 public Color m_unlitBarColor = new(48, 48, 48);
15
17
19
20 public float m_flashCount;
21
23
24 public float Value {
25 get => m_value;
26 set => m_value = MathUtils.Saturate(value);
27 }
28
29 public int BarsCount {
30 get => m_barsCount;
31 set => m_barsCount = Math.Clamp(value, 1, 1000);
32 }
33
34 public bool FlipDirection { get; set; }
35
36 public Vector2 BarSize { get; set; }
37
38 public float Spacing { get; set; }
39
41 get => m_litBarColor;
42 set => m_litBarColor = value;
43 }
44
46 get => m_litBarColor2;
47 set => m_litBarColor2 = value;
48 }
49
51 get => m_unlitBarColor;
52 set => m_unlitBarColor = value;
53 }
54
55 public bool BarBlending { get; set; }
56
57 public bool HalfBars { get; set; }
58
60 get => m_barSubtexture;
61 set => m_barSubtexture = value;
62 }
63
64 public bool TextureLinearFilter {
66 set => m_textureLinearFilter = value;
67 }
68
70 get => m_layoutDirection;
71 set => m_layoutDirection = value;
72 }
73
74 public ValueBarWidget() {
75 IsHitTestVisible = false;
76 BarSize = new Vector2(24f);
77 BarBlending = true;
79 }
80
81 public void Flash(int count) {
83 }
84
85 public override void Draw(DrawContext dc) {
86 BaseBatch baseBatch = BarSubtexture == null
89 BarSubtexture.Texture,
90 false,
91 0,
93 null,
94 null,
96 );
97 int num;
98 int start = 0;
99 if (baseBatch is TexturedBatch2D batch2D) {
100 num = batch2D.TriangleVertices.Count;
101 }
102 else {
103 start = ((FlatBatch2D)baseBatch).LineVertices.Count;
104 num = ((FlatBatch2D)baseBatch).TriangleVertices.Count;
105 }
106 Vector2 zero = Vector2.Zero;
107 if (m_layoutDirection == LayoutDirection.Horizontal) {
108 zero.X += Spacing / 2f;
109 }
110 else {
111 zero.Y += Spacing / 2f;
112 }
113 int num2 = HalfBars ? 1 : 2;
114 for (int i = 0; i < 2 * BarsCount; i += num2) {
115 bool flag = i % 2 == 0;
116 float num3 = 0.5f * i;
117 float num4 = !FlipDirection
118 ? Math.Clamp((Value - num3 / BarsCount) * BarsCount, 0f, 1f)
119 : Math.Clamp((Value - (BarsCount - num3 - 1f) / BarsCount) * BarsCount, 0f, 1f);
120 if (!BarBlending) {
121 num4 = MathF.Ceiling(num4);
122 }
123 float s = m_flashCount > 0f ? 1f - MathF.Abs(MathF.Sin(m_flashCount * (float)Math.PI)) : 1f;
124 Color c = LitBarColor;
126 && BarsCount > 1) {
127 c = Color.Lerp(LitBarColor, LitBarColor2, num3 / (BarsCount - 1));
128 }
129 Color color = Color.Lerp(UnlitBarColor, c, num4) * s * GlobalColorTransform;
130 if (HalfBars) {
131 if (flag) {
132 Vector2 zero2 = Vector2.Zero;
133 Vector2 v = m_layoutDirection == LayoutDirection.Horizontal ? new Vector2(0.5f, 1f) : new Vector2(1f, 0.5f);
134 if (baseBatch is TexturedBatch2D texturedBatch2D) {
135 if (BarSubtexture != null) {
136 Vector2 topLeft = BarSubtexture.TopLeft;
137 Vector2 texCoord = new(
138 MathUtils.Lerp(BarSubtexture.TopLeft.X, BarSubtexture.BottomRight.X, v.X),
139 MathUtils.Lerp(BarSubtexture.TopLeft.Y, BarSubtexture.BottomRight.Y, v.Y)
140 );
141 texturedBatch2D.QueueQuad(zero + zero2 * BarSize, zero + v * BarSize, 0f, topLeft, texCoord, color);
142 }
143 }
144 else {
145 ((FlatBatch2D)baseBatch).QueueQuad(zero + zero2 * BarSize, zero + v * BarSize, 0f, color);
146 }
147 }
148 else {
149 Vector2 v2 = m_layoutDirection == LayoutDirection.Horizontal ? new Vector2(0.5f, 0f) : new Vector2(0f, 0.5f);
150 Vector2 one = Vector2.One;
151 if (baseBatch is TexturedBatch2D texturedBatch2D) {
152 if (BarSubtexture != null) {
153 Vector2 texCoord2 = new(
154 MathUtils.Lerp(BarSubtexture.TopLeft.X, BarSubtexture.BottomRight.X, v2.X),
155 MathUtils.Lerp(BarSubtexture.TopLeft.Y, BarSubtexture.BottomRight.Y, v2.Y)
156 );
157 Vector2 bottomRight = BarSubtexture.BottomRight;
158 texturedBatch2D.QueueQuad(zero + v2 * BarSize, zero + one * BarSize, 0f, texCoord2, bottomRight, color);
159 }
160 }
161 else {
162 ((FlatBatch2D)baseBatch).QueueQuad(zero + v2 * BarSize, zero + one * BarSize, 0f, color);
163 }
164 }
165 }
166 else {
167 Vector2 zero3 = Vector2.Zero;
168 Vector2 one2 = Vector2.One;
169 if (baseBatch is TexturedBatch2D texturedBatch2D) {
170 if (BarSubtexture != null) {
171 Vector2 topLeft2 = BarSubtexture.TopLeft;
172 Vector2 bottomRight2 = BarSubtexture.BottomRight;
173 texturedBatch2D.QueueQuad(zero + zero3 * BarSize, zero + one2 * BarSize, 0f, topLeft2, bottomRight2, color);
174 }
175 }
176 else {
177 ((FlatBatch2D)baseBatch).QueueQuad(zero + zero3 * BarSize, zero + one2 * BarSize, 0f, color);
178 ((FlatBatch2D)baseBatch).QueueRectangle(
179 zero + zero3 * BarSize,
180 zero + one2 * BarSize,
181 0f,
182 Color.MultiplyColorOnly(color, 0.75f)
183 );
184 }
185 }
186 if (!flag
187 || !HalfBars) {
188 if (m_layoutDirection == LayoutDirection.Horizontal) {
189 zero.X += BarSize.X + Spacing;
190 }
191 else {
192 zero.Y += BarSize.Y + Spacing;
193 }
194 }
195 }
196 if (baseBatch is TexturedBatch2D batch) {
197 batch.TransformTriangles(GlobalTransform, num);
198 }
199 else {
200 ((FlatBatch2D)baseBatch).TransformLines(GlobalTransform, start);
201 ((FlatBatch2D)baseBatch).TransformTriangles(GlobalTransform, num);
202 }
204 }
205
206 public override void MeasureOverride(Vector2 parentAvailableSize) {
207 IsDrawRequired = true;
209 ? new Vector2((BarSize.X + Spacing) * BarsCount, BarSize.Y)
210 : new Vector2(BarSize.X, (BarSize.Y + Spacing) * BarsCount);
211 }
212 }
213}
static readonly DepthStencilState None
TexturedBatch2D TexturedBatch(Texture2D texture, bool useAlphaTest=false, int layer=0, DepthStencilState depthStencilState=null, RasterizerState rasterizerState=null, BlendState blendState=null, SamplerState samplerState=null)
FlatBatch2D FlatBatch(int layer=0, DepthStencilState depthStencilState=null, RasterizerState rasterizerState=null, BlendState blendState=null)
static float Saturate(float x)
static int Max(int x1, int x2)
static float Lerp(float x1, float x2, float f)
static float FrameDuration
定义 Time.cs:46
override void Draw(DrawContext dc)
LayoutDirection m_layoutDirection
override void MeasureOverride(Vector2 parentAvailableSize)
LayoutDirection LayoutDirection
readonly PrimitivesRenderer2D PrimitivesRenderer2D
Color GlobalColorTransform
Vector2 DesiredSize
bool IsDrawRequired
virtual bool IsHitTestVisible
Matrix GlobalTransform
static Color Lerp(Color c1, Color c2, float f)
static Color Transparent
定义 Color.cs:5
static Color MultiplyColorOnly(Color c, float s)
static readonly Vector2 Zero
static readonly Vector2 One