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

refactor!: Make TextElement more usable on its own #2679

Merged
merged 3 commits into from
Aug 26, 2023
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
2 changes: 1 addition & 1 deletion examples/lib/stories/input/hardware_keyboard_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class KeyboardKey extends PositionComponent {
void render(Canvas canvas) {
if (visible) {
canvas.drawRRect(rect, borderPaint);
textElement.render(canvas);
textElement.draw(canvas);
}
}
}
2 changes: 1 addition & 1 deletion examples/lib/stories/rendering/rich_text_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ class MyTextComponent extends PositionComponent {

@override
void render(Canvas canvas) {
element.render(canvas);
element.draw(canvas);
}
}
2 changes: 1 addition & 1 deletion packages/flame/lib/src/components/text_box_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class TextBoxComponent<T extends TextRenderer> extends TextComponent {
(boxHeight - nLines * _lineHeight) * align.y +
i * _lineHeight,
);
textRenderer.renderElement(canvas, textElement, position, anchor: anchor);
textElement.render(canvas, position, anchor: anchor);

charCount += lines[i].length;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/components/text_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ class TextComponent<T extends TextRenderer> extends PositionComponent {

@override
void render(Canvas canvas) {
_textElement.render(canvas);
_textElement.draw(canvas);
}
}
2 changes: 1 addition & 1 deletion packages/flame/lib/src/text/elements/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ abstract class Element {
/// In order to render the element at a different location, consider either
/// calling the [translate] method, or applying a translation transform to the
/// canvas itself.
void render(Canvas canvas);
void draw(Canvas canvas);
}
4 changes: 2 additions & 2 deletions packages/flame/lib/src/text/elements/group_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GroupElement extends BlockElement {
}

@override
void render(Canvas canvas) {
children.forEach((child) => child.render(canvas));
void draw(Canvas canvas) {
children.forEach((child) => child.draw(canvas));
}
}
4 changes: 2 additions & 2 deletions packages/flame/lib/src/text/elements/group_text_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class GroupTextElement extends TextElement {
LineMetrics get metrics => _metrics;

@override
void render(Canvas canvas) {
void draw(Canvas canvas) {
for (final child in _children) {
child.render(canvas);
child.draw(canvas);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/text/elements/rect_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RectElement extends Element {
}

@override
void render(Canvas canvas) {
void draw(Canvas canvas) {
canvas.drawRect(_rect, _paint);
}
}
2 changes: 1 addition & 1 deletion packages/flame/lib/src/text/elements/rrect_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RRectElement extends Element {
}

@override
void render(Canvas canvas) {
void draw(Canvas canvas) {
canvas.drawRRect(_rrect, _paint);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SpriteFontTextElement extends TextElement {
}

@override
void render(Canvas canvas) {
void draw(Canvas canvas) {
canvas.drawRawAtlas(source, transforms, rects, null, null, null, paint);
}
}
15 changes: 15 additions & 0 deletions packages/flame/lib/src/text/elements/text_element.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:flame/extensions.dart';
import 'package:flame/src/anchor.dart';
import 'package:flame/src/text/common/line_metrics.dart';
import 'package:flame/src/text/elements/element.dart';

Expand All @@ -6,4 +8,17 @@ import 'package:flame/src/text/elements/element.dart';
abstract class TextElement extends Element {
/// The dimensions of this line.
LineMetrics get metrics;

void render(
Canvas canvas,
Vector2 position, {
Anchor anchor = Anchor.topLeft,
}) {
final box = metrics;
translate(
position.x - box.width * anchor.x,
position.y - box.height * anchor.y - box.top,
);
draw(canvas);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TextPainterTextElement extends TextElement {
void translate(double dx, double dy) => _box.translate(dx, dy);

@override
void render(Canvas canvas) {
void draw(Canvas canvas) {
_textPainter.paint(canvas, Offset(_box.left, _box.top));
}
}
17 changes: 1 addition & 16 deletions packages/flame/lib/src/text/text_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,7 @@ class TextRenderer<T extends TextFormatter> {
Vector2 position, {
Anchor anchor = Anchor.topLeft,
}) {
final element = format(text);
renderElement(canvas, element, position, anchor: anchor);
}

void renderElement(
Canvas canvas,
TextElement element,
Vector2 position, {
Anchor anchor = Anchor.topLeft,
}) {
final box = element.metrics;
element.translate(
position.x - box.width * anchor.x,
position.y - box.height * anchor.y - box.top,
);
element.render(canvas);
format(text).render(canvas, position, anchor: anchor);
}

/// A registry containing default providers for every [TextRenderer] subclass;
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/test/text/text_renderer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CustomTextElement extends TextElement {
LineMetrics get metrics => LineMetrics();

@override
void render(Canvas canvas) {}
void draw(Canvas canvas) {}

@override
void translate(double dx, double dy) {}
Expand Down
2 changes: 1 addition & 1 deletion packages/flame_test/lib/src/debug_text_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class _DebugTextElement extends TextElement {
late final LineMetrics metrics;

@override
void render(Canvas canvas) {
void draw(Canvas canvas) {
canvas.save();
if (style.fontStyle == FontStyle.italic) {
canvas.skew(-0.25, 0);
Expand Down
2 changes: 1 addition & 1 deletion packages/flame_test/test/debug_text_formatter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class TextElementsComponent extends PositionComponent {
@override
void render(Canvas canvas) {
for (final element in elements) {
element.render(canvas);
element.draw(canvas);
}
}
}