/
utils.dart
66 lines (62 loc) · 1.59 KB
/
utils.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
import 'dart:math';
import 'package:flame/src/text/elements/group_element.dart';
import 'package:flame/text.dart';
import 'package:meta/meta.dart';
@internal
double collapseMargin(double margin1, double margin2) {
if (margin1 >= 0) {
return (margin2 < 0) ? margin1 + margin2 : max(margin1, margin2);
} else {
return (margin2 < 0) ? min(margin1, margin2) : margin1 + margin2;
}
}
@internal
TextElement? makeBackground(
BackgroundStyle? style,
double width,
double height,
) {
if (style == null) {
return null;
}
final out = <TextElement>[];
final backgroundPaint = style.backgroundPaint;
final borderPaint = style.borderPaint;
final borders = style.borderWidths;
final radius = style.borderRadius;
if (backgroundPaint != null) {
if (radius == 0) {
out.add(RectElement(width, height, backgroundPaint));
} else {
out.add(RRectElement(width, height, radius, backgroundPaint));
}
}
if (borderPaint != null) {
if (radius == 0) {
out.add(
RectElement(
width - borders.horizontal / 2,
height - borders.vertical / 2,
borderPaint,
)..translate(borders.left / 2, borders.top / 2),
);
} else {
out.add(
RRectElement(
width - borders.horizontal / 2,
height - borders.vertical / 2,
radius,
borderPaint,
)..translate(borders.left / 2, borders.top / 2),
);
}
}
if (out.isEmpty) {
return null;
}
if (out.length == 1) {
return out.first;
} else {
return GroupElement(width: width, height: height, children: out);
}
}