-
-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add basic infrastructure for a Context API
- Loading branch information
1 parent
ce7822a
commit a6631d3
Showing
3 changed files
with
145 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
abstract class ComponentContext {} |
112 changes: 112 additions & 0 deletions
112
packages/flame/test/components/component_context_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,112 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:canvas_test/canvas_test.dart'; | ||
import 'package:flame/components.dart'; | ||
import 'package:flame/src/components/core/component_context.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group('Component Context', () { | ||
testWithFlameGame('simple parent and child', (game) async { | ||
final child = _ChildReadsContext(); | ||
final parent = _ParentWithContext( | ||
startingValue: 42, | ||
children: [ | ||
child, | ||
], | ||
); | ||
game.add(parent); | ||
|
||
await game.ready(); | ||
game.render(MockCanvas()); | ||
|
||
expect(child.myContext, 42); | ||
}); | ||
|
||
testWithFlameGame('complex parent and child', (game) async { | ||
final child1 = _ChildReadsContext(); | ||
final child2 = _ChildReadsContext(); | ||
final child3 = _ChildReadsContext(); | ||
|
||
final parent = Component( | ||
children: [ | ||
child1, | ||
_ParentWithContext( | ||
startingValue: 1, | ||
children: [ | ||
child2, | ||
_ParentWithContext( | ||
startingValue: 2, | ||
children: [ | ||
child3, | ||
], | ||
), | ||
], | ||
), | ||
], | ||
); | ||
game.add(parent); | ||
|
||
await game.ready(); | ||
game.render(MockCanvas()); | ||
|
||
expect(child1.myContext, null); | ||
expect(child2.myContext, 1); | ||
expect(child3.myContext, 2); | ||
}); | ||
|
||
testWithFlameGame('mutating context', (game) async { | ||
final child = _ChildReadsContext(); | ||
|
||
final parent = _ParentWithContext( | ||
startingValue: 10, | ||
children: [ | ||
child, | ||
], | ||
); | ||
game.add(parent); | ||
|
||
await game.ready(); | ||
final canvas = MockCanvas(); | ||
|
||
game.render(canvas); | ||
expect(child.myContext, 10); | ||
|
||
parent._myContext.value = 20; | ||
game.render(canvas); | ||
expect(child.myContext, 20); | ||
}); | ||
}); | ||
} | ||
|
||
class _IntContext extends ComponentContext { | ||
int value; | ||
|
||
_IntContext(this.value); | ||
} | ||
|
||
class _ParentWithContext extends Component { | ||
final int startingValue; | ||
late final _IntContext _myContext = _IntContext(startingValue); | ||
|
||
_ParentWithContext({ | ||
required this.startingValue, | ||
super.children, | ||
}); | ||
|
||
@override | ||
_IntContext? createContext() => _myContext; | ||
} | ||
|
||
class _ChildReadsContext extends Component { | ||
int? myContext; | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
final context = findContext<_IntContext>(); | ||
myContext = context?.value; | ||
|
||
super.render(canvas); | ||
} | ||
} |