-
-
Notifications
You must be signed in to change notification settings - Fork 899
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
perf: Use TextElements within the TextComponent #1802
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flame/src/text/formatter_text_renderer.dart'; | ||
import 'package:flame/src/text/inline/text_element.dart'; | ||
import 'package:flame/src/text/text_renderer.dart'; | ||
import 'package:flutter/painting.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
class TextComponent<T extends TextRenderer> extends PositionComponent { | ||
String _text; | ||
T _textRenderer; | ||
TextComponent({ | ||
String? text, | ||
T? textRenderer, | ||
super.position, | ||
super.size, | ||
super.scale, | ||
super.angle, | ||
super.anchor, | ||
super.children, | ||
super.priority, | ||
}) : _text = text ?? '', | ||
_textRenderer = textRenderer ?? TextRenderer.createDefault<T>() { | ||
updateBounds(); | ||
} | ||
|
||
String get text => _text; | ||
|
||
String _text; | ||
set text(String text) { | ||
if (_text != text) { | ||
_text = text; | ||
|
@@ -19,35 +33,34 @@ class TextComponent<T extends TextRenderer> extends PositionComponent { | |
} | ||
|
||
T get textRenderer => _textRenderer; | ||
|
||
T _textRenderer; | ||
set textRenderer(T textRenderer) { | ||
_textRenderer = textRenderer; | ||
updateBounds(); | ||
} | ||
|
||
TextComponent({ | ||
String? text, | ||
T? textRenderer, | ||
super.position, | ||
super.size, | ||
super.scale, | ||
super.angle, | ||
super.anchor, | ||
super.children, | ||
super.priority, | ||
}) : _text = text ?? '', | ||
_textRenderer = textRenderer ?? TextRenderer.createDefault<T>() { | ||
updateBounds(); | ||
} | ||
TextElement? _textElement; | ||
|
||
@internal | ||
void updateBounds() { | ||
final expectedSize = textRenderer.measureText(_text); | ||
size.setValues(expectedSize.x, expectedSize.y); | ||
if (_textRenderer is FormatterTextRenderer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we put this logic inside It doens't seems right to put logic of a specific renderer on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have 3 I would argue in favor of merging those two classes anyways, if the team agrees. But that ought to be in a separate PR, at which point we can simply remove the "else" part of this if-statement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I vote for merging them too, I don't think there are many (if anyone at all) that has made their own |
||
_textElement = | ||
(_textRenderer as FormatterTextRenderer).formatter.format(_text); | ||
final measurements = _textElement!.lastLine.metrics; | ||
_textElement!.lastLine.translate(0, measurements.ascent); | ||
size.setValues(measurements.width, measurements.height); | ||
} else { | ||
final expectedSize = textRenderer.measureText(_text); | ||
size.setValues(expectedSize.x, expectedSize.y); | ||
} | ||
} | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
_textRenderer.render(canvas, text, Vector2.zero()); | ||
if (_textElement != null) { | ||
_textElement!.render(canvas); | ||
} else { | ||
_textRenderer.render(canvas, text, Vector2.zero()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally we have the private at the top and the getter/setter below it right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't that style become weird when adding dartdocs to the getter though? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can see, there is no Dart or Flutter official recommendations about this. The style that I'm currently using is taken from the example here: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#perform-dirty-checks-in-setters.
In practice, Flutter repo uses several styles: either
or
I guess we just need to decide which style we like more, and put this into our own style guide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the first one, since we usually don't add dartdocs to both the public facing getter and the underlying value.