mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 03:15:43 +08:00
40 lines
1.5 KiB
Dart
40 lines
1.5 KiB
Dart
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter/widgets.dart' hide WidgetBuilder;
|
|
import 'package:vector_math/vector_math_64.dart';
|
|
|
|
import 'game_render_box.dart';
|
|
import 'game.dart';
|
|
|
|
/// This a widget to embed a game inside the Widget tree. You can use it in pair with [BaseGame] or any other more complex [Game], as desired.
|
|
///
|
|
/// It handles for you positioning, size constraints and other factors that arise when your game is embedded within the component tree.
|
|
/// Provided it with a [Game] instance for your game and the optional size of the widget.
|
|
/// Creating this without a fixed size might mess up how other components are rendered with relation to this one in the tree.
|
|
/// You can bind Gesture Recognizers immediately around this to add controls to your widgets, with easy coordinate conversions.
|
|
class EmbeddedGameWidget extends LeafRenderObjectWidget {
|
|
final Game game;
|
|
final Vector2 size;
|
|
|
|
EmbeddedGameWidget(this.game, {this.size});
|
|
|
|
@override
|
|
RenderBox createRenderObject(BuildContext context) {
|
|
return RenderConstrainedBox(
|
|
child: GameRenderBox(context, game),
|
|
additionalConstraints:
|
|
BoxConstraints.expand(width: size?.x, height: size?.y),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void updateRenderObject(
|
|
BuildContext context,
|
|
RenderConstrainedBox renderBox,
|
|
) {
|
|
renderBox
|
|
..child = GameRenderBox(context, game)
|
|
..additionalConstraints =
|
|
BoxConstraints.expand(width: size?.x, height: size?.y);
|
|
}
|
|
}
|