Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add copyWith method on the TextBoxConfig #3099

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading