Skip to content

Commit 48f12af

Browse files
authored
make onTick overridable. Make delta positive always. (#13)
* make onTick overridable. Make delta positive always. * tests fixes * dart and flutter bump * cicd flutter * format
1 parent 1ff756e commit 48f12af

File tree

10 files changed

+517
-627
lines changed

10 files changed

+517
-627
lines changed

.github/actions/setup/action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inputs:
55
flutter-version:
66
description: 'The version of Flutter to use'
77
required: false
8-
default: '3.24.5'
8+
default: '3.35.3'
99
pub-cache:
1010
description: 'The name of the pub cache variable'
1111
required: false

.github/workflows/checkout.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jobs:
6666
- name: 🚂 Setup Flutter and dependencies
6767
uses: ./.github/actions/setup
6868
with:
69-
flutter-version: 3.27.1
69+
flutter-version: 3.35.3
7070

7171
- name: 👷 Install Dependencies
7272
timeout-minutes: 1

lib/src/collisions/quadtree.dart

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@ extension type QuadTree$QueryResult._(Float32List _bytes) {
4949
/// The walk stops when it iterates over all objects or
5050
/// when the callback returns false.
5151
void forEach(
52-
bool Function(
53-
int id,
54-
double left,
55-
double top,
56-
double width,
57-
double height,
58-
) cb,
52+
bool Function(int id, double left, double top, double width, double height)
53+
cb,
5954
) {
6055
if (isEmpty) return;
6156
final ids = Uint32List.sublistView(_bytes);
@@ -183,14 +178,13 @@ final class QuadTree {
183178
required Float32List objects,
184179
required Uint32List recycledIds,
185180
required Uint32List id2node,
186-
}) :
187-
// Nodes
188-
_nodes = nodes,
189-
_recycledNodes = recycledNodes,
190-
// Objects
191-
_objects = objects,
192-
_recycledIds = recycledIds,
193-
_id2node = id2node;
181+
}) : // Nodes
182+
_nodes = nodes,
183+
_recycledNodes = recycledNodes,
184+
// Objects
185+
_objects = objects,
186+
_recycledIds = recycledIds,
187+
_id2node = id2node;
194188

195189
// --------------------------------------------------------------------------
196190
// PROPERTIES
@@ -290,10 +284,7 @@ final class QuadTree {
290284

291285
// Get the root node of the QuadTree
292286
// or create a new one if it does not exist.
293-
final root = _root ??= _createNode(
294-
parent: null,
295-
boundary: boundary,
296-
);
287+
final root = _root ??= _createNode(parent: null, boundary: boundary);
297288

298289
// Create a new object in the QuadTree.
299290
final objectId = _getNextObjectId();
@@ -434,10 +425,7 @@ final class QuadTree {
434425

435426
// Resize recycled ids array if needed
436427
if (_recycledIdsCount == _recycledIds.length)
437-
_recycledIds = _resizeUint32List(
438-
_recycledIds,
439-
_recycledIds.length << 1,
440-
);
428+
_recycledIds = _resizeUint32List(_recycledIds, _recycledIds.length << 1);
441429
_recycledIds[_recycledIdsCount++] = objectId;
442430

443431
return true;
@@ -452,13 +440,8 @@ final class QuadTree {
452440
/// The walk stops when it iterates over all objects or
453441
/// when the callback returns false.
454442
void forEach(
455-
bool Function(
456-
int id,
457-
double left,
458-
double top,
459-
double width,
460-
double height,
461-
) cb,
443+
bool Function(int id, double left, double top, double width, double height)
444+
cb,
462445
) {
463446
final root = _root;
464447
if (root == null) return;
@@ -1013,7 +996,8 @@ final class QuadTree {
1013996
// --------------------------------------------------------------------------
1014997

1015998
@override
1016-
String toString() => 'QuadTree{'
999+
String toString() =>
1000+
'QuadTree{'
10171001
'nodes: $nodes, '
10181002
'objects: $length'
10191003
'}';
@@ -1137,13 +1121,8 @@ final class QuadTree$Node {
11371121
/// when the callback returns false.
11381122
@pragma('vm:prefer-inline')
11391123
void forEach(
1140-
bool Function(
1141-
int id,
1142-
double left,
1143-
double top,
1144-
double width,
1145-
double height,
1146-
) cb,
1124+
bool Function(int id, double left, double top, double width, double height)
1125+
cb,
11471126
) {
11481127
if (isEmpty) return;
11491128
if (subdivided) {
@@ -1184,12 +1163,7 @@ final class QuadTree$Node {
11841163
final top = boundary.top;
11851164
final nw = _northWest = tree._createNode(
11861165
parent: this,
1187-
boundary: ui.Rect.fromLTWH(
1188-
left,
1189-
top,
1190-
halfWidth,
1191-
halfHeight,
1192-
),
1166+
boundary: ui.Rect.fromLTWH(left, top, halfWidth, halfHeight),
11931167
),
11941168
ne = _northEast = tree._createNode(
11951169
parent: this,
@@ -1327,7 +1301,8 @@ final class QuadTree$Node {
13271301
identical(this, other) || other is QuadTree$Node && id == other.id;
13281302

13291303
@override
1330-
String toString() => r'QuadTree$Node{'
1304+
String toString() =>
1305+
r'QuadTree$Node{'
13311306
'id: $id, '
13321307
'objects: $length, '
13331308
'subdivided: $_subdivided'

lib/src/repaint.dart

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,16 @@ class RePaint extends LeafRenderObjectWidget {
4646
Listenable? repaint,
4747
bool repaintBoundary = false,
4848
Key? key,
49-
}) =>
50-
RePaintInline<T>(
51-
render: render,
52-
setUp: setUp,
53-
update: update,
54-
tearDown: tearDown,
55-
frameRate: frameRate,
56-
repaint: repaint,
57-
repaintBoundary: repaintBoundary,
58-
key: key,
59-
);
49+
}) => RePaintInline<T>(
50+
render: render,
51+
setUp: setUp,
52+
update: update,
53+
tearDown: tearDown,
54+
frameRate: frameRate,
55+
repaint: repaint,
56+
repaintBoundary: repaintBoundary,
57+
key: key,
58+
);
6059

6160
/// The painter controller, used to update and paint the scene.
6261
/// For example, a game controller or a custom painter.
@@ -76,10 +75,10 @@ class RePaint extends LeafRenderObjectWidget {
7675

7776
@override
7877
RenderObject createRenderObject(BuildContext context) => RePaintBox(
79-
painter: painter,
80-
context: context,
81-
isRepaintBoundary: repaintBoundary,
82-
);
78+
painter: painter,
79+
context: context,
80+
isRepaintBoundary: repaintBoundary,
81+
);
8382

8483
@override
8584
void updateRenderObject(BuildContext context, RePaintBox renderObject) {
@@ -92,7 +91,8 @@ class RePaint extends LeafRenderObjectWidget {
9291
renderObject._painter = painter
9392
..mount(renderObject, renderObject.owner!)
9493
..lifecycle(
95-
WidgetsBinding.instance.lifecycleState ?? AppLifecycleState.resumed);
94+
WidgetsBinding.instance.lifecycleState ?? AppLifecycleState.resumed,
95+
);
9696
}
9797
}
9898

@@ -136,9 +136,9 @@ class RePaintBox extends RenderBox with WidgetsBindingObserver {
136136
required RePainter painter,
137137
required BuildContext context,
138138
required bool isRepaintBoundary,
139-
}) : _painter = painter,
140-
_context = context,
141-
_$isRepaintBoundary = isRepaintBoundary;
139+
}) : _painter = painter,
140+
_context = context,
141+
_$isRepaintBoundary = isRepaintBoundary;
142142

143143
/// Current controller.
144144
RePainter get painter => _painter;
@@ -185,19 +185,17 @@ class RePaintBox extends RenderBox with WidgetsBindingObserver {
185185
_painter
186186
..mount(this, owner)
187187
..lifecycle(
188-
WidgetsBinding.instance.lifecycleState ?? AppLifecycleState.resumed);
188+
WidgetsBinding.instance.lifecycleState ?? AppLifecycleState.resumed,
189+
);
189190
WidgetsBinding.instance.addObserver(this);
190-
_ticker = Ticker(_onTick, debugLabel: 'RePaintBox')..start();
191+
_ticker = Ticker(onTick, debugLabel: 'RePaintBox')..start();
191192
}
192193

193194
@override
194195
bool hitTestSelf(Offset position) => true;
195196

196197
@override
197-
bool hitTestChildren(
198-
BoxHitTestResult result, {
199-
required Offset position,
200-
}) =>
198+
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) =>
201199
false;
202200

203201
@override
@@ -250,10 +248,13 @@ class RePaintBox extends RenderBox with WidgetsBindingObserver {
250248
Duration _lastFrameTime = Duration.zero;
251249

252250
/// This method is periodically invoked by the [_ticker].
253-
void _onTick(Duration elapsed) {
251+
void onTick(Duration elapsed) {
254252
if (!attached) return;
253+
// Delta can be negative when widget is paused.
254+
// Sometimes even like "-15" seconds.
255255
final delta = elapsed - _lastFrameTime;
256-
final deltaMs = delta.inMicroseconds / Duration.microsecondsPerMillisecond;
256+
final deltaMs =
257+
delta.inMicroseconds.abs() / Duration.microsecondsPerMillisecond;
257258
_lastFrameTime = elapsed;
258259
// Update game scene and prepare for rendering.
259260
_painter.update(this, elapsed, deltaMs);

pubspec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ platforms:
3636
# path: example.png
3737

3838
environment:
39-
sdk: ^3.6.0
40-
flutter: ">=3.27.0"
39+
sdk: ^3.8.0
40+
flutter: ">=3.35.3"
4141

4242
dependencies:
4343
flutter:
@@ -52,5 +52,5 @@ dev_dependencies:
5252
fake_async: ^1.3.0
5353
flutter_lints: ">=4.0.0 <6.0.0"
5454
benchmark_harness: ^2.3.1
55-
flame: ^1.23.0
56-
vector_math: ^2.1.4
55+
flame: ^1.34.0
56+
vector_math: ^2.2.0

0 commit comments

Comments
 (0)