diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cfa68671..e5eab514b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Explicitly define what fields an effect on PositionComponent modifies - Properly propagate onMount and onRemove to children - Adding Canvas extensions + - Remove Resizable mixin - Use config defaults for TextBoxComponent - Fixing Game Render Box for flutter >= 1.25 diff --git a/doc/examples/debug/lib/main.dart b/doc/examples/debug/lib/main.dart index 93a0d2600..c9c9b5d82 100644 --- a/doc/examples/debug/lib/main.dart +++ b/doc/examples/debug/lib/main.dart @@ -2,7 +2,6 @@ import 'package:flame/game.dart'; import 'package:flame/flame.dart'; import 'package:flame/extensions/vector2.dart'; import 'package:flame/components/sprite_component.dart'; -import 'package:flame/components/mixins/resizable.dart'; import 'package:flame/text_config.dart'; import 'package:flutter/material.dart' hide Image; @@ -20,8 +19,9 @@ void main() async { ); } -class AndroidComponent extends SpriteComponent with Resizable { +class AndroidComponent extends SpriteComponent { static const int SPEED = 150; + Vector2 _gameSize; int xDirection = 1; int yDirection = 1; @@ -30,7 +30,7 @@ class AndroidComponent extends SpriteComponent with Resizable { @override void update(double dt) { super.update(dt); - if (gameSize == null) { + if (_gameSize == null) { return; } @@ -39,17 +39,23 @@ class AndroidComponent extends SpriteComponent with Resizable { final rect = toRect(); if ((x <= 0 && xDirection == -1) || - (rect.right >= gameSize.x && xDirection == 1)) { + (rect.right >= _gameSize.x && xDirection == 1)) { xDirection = xDirection * -1; } y += yDirection * SPEED * dt; if ((y <= 0 && yDirection == -1) || - (rect.bottom >= gameSize.y && yDirection == 1)) { + (rect.bottom >= _gameSize.y && yDirection == 1)) { yDirection = yDirection * -1; } } + + @override + void onGameResize(Vector2 gameSize) { + super.onGameResize(gameSize); + _gameSize = gameSize; + } } class MyGame extends BaseGame { diff --git a/lib/components/component.dart b/lib/components/component.dart index a9780f663..b99900f6a 100644 --- a/lib/components/component.dart +++ b/lib/components/component.dart @@ -49,8 +49,6 @@ abstract class Component { /// It receives the new game size. /// Executed right after the component is attached to a game and right before [onMount] is called - /// - /// Use [Resizable] to save the gameSize in a component. void onGameResize(Vector2 gameSize) {} /// Remove the component from the game it is added to in the next tick diff --git a/lib/components/mixins/resizable.dart b/lib/components/mixins/resizable.dart deleted file mode 100644 index ae40581e4..000000000 --- a/lib/components/mixins/resizable.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'package:meta/meta.dart'; - -import '../../extensions/vector2.dart'; -import '../component.dart'; - -/// A [Component] mixin to make your component keep track of the size of the game viewport. -mixin Resizable on Component { - /// This is the current updated screen size. - Vector2 gameSize; - - /// Implementation provided by this mixin to the resize hook. - /// This is a hook called by [BaseGame] to let this component know that the screen (or flame draw area) has been update. - @override - @mustCallSuper - void onGameResize(Vector2 gameSize) { - super.onGameResize(gameSize); - this.gameSize = gameSize; - } -} diff --git a/lib/components/text_box_component.dart b/lib/components/text_box_component.dart index d3d0fa7ee..514f5389e 100644 --- a/lib/components/text_box_component.dart +++ b/lib/components/text_box_component.dart @@ -7,7 +7,6 @@ import 'package:flutter/widgets.dart' hide Image; import '../palette.dart'; import '../text_config.dart'; import '../extensions/vector2.dart'; -import 'mixins/resizable.dart'; import 'position_component.dart'; class TextBoxConfig { @@ -26,9 +25,10 @@ class TextBoxConfig { }); } -class TextBoxComponent extends PositionComponent with Resizable { +class TextBoxComponent extends PositionComponent { static final Paint _imagePaint = BasicPalette.white.paint ..filterQuality = FilterQuality.high; + Vector2 _gameSize; String _text; TextConfig _config; @@ -157,9 +157,15 @@ class TextBoxComponent extends PositionComponent with Resizable { c.drawImage(_cache, Offset.zero, _imagePaint); } + @override + void onGameResize(Vector2 gameSize) { + super.onGameResize(gameSize); + _gameSize = gameSize; + } + Future _redrawCache() { final PictureRecorder recorder = PictureRecorder(); - final Canvas c = Canvas(recorder, gameSize.toRect()); + final Canvas c = Canvas(recorder, _gameSize.toRect()); _fullRender(c); return recorder.endRecording().toImage(width.toInt(), height.toInt()); } diff --git a/test/base_game_test.dart b/test/base_game_test.dart index 84c1897ca..a4ed8e3bf 100644 --- a/test/base_game_test.dart +++ b/test/base_game_test.dart @@ -2,7 +2,6 @@ import 'dart:ui'; import 'package:flame/components/position_component.dart'; import 'package:flame/components/mixins/has_game_ref.dart'; -import 'package:flame/components/mixins/resizable.dart'; import 'package:flame/components/mixins/tapable.dart'; import 'package:flame/game.dart'; import 'package:flame/game/base_game.dart'; @@ -16,12 +15,12 @@ import 'package:flutter_test/flutter_test.dart' as flutter; class MyGame extends BaseGame with HasTapableComponents {} -class MyComponent extends PositionComponent - with Tapable, Resizable, HasGameRef { +class MyComponent extends PositionComponent with Tapable, HasGameRef { bool tapped = false; bool isUpdateCalled = false; bool isRenderCalled = false; int onRemoveCallCounter = 0; + Vector2 gameSize; @override bool onTapDown(TapDownDetails details) { @@ -41,6 +40,12 @@ class MyComponent extends PositionComponent isRenderCalled = true; } + @override + void onGameResize(Vector2 gameSize) { + super.onGameResize(gameSize); + this.gameSize = gameSize; + } + @override bool checkOverlap(Vector2 v) => true; diff --git a/test/components/composed_component_test.dart b/test/components/composed_component_test.dart index c9d25ee1d..514b0876b 100644 --- a/test/components/composed_component_test.dart +++ b/test/components/composed_component_test.dart @@ -2,7 +2,6 @@ import 'dart:ui'; import 'package:flame/components/position_component.dart'; import 'package:flame/components/mixins/has_game_ref.dart'; -import 'package:flame/components/mixins/resizable.dart'; import 'package:flame/components/mixins/tapable.dart'; import 'package:flame/game/base_game.dart'; import 'package:flame/extensions/vector2.dart'; @@ -13,7 +12,9 @@ import '../util/mock_canvas.dart'; class MyGame extends BaseGame with HasTapableComponents {} -class MyTap extends PositionComponent with Tapable, Resizable { +class MyTap extends PositionComponent with Tapable { + Vector2 gameSize; + bool tapped = false; bool updated = false; bool rendered = false; @@ -30,6 +31,12 @@ class MyTap extends PositionComponent with Tapable, Resizable { rendered = true; } + @override + void onGameResize(Vector2 gameSize) { + super.onGameResize(gameSize); + this.gameSize = gameSize; + } + @override bool onTapDown(TapDownDetails details) { tapped = true; diff --git a/test/components/resizable_test.dart b/test/components/resizable_test.dart index 5913e83d2..71665e30c 100644 --- a/test/components/resizable_test.dart +++ b/test/components/resizable_test.dart @@ -3,14 +3,20 @@ import 'package:flame/extensions/vector2.dart'; import 'package:test/test.dart'; import 'package:flame/components/position_component.dart'; -import 'package:flame/components/mixins/resizable.dart'; -class MyComponent extends PositionComponent with Resizable { +class MyComponent extends PositionComponent { String name; @override Vector2 size = Vector2(2.0, 2.0); + Vector2 gameSize; MyComponent(this.name); + + @override + void onGameResize(Vector2 gameSize) { + super.onGameResize(gameSize); + this.gameSize = gameSize; + } } class MyGame extends BaseGame {} @@ -33,7 +39,7 @@ void main() { game.onResize(size); expect(a.gameSize, size); }); - test('game calls doesnt change component size', () { + test("game calls doesn't change component size", () { final MyComponent a = MyComponent('a'); final MyGame game = MyGame(); game.add(a);