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
PanelWidget.cs
1using System.Drawing;
2using System.Numerics;
7using SkiaSharp;
8
10
14public sealed class PanelWidget : ContainerWidget
15{
16 private int _panelZOrder;
17
26 public PanelWidget(RenderSurfaceHostBase renderSurfaceHost,
27 View view,
28 Rectangle bounds,
29 Color backgroundColor,
30 string? nickname = null)
31 : base(renderSurfaceHost, DirectDrawingMode.View, ValidateBounds(bounds).Location, nickname)
32 {
33 ArgumentNullException.ThrowIfNull(view);
34
35 Background = new DirectRectangle(backgroundColor, renderSurfaceHost, view, bounds, $"{Nickname}.background")
36 .SetFilled(true)
37 .SetStrokeWidth(0f);
38
39 Add(Background, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
40 DisableInput();
41 }
42
51 public PanelWidget(RenderSurfaceHostBase renderSurfaceHost,
52 SceneLayer sceneLayer,
53 Rectangle bounds,
54 Color backgroundColor,
55 string? nickname = null)
56 : base(renderSurfaceHost, DirectDrawingMode.SceneLayer, ValidateBounds(bounds).Location, nickname)
57 {
58 ArgumentNullException.ThrowIfNull(sceneLayer);
59
60 Background = new DirectRectangle(backgroundColor, renderSurfaceHost, sceneLayer, bounds, $"{Nickname}.background")
61 .SetFilled(true)
62 .SetStrokeWidth(0f);
63
64 Add(Background, keepCurrentOffset: false, explicitLocalOffsetPx: Vector2.Zero);
65 DisableInput();
66 }
67
72
76 public Rectangle Bounds => Mode == DirectDrawingMode.View
77 ? Background.ScreenBounds
78 : Background.WorldBounds;
79
83 public Size Size
84 {
85 get => Bounds.Size;
86 set
87 {
88 ValidateSize(value);
89
90 if (Mode == DirectDrawingMode.View)
91 Background.ScreenBounds = new Rectangle(Background.ScreenBounds.Location, value);
92 else
93 Background.WorldBounds = new Rectangle(Background.WorldBounds.Location, value);
94 }
95 }
96
103 public PanelWidget AddWidget(WidgetBase widget, Point? offsetPx = null)
104 {
105 ArgumentNullException.ThrowIfNull(widget);
106
107 Point offset = offsetPx ?? Point.Empty;
108 Add(widget,
109 keepCurrentOffset: false,
110 explicitLocalOffsetPx: new Vector2(offset.X, offset.Y));
111 SetWidgetZOrder(widget, _panelZOrder + 1);
112 return this;
113 }
114
121 public PanelWidget RemoveWidget(WidgetBase widget, bool dispose = false)
122 {
123 RemoveChild(widget, dispose);
124 return this;
125 }
126
132 public PanelWidget SetBackgroundColor(Color color)
133 {
134 Background.SetColor(color);
135 RefreshBackground();
136 return this;
137 }
138
144 public PanelWidget SetBorderColor(Color color)
145 {
146 Background.SetBorderColor(color);
147 RefreshBackground();
148 return this;
149 }
150
156 public PanelWidget SetStrokeWidth(float width)
157 {
158 Background.SetStrokeWidth(width);
159 RefreshBackground();
160 return this;
161 }
162
168 public PanelWidget SetCornerRadius(float radius)
169 {
170 Background.SetCornerRadius(radius);
171 RefreshBackground();
172 return this;
173 }
174
184 public PanelWidget SetBackgroundImage(SKBitmap bitmap,
186 float scale = 1f,
187 SKPoint? offsetPx = null,
188 SKFilterQuality filterQuality = SKFilterQuality.Medium)
189 {
190 Background.SetFillImage(bitmap, mode, scale, offsetPx, filterQuality);
191 return this;
192 }
193
203 public PanelWidget SetBackgroundImage(SKImage image,
205 float scale = 1f,
206 SKPoint? offsetPx = null,
207 SKFilterQuality filterQuality = SKFilterQuality.Medium)
208 {
209 Background.SetFillImage(image, mode, scale, offsetPx, filterQuality);
210 return this;
211 }
212
218 {
219 Background.ClearFillImage();
220 return this;
221 }
222
228 public PanelWidget SetPanelZOrder(int zOrder)
229 {
230 _panelZOrder = zOrder;
231 Background.ZOrder = zOrder;
232
233 foreach (WidgetBase widget in ChildWidgets)
234 SetWidgetZOrder(widget, zOrder + 1);
235
236 return this;
237 }
238
239 private void RefreshBackground()
240 {
242 }
243
244 private void DisableInput()
245 {
246 IsInputEnabled = false;
247 IsPointerInputEnabled = false;
249 CanReceiveFocus = false;
250 }
251
252 private static Rectangle ValidateBounds(Rectangle bounds)
253 {
254 ValidateSize(bounds.Size);
255 return bounds;
256 }
257
258 private static void ValidateSize(Size size)
259 {
260 if (size.Width <= 0 || size.Height <= 0)
261 throw new ArgumentOutOfRangeException(nameof(size), size, "Panel size must be positive.");
262 }
263
264 private static void SetWidgetZOrder(WidgetBase widget, int zOrder)
265 {
266 DirectDrawingBase[] visuals = EnumerateVisuals(widget).ToArray();
267 if (visuals.Length == 0)
268 return;
269
270 int currentMin = visuals.Min(static visual => visual.ZOrder);
271 int delta = zOrder - currentMin;
272
273 foreach (DirectDrawingBase visual in visuals)
274 visual.ZOrder += delta;
275 }
276
277 private static IEnumerable<DirectDrawingBase> EnumerateVisuals(IDirectCompositeContainer container)
278 {
279 foreach (IDirectCompositeChild child in container.Children)
280 {
281 if (child is DirectDrawingBase drawing)
282 yield return drawing;
283
284 if (child is IDirectCompositeContainer nestedContainer)
285 {
286 foreach (DirectDrawingBase nestedDrawing in EnumerateVisuals(nestedContainer))
287 yield return nestedDrawing;
288 }
289 }
290 }
291}
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.