Skip to content

Commit

Permalink
feat: Add copyWith method on the TextBoxConfig (#3099)
Browse files Browse the repository at this point in the history
Simplifies the creation of the `TextBoxConfig` in the
`ScrollTextBoxComponent` by adding a `copyWith` method to it and making
it `const`.
  • Loading branch information
spydon committed Mar 26, 2024
1 parent 8cd9e12 commit b946ba7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
13 changes: 8 additions & 5 deletions examples/lib/stories/rendering/text_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
),
],
Expand Down Expand Up @@ -138,6 +138,9 @@ class MyTextBox extends TextBoxComponent {
Future<void> 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();
Expand All @@ -152,7 +155,7 @@ class MyTextBox extends TextBoxComponent {

class MyScrollTextBox extends ScrollTextBoxComponent {
late Paint paint;
late Rect bgRect;
late Rect backgroundRect;

MyScrollTextBox(
String text, {
Expand All @@ -165,15 +168,15 @@ class MyScrollTextBox extends ScrollTextBoxComponent {
@override
FutureOr<void> onLoad() {
paint = Paint();
bgRect = Rect.fromLTWH(0, 0, width, height);
backgroundRect = Rect.fromLTWH(0, 0, width, height);

paint.color = Colors.white10;
return super.onLoad();
}

@override
void render(Canvas canvas) {
canvas.drawRect(bgRect, paint);
canvas.drawRect(backgroundRect, paint);
super.render(canvas);
}
}
16 changes: 2 additions & 14 deletions packages/flame/lib/src/components/scroll_text_box_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,7 @@ class ScrollTextBoxComponent<T extends TextRenderer> 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<T>(
text: text,
Expand Down Expand Up @@ -119,7 +107,7 @@ class _ScrollTextBoxComponent<T extends TextRenderer> extends TextBoxComponent
}) : super(
text: text ?? '',
textRenderer: textRenderer ?? TextPaint(),
boxConfig: boxConfig ?? TextBoxConfig(),
boxConfig: boxConfig ?? const TextBoxConfig(),
);

@override
Expand Down
20 changes: 18 additions & 2 deletions packages/flame/lib/src/components/text_box_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends TextRenderer> extends TextComponent {
Expand Down Expand Up @@ -81,7 +97,7 @@ class TextBoxComponent<T extends TextRenderer> extends TextComponent {
super.children,
super.priority,
super.key,
}) : _boxConfig = boxConfig ?? TextBoxConfig(),
}) : _boxConfig = boxConfig ?? const TextBoxConfig(),
_fixedSize = size != null,
align = align ?? Anchor.topLeft,
pixelRatio = pixelRatio ??
Expand Down
8 changes: 4 additions & 4 deletions packages/flame/test/components/text_box_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
Expand All @@ -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,
),
);
Expand All @@ -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,
),
);
Expand All @@ -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,
),
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/test/text/sprite_font_renderer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b946ba7

Please sign in to comment.