mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
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`.
This commit is contained in:
@ -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<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();
|
||||
@ -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<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();
|
||||
@ -173,7 +176,7 @@ class MyScrollTextBox extends ScrollTextBoxComponent {
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
canvas.drawRect(bgRect, paint);
|
||||
canvas.drawRect(backgroundRect, paint);
|
||||
super.render(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,
|
||||
@ -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
|
||||
|
||||
@ -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 {
|
||||
@ -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 ??
|
||||
|
||||
@ -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,
|
||||
),
|
||||
|
||||
@ -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(
|
||||
|
||||
Reference in New Issue
Block a user