mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 19:12:31 +08:00
56 lines
1.5 KiB
Dart
56 lines
1.5 KiB
Dart
|
|
import 'dart:ui';
|
|
|
|
import 'package:flame/components/component.dart';
|
|
import 'package:flame/components/resizable.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:ordered_set/comparing.dart';
|
|
import 'package:ordered_set/ordered_set.dart';
|
|
|
|
/// A component that lets your component be composed by others
|
|
/// It resembles [BaseGame]. It has an [components] property and an [add] method
|
|
mixin ComposedComponent on Component, Resizable {
|
|
OrderedSet<Component> components = new OrderedSet(Comparing.on((c) => c.priority()));
|
|
|
|
@override
|
|
render(Canvas canvas) {
|
|
canvas.save();
|
|
components.forEach((comp) => _renderComponent(canvas, comp));
|
|
canvas.restore();
|
|
}
|
|
|
|
void _renderComponent(Canvas canvas, Component c) {
|
|
c.render(canvas);
|
|
canvas.restore();
|
|
canvas.save();
|
|
}
|
|
|
|
@override
|
|
void update(double t) {
|
|
components.forEach((c) => c.update(t));
|
|
components.removeWhere((c) => c.destroy());
|
|
}
|
|
|
|
void add(Component c) {
|
|
this.components.add(c);
|
|
|
|
if(this is Resizable){
|
|
// first time resize
|
|
Resizable thisResizable = this as Resizable;
|
|
if (thisResizable.size != null) {
|
|
c.resize(thisResizable.size);
|
|
}
|
|
}
|
|
}
|
|
|
|
List<Resizable> children() => this.components.where((r) => r is Resizable).cast<Resizable>();
|
|
|
|
@override
|
|
void resize(Size size) {
|
|
if(this is Resizable){
|
|
Resizable thisResizable = this as Resizable;
|
|
thisResizable.size = size;
|
|
components.forEach((c) => c.resize(size));
|
|
}
|
|
}
|
|
} |