Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
ButtonWidget.cs
1using System.Drawing;
7using SkiaSharp;
8
10
14public sealed class ButtonWidget : WidgetBase
15{
16 private Color _normalColor = Color.FromArgb(
17 255,
18 60,
19 60,
20 68);
21
22 private Color _hoverColor = Color.FromArgb(
23 255,
24 78,
25 78,
26 88);
27
28 private Color _pressedColor = Color.FromArgb(
29 255,
30 42,
31 42,
32 48);
33
37 public event Action? Clicked;
38
39 #region constructors
40
50 RenderSurfaceHostBase renderSurfaceHost,
51 View view,
52 Rectangle bounds,
53 string text,
54 string? nickname = null)
55 : base(
56 renderSurfaceHost,
58 bounds.Location,
59 nickname)
60 {
61 Background = CreateBackground(renderSurfaceHost, view, bounds);
62
63 Label = CreateLabel(
64 renderSurfaceHost,
65 view,
66 bounds,
67 text);
68
69 CompleteInitialization();
70 }
71
81 RenderSurfaceHostBase renderSurfaceHost,
82 SceneLayer sceneLayer,
83 Rectangle bounds,
84 string text,
85 string? nickname = null)
86 : base(
87 renderSurfaceHost,
89 bounds.Location,
90 nickname)
91 {
92 Background = CreateBackground(renderSurfaceHost, sceneLayer, bounds);
93
94 Label = CreateLabel(
95 renderSurfaceHost,
96 sceneLayer,
97 bounds,
98 text);
99
100 CompleteInitialization();
101 }
102
103 #endregion constructors
104
105 #region public properties
106
111
115 public TextBlock Label { get; }
116
117 #endregion public properties
118
119 #region public methods
120
126 public ButtonWidget SetText(string text)
127 {
128 Label.SetText(text);
129 return this;
130 }
131
140 Color normal,
141 Color hover,
142 Color pressed)
143 {
144 _normalColor = normal;
145 _hoverColor = hover;
146 _pressedColor = pressed;
147
148 UpdateBackgroundColor(normal);
149
150 return this;
151 }
152
158 public ButtonWidget SetTextColor(Color color)
159 {
160 Label.SetColors(color, Color.Transparent);
161
162 return this;
163 }
164
170 public ButtonWidget SetButtonZOrder(int zOrder)
171 {
172 Background.ZOrder = zOrder;
173 Label.ZOrder = zOrder + 1;
174
175 return this;
176 }
177
181 public void PerformClick()
182 {
183 if (!IsInputEnabled)
184 return;
185
186 Clicked?.Invoke();
187 }
188
189 #endregion public methods
190
191 #region protected methods
192
194 protected override void OnPointerEnter(WidgetPointerEventArgs args)
195 {
196 base.OnPointerEnter(args);
197 UpdateBackgroundColor(_hoverColor);
198 }
199
201 protected override void OnPointerLeave(WidgetPointerEventArgs args)
202 {
203 base.OnPointerLeave(args);
204 UpdateBackgroundColor(_normalColor);
205 }
206
208 protected override void OnPointerDown(WidgetPointerEventArgs args)
209 {
210 base.OnPointerDown(args);
211
212 if (args.IsPrimaryButton)
213 UpdateBackgroundColor(_pressedColor);
214 }
215
217 protected override void OnPointerUp(WidgetPointerEventArgs args)
218 {
219 base.OnPointerUp(args);
220 UpdateBackgroundColor(_hoverColor);
221 }
222
224 protected override void OnPointerClick(WidgetPointerEventArgs args)
225 {
226 base.OnPointerClick(args);
227
228 if (!args.IsPrimaryButton)
229 return;
230
231 args.Handled = true;
232 PerformClick();
233 }
234
236 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
237 {
238 base.OnKeyboardInput(args);
239
240 if (args.KeyAction != KeyAction.Pressed)
241 {
242 return;
243 }
244
245 // Standard Enter and Space virtual-key values.
246 if (args.Key is not 13 and not 32)
247 return;
248
249 args.Handled = true;
250 PerformClick();
251 }
252
253 #endregion protected methods
254
255 #region private methods
256
257 private void UpdateBackgroundColor(Color color)
258 {
259 Background.SetColor(color);
260
261 // DirectRectangle's current color setter rebuilds its paint but does not
262 // enqueue a dirty region by itself. Reapplying the existing position
263 // performs the required old/new refresh without moving the button.
265 }
266
267 private void CompleteInitialization()
268 {
270 Add(Label);
271
273
274 CanReceiveFocus = true;
276 }
277
278 private DirectRectangle CreateBackground(RenderSurfaceHostBase host, View view, Rectangle bounds)
279 {
280 return new DirectRectangle(
281 _normalColor,
282 host,
283 view,
284 bounds,
285 $"{Nickname}.background").SetFilled(true)
287 Color.FromArgb(
288 255,
289 150,
290 150,
291 160))
292 .SetStrokeWidth(1.5f)
293 .SetCornerRadius(5f);
294 }
295
296 private DirectRectangle CreateBackground(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds)
297 {
298 return new DirectRectangle(
299 _normalColor,
300 host,
301 layer,
302 bounds,
303 $"{Nickname}.background").SetFilled(true)
305 Color.FromArgb(
306 255,
307 150,
308 150,
309 160))
310 .SetStrokeWidth(1.5f)
311 .SetCornerRadius(5f);
312 }
313
314 private static TextBlock CreateLabel(
315 RenderSurfaceHostBase host,
316 View view,
317 Rectangle bounds,
318 string text)
319 {
320 return new TextBlock(host, view, bounds).SetText(text)
321 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
322 .SetColors(SKColors.White, SKColors.Transparent)
323 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
324 .EnableWrapping(false);
325 }
326
327 private static TextBlock CreateLabel(
328 RenderSurfaceHostBase host,
329 SceneLayer layer,
330 Rectangle bounds,
331 string text)
332 {
333 return new TextBlock(
334 host,
335 layer,
336 view: null,
337 worldBounds: bounds).SetText(text)
338 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
339 .SetColors(SKColors.White, SKColors.Transparent)
340 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
341 .EnableWrapping(false);
342 }
343
344 #endregion private methods
345}
SceneLayer? SceneLayer
Gets the scene layer shared by all children in scene-layer mode.
View? View
Gets the view shared by all children in view mode.
virtual DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.
Vector2 GetPosition()
Gets the current position of this direct drawing in pixel coordinates.
void SetPosition(Vector2 p)
Sets the position of this direct drawing to the specified pixel coordinates.
Renders a configurable rectangle with support for fill, border, rounded corners, dash patterns,...
DirectRectangle SetFilled(bool isFilled)
Sets whether the rectangle interior is filled with the base color (or pattern).
DirectRectangle SetBorderColor(Color color)
Sets a distinct border (stroke) color, independent of the fill color.
DirectRectangle SetColor(Color color)
Sets the base color used for both fill and stroke (unless a distinct border color is specified).
DirectRectangle SetStrokeWidth(float width)
Sets the width (thickness) of the rectangle's stroke (outline) in pixels.
DirectRectangle SetCornerRadius(float radius)
Sets the radius for rounded corners in pixels.
A retained-mode text drawable with wrapping, multi-line ( ) support, horizontal/vertical alignment,...
Definition TextBlock.cs:44
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
Represents a rendered view of a scene, combining a camera position with a viewport configuration to c...
Definition View.cs:14
Represents a single layer within a Scene, containing a grid of tiles and supporting various coordinat...
Definition SceneLayer.cs:40
ButtonWidget SetTextColor(Color color)
Sets the button text color.
override void OnPointerDown(WidgetPointerEventArgs args)
Called when a pointer press begins on the widget.
ButtonWidget SetBackgroundColors(Color normal, Color hover, Color pressed)
Sets the normal, hover, and pressed background colors.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
TextBlock Label
Gets the text block used as the button label.
override void OnPointerLeave(WidgetPointerEventArgs args)
Called when a pointer leaves the widget bounds.
DirectRectangle Background
Gets the rectangle used as the button background and border.
Action? Clicked
Occurs when the button is activated by pointer or keyboard.
override void OnPointerUp(WidgetPointerEventArgs args)
Called when a pointer press is released for the widget.
override void OnPointerClick(WidgetPointerEventArgs args)
Called when a pointer click is dispatched to the widget.
ButtonWidget SetText(string text)
Sets the button text.
ButtonWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, string text, string? nickname=null)
Initializes a scene-layer button.
ButtonWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, string text, string? nickname=null)
Initializes a view-level button.
override void OnPointerEnter(WidgetPointerEventArgs args)
Called when a pointer enters the widget bounds.
void PerformClick()
Programmatically activates the button.
ButtonWidget SetButtonZOrder(int zOrder)
Sets the background Z-order and places the label immediately above it.
bool CanReceiveFocus
Gets or sets a value indicating whether the widget can receive keyboard focus.
WidgetBase(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the WidgetBase class.
Definition WidgetBase.cs:95
bool IsKeyboardInputEnabled
Gets or sets a value indicating whether the widget can receive keyboard input.
bool IsInputEnabled
Gets or sets a value indicating whether the widget can participate in input routing.
Provides data for widget keyboard input events routed by WidgetInputRouter.
int Key
Gets the integer key identifier.
KeyAction KeyAction
Gets the action associated with this key event.
Event arguments for pointer-based widget callbacks.
bool IsPrimaryButton
Gets whether the pointer event represents a primary-button interaction.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
KeyAction
Represents the type of action performed on a keyboard key, indicating whether the key was pressed,...
Definition KeyAction.cs:10