Fix order of parent -> children rendering (#684)

* Fix order of parent -> children rendering

* Fix import

* Move debug rendering

* Call renderTree on children too

* Component to have renderTree
This commit is contained in:
Lukas Klingsbo
2021-02-24 20:17:28 +01:00
committed by GitHub
parent 9ab9003c85
commit cb15de7e0f
4 changed files with 19 additions and 6 deletions

View File

@@ -24,6 +24,7 @@
- Added `toImage` method for the `Sprite` class - Added `toImage` method for the `Sprite` class
- Uniform use of `dt` instead of `t` in all update methods - Uniform use of `dt` instead of `t` in all update methods
- Add more optional arguments for unified constructors of components - Add more optional arguments for unified constructors of components
- Fix order that parent -> children render in
## 1.0.0-rc6 ## 1.0.0-rc6
- Use `Offset` type directly in `JoystickAction.update` calculations - Use `Offset` type directly in `JoystickAction.update` calculations

View File

@@ -69,15 +69,22 @@ abstract class BaseComponent extends Component {
@override @override
void render(Canvas canvas) { void render(Canvas canvas) {
prepareCanvas(canvas); prepareCanvas(canvas);
}
@mustCallSuper
@override
void renderTree(Canvas canvas) {
render(canvas);
_children.forEach((c) {
canvas.save();
c.renderTree(canvas);
canvas.restore();
});
// Any debug rendering should be rendered on top of everything
if (debugMode) { if (debugMode) {
renderDebugMode(canvas); renderDebugMode(canvas);
} }
_children.forEach((c) {
canvas.save();
c.render(canvas);
canvas.restore();
});
} }
void renderDebugMode(Canvas canvas) {} void renderDebugMode(Canvas canvas) {}

View File

@@ -46,6 +46,10 @@ abstract class Component {
/// Renders this component on the provided Canvas [c]. /// Renders this component on the provided Canvas [c].
void render(Canvas c) {} void render(Canvas c) {}
/// This is used to render this component and potential children on subclasses
/// of [Component] on the provided Canvas [c].
void renderTree(Canvas c) => render(c);
/// It receives the new game size. /// It receives the new game size.
/// Executed right after the component is attached to a game and right before [onMount] is called /// Executed right after the component is attached to a game and right before [onMount] is called
void onGameResize(Vector2 gameSize) {} void onGameResize(Vector2 gameSize) {}

View File

@@ -4,6 +4,7 @@ import 'package:meta/meta.dart';
import 'package:ordered_set/comparing.dart'; import 'package:ordered_set/comparing.dart';
import 'package:ordered_set/ordered_set.dart'; import 'package:ordered_set/ordered_set.dart';
import '../../components.dart';
import '../components/component.dart'; import '../components/component.dart';
import '../components/mixins/collidable.dart'; import '../components/mixins/collidable.dart';
import '../components/mixins/draggable.dart'; import '../components/mixins/draggable.dart';
@@ -131,7 +132,7 @@ class BaseGame extends Game with FPSCounter {
if (!c.isHud) { if (!c.isHud) {
canvas.translate(-camera.x, -camera.y); canvas.translate(-camera.x, -camera.y);
} }
c.render(canvas); c.renderTree(canvas);
canvas.restore(); canvas.restore();
canvas.save(); canvas.save();
} }