Gondwana Game Engine Development (master) - 2.6.1
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(255, 60, 60, 68);
17 private Color _hoverColor = Color.FromArgb(255, 78, 78, 88);
18 private Color _pressedColor = Color.FromArgb(255, 42, 42, 48);
19
23 public event Action? Clicked;
24
25 #region constructors
26
35 public ButtonWidget(RenderSurfaceHostBase renderSurfaceHost,
36 View view,
37 Rectangle bounds,
38 string text,
39 string? nickname = null)
40 : base(renderSurfaceHost, DirectDrawingMode.View, bounds.Location, nickname)
41 {
42 Background = CreateBackground(renderSurfaceHost, view, bounds);
43 Label = CreateLabel(renderSurfaceHost, view, bounds, text);
44
45 CompleteInitialization();
46 }
47
56 public ButtonWidget(RenderSurfaceHostBase renderSurfaceHost,
57 SceneLayer sceneLayer,
58 Rectangle bounds,
59 string text,
60 string? nickname = null)
61 : base(renderSurfaceHost, DirectDrawingMode.SceneLayer, bounds.Location, nickname)
62 {
63 Background = CreateBackground(renderSurfaceHost, sceneLayer, bounds);
64 Label = CreateLabel(renderSurfaceHost, sceneLayer, bounds, text);
65
66 CompleteInitialization();
67 }
68
69 #endregion constructors
70
71 #region public properties
72
77
81 public TextBlock Label { get; }
82
83 #endregion public properties
84
85 #region public methods
86
92 public ButtonWidget SetText(string text)
93 {
94 Label.SetText(text);
95 return this;
96 }
97
106 Color hover,
107 Color pressed)
108 {
109 _normalColor = normal;
110 _hoverColor = hover;
111 _pressedColor = pressed;
112
113 UpdateBackgroundColor(normal);
114
115 return this;
116 }
117
123 public ButtonWidget SetTextColor(Color color)
124 {
125 Label.SetColors(color, Color.Transparent);
126
127 return this;
128 }
129
135 public ButtonWidget SetButtonZOrder(int zOrder)
136 {
137 Background.ZOrder = zOrder;
138 Label.ZOrder = zOrder + 1;
139
140 return this;
141 }
142
146 public void PerformClick()
147 {
148 if (!IsInputEnabled)
149 return;
150
151 Clicked?.Invoke();
152 }
153
154 #endregion public methods
155
156 #region protected methods
157
159 protected override void OnPointerEnter(WidgetPointerEventArgs args)
160 {
161 base.OnPointerEnter(args);
162 UpdateBackgroundColor(_hoverColor);
163 }
164
166 protected override void OnPointerLeave(WidgetPointerEventArgs args)
167 {
168 base.OnPointerLeave(args);
169 UpdateBackgroundColor(_normalColor);
170 }
171
173 protected override void OnPointerDown(WidgetPointerEventArgs args)
174 {
175 base.OnPointerDown(args);
176
177 if (args.IsPrimaryButton)
178 UpdateBackgroundColor(_pressedColor);
179 }
180
182 protected override void OnPointerUp(WidgetPointerEventArgs args)
183 {
184 base.OnPointerUp(args);
185 UpdateBackgroundColor(_hoverColor);
186 }
187
189 protected override void OnPointerClick(WidgetPointerEventArgs args)
190 {
191 base.OnPointerClick(args);
192
193 if (!args.IsPrimaryButton)
194 return;
195
196 args.Handled = true;
197 PerformClick();
198 }
199
201 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
202 {
203 base.OnKeyboardInput(args);
204
205 if (args.KeyAction != KeyAction.Pressed)
206 {
207 return;
208 }
209
210 // Standard Enter and Space virtual-key values.
211 if (args.Key is not 13 and not 32)
212 return;
213
214 args.Handled = true;
215 PerformClick();
216 }
217
218 #endregion protected methods
219
220 #region private methods
221
222 private void UpdateBackgroundColor(Color color)
223 {
224 Background.SetColor(color);
225
226 // DirectRectangle's current color setter rebuilds its paint but does not
227 // enqueue a dirty region by itself. Reapplying the existing position
228 // performs the required old/new refresh without moving the button.
230 }
231
232 private void CompleteInitialization()
233 {
235 Add(Label);
236
238
239 CanReceiveFocus = true;
241 }
242
243 private DirectRectangle CreateBackground(RenderSurfaceHostBase host, View view, Rectangle bounds)
244 {
245 return new DirectRectangle(_normalColor,
246 host,
247 view,
248 bounds,
249 $"{Nickname}.background").SetFilled(true)
250 .SetBorderColor(Color.FromArgb(255, 150, 150, 160))
251 .SetStrokeWidth(1.5f)
252 .SetCornerRadius(5f);
253 }
254
255 private DirectRectangle CreateBackground(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds)
256 {
257 return new DirectRectangle(_normalColor,
258 host,
259 layer,
260 bounds,
261 $"{Nickname}.background").SetFilled(true)
262 .SetBorderColor(Color.FromArgb(255, 150, 150, 160))
263 .SetStrokeWidth(1.5f)
264 .SetCornerRadius(5f);
265 }
266
267 private static TextBlock CreateLabel(RenderSurfaceHostBase host, View view, Rectangle bounds, string text)
268 {
269 return new TextBlock(host, view, bounds).SetText(text)
270 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
271 .SetColors(SKColors.White, SKColors.Transparent)
272 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
273 .EnableWrapping(false);
274 }
275
276 private static TextBlock CreateLabel(RenderSurfaceHostBase host, SceneLayer layer, Rectangle bounds, string text)
277 {
278 return new TextBlock(host,
279 layer,
280 view: null,
281 worldBounds: bounds).SetText(text)
282 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
283 .SetColors(SKColors.White, SKColors.Transparent)
284 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
285 .EnableWrapping(false);
286 }
287
288 #endregion private methods
289}
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