/
rectangle_component.dart
121 lines (115 loc) · 3.08 KB
/
rectangle_component.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:meta/meta.dart';
class RectangleComponent extends PolygonComponent {
RectangleComponent({
super.position,
super.size,
super.scale,
super.angle,
super.anchor,
super.children,
super.priority,
super.paint,
super.paintLayers,
super.key,
}) : super(sizeToVertices(size ?? Vector2.zero(), anchor)) {
size.addListener(
() => refreshVertices(
newVertices: sizeToVertices(size, anchor),
shrinkToBoundsOverride: false,
),
);
}
RectangleComponent.square({
double size = 0,
super.position,
super.scale,
super.angle,
super.anchor,
super.priority,
super.paint,
super.paintLayers,
super.children,
super.key,
}) : super(sizeToVertices(Vector2.all(size), anchor)) {
this.size.addListener(
() => refreshVertices(
newVertices: sizeToVertices(this.size, anchor),
shrinkToBoundsOverride: false,
),
);
}
/// With this constructor you define the [RectangleComponent] in relation to
/// the `parentSize`. For example having [relation] as of (0.8, 0.5) would
/// create a rectangle that fills 80% of the width and 50% of the height of
/// `parentSize`.
RectangleComponent.relative(
Vector2 relation, {
required super.parentSize,
super.position,
super.scale,
super.angle,
super.anchor,
super.priority,
super.paint,
super.paintLayers,
super.shrinkToBounds,
super.key,
}) : super.relative([
relation.clone(),
Vector2(relation.x, -relation.y),
-relation,
Vector2(-relation.x, relation.y),
]) {
size.addListener(
() => refreshVertices(
newVertices: sizeToVertices(size, anchor),
shrinkToBoundsOverride: false,
),
);
}
/// This factory will create a [RectangleComponent] from a positioned [Rect].
factory RectangleComponent.fromRect(
Rect rect, {
Vector2? scale,
double? angle,
Anchor anchor = Anchor.topLeft,
int? priority,
Paint? paint,
List<Paint>? paintLayers,
ComponentKey? key,
}) {
return RectangleComponent(
position: anchor == Anchor.topLeft
? rect.topLeft.toVector2()
: Anchor.topLeft.toOtherAnchorPosition(
rect.topLeft.toVector2(),
anchor,
rect.size.toVector2(),
),
size: rect.size.toVector2(),
scale: scale,
angle: angle,
anchor: anchor,
priority: priority,
paint: paint,
paintLayers: paintLayers,
key: key,
);
}
@protected
static List<Vector2> sizeToVertices(
Vector2 size,
Anchor? componentAnchor,
) {
final anchor = componentAnchor ?? Anchor.topLeft;
return [
Vector2(-size.x * anchor.x, -size.y * anchor.y),
Vector2(-size.x * anchor.x, size.y - size.y * anchor.y),
Vector2(size.x - size.x * anchor.x, size.y - size.y * anchor.y),
Vector2(size.x - size.x * anchor.x, -size.y * anchor.y),
];
}
}