Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add TextElementComponent (#2694)
Add TextElementComponent
- Loading branch information
1 parent
6dbcd0b
commit 10fb65f
Showing
6 changed files
with
213 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/flame/lib/src/components/text_element_component.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components.dart'; | ||
import 'package:flame/text.dart'; | ||
|
||
class TextElementComponent extends PositionComponent { | ||
TextElement element; | ||
|
||
TextElementComponent({ | ||
required this.element, | ||
super.position, | ||
super.size, | ||
super.scale, | ||
super.angle, | ||
super.anchor, | ||
super.children, | ||
super.priority, | ||
super.key, | ||
}); | ||
|
||
factory TextElementComponent.fromDocument({ | ||
required DocumentRoot document, | ||
DocumentStyle? style, | ||
Vector2? position, | ||
Vector2? size, | ||
Vector2? scale, | ||
double? angle, | ||
Anchor? anchor, | ||
List<Component>? children, | ||
int priority = 0, | ||
ComponentKey? key, | ||
}) { | ||
final effectiveStyle = style ?? DocumentStyle(); | ||
final effectiveSize = _coalesceSize(effectiveStyle, size); | ||
final element = document.format( | ||
effectiveStyle, | ||
width: effectiveSize.x, | ||
height: effectiveSize.y, | ||
); | ||
return TextElementComponent( | ||
element: element, | ||
position: position, | ||
size: effectiveSize, | ||
scale: scale, | ||
angle: angle, | ||
anchor: anchor, | ||
children: children, | ||
priority: priority, | ||
key: key, | ||
); | ||
} | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
element.draw(canvas); | ||
} | ||
|
||
static Vector2 _coalesceSize(DocumentStyle style, Vector2? size) { | ||
final width = style.width ?? size?.x; | ||
final height = style.height ?? size?.y; | ||
if (width == null || height == null) { | ||
throw ArgumentError('Either style.width or size.x must be provided.'); | ||
} | ||
if ((style.width != null && style.width != width) || | ||
(size?.x != null && size?.x != width)) { | ||
throw ArgumentError( | ||
'style.width and size.x, if both provided, must match.', | ||
); | ||
} | ||
if ((style.height != null && style.height != height) || | ||
(size?.y != null && size?.y != height)) { | ||
throw ArgumentError( | ||
'style.height and size.y, if both provided, must match.', | ||
); | ||
} | ||
return Vector2(width, height); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/flame/test/components/element_component_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/text.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('ElementComponent', () { | ||
test('size can be specified via the size parameter', () { | ||
final c = TextElementComponent.fromDocument( | ||
document: DocumentRoot([]), | ||
size: Vector2(100, 200), | ||
); | ||
expect(c.size, equals(Vector2(100, 200))); | ||
}); | ||
test('size can be specified via the style', () { | ||
final c = TextElementComponent.fromDocument( | ||
document: DocumentRoot([]), | ||
style: DocumentStyle(width: 100, height: 200), | ||
); | ||
expect(c.size, equals(Vector2(100, 200))); | ||
}); | ||
test('size can be super-specified if matching', () { | ||
final c = TextElementComponent.fromDocument( | ||
document: DocumentRoot([]), | ||
style: DocumentStyle(width: 100, height: 200), | ||
size: Vector2(100, 200), | ||
); | ||
expect(c.size, equals(Vector2(100, 200))); | ||
}); | ||
test('size must be specified', () { | ||
expect( | ||
() { | ||
TextElementComponent.fromDocument( | ||
document: DocumentRoot([]), | ||
style: DocumentStyle(), | ||
); | ||
}, | ||
throwsA( | ||
predicate((e) { | ||
return e is ArgumentError && | ||
e.message == 'Either style.width or size.x must be provided.'; | ||
}), | ||
), | ||
); | ||
}); | ||
test('size cannot be over-specified if mismatched', () { | ||
expect( | ||
() { | ||
TextElementComponent.fromDocument( | ||
document: DocumentRoot([]), | ||
style: DocumentStyle(width: 100, height: 200), | ||
size: Vector2(100, 300), | ||
); | ||
}, | ||
throwsA( | ||
predicate((e) { | ||
return e is ArgumentError && | ||
e.message == | ||
'style.height and size.y, if both provided, must match.'; | ||
}), | ||
), | ||
); | ||
}); | ||
}); | ||
} |