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
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(
25 View view,
26 Rectangle bounds,
27 string text,
28 MenuBarTheme theme,
29 string? nickname = null,
30 char? mnemonic = null)
31 : base(
32 host,
34 bounds.Location,
35 nickname)
36 {
37 Mnemonic = mnemonic;
38 _theme = theme ?? throw new ArgumentNullException(nameof(theme));
39 Text = text ?? throw new ArgumentNullException(nameof(text));
40
42 _theme.HeaderNormalColor,
43 host,
44 view,
45 bounds,
46 $"{Nickname}.background")
47 .SetFilled(true)
48 .SetStrokeWidth(0f);
49
50 Label = new TextBlock(
51 host,
52 view,
53 bounds,
54 $"{Nickname}.label")
56 .SetFont(SKTypeface.Default, _theme.FontSize, _theme.MinimumFontSize)
57 .SetColors(ToSkColor(_theme.TextColor), SKColors.Transparent)
58 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
59 .EnableWrapping(false);
60
62 Add(Label);
63
64 CanReceiveFocus = true;
67 }
68
70 public char? Mnemonic { get; }
71
73 public string Text { get; private set; }
74
77
79 public TextBlock Label { get; }
80
82 public bool IsOpen => _isOpen;
83
87 public MenuHeaderWidget SetText(string text)
88 {
89 Text = text ?? throw new ArgumentNullException(nameof(text));
90 Label.SetText(Text);
91 return this;
92 }
93
94 internal void SetOpen(bool isOpen)
95 {
96 _isOpen = isOpen;
97 UpdateVisualState();
98 }
99
100 internal void SetMenuBarActive(bool isActive)
101 {
102 _isMenuBarActive = isActive;
103 }
104
105 internal void SetHeaderZOrder(int zOrder)
106 {
107 Background.ZOrder = zOrder;
108 Label.ZOrder = zOrder + 1;
109 }
110
111 internal void SetBounds(Rectangle bounds)
112 {
113 SetPosition(bounds.X, bounds.Y);
114
115 Background.ScreenBounds = new Rectangle(
116 Background.ScreenBounds.Location,
117 bounds.Size);
118
119 Label.ScreenBounds = new Rectangle(
120 Label.ScreenBounds.Location,
121 bounds.Size);
122 }
123
125 protected override void OnPointerEnter(WidgetPointerEventArgs args)
126 {
127 base.OnPointerEnter(args);
128 _isPointerOver = true;
129 UpdateVisualState();
130 Hovered?.Invoke(this);
131 }
132
134 protected override void OnPointerLeave(WidgetPointerEventArgs args)
135 {
136 base.OnPointerLeave(args);
137 _isPointerOver = false;
138 UpdateVisualState();
139 }
140
142 protected override void OnPointerDown(WidgetPointerEventArgs args)
143 {
144 base.OnPointerDown(args);
145
146 if (args.IsPrimaryButton)
147 SetBackgroundColor(_theme.HeaderPressedColor);
148 }
149
151 protected override void OnPointerUp(WidgetPointerEventArgs args)
152 {
153 base.OnPointerUp(args);
154 UpdateVisualState();
155 }
156
158 protected override void OnPointerClick(WidgetPointerEventArgs args)
159 {
160 base.OnPointerClick(args);
161
162 if (!args.IsPrimaryButton)
163 return;
164
165 args.Handled = true;
166 Invoked?.Invoke(this);
167 }
168
170 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
171 {
172 base.OnKeyboardInput(args);
173
174 if (args.KeyAction != KeyAction.Pressed)
175 return;
176
177 // Once any dropdown is open, leave menu-navigation keys unhandled so
178 // they bubble through ContainerWidget to MenuBarWidget.
179 if (_isMenuBarActive)
180 return;
181
182 // Enter, Space, or Down Arrow opens a closed menu header.
183 if (args.Key is not 13 and not 32 and not 40)
184 return;
185
186 args.Handled = true;
187 Invoked?.Invoke(this);
188 }
189
190 private void UpdateVisualState()
191 {
192 Color color = _isOpen
193 ? _theme.HeaderOpenColor
194 : _isPointerOver
195 ? _theme.HeaderHoverColor
196 : _theme.HeaderNormalColor;
197
198 SetBackgroundColor(color);
199 }
200
201 private void SetBackgroundColor(Color color)
202 {
203 Background.SetColor(color);
205 }
206
207 private static SKColor ToSkColor(Color color)
208 {
209 return new SKColor(
210 color.R,
211 color.G,
212 color.B,
213 color.A);
214 }
215}
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:414
TextBlock EnableWrapping(bool enable=true)
Enables or disables word wrapping when laying out paragraphs.
Definition TextBlock.cs:527
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:453
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:430
TextBlock SetText(string text)
Sets the current text content and rebuilds layout as needed. If no reveal animation is active,...
Definition TextBlock.cs:378
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.