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
ConversationBox.cs
1using System.Drawing;
7using SkiaSharp;
8
10
15public sealed class ConversationBox : WidgetBase
16{
17 private const int SpeakerHeight = 30;
18 private const int ContinueIndicatorWidth = 28;
19 private const int ContinueIndicatorHeight = 22;
20 private const int ContentInset = 10;
21
22 private string _speaker;
23 private string _text;
24 private bool _continueIndicatorVisible = true;
25
29 public event Action? AdvanceRequested;
30
41 RenderSurfaceHostBase renderSurfaceHost,
42 View view,
43 Rectangle bounds,
44 string speaker,
45 string text,
46 string? nickname = null)
47 : base(
48 renderSurfaceHost,
50 ValidateBounds(bounds).Location,
51 nickname)
52 {
53 ArgumentNullException.ThrowIfNull(view);
54
55 _speaker = speaker ?? string.Empty;
56 _text = text ?? string.Empty;
57
58 Panel = CreatePanel(renderSurfaceHost, view, bounds);
59
60 SpeakerText = CreateSpeakerText(
61 renderSurfaceHost,
62 view,
63 GetSpeakerBounds(bounds),
64 _speaker);
65
66 BodyText = CreateBodyText(
67 renderSurfaceHost,
68 view,
69 GetBodyBounds(bounds),
70 _text);
71
72 ContinueIndicator = CreateContinueIndicator(renderSurfaceHost, view, GetContinueBounds(bounds));
73
74 Add(Panel);
78
79 CompleteInitialization();
80 }
81
92 RenderSurfaceHostBase renderSurfaceHost,
93 SceneLayer sceneLayer,
94 Rectangle bounds,
95 string speaker,
96 string text,
97 string? nickname = null)
98 : base(
99 renderSurfaceHost,
101 ValidateBounds(bounds).Location,
102 nickname)
103 {
104 ArgumentNullException.ThrowIfNull(sceneLayer);
105
106 _speaker = speaker ?? string.Empty;
107 _text = text ?? string.Empty;
108
109 Panel = CreatePanel(renderSurfaceHost, sceneLayer, bounds);
110
111 SpeakerText = CreateSpeakerText(
112 renderSurfaceHost,
113 sceneLayer,
114 GetSpeakerBounds(bounds),
115 _speaker);
116
117 BodyText = CreateBodyText(
118 renderSurfaceHost,
119 sceneLayer,
120 GetBodyBounds(bounds),
121 _text);
122
123 ContinueIndicator = CreateContinueIndicator(renderSurfaceHost, sceneLayer, GetContinueBounds(bounds));
124
125 Add(Panel);
127 Add(BodyText);
129
130 CompleteInitialization();
131 }
132
136 public DirectRectangle Panel { get; }
137
141 public TextBlock SpeakerText { get; }
142
146 public TextBlock BodyText { get; }
147
152
156 public string Speaker => _speaker;
157
161 public string Text => _text;
162
166 public Rectangle Bounds => Mode == DirectDrawingMode.View
167 ? Panel.ScreenBounds
168 : Panel.WorldBounds;
169
174 public int PrimaryAdvanceKey { get; set; } = 13;
175
180 public int SecondaryAdvanceKey { get; set; } = 32;
181
188 public ConversationBox SetConversation(string speaker, string text)
189 {
190 SetSpeaker(speaker);
191 SetText(text);
192 return this;
193 }
194
200 public ConversationBox SetSpeaker(string speaker)
201 {
202 _speaker = speaker ?? string.Empty;
203 SpeakerText.SetText(_speaker);
204 return this;
205 }
206
212 public ConversationBox SetText(string text)
213 {
214 _text = text ?? string.Empty;
215 BodyText.SetText(_text);
216 return this;
217 }
218
224 public ConversationBox ShowContinueIndicator(bool visible = true)
225 {
226 _continueIndicatorVisible = visible;
227 ContinueIndicator.Visible = visible;
228 return this;
229 }
230
237 public ConversationBox SetTextColors(SKColor speakerColor, SKColor bodyColor)
238 {
239 SpeakerText.SetColors(speakerColor, SKColors.Transparent);
240 BodyText.SetColors(bodyColor, SKColors.Transparent);
241 return this;
242 }
243
250 public ConversationBox SetPanelColors(Color fill, Color border)
251 {
252 Panel.SetColor(fill)
253 .SetBorderColor(border);
254
255 Panel.SetPosition(Panel.GetPosition());
256 return this;
257 }
258
265 {
266 Panel.ZOrder = zOrder;
267 SpeakerText.ZOrder = zOrder + 1;
268 BodyText.ZOrder = zOrder + 1;
269 ContinueIndicator.ZOrder = zOrder + 2;
270 return this;
271 }
272
276 public void Advance()
277 {
278 if (!IsInputEnabled)
279 return;
280
281 AdvanceRequested?.Invoke();
282 }
283
285 protected override void ProcessShown()
286 {
287 base.ProcessShown();
288 ContinueIndicator.Visible = _continueIndicatorVisible;
289 }
290
292 protected override void OnPointerClick(WidgetPointerEventArgs args)
293 {
294 base.OnPointerClick(args);
295
296 if (!args.IsPrimaryButton)
297 return;
298
299 args.Handled = true;
300 Advance();
301 }
302
304 protected override void OnKeyboardInput(WidgetKeyboardEventArgs args)
305 {
306 base.OnKeyboardInput(args);
307
308 if (args.KeyAction != KeyAction.Pressed ||
309 args.Key != PrimaryAdvanceKey && args.Key != SecondaryAdvanceKey)
310 {
311 return;
312 }
313
314 args.Handled = true;
315 Advance();
316 }
317
318 private void CompleteInitialization()
319 {
320 CanReceiveFocus = true;
323 }
324
325 private static DirectRectangle CreatePanel(
327 View view,
328 Rectangle bounds)
329 {
330 return ConfigurePanel(
331 new DirectRectangle(
332 Color.FromArgb(
333 238,
334 24,
335 25,
336 32),
337 host,
338 view,
339 bounds));
340 }
341
342 private static DirectRectangle CreatePanel(
344 SceneLayer layer,
345 Rectangle bounds)
346 {
347 return ConfigurePanel(
348 new DirectRectangle(
349 Color.FromArgb(
350 238,
351 24,
352 25,
353 32),
354 host,
355 layer,
356 bounds));
357 }
358
359 private static DirectRectangle ConfigurePanel(DirectRectangle panel)
360 {
361 return panel.SetFilled(true)
363 Color.FromArgb(
364 245,
365 205,
366 205,
367 215))
368 .SetStrokeWidth(2f)
369 .SetCornerRadius(7f);
370 }
371
372 private static TextBlock CreateSpeakerText(
373 RenderSurfaceHostBase host,
374 View view,
375 Rectangle bounds,
376 string speaker)
377 {
378 return ConfigureSpeakerText(new TextBlock(host, view, bounds), speaker);
379 }
380
381 private static TextBlock CreateSpeakerText(
382 RenderSurfaceHostBase host,
383 SceneLayer layer,
384 Rectangle bounds,
385 string speaker)
386 {
387 return ConfigureSpeakerText(
388 new TextBlock(
389 host,
390 layer,
391 view: null,
392 worldBounds: bounds),
393 speaker);
394 }
395
396 private static TextBlock ConfigureSpeakerText(TextBlock textBlock, string speaker)
397 {
398 return textBlock.SetText(speaker)
399 .SetFont(SKTypeface.Default, 17f, minSize: 11f)
400 .SetColors(new SKColor(255, 224, 145), SKColors.Transparent)
401 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
402 .SetPadding(2f, 0f)
403 .EnableWrapping(false);
404 }
405
406 private static TextBlock CreateBodyText(
407 RenderSurfaceHostBase host,
408 View view,
409 Rectangle bounds,
410 string text)
411 {
412 return ConfigureBodyText(new TextBlock(host, view, bounds), text);
413 }
414
415 private static TextBlock CreateBodyText(
416 RenderSurfaceHostBase host,
417 SceneLayer layer,
418 Rectangle bounds,
419 string text)
420 {
421 return ConfigureBodyText(
422 new TextBlock(
423 host,
424 layer,
425 view: null,
426 worldBounds: bounds),
427 text);
428 }
429
430 private static TextBlock ConfigureBodyText(TextBlock textBlock, string text)
431 {
432 return textBlock.SetText(text)
433 .SetFont(SKTypeface.Default, 16f, minSize: 10f)
434 .SetColors(SKColors.White, SKColors.Transparent)
435 .SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Top)
436 .SetPadding(2f, 2f)
437 .EnableWrapping(true);
438 }
439
440 private static TextBlock CreateContinueIndicator(
441 RenderSurfaceHostBase host,
442 View view,
443 Rectangle bounds)
444 {
445 return ConfigureContinueIndicator(new TextBlock(host, view, bounds));
446 }
447
448 private static TextBlock CreateContinueIndicator(
449 RenderSurfaceHostBase host,
450 SceneLayer layer,
451 Rectangle bounds)
452 {
453 return ConfigureContinueIndicator(
454 new TextBlock(
455 host,
456 layer,
457 view: null,
458 worldBounds: bounds));
459 }
460
461 private static TextBlock ConfigureContinueIndicator(TextBlock textBlock)
462 {
463 return textBlock.SetText("▼")
464 .SetFont(SKTypeface.Default, 14f, minSize: 9f)
465 .SetColors(SKColors.White, SKColors.Transparent)
466 .SetAlignment(SKTextAlign.Center, TextBlock.VerticalAlign.Center)
467 .EnableWrapping(false);
468 }
469
470 private static Rectangle GetSpeakerBounds(Rectangle bounds)
471 {
472 return new Rectangle(
473 bounds.X + ContentInset,
474 bounds.Y + 4,
475 Math.Max(1, bounds.Width - ContentInset * 2),
476 SpeakerHeight);
477 }
478
479 private static Rectangle GetBodyBounds(Rectangle bounds)
480 {
481 int top = bounds.Y + SpeakerHeight + 4;
482 int bottom = bounds.Bottom - ContinueIndicatorHeight - 4;
483
484 return new Rectangle(
485 bounds.X + ContentInset,
486 top,
487 Math.Max(1, bounds.Width - ContentInset * 2),
488 Math.Max(1, bottom - top));
489 }
490
491 private static Rectangle GetContinueBounds(Rectangle bounds)
492 {
493 return new Rectangle(
494 bounds.Right - ContentInset - ContinueIndicatorWidth,
495 bounds.Bottom - ContinueIndicatorHeight - 4,
496 ContinueIndicatorWidth,
497 ContinueIndicatorHeight);
498 }
499
500 private static Rectangle ValidateBounds(Rectangle bounds)
501 {
502 if (bounds.Width < 120 || bounds.Height < SpeakerHeight + ContinueIndicatorHeight + 24)
503 throw new ArgumentOutOfRangeException(nameof(bounds), bounds, "Conversation-box bounds are too small.");
504
505 return bounds;
506 }
507}
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.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
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 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
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
TextBlock SetPadding(float horizontal, float vertical)
Sets symmetric horizontal and vertical padding and invalidates cached layout.
Definition TextBlock.cs:336
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 ...
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
ConversationBox SetText(string text)
Changes the conversation body text.
DirectRectangle Panel
Gets the panel background.
override void ProcessShown()
Performs processing when the widget is shown before notification callbacks are invoked.
ConversationBox(RenderSurfaceHostBase renderSurfaceHost, SceneLayer sceneLayer, Rectangle bounds, string speaker, string text, string? nickname=null)
Creates a scene-layer conversation box.
void Advance()
Programmatically requests the next conversation step.
string Text
Gets the current conversation text.
ConversationBox ShowContinueIndicator(bool visible=true)
Shows or hides the continue indicator.
ConversationBox SetConversationZOrder(int zOrder)
Sets the base Z-order used by the conversation visuals.
ConversationBox SetPanelColors(Color fill, Color border)
Sets the panel fill and border colors.
ConversationBox(RenderSurfaceHostBase renderSurfaceHost, View view, Rectangle bounds, string speaker, string text, string? nickname=null)
Creates a view-level conversation box.
ConversationBox SetTextColors(SKColor speakerColor, SKColor bodyColor)
Sets the speaker and body text colors.
TextBlock BodyText
Gets the conversation body text block.
Rectangle Bounds
Gets the conversation bounds in its native coordinate space.
TextBlock SpeakerText
Gets the speaker-name text block.
ConversationBox SetSpeaker(string speaker)
Changes the speaker name.
TextBlock ContinueIndicator
Gets the small visual indicating that the conversation can advance.
override void OnKeyboardInput(WidgetKeyboardEventArgs args)
Called when keyboard input is routed to the widget.
int SecondaryAdvanceKey
Gets or sets the secondary key used to request the next conversation step. Defaults to Space (32).
int PrimaryAdvanceKey
Gets or sets the primary key used to request the next conversation step. Defaults to Enter (13).
ConversationBox SetConversation(string speaker, string text)
Changes the speaker and body text together.
string Speaker
Gets the current speaker name.
Action? AdvanceRequested
Occurs when the player requests the next line or conversation step.
override void OnPointerClick(WidgetPointerEventArgs args)
Called when a pointer click is dispatched to the widget.
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.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.
KeyAction
Represents the type of action performed on a keyboard key, indicating whether the key was pressed,...
Definition KeyAction.cs:10