/
game_render_box.dart
145 lines (115 loc) · 2.99 KB
/
game_render_box.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import 'package:flame/game.dart';
import 'package:flame/src/game/game_loop.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart' hide WidgetBuilder;
/// A [RenderObjectWidget] that renders the [GameRenderBox].
///
/// This is the widget that is used by the [GameWidget] to ACTUALLY
/// render the game.
class RenderGameWidget extends LeafRenderObjectWidget {
const RenderGameWidget({
required this.game,
required this.addRepaintBoundary,
super.key,
});
final Game game;
final bool addRepaintBoundary;
@override
RenderBox createRenderObject(BuildContext context) {
return GameRenderBox(game, context, isRepaintBoundary: addRepaintBoundary);
}
@override
void updateRenderObject(BuildContext context, GameRenderBox renderObject) {
renderObject
..game = game
..buildContext = context
..isRepaintBoundary = addRepaintBoundary;
}
}
class GameRenderBox extends RenderBox with WidgetsBindingObserver {
GameRenderBox(
this._game,
this.buildContext, {
required bool isRepaintBoundary,
}) : _isRepaintBoundary = isRepaintBoundary;
GameLoop? gameLoop;
BuildContext buildContext;
Game _game;
Game get game => _game;
set game(Game value) {
// Identities are equal, no need to update.
if (_game == value) {
return;
}
if (attached) {
_detachGame();
}
_game = value;
if (attached) {
_attachGame(owner!);
}
}
bool _isRepaintBoundary;
set isRepaintBoundary(bool value) {
if (_isRepaintBoundary == value) {
return;
}
_isRepaintBoundary = value;
markNeedsCompositingBitsUpdate();
}
@override
bool get isRepaintBoundary => _isRepaintBoundary;
@override
bool get sizedByParent => true;
@override
Size computeDryLayout(BoxConstraints constraints) => constraints.biggest;
@override
void attach(PipelineOwner owner) {
super.attach(owner);
_attachGame(owner);
}
void _attachGame(PipelineOwner owner) {
game.attach(owner, this);
final gameLoop = this.gameLoop = GameLoop(gameLoopCallback);
if (!game.paused) {
gameLoop.start();
}
_bindLifecycleListener();
}
@override
void detach() {
super.detach();
_detachGame();
}
void _detachGame() {
game.detach();
gameLoop?.dispose();
gameLoop = null;
_unbindLifecycleListener();
}
void gameLoopCallback(double dt) {
assert(attached);
if (!attached) {
return;
}
game.update(dt);
markNeedsPaint();
}
@override
void paint(PaintingContext context, Offset offset) {
context.canvas.save();
context.canvas.translate(offset.dx, offset.dy);
game.render(context.canvas);
context.canvas.restore();
}
void _bindLifecycleListener() {
WidgetsBinding.instance.addObserver(this);
}
void _unbindLifecycleListener() {
WidgetsBinding.instance.removeObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
game.lifecycleStateChange(state);
}
}