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

perf: Use TextElements within the TextComponent #1802

Merged
merged 3 commits into from
Jul 21, 2022
Merged
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
55 changes: 34 additions & 21 deletions packages/flame/lib/src/components/text_component.dart
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  String _text;
  String get text => _text;

Normally we have the private at the top and the getter/setter below it right?

Copy link
Member

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? 🤔

Copy link
Contributor Author

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

  /// The parent of this node in the tree.
  AbstractNode? get parent => _parent;
  AbstractNode? _parent;

  /// The current value stored in this notifier.
  ///
  /// When the value is replaced with something that is not equal to the old
  /// value as evaluated by the equality operator ==, this class notifies its
  /// listeners.
  @override
  T get value => _value;
  T _value;
  set value(T newValue) {
    if (_value == newValue)
      return;
    _value = newValue;
    notifyListeners();
  }

or

  /// Current code point.
  ///
  /// If the iterator has hit either end, the [_currentCodePoint] is -1
  /// and `_position == _nextPosition`.
  int _currentCodePoint = -1;

  ...

  /// The rune (integer Unicode code point) starting at the current position in
  /// the string.
  ///
  /// The value is -1 if there is no current code point.
  int get current => _currentCodePoint;

I guess we just need to decide which style we like more, and put this into our own style guide.

Copy link
Member

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.

set text(String text) {
if (_text != text) {
_text = text;
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we put this logic inside FormatterTextRenderer somehow?

It doens't seems right to put logic of a specific renderer on the TextComponent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 3 TextRenderer implementations right now, and I had an idea for a fourth, but all of them are FormatterTextRenderers. Ideally, we would merge FormatterTextRenderer API into the TextRenderer, but that would be a breaking change for anyone who implements their own TextRenderer.

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.

Copy link
Member

Choose a reason for hiding this comment

The 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 TextRenderer implementation.

_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());
}
}
}