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
PanelWidget.cs
1using System.Drawing;
2using System.Numerics;
7using SkiaSharp;
8
10
14public sealed class PanelWidget : ContainerWidget
15{
16 private int _panelZOrder;
17
27 RenderSurfaceHostBase renderSurfaceHost,
28 View view,
29 Rectangle bounds,
30 Color backgroundColor,
31 string? nickname = null)
32 : base(
33 renderSurfaceHost,
35 ValidateBounds(bounds).Location,
36 nickname)
37 {
38 ArgumentNullException.ThrowIfNull(view);
39
41 backgroundColor,
42 renderSurfaceHost,
43 view,
44 bounds,
45 $"{Nickname}.background")
46 .SetFilled(true)
47 .SetStrokeWidth(0f);
48
49 Add(Background, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
50 DisableInput();
51 }
52
62 RenderSurfaceHostBase renderSurfaceHost,
63 SceneLayer sceneLayer,
64 Rectangle bounds,
65 Color backgroundColor,
66 string? nickname = null)
67 : base(
68 renderSurfaceHost,
70 ValidateBounds(bounds).Location,
71 nickname)
72 {
73 ArgumentNullException.ThrowIfNull(sceneLayer);
74
76 backgroundColor,
77 renderSurfaceHost,
78 sceneLayer,
79 bounds,
80 $"{Nickname}.background")
81 .SetFilled(true)
82 .SetStrokeWidth(0f);
83
84 Add(Background, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
85 DisableInput();
86 }
87
92
96 public Rectangle Bounds => Mode == DirectDrawingMode.View
97 ? Background.ScreenBounds
98 : Background.WorldBounds;
99
103 public Size Size
104 {
105 get => Bounds.Size;
106 set
107 {
108 ValidateSize(value);
109
110 if (Mode == DirectDrawingMode.View)
111 Background.ScreenBounds = new Rectangle(Background.ScreenBounds.Location, value);
112 else
113 Background.WorldBounds = new Rectangle(Background.WorldBounds.Location, value);
114 }
115 }
116
123 public PanelWidget AddWidget(WidgetBase widget, Point? offsetPx = null)
124 {
125 ArgumentNullException.ThrowIfNull(widget);
126
127 Point offset = offsetPx ?? Point.Empty;
128
129 Add(
130 widget,
131 keepCurrentOffset: false,
132 explicitLocalOffsetPx: new Vector2(offset.X, offset.Y));
133
134 SetWidgetZOrder(widget, _panelZOrder + 1);
135 return this;
136 }
137
144 public PanelWidget RemoveWidget(WidgetBase widget, bool dispose = false)
145 {
146 RemoveChild(widget, dispose);
147 return this;
148 }
149
155 public PanelWidget SetBackgroundColor(Color color)
156 {
157 Background.SetColor(color);
158 RefreshBackground();
159 return this;
160 }
161
167 public PanelWidget SetBorderColor(Color color)
168 {
169 Background.SetBorderColor(color);
170 RefreshBackground();
171 return this;
172 }
173
179 public PanelWidget SetStrokeWidth(float width)
180 {
181 Background.SetStrokeWidth(width);
182 RefreshBackground();
183 return this;
184 }
185
191 public PanelWidget SetCornerRadius(float radius)
192 {
193 Background.SetCornerRadius(radius);
194 RefreshBackground();
195 return this;
196 }
197
208 SKBitmap bitmap,
210 float scale = 1f,
211 SKPoint? offsetPx = null,
212 SKFilterQuality filterQuality = SKFilterQuality.Medium)
213 {
214 Background.SetFillImage(
215 bitmap,
216 mode,
217 scale,
218 offsetPx,
219 filterQuality);
220
221 return this;
222 }
223
234 SKImage image,
236 float scale = 1f,
237 SKPoint? offsetPx = null,
238 SKFilterQuality filterQuality = SKFilterQuality.Medium)
239 {
240 Background.SetFillImage(
241 image,
242 mode,
243 scale,
244 offsetPx,
245 filterQuality);
246
247 return this;
248 }
249
255 {
256 Background.ClearFillImage();
257 return this;
258 }
259
265 public PanelWidget SetPanelZOrder(int zOrder)
266 {
267 _panelZOrder = zOrder;
268 Background.ZOrder = zOrder;
269
270 foreach (WidgetBase widget in ChildWidgets)
271 SetWidgetZOrder(widget, zOrder + 1);
272
273 return this;
274 }
275
276 private void RefreshBackground()
277 {
279 }
280
281 private void DisableInput()
282 {
283 IsInputEnabled = false;
284 IsPointerInputEnabled = false;
286 CanReceiveFocus = false;
287 }
288
289 private static Rectangle ValidateBounds(Rectangle bounds)
290 {
291 ValidateSize(bounds.Size);
292 return bounds;
293 }
294
295 private static void ValidateSize(Size size)
296 {
297 if (size.Width <= 0 || size.Height <= 0)
298 throw new ArgumentOutOfRangeException(nameof(size), size, "Panel size must be positive.");
299 }
300
301 private static void SetWidgetZOrder(WidgetBase widget, int zOrder)
302 {
303 DirectDrawingBase[] visuals = EnumerateVisuals(widget).ToArray();
304
305 if (visuals.Length == 0)
306 return;
307
308 int currentMin = visuals.Min(static visual => visual.ZOrder);
309 int delta = zOrder - currentMin;
310
311 foreach (DirectDrawingBase visual in visuals)
312 visual.ZOrder += delta;
313 }
314
315 private static IEnumerable<DirectDrawingBase> EnumerateVisuals(IDirectCompositeContainer container)
316 {
317 foreach (IDirectCompositeChild child in container.Children)
318 {
319 if (child is DirectDrawingBase drawing)
320 yield return drawing;
321
322 if (child is IDirectCompositeContainer nestedContainer)
323 {
324 foreach (DirectDrawingBase nestedDrawing in EnumerateVisuals(nestedContainer))
325 yield return nestedDrawing;
326 }
327 }
328 }
329}
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.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
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 SetStrokeWidth(float width)
Sets the width (thickness) of the rectangle's stroke (outline) in pixels.
ImageFillMode
Defines how an image fills the rectangle interior.
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
bool RemoveChild(WidgetBase widget, bool dispose=false)
Removes a child widget from the container.
override DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.The current composite.
ContainerWidget(RenderSurfaceHostBase renderSurfaceHost, DirectDrawingMode mode, PointF anchor=default, string? nickname=null)
Initializes a new instance of the ContainerWidget class.
IReadOnlyList< WidgetBase > ChildWidgets
Gets a snapshot of the child widgets directly owned by this container.
PanelWidget AddWidget(WidgetBase widget, Point? offsetPx=null)
Adds a child widget at the supplied local offset from the panel's upper-left corner.
PanelWidget SetPanelZOrder(int zOrder)
Places the background at the requested Z-order and child widgets immediately above it.
PanelWidget SetStrokeWidth(float width)
Sets the panel border width.
PanelWidget(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, Color backgroundColor, string? nickname=null)
Creates a scene-layer panel.
PanelWidget ClearBackgroundImage()
Clears an image background and returns to the configured solid fill.
PanelWidget(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, Color backgroundColor, string? nickname=null)
Creates a view-level panel.
PanelWidget SetBackgroundImage(SKImage image, DirectRectangle.ImageFillMode mode=DirectRectangle.ImageFillMode.Stretch, float scale=1f, SKPoint? offsetPx=null, SKFilterQuality filterQuality=SKFilterQuality.Medium)
Uses an image as the panel background.
DirectRectangle Background
Gets the panel background drawing.
PanelWidget SetBackgroundImage(SKBitmap bitmap, DirectRectangle.ImageFillMode mode=DirectRectangle.ImageFillMode.Stretch, float scale=1f, SKPoint? offsetPx=null, SKFilterQuality filterQuality=SKFilterQuality.Medium)
Uses a bitmap as the panel background.
PanelWidget RemoveWidget(WidgetBase widget, bool dispose=false)
Removes a child widget without disposing it unless requested.
PanelWidget SetBorderColor(Color color)
Sets the panel border color.
Size Size
Gets or sets the panel size while preserving its current position.
Rectangle Bounds
Gets the panel bounds in its native coordinate space.
PanelWidget SetCornerRadius(float radius)
Sets the panel corner radius.
PanelWidget SetBackgroundColor(Color color)
Sets the solid background color.
bool CanReceiveFocus
Gets or sets a value indicating whether the widget can receive keyboard focus.
bool IsPointerInputEnabled
Gets or sets a value indicating whether the widget can receive pointer input.
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.
IReadOnlyCollection< IDirectCompositeChild > Children
Gets the read-only collection of child elements in this container.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.