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
MenuHeaderWidget.cs
1using System.Drawing;
6using SkiaSharp;
7
9
13public sealed class MenuHeaderWidget : WidgetBase
14{
15 private readonly MenuBarTheme _theme;
16 private bool _isPointerOver;
17 private bool _isOpen;
18 private bool _isMenuBarActive;
19
20 internal event Action<MenuHeaderWidget>? Invoked;
21 internal event Action<MenuHeaderWidget>? Hovered;
22
23 internal MenuHeaderWidget(RenderSurfaceHostBase host,
24 View view,
25 Rectangle bounds,
26 string text,
27 MenuBarTheme theme,
28 string? nickname = null, char? mnemonic = null)
29 : base(host, DirectDrawingMode.View, bounds.Location, nickname)
30 {
31 Mnemonic = mnemonic;
32 _theme = theme ?? throw new ArgumentNullException(nameof(theme));
33 Text = text ?? throw new ArgumentNullException(nameof(text));
34
36 _theme.HeaderNormalColor,
37 host,
38 view,
39 bounds,
40 $"{Nickname}.background")
41 .SetFilled(true)
42 .SetStrokeWidth(0f);
43
44 Label = new TextBlock(
45 host,
46 view,
47 bounds,
48 $"{Nickname}.label")
50 .SetFont(SKTypeface.Default, _theme.FontSize, _theme.MinimumFontSize)
51 .SetColors(ToSkColor(_theme.TextColor), SKColors.Transparent)
52 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
53 .EnableWrapping(false);
54
56 Add(Label);
57
58 CanReceiveFocus = true;
61 }
62
64 public char? Mnemonic { get; }
65
67 public string Text { get; private set; }
68
71
73 public TextBlock Label { get; }
74
76 public bool IsOpen => _isOpen;
77
81 public MenuHeaderWidget SetText(string text)
82 {
83 Text = text ?? throw new ArgumentNullException(nameof(text));
84 Label.SetText(Text);
85 return this;
86 }
87
88 internal void SetOpen(bool isOpen)
89 {
90 _isOpen = isOpen;
91 UpdateVisualState();
92 }
93
94 internal void SetMenuBarActive(bool isActive)
95 {
96 _isMenuBarActive = isActive;
97 }
98
99 internal void SetHeaderZOrder(int zOrder)
100 {
101 Background.ZOrder = zOrder;
102 Label.ZOrder = zOrder + 1;
103 }
104
105 internal void SetBounds(Rectangle bounds)
106 {
107 SetPosition(bounds.X, bounds.Y);
108
109 Background.ScreenBounds = new Rectangle(
110 Background.ScreenBounds.Location,
111 bounds.Size);
112
113 Label.ScreenBounds = new Rectangle(
114 Label.ScreenBounds.Location,
115 bounds.Size);
116 }
117
119 protected override void OnPointerEnter(WidgetPointerEventArgs args)
120 {
121 base.OnPointerEnter(args);
122 _isPointerOver = true;
123 UpdateVisualState();
124 Hovered?.Invoke(this);
125 }
126
128 protected override void OnPointerLeave(WidgetPointerEventArgs args)
129 {
130 base.OnPointerLeave(args);
131 _isPointerOver = false;
132 UpdateVisualState();
133 }
134
136 protected override void OnPointerDown(WidgetPointerEventArgs args)
137 {
138 base.OnPointerDown(args);
139
140 if (args.IsPrimaryButton)
141 SetBackgroundColor(_theme.HeaderPressedColor);
142 }
143
145 protected override void OnPointerUp(WidgetPointerEventArgs args)
146 {
147 base.OnPointerUp(args);
148 UpdateVisualState();
149 }
150
152 protected override void OnPointerClick(WidgetPointerEventArgs args)
153 {
154 base.OnPointerClick(args);
155
156 if (!args.IsPrimaryButton)
157 return;
158
159 args.Handled = true;
160 Invoked?.Invoke(this);
161 }
162
164 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
165 {
166 base.OnKeyboardInput(args);
167
168 if (args.KeyAction != KeyAction.Pressed)
169 return;
170
171 // Once any dropdown is open, leave menu-navigation keys unhandled so
172 // they bubble through ContainerWidget to MenuBarWidget.
173 if (_isMenuBarActive)
174 return;
175
176 // Enter, Space, or Down Arrow opens a closed menu header.
177 if (args.Key is not 13 and not 32 and not 40)
178 return;
179
180 args.Handled = true;
181 Invoked?.Invoke(this);
182 }
183
184 private void UpdateVisualState()
185 {
186 Color color = _isOpen
187 ? _theme.HeaderOpenColor
188 : _isPointerOver
189 ? _theme.HeaderHoverColor
190 : _theme.HeaderNormalColor;
191
192 SetBackgroundColor(color);
193 }
194
195 private void SetBackgroundColor(Color color)
196 {
197 Background.SetColor(color);
199 }
200
201 private static SKColor ToSkColor(Color color)
202 {
203 return new SKColor(color.R, color.G, color.B, color.A);
204 }
205}
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.
void SetPosition(Vector2 position)
Sets the composite anchor and repositions all children from their stored offsets.
Rectangle ScreenBounds
Gets or sets the screen-space bounds of this direct drawing in pixels.
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 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.
A retained-mode text drawable with wrapping, multi-line ( ) support, horizontal/vertical alignment,...
Definition TextBlock.cs:44
TextBlock SetFont(SKTypeface typeface, float size, float? minSize=null)
Sets the font face and size (and optional minimum size for auto-shrink).
Definition TextBlock.cs:394
TextBlock EnableWrapping(bool enable=true)
Enables or disables word wrapping when laying out paragraphs.
Definition TextBlock.cs:503
TextBlock SetAlignment(SKTextAlign h, VerticalAlign v)
Sets the horizontal and vertical alignment used when drawing the laid-out lines within the bounds.
Definition TextBlock.cs:433
VerticalAlign
Vertical alignment options for multi-line text within the control's bounds.
Definition TextBlock.cs:49
TextBlock SetColors(SKColor fg, SKColor bg)
Sets the foreground (text) and background colors using Skia colors.
Definition TextBlock.cs:410
TextBlock SetText(string text)
Sets the current text content and rebuilds layout as needed. If no reveal animation is active,...
Definition TextBlock.cs:358
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
DirectRectangle Background
Gets the header background drawing.
MenuHeaderWidget SetText(string text)
Changes the displayed header text.
bool IsOpen
Gets whether this header currently owns the open dropdown.
string Text
Gets the displayed header text.
override void OnPointerEnter(WidgetPointerEventArgs args)
Called when a pointer enters the widget bounds.
char? Mnemonic
Gets the explicit, case-insensitive Alt access character.
TextBlock Label
Gets the header label drawing.
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.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
override void OnPointerLeave(WidgetPointerEventArgs args)
Called when a pointer leaves the widget bounds.
override void OnPointerDown(WidgetPointerEventArgs args)
Called when a pointer press begins on the widget.
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.
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
record MenuBarTheme
Defines the dimensions and colors used by menu bar widgets.