From b946ba70cbfb5793a8d4d7c61d6ba029fbc303ab Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Tue, 26 Mar 2024 23:21:26 +0100 Subject: [PATCH] feat: Add `copyWith` method on the `TextBoxConfig` (#3099) Simplifies the creation of the `TextBoxConfig` in the `ScrollTextBoxComponent` by adding a `copyWith` method to it and making it `const`. --- .../lib/stories/rendering/text_example.dart | 13 +++++++----- .../components/scroll_text_box_component.dart | 16 ++------------- .../src/components/text_box_component.dart | 20 +++++++++++++++++-- .../components/text_box_component_test.dart | 8 ++++---- .../test/text/sprite_font_renderer_test.dart | 4 ++-- 5 files changed, 34 insertions(+), 27 deletions(-) diff --git a/examples/lib/stories/rendering/text_example.dart b/examples/lib/stories/rendering/text_example.dart index 197187764c4..14f5dad3c07 100644 --- a/examples/lib/stories/rendering/text_example.dart +++ b/examples/lib/stories/rendering/text_example.dart @@ -77,9 +77,9 @@ class TextExample extends FlameGame { size: Vector2(200, 150), position: Vector2(size.x / 2, size.y / 2 + 100), anchor: Anchor.topCenter, - boxConfig: TextBoxConfig( + boxConfig: const TextBoxConfig( timePerChar: 0.005, - margins: const EdgeInsets.fromLTRB(10, 10, 10, 10), + margins: EdgeInsets.fromLTRB(10, 10, 10, 10), ), ), ], @@ -138,6 +138,9 @@ class MyTextBox extends TextBoxComponent { Future onLoad() { paint = Paint(); bgRect = Rect.fromLTWH(0, 0, width, height); + size.addListener(() { + bgRect = Rect.fromLTWH(0, 0, width, height); + }); paint.color = Colors.white10; return super.onLoad(); @@ -152,7 +155,7 @@ class MyTextBox extends TextBoxComponent { class MyScrollTextBox extends ScrollTextBoxComponent { late Paint paint; - late Rect bgRect; + late Rect backgroundRect; MyScrollTextBox( String text, { @@ -165,7 +168,7 @@ class MyScrollTextBox extends ScrollTextBoxComponent { @override FutureOr onLoad() { paint = Paint(); - bgRect = Rect.fromLTWH(0, 0, width, height); + backgroundRect = Rect.fromLTWH(0, 0, width, height); paint.color = Colors.white10; return super.onLoad(); @@ -173,7 +176,7 @@ class MyScrollTextBox extends ScrollTextBoxComponent { @override void render(Canvas canvas) { - canvas.drawRect(bgRect, paint); + canvas.drawRect(backgroundRect, paint); super.render(canvas); } } diff --git a/packages/flame/lib/src/components/scroll_text_box_component.dart b/packages/flame/lib/src/components/scroll_text_box_component.dart index ea7599e88f2..45e74777b54 100644 --- a/packages/flame/lib/src/components/scroll_text_box_component.dart +++ b/packages/flame/lib/src/components/scroll_text_box_component.dart @@ -48,19 +48,7 @@ class ScrollTextBoxComponent extends PositionComponent { final marginBottom = boxConfig?.margins.bottom ?? 0; final innerMargins = EdgeInsets.fromLTRB(0, marginTop, 0, marginBottom); - boxConfig ??= TextBoxConfig(); - boxConfig = TextBoxConfig( - timePerChar: boxConfig.timePerChar, - dismissDelay: boxConfig.dismissDelay, - growingBox: boxConfig.growingBox, - maxWidth: size.x, - margins: EdgeInsets.fromLTRB( - boxConfig.margins.left, - 0, - boxConfig.margins.right, - 0, - ), - ); + boxConfig = (boxConfig ?? const TextBoxConfig()).copyWith(maxWidth: size.x); _scrollTextBoxComponent = _ScrollTextBoxComponent( text: text, @@ -119,7 +107,7 @@ class _ScrollTextBoxComponent extends TextBoxComponent }) : super( text: text ?? '', textRenderer: textRenderer ?? TextPaint(), - boxConfig: boxConfig ?? TextBoxConfig(), + boxConfig: boxConfig ?? const TextBoxConfig(), ); @override diff --git a/packages/flame/lib/src/components/text_box_component.dart b/packages/flame/lib/src/components/text_box_component.dart index f578014b58d..d753b9c0266 100644 --- a/packages/flame/lib/src/components/text_box_component.dart +++ b/packages/flame/lib/src/components/text_box_component.dart @@ -37,13 +37,29 @@ class TextBoxConfig { /// beginning (both width and height). final bool growingBox; - TextBoxConfig({ + const TextBoxConfig({ this.maxWidth = 200.0, this.margins = const EdgeInsets.all(8.0), this.timePerChar = 0.0, this.dismissDelay, this.growingBox = false, }); + + TextBoxConfig copyWith({ + double? maxWidth, + EdgeInsets? margins, + double? timePerChar, + double? dismissDelay, + bool? growingBox, + }) { + return TextBoxConfig( + maxWidth: maxWidth ?? this.maxWidth, + margins: margins ?? this.margins, + timePerChar: timePerChar ?? this.timePerChar, + dismissDelay: dismissDelay ?? this.dismissDelay, + growingBox: growingBox ?? this.growingBox, + ); + } } class TextBoxComponent extends TextComponent { @@ -81,7 +97,7 @@ class TextBoxComponent extends TextComponent { super.children, super.priority, super.key, - }) : _boxConfig = boxConfig ?? TextBoxConfig(), + }) : _boxConfig = boxConfig ?? const TextBoxConfig(), _fixedSize = size != null, align = align ?? Anchor.topLeft, pixelRatio = pixelRatio ?? diff --git a/packages/flame/test/components/text_box_component_test.dart b/packages/flame/test/components/text_box_component_test.dart index d84b6cb2097..518f560bb52 100644 --- a/packages/flame/test/components/text_box_component_test.dart +++ b/packages/flame/test/components/text_box_component_test.dart @@ -11,7 +11,7 @@ void main() { test('size is properly computed', () { final c = TextBoxComponent( text: 'The quick brown fox jumps over the lazy dog.', - boxConfig: TextBoxConfig( + boxConfig: const TextBoxConfig( maxWidth: 100.0, ), ); @@ -23,7 +23,7 @@ void main() { test('size is properly computed with new line character', () { final c = TextBoxComponent( text: 'The quick brown fox \n jumps over the lazy dog.', - boxConfig: TextBoxConfig( + boxConfig: const TextBoxConfig( maxWidth: 100.0, ), ); @@ -35,7 +35,7 @@ void main() { test('lines are properly computed with new line character', () { final c = TextBoxComponent( text: 'The quick brown fox \n jumps over the lazy dog.', - boxConfig: TextBoxConfig( + boxConfig: const TextBoxConfig( maxWidth: 400.0, ), ); @@ -51,7 +51,7 @@ void main() { (game) async { final component = TextBoxComponent( text: 'foo bar', - boxConfig: TextBoxConfig( + boxConfig: const TextBoxConfig( dismissDelay: 10.0, timePerChar: 1.0, ), diff --git a/packages/flame/test/text/sprite_font_renderer_test.dart b/packages/flame/test/text/sprite_font_renderer_test.dart index 08b89312529..262e73f41ad 100644 --- a/packages/flame/test/text/sprite_font_renderer_test.dart +++ b/packages/flame/test/text/sprite_font_renderer_test.dart @@ -37,12 +37,12 @@ void main() { TextBoxComponent( text: textSample, textRenderer: await createRenderer(letterSpacing: 1), - boxConfig: TextBoxConfig(maxWidth: 800), + boxConfig: const TextBoxConfig(maxWidth: 800), ), TextBoxComponent( text: textSample, textRenderer: await createRenderer(scale: 2), - boxConfig: TextBoxConfig(maxWidth: 800), + boxConfig: const TextBoxConfig(maxWidth: 800), position: Vector2(0, 100), ), TextComponent(