Remove Resizable mixin (#589)

* Remove Resizable mixin

* Update examples and changelog

* Fixed formatting

* Remove unused import
This commit is contained in:
Lukas Klingsbo
2020-12-22 15:55:29 +01:00
committed by GitHub
parent dfb3a71c4f
commit 18d04c13c0
8 changed files with 47 additions and 37 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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

View File

@ -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;
}
}

View File

@ -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<Image> _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());
}

View File

@ -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;

View File

@ -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;

View File

@ -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);