From 59d12caba371cd229a181db9c308287851063a5c Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 3 Jun 2024 11:41:44 -0300 Subject: [PATCH 1/8] adds toSimpleDirectionAnimation --- lib/npc/enemy/platform_enemy.dart | 19 +---------------- .../platform_animations.dart | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/lib/npc/enemy/platform_enemy.dart b/lib/npc/enemy/platform_enemy.dart index 275448c76..47e8ad233 100644 --- a/lib/npc/enemy/platform_enemy.dart +++ b/lib/npc/enemy/platform_enemy.dart @@ -14,24 +14,7 @@ class PlatformEnemy extends SimpleEnemy initDirection: initDirection, speed: speed, life: life, - animation: SimpleDirectionAnimation( - idleRight: animation.idleRight, - runRight: animation.runRight, - idleLeft: animation.idleLeft, - runLeft: animation.runLeft, - others: { - if (animation.jump?.jumpUpRight != null) - JumpAnimationsEnum.jumpUpRight: animation.jump!.jumpUpRight, - if (animation.jump?.jumpUpLeft != null) - JumpAnimationsEnum.jumpUpLeft: animation.jump!.jumpUpLeft!, - if (animation.jump?.jumpDownRight != null) - JumpAnimationsEnum.jumpDownRight: animation.jump!.jumpDownRight, - if (animation.jump?.jumpDownLeft != null) - JumpAnimationsEnum.jumpDownLeft: animation.jump!.jumpDownLeft!, - ...animation.others ?? {}, - }, - centerAnchor: animation.centerAnchor, - ), + animation: animation.toSimpleDirectionAnimation(), ) { setupJumper(maxJump: countJumps); } diff --git a/lib/util/direction_animations/platform_animations.dart b/lib/util/direction_animations/platform_animations.dart index c9c3cc483..9853d4e99 100644 --- a/lib/util/direction_animations/platform_animations.dart +++ b/lib/util/direction_animations/platform_animations.dart @@ -69,4 +69,25 @@ class PlatformAnimations { centerAnchor: centerAnchor ?? this.centerAnchor, ); } + + SimpleDirectionAnimation toSimpleDirectionAnimation() { + return SimpleDirectionAnimation( + idleRight: idleRight, + runRight: runRight, + idleLeft: idleLeft, + runLeft: runLeft, + others: { + if (jump?.jumpUpRight != null) + JumpAnimationsEnum.jumpUpRight: jump!.jumpUpRight, + if (jump?.jumpUpLeft != null) + JumpAnimationsEnum.jumpUpLeft: jump!.jumpUpLeft!, + if (jump?.jumpDownRight != null) + JumpAnimationsEnum.jumpDownRight: jump!.jumpDownRight, + if (jump?.jumpDownLeft != null) + JumpAnimationsEnum.jumpDownLeft: jump!.jumpDownLeft!, + ...others ?? {}, + }, + centerAnchor: centerAnchor, + ); + } } From d8cc906c4176b3dfbf5aa92b3fe0bd790d7d7bc6 Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 3 Jun 2024 11:44:38 -0300 Subject: [PATCH 2/8] remove animation required --- lib/npc/enemy/platform_enemy.dart | 4 ++-- lib/player/platform_player.dart | 25 +------------------------ 2 files changed, 3 insertions(+), 26 deletions(-) diff --git a/lib/npc/enemy/platform_enemy.dart b/lib/npc/enemy/platform_enemy.dart index 47e8ad233..481ec606e 100644 --- a/lib/npc/enemy/platform_enemy.dart +++ b/lib/npc/enemy/platform_enemy.dart @@ -5,7 +5,7 @@ class PlatformEnemy extends SimpleEnemy PlatformEnemy({ required super.position, required super.size, - required PlatformAnimations animation, + PlatformAnimations? animation, Direction initDirection = Direction.right, double? speed, double life = 100, @@ -14,7 +14,7 @@ class PlatformEnemy extends SimpleEnemy initDirection: initDirection, speed: speed, life: life, - animation: animation.toSimpleDirectionAnimation(), + animation: animation?.toSimpleDirectionAnimation(), ) { setupJumper(maxJump: countJumps); } diff --git a/lib/player/platform_player.dart b/lib/player/platform_player.dart index 42e0f4218..e294f99a0 100644 --- a/lib/player/platform_player.dart +++ b/lib/player/platform_player.dart @@ -14,30 +14,7 @@ class PlatformPlayer extends SimplePlayer initDirection: initDirection, speed: speed, life: life, - animation: animation != null - ? SimpleDirectionAnimation( - idleRight: animation.idleRight, - runRight: animation.runRight, - idleLeft: animation.idleLeft, - runLeft: animation.runLeft, - centerAnchor: animation.centerAnchor, - others: { - if (animation.jump?.jumpUpRight != null) - JumpAnimationsEnum.jumpUpRight: - animation.jump!.jumpUpRight, - if (animation.jump?.jumpUpLeft != null) - JumpAnimationsEnum.jumpUpLeft: - animation.jump!.jumpUpLeft!, - if (animation.jump?.jumpDownRight != null) - JumpAnimationsEnum.jumpDownRight: - animation.jump!.jumpDownRight, - if (animation.jump?.jumpDownLeft != null) - JumpAnimationsEnum.jumpDownLeft: - animation.jump!.jumpDownLeft!, - ...animation.others ?? {}, - }, - ) - : null, + animation: animation?.toSimpleDirectionAnimation(), ) { setupJumper(maxJump: countJumps); } From a27cbe42fc40b45ed7ed6d1beeb8341af3487ae2 Mon Sep 17 00:00:00 2001 From: Rafael Date: Sun, 9 Jun 2024 01:48:57 -0300 Subject: [PATCH 3/8] fix bug --- CHANGELOG.md | 4 ++ lib/input/keyboard/keyboard.dart | 34 +++++++------- lib/input/player_controller.dart | 8 ++-- lib/joystick/joystick.dart | 15 ++++-- lib/joystick/joystick_action.dart | 36 ++++++++++----- lib/joystick/joystick_directional.dart | 64 ++++++++++++++++---------- 6 files changed, 103 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdeb67c9b..14049e4ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Next + +- Fix Joystick bug when viewport is fixed resolution. [#526](https://github.com/RafaelBarbosatec/bonfire/issues/526) + # 3.9.4 - Fix bug in `FollowerWidget`. - Fix bug in `SimpleDirectionAnimation` where fast animation direction did not change when character direction changed. Thanks [tkshnwesper](https://github.com/tkshnwesper) diff --git a/lib/input/keyboard/keyboard.dart b/lib/input/keyboard/keyboard.dart index 2f720aa5c..3fe0e16e6 100644 --- a/lib/input/keyboard/keyboard.dart +++ b/lib/input/keyboard/keyboard.dart @@ -37,7 +37,7 @@ class Keyboard extends PlayerController with KeyboardEventListener { !event.synthesized && !_directionalIsIdle) { _directionalIsIdle = true; - joystickChangeDirectional( + onJoystickChangeDirectional( JoystickDirectionalEvent( directional: JoystickMoveDirectional.IDLE, intensity: 0.0, @@ -64,14 +64,14 @@ class Keyboard extends PlayerController with KeyboardEventListener { } else { /// Process action events if (event is KeyDownEvent) { - joystickAction( + onJoystickAction( JoystickActionEvent( id: event.logicalKey, event: ActionEvent.DOWN, ), ); } else if (event is KeyUpEvent) { - joystickAction( + onJoystickAction( JoystickActionEvent( id: event.logicalKey, event: ActionEvent.UP, @@ -109,15 +109,17 @@ class Keyboard extends PlayerController with KeyboardEventListener { void _sendOneDirection(LogicalKeyboardKey key) { if (isUpPressed(key)) { - joystickChangeDirectional(JoystickDirectionalEvent( - directional: JoystickMoveDirectional.MOVE_UP, - intensity: 1.0, - radAngle: 0.0, - isKeyboard: true, - )); + onJoystickChangeDirectional( + JoystickDirectionalEvent( + directional: JoystickMoveDirectional.MOVE_UP, + intensity: 1.0, + radAngle: 0.0, + isKeyboard: true, + ), + ); } if (isDownPressed(key)) { - joystickChangeDirectional(JoystickDirectionalEvent( + onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN, intensity: 1.0, radAngle: 0.0, @@ -126,7 +128,7 @@ class Keyboard extends PlayerController with KeyboardEventListener { } if (isLeftPressed(key)) { - joystickChangeDirectional(JoystickDirectionalEvent( + onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_LEFT, intensity: 1.0, radAngle: 0.0, @@ -135,7 +137,7 @@ class Keyboard extends PlayerController with KeyboardEventListener { } if (isRightPressed(key)) { - joystickChangeDirectional(JoystickDirectionalEvent( + onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_RIGHT, intensity: 1.0, radAngle: 0.0, @@ -147,7 +149,7 @@ class Keyboard extends PlayerController with KeyboardEventListener { void _sendTwoDirection(LogicalKeyboardKey key1, LogicalKeyboardKey key2) { if (isRightPressed(key1) && isDownPressed(key2) || isDownPressed(key1) && isRightPressed(key2)) { - joystickChangeDirectional(JoystickDirectionalEvent( + onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN_RIGHT, intensity: 1.0, radAngle: 0.0, @@ -157,7 +159,7 @@ class Keyboard extends PlayerController with KeyboardEventListener { if (isLeftPressed(key1) && isDownPressed(key2) || isDownPressed(key1) && isLeftPressed(key2)) { - joystickChangeDirectional(JoystickDirectionalEvent( + onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN_LEFT, intensity: 1.0, radAngle: 0.0, @@ -167,7 +169,7 @@ class Keyboard extends PlayerController with KeyboardEventListener { if (isLeftPressed(key1) && isUpPressed(key2) || isUpPressed(key1) && isLeftPressed(key2)) { - joystickChangeDirectional(JoystickDirectionalEvent( + onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_UP_LEFT, intensity: 1.0, radAngle: 0.0, @@ -177,7 +179,7 @@ class Keyboard extends PlayerController with KeyboardEventListener { if (isRightPressed(key1) && isUpPressed(key2) || isUpPressed(key1) && isRightPressed(key2)) { - joystickChangeDirectional(JoystickDirectionalEvent( + onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_UP_RIGHT, intensity: 1.0, radAngle: 0.0, diff --git a/lib/input/player_controller.dart b/lib/input/player_controller.dart index 2b12c2b8d..f37c52da0 100644 --- a/lib/input/player_controller.dart +++ b/lib/input/player_controller.dart @@ -70,7 +70,7 @@ mixin PlayerControllerListener { void onJoystickAction(JoystickActionEvent event) {} } -abstract class PlayerController extends GameComponent { +abstract class PlayerController extends GameComponent with PlayerControllerListener{ final dynamic id; final List _observers = []; @@ -80,13 +80,15 @@ abstract class PlayerController extends GameComponent { } } - void joystickChangeDirectional(JoystickDirectionalEvent event) { + @override + void onJoystickChangeDirectional(JoystickDirectionalEvent event) { for (var o in _observers) { o.onJoystickChangeDirectional(event); } } - void joystickAction(JoystickActionEvent event) { + @override + void onJoystickAction(JoystickActionEvent event) { for (var o in _observers) { o.onJoystickAction(event); } diff --git a/lib/joystick/joystick.dart b/lib/joystick/joystick.dart index f1f213dad..7406728b7 100644 --- a/lib/joystick/joystick.dart +++ b/lib/joystick/joystick.dart @@ -22,20 +22,21 @@ class Joystick extends PlayerController { } void initialize(Vector2 size) async { - directional?.initialize(size, this); + if (!hasGameRef) return; + directional?.initialize(this, gameRef.camera.viewport); for (var action in actions) { - action.initialize(size, this); + action.initialize(this, gameRef.camera.viewport); } } Future updateDirectional(JoystickDirectional? directional) async { - directional?.initialize(gameRef.size, this); + directional?.initialize(this, gameRef.camera.viewport); await directional?.onLoad(); _directional = directional; } Future addAction(JoystickAction action) async { - action.initialize(gameRef.size, this); + action.initialize(this, gameRef.camera.viewport); await action.onLoad(); actions.add(action); } @@ -104,6 +105,12 @@ class Joystick extends PlayerController { super.onGameResize(size); } + @override + void onMount() { + initialize(size); + super.onMount(); + } + @override Future onLoad() async { await super.onLoad(); diff --git a/lib/joystick/joystick_action.dart b/lib/joystick/joystick_action.dart index b99ef83a6..bac16dca9 100644 --- a/lib/joystick/joystick_action.dart +++ b/lib/joystick/joystick_action.dart @@ -1,6 +1,7 @@ import 'dart:math'; import 'package:bonfire/bonfire.dart'; +import 'package:flame/camera.dart' as camera; import 'package:flutter/material.dart'; class JoystickAction { @@ -29,7 +30,9 @@ class JoystickAction { Paint? _paintBackground; Paint? _paintAction; Paint? _paintActionPressed; - PlayerController? _joystickController; + late PlayerControllerListener _controller; + Vector2 _screenSize = Vector2.zero(); + late camera.Viewport _viewport; bool isPressed = false; AssetsLoader? _loader = AssetsLoader(); @@ -61,14 +64,24 @@ class JoystickAction { _tileSize = _sizeBackgroundDirection / 2; } - void initialize(Vector2 screenSize, PlayerController joystickController) { - _joystickController = joystickController; + Offset getViewportPosition(Offset position) { + return _viewport.globalToLocal(position.toVector2()).toOffset(); + } + + void initialize( + PlayerControllerListener controller, + camera.Viewport viewport, + ) { + if (_screenSize == viewport.virtualSize) return; + _viewport = viewport; + _screenSize = viewport.virtualSize.clone(); + _controller = controller; double radius = size / 2; final screenRect = Rect.fromLTRB( margin.left + radius, margin.top + radius, - screenSize.x - margin.right - radius, - screenSize.y - margin.bottom - radius, + _screenSize.x - margin.right - radius, + _screenSize.y - margin.bottom - radius, ); Offset osBackground = alignment.withinRect(screenRect); @@ -161,7 +174,7 @@ class JoystickAction { double intensity = dist / _tileSize; - _joystickController?.joystickAction( + _controller.onJoystickAction( JoystickActionEvent( id: actionId, event: ActionEvent.MOVE, @@ -176,13 +189,14 @@ class JoystickAction { } void actionDown(int pointer, Offset localPosition) { - if (!_dragging && _rect != null && _rect!.contains(localPosition)) { + final pos = getViewportPosition(localPosition); + if (!_dragging && _rect != null && _rect!.contains(pos)) { _pointer = pointer; if (enableDirection) { - _dragPosition = localPosition; + _dragPosition = pos; _dragging = true; } - _joystickController?.joystickAction( + _controller.onJoystickAction( JoystickActionEvent( id: actionId, event: ActionEvent.DOWN, @@ -195,7 +209,7 @@ class JoystickAction { void actionMove(int pointer, Offset localPosition) { if (pointer == _pointer) { if (_dragging) { - _dragPosition = localPosition; + _dragPosition = getViewportPosition(localPosition); } } } @@ -208,7 +222,7 @@ class JoystickAction { _dragPosition = rectBackgroundDirection.center; }); - _joystickController?.joystickAction( + _controller.onJoystickAction( JoystickActionEvent( id: actionId, event: ActionEvent.UP, diff --git a/lib/joystick/joystick_directional.dart b/lib/joystick/joystick_directional.dart index ec5d4ac6a..22bfe60ff 100644 --- a/lib/joystick/joystick_directional.dart +++ b/lib/joystick/joystick_directional.dart @@ -1,6 +1,7 @@ import 'dart:math'; import 'package:bonfire/bonfire.dart'; +import 'package:flame/camera.dart' as camera; import 'package:flutter/material.dart'; class JoystickDirectional { @@ -26,9 +27,11 @@ class JoystickDirectional { int _pointerDragging = 0; - PlayerController? _joystickController; + late PlayerControllerListener _controller; - Vector2? _screenSize; + late camera.Viewport _viewPort; + + Vector2 _screenSize = Vector2.zero(); AssetsLoader? _loader = AssetsLoader(); @@ -57,16 +60,25 @@ class JoystickDirectional { _tileSize = size / 2; } - void initialize(Vector2 screenSize, PlayerController joystickController) { - _screenSize = screenSize; - _joystickController = joystickController; + Offset getViewportPosition(Offset position) { + return _viewPort.globalToLocal(position.toVector2()).toOffset(); + } + + void initialize( + PlayerControllerListener controller, + camera.Viewport viewPort, + ) { + if (_screenSize == viewPort.virtualSize) return; + _viewPort = viewPort; + _screenSize = viewPort.virtualSize.clone(); + _controller = controller; final radius = size / 2; final screenRect = Rect.fromLTRB( margin.left + radius, margin.top + radius, - screenSize.x - margin.right - radius, - screenSize.y - margin.bottom - radius, + _screenSize.x - margin.right - radius, + _screenSize.y - margin.bottom - radius, ); Offset osBackground = alignment.withinRect(screenRect); @@ -166,7 +178,7 @@ class JoystickDirectional { double intensity = dist / _tileSize; if (intensity == 0) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.IDLE, intensity: intensity, radAngle: radAngle, @@ -175,7 +187,7 @@ class JoystickDirectional { } if (degrees > -22.5 && degrees <= 22.5) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_RIGHT, intensity: intensity, radAngle: radAngle, @@ -183,7 +195,7 @@ class JoystickDirectional { } if (enableDiagonalInput && degrees > 22.5 && degrees <= 67.5) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN_RIGHT, intensity: intensity, radAngle: radAngle, @@ -191,7 +203,7 @@ class JoystickDirectional { } if (degrees > 67.5 && degrees <= 112.5) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN, intensity: intensity, radAngle: radAngle, @@ -199,7 +211,7 @@ class JoystickDirectional { } if (enableDiagonalInput && degrees > 112.5 && degrees <= 157.5) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_DOWN_LEFT, intensity: intensity, radAngle: radAngle, @@ -208,7 +220,7 @@ class JoystickDirectional { if ((degrees > 157.5 && degrees <= 180) || (degrees >= -180 && degrees <= -157.5)) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_LEFT, intensity: intensity, radAngle: radAngle, @@ -216,7 +228,7 @@ class JoystickDirectional { } if (enableDiagonalInput && degrees > -157.5 && degrees <= -112.5) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_UP_LEFT, intensity: intensity, radAngle: radAngle, @@ -224,7 +236,7 @@ class JoystickDirectional { } if (degrees > -112.5 && degrees <= -67.5) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_UP, intensity: intensity, radAngle: radAngle, @@ -232,7 +244,7 @@ class JoystickDirectional { } if (enableDiagonalInput && degrees > -67.5 && degrees <= -22.5) { - _joystickController?.joystickChangeDirectional(JoystickDirectionalEvent( + _controller.onJoystickChangeDirectional(JoystickDirectionalEvent( directional: JoystickMoveDirectional.MOVE_UP_RIGHT, intensity: intensity, radAngle: radAngle, @@ -249,7 +261,9 @@ class JoystickDirectional { void directionalDown(int pointer, Offset localPosition) { if (_backgroundRect == null) return; - _updateDirectionalRect(localPosition); + final pos = getViewportPosition(localPosition); + + _updateDirectionalRect(pos); _backgroundRect?.let((backgroundRect) { Rect directional = Rect.fromLTWH( @@ -258,9 +272,9 @@ class JoystickDirectional { backgroundRect.width + 100, backgroundRect.height + 100, ); - if (!_dragging && directional.contains(localPosition)) { + if (!_dragging && directional.contains(pos)) { _dragging = true; - _dragPosition = localPosition; + _dragPosition = pos; _pointerDragging = pointer; } }); @@ -269,7 +283,9 @@ class JoystickDirectional { void directionalMove(int pointer, Offset localPosition) { if (pointer == _pointerDragging) { if (_dragging) { - _dragPosition = localPosition; + _dragPosition = getViewportPosition( + localPosition, + ); } } } @@ -278,7 +294,7 @@ class JoystickDirectional { if (pointer == _pointerDragging) { _dragging = false; _dragPosition = _backgroundRect?.center; - _joystickController?.joystickChangeDirectional( + _controller.onJoystickChangeDirectional( JoystickDirectionalEvent( directional: JoystickMoveDirectional.IDLE, intensity: 0.0, @@ -289,15 +305,15 @@ class JoystickDirectional { } void _updateDirectionalRect(Offset position) { - if (isFixed || _screenSize == null) return; + if (isFixed) return; if (alignment.x == -1) { - if (position.dx > _screenSize!.x * 0.33) { + if (position.dx > _screenSize.x * 0.33) { return; } } if (alignment.x == 1) { - if (position.dx < _screenSize!.x * 0.66) { + if (position.dx < _screenSize.x * 0.66) { return; } } From df2695dabb7f9199353053d0eea99afa087c59ac Mon Sep 17 00:00:00 2001 From: Rafael Date: Sun, 9 Jun 2024 02:04:58 -0300 Subject: [PATCH 4/8] increment version --- CHANGELOG.md | 2 +- pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14049e4ff..df83ff855 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# Next +# 3.9.5 - Fix Joystick bug when viewport is fixed resolution. [#526](https://github.com/RafaelBarbosatec/bonfire/issues/526) diff --git a/pubspec.yaml b/pubspec.yaml index 48b5646ef..90224cb90 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: bonfire description: (RPG maker) Create RPG-style or similar games more simply with Flame. -version: 3.9.4 +version: 3.9.5 homepage: https://bonfire-engine.github.io repository: https://github.com/RafaelBarbosatec/bonfire issue_tracker: https://github.com/RafaelBarbosatec/bonfire/issues From dcab42641011c46092313a93a9ccf5994c2734d1 Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Sun, 9 Jun 2024 19:48:17 -0300 Subject: [PATCH 5/8] fix bug viewport resolution fixed --- lib/input/gestures/drag_gesture.dart | 4 ++++ lib/input/gestures/gesture_event.dart | 3 ++- lib/input/gestures/pinch_gesture.dart | 2 ++ lib/input/gestures/tap_gesture.dart | 2 ++ lib/lighting/lighting_component.dart | 2 +- lib/util/extensions/game_component_extensions.dart | 14 ++++++++++++++ lib/util/follower_widget.dart | 12 ++++++++++-- 7 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/input/gestures/drag_gesture.dart b/lib/input/gestures/drag_gesture.dart index 2f6067951..570374314 100644 --- a/lib/input/gestures/drag_gesture.dart +++ b/lib/input/gestures/drag_gesture.dart @@ -17,6 +17,7 @@ mixin DragGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, + globalToViewportPosition: globalToViewportPosition, ); bool handler = false; @@ -46,6 +47,7 @@ mixin DragGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, + globalToViewportPosition: globalToViewportPosition, ); bool canMove = hasGameRef && _startDragPosition != null && @@ -79,6 +81,7 @@ mixin DragGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, + globalToViewportPosition: globalToViewportPosition, ); if (gEvent.pointer == _pointer && _inMoving) { _startDragPosition = null; @@ -96,6 +99,7 @@ mixin DragGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, + globalToViewportPosition: globalToViewportPosition, ); if (gEvent.pointer == _pointer && _inMoving) { _startDragPosition = null; diff --git a/lib/input/gestures/gesture_event.dart b/lib/input/gestures/gesture_event.dart index a47d96613..eeb3fe078 100644 --- a/lib/input/gestures/gesture_event.dart +++ b/lib/input/gestures/gesture_event.dart @@ -19,8 +19,9 @@ class GestureEvent { factory GestureEvent.fromPointerEvent( PointerEvent event, { required Vector2 Function(Vector2 position) screenToWorld, + required Offset Function(Offset position) globalToViewportPosition, }) { - final position = event.localPosition.toVector2(); + final position = globalToViewportPosition(event.localPosition).toVector2(); return GestureEvent( pointer: event.pointer, kind: event.kind, diff --git a/lib/input/gestures/pinch_gesture.dart b/lib/input/gestures/pinch_gesture.dart index 464c61103..d25f436d5 100644 --- a/lib/input/gestures/pinch_gesture.dart +++ b/lib/input/gestures/pinch_gesture.dart @@ -25,6 +25,7 @@ mixin PinchGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, + globalToViewportPosition: globalToViewportPosition, ); if (!_fingers.contains(event.pointer)) { _fingers.add( @@ -62,6 +63,7 @@ mixin PinchGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, + globalToViewportPosition: globalToViewportPosition, ); _updateFingers(gEvent); _handleMove(); diff --git a/lib/input/gestures/tap_gesture.dart b/lib/input/gestures/tap_gesture.dart index 717f602e4..93d161da0 100644 --- a/lib/input/gestures/tap_gesture.dart +++ b/lib/input/gestures/tap_gesture.dart @@ -9,6 +9,7 @@ mixin TapGesture on GameComponent { final tapEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, + globalToViewportPosition: globalToViewportPosition, ); bool handler = false; @@ -34,6 +35,7 @@ mixin TapGesture on GameComponent { final tapEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, + globalToViewportPosition: globalToViewportPosition, ); if (enableTab && tapEvent.pointer == _pointer && hasGameRef) { diff --git a/lib/lighting/lighting_component.dart b/lib/lighting/lighting_component.dart index ce6745038..572bfd89c 100644 --- a/lib/lighting/lighting_component.dart +++ b/lib/lighting/lighting_component.dart @@ -187,7 +187,7 @@ class LightingComponent extends GameComponent implements LightingInterface { @override void onGameResize(Vector2 size) { - bounds = Rect.fromLTWH(left, top, size.x, size.y); + bounds = gameRef.camera.viewport.virtualSize.toRect(); super.onGameResize(size); } } diff --git a/lib/util/extensions/game_component_extensions.dart b/lib/util/extensions/game_component_extensions.dart index 8bb532adb..1a0d47f4c 100644 --- a/lib/util/extensions/game_component_extensions.dart +++ b/lib/util/extensions/game_component_extensions.dart @@ -415,4 +415,18 @@ extension GameComponentExtensions on GameComponent { comp.translate(displacement); } } + + Offset globalToViewportPosition(Offset position) { + if (!hasGameRef) return position; + return gameRef.camera.viewport + .globalToLocal(position.toVector2()) + .toOffset(); + } + + Offset viewportPositionToGlobal(Offset position) { + if (!hasGameRef) return position; + return gameRef.camera.viewport + .localToGlobal(position.toVector2()) + .toOffset(); + } } diff --git a/lib/util/follower_widget.dart b/lib/util/follower_widget.dart index 86d41000b..8a429b839 100644 --- a/lib/util/follower_widget.dart +++ b/lib/util/follower_widget.dart @@ -86,6 +86,7 @@ class FollowerWidget extends StatefulWidget { class FollowerWidgetState extends State { Vector2 targetPosition = Vector2.zero(); Vector2? widgetPosition; + Vector2 gameSize = Vector2.zero(); double lastZoom = 0.0; Vector2 lastCameraPosition = Vector2.zero(); late BonfireCamera camera; @@ -128,17 +129,24 @@ class FollowerWidgetState extends State { void _positionListener() { camera = widget.target.gameRef.camera; final absolutePosition = widget.target.absolutePosition; + if (targetPosition != absolutePosition || camera.zoom != lastZoom || - camera.position != lastCameraPosition) { + camera.position != lastCameraPosition || + camera.canvasSize != gameSize) { + gameSize = camera.canvasSize.clone(); lastZoom = camera.zoom; + targetPosition = absolutePosition.clone(); lastCameraPosition = camera.position.clone(); if (mounted) { setState(() { - widgetPosition = widget.target.gameRef.worldToScreen( + final globalPosition = widget.target.gameRef.worldToScreen( targetPosition, ); + widgetPosition = widget.target + .viewportPositionToGlobal(globalPosition.toOffset()) + .toVector2(); }); } } From d95428ec63f30711d22013eaec4f694be656ac0c Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Sun, 9 Jun 2024 19:52:20 -0300 Subject: [PATCH 6/8] format --- lib/input/player_controller.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/input/player_controller.dart b/lib/input/player_controller.dart index f37c52da0..e6ce4967c 100644 --- a/lib/input/player_controller.dart +++ b/lib/input/player_controller.dart @@ -70,7 +70,8 @@ mixin PlayerControllerListener { void onJoystickAction(JoystickActionEvent event) {} } -abstract class PlayerController extends GameComponent with PlayerControllerListener{ +abstract class PlayerController extends GameComponent + with PlayerControllerListener { final dynamic id; final List _observers = []; From 1d55a18206ca3e6b2cb6e283b77c190a747ce6fd Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 10 Jun 2024 09:39:10 -0300 Subject: [PATCH 7/8] viewport improvements --- lib/base/bonfire_game.dart | 20 +++++++++++++++++-- lib/base/bonfire_game_interface.dart | 6 ++++++ lib/input/gestures/drag_gesture.dart | 8 ++++---- lib/input/gestures/gesture_event.dart | 9 +++++---- lib/input/gestures/pinch_gesture.dart | 4 ++-- lib/input/gestures/tap_gesture.dart | 4 ++-- .../extensions/game_component_extensions.dart | 8 ++------ lib/util/extensions/viewport_extension.dart | 13 ++++++++++++ lib/util/follower_widget.dart | 8 +++----- 9 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 lib/util/extensions/viewport_extension.dart diff --git a/lib/base/bonfire_game.dart b/lib/base/bonfire_game.dart index cc728b3a7..3459de17b 100644 --- a/lib/base/bonfire_game.dart +++ b/lib/base/bonfire_game.dart @@ -258,12 +258,28 @@ class BonfireGame extends BaseGame implements BonfireGameInterface { @override Vector2 worldToScreen(Vector2 position) { - return camera.worldToScreen(position); + final worldPosition = camera.worldToScreen(position); + return viewportPositionToGlobal( + worldPosition, + ); } @override Vector2 screenToWorld(Vector2 position) { - return camera.screenToWorld(position); + final viewportPosition = globalToViewportPosition( + position, + ); + return camera.screenToWorld(viewportPosition); + } + + @override + Vector2 globalToViewportPosition(Vector2 position) { + return camera.viewport.globalToLocal(position); + } + + @override + Vector2 viewportPositionToGlobal(Vector2 position) { + return camera.viewport.localToGlobal(position); } @override diff --git a/lib/base/bonfire_game_interface.dart b/lib/base/bonfire_game_interface.dart index 8fe2b8473..e6fdad960 100644 --- a/lib/base/bonfire_game_interface.dart +++ b/lib/base/bonfire_game_interface.dart @@ -102,6 +102,12 @@ abstract class BonfireGameInterface { /// This method convert screen position to word position Vector2 screenToWorld(Vector2 screenPosition); + /// This method convert viewport position to word position + Vector2 globalToViewportPosition(Vector2 position); + + /// This method convert viewport position to screen position + Vector2 viewportPositionToGlobal(Vector2 position); + /// Used to check if a component is visible in the camera. bool isVisibleInCamera(GameComponent c); diff --git a/lib/input/gestures/drag_gesture.dart b/lib/input/gestures/drag_gesture.dart index 570374314..fa4e059f8 100644 --- a/lib/input/gestures/drag_gesture.dart +++ b/lib/input/gestures/drag_gesture.dart @@ -17,7 +17,7 @@ mixin DragGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, - globalToViewportPosition: globalToViewportPosition, + globalToViewportPosition: gameRef.globalToViewportPosition, ); bool handler = false; @@ -47,7 +47,7 @@ mixin DragGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, - globalToViewportPosition: globalToViewportPosition, + globalToViewportPosition: gameRef.globalToViewportPosition, ); bool canMove = hasGameRef && _startDragPosition != null && @@ -81,7 +81,7 @@ mixin DragGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, - globalToViewportPosition: globalToViewportPosition, + globalToViewportPosition: gameRef.globalToViewportPosition, ); if (gEvent.pointer == _pointer && _inMoving) { _startDragPosition = null; @@ -99,7 +99,7 @@ mixin DragGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, - globalToViewportPosition: globalToViewportPosition, + globalToViewportPosition: gameRef.globalToViewportPosition, ); if (gEvent.pointer == _pointer && _inMoving) { _startDragPosition = null; diff --git a/lib/input/gestures/gesture_event.dart b/lib/input/gestures/gesture_event.dart index eeb3fe078..432b124a6 100644 --- a/lib/input/gestures/gesture_event.dart +++ b/lib/input/gestures/gesture_event.dart @@ -19,14 +19,15 @@ class GestureEvent { factory GestureEvent.fromPointerEvent( PointerEvent event, { required Vector2 Function(Vector2 position) screenToWorld, - required Offset Function(Offset position) globalToViewportPosition, + required Vector2 Function(Vector2 position) globalToViewportPosition, }) { - final position = globalToViewportPosition(event.localPosition).toVector2(); + final eventPosition = event.localPosition.toVector2(); + final screenPosition = globalToViewportPosition(eventPosition); return GestureEvent( pointer: event.pointer, kind: event.kind, - screenPosition: position, - worldPosition: screenToWorld(position), + screenPosition: screenPosition, + worldPosition: screenToWorld(eventPosition), ); } } diff --git a/lib/input/gestures/pinch_gesture.dart b/lib/input/gestures/pinch_gesture.dart index d25f436d5..ae120409f 100644 --- a/lib/input/gestures/pinch_gesture.dart +++ b/lib/input/gestures/pinch_gesture.dart @@ -25,7 +25,7 @@ mixin PinchGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, - globalToViewportPosition: globalToViewportPosition, + globalToViewportPosition: gameRef.globalToViewportPosition, ); if (!_fingers.contains(event.pointer)) { _fingers.add( @@ -63,7 +63,7 @@ mixin PinchGesture on GameComponent { final gEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, - globalToViewportPosition: globalToViewportPosition, + globalToViewportPosition: gameRef.globalToViewportPosition, ); _updateFingers(gEvent); _handleMove(); diff --git a/lib/input/gestures/tap_gesture.dart b/lib/input/gestures/tap_gesture.dart index 93d161da0..abc4a2af6 100644 --- a/lib/input/gestures/tap_gesture.dart +++ b/lib/input/gestures/tap_gesture.dart @@ -9,7 +9,7 @@ mixin TapGesture on GameComponent { final tapEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, - globalToViewportPosition: globalToViewportPosition, + globalToViewportPosition: gameRef.globalToViewportPosition, ); bool handler = false; @@ -35,7 +35,7 @@ mixin TapGesture on GameComponent { final tapEvent = GestureEvent.fromPointerEvent( event, screenToWorld: gameRef.screenToWorld, - globalToViewportPosition: globalToViewportPosition, + globalToViewportPosition: gameRef.globalToViewportPosition, ); if (enableTab && tapEvent.pointer == _pointer && hasGameRef) { diff --git a/lib/util/extensions/game_component_extensions.dart b/lib/util/extensions/game_component_extensions.dart index 1a0d47f4c..4c0a9cc61 100644 --- a/lib/util/extensions/game_component_extensions.dart +++ b/lib/util/extensions/game_component_extensions.dart @@ -418,15 +418,11 @@ extension GameComponentExtensions on GameComponent { Offset globalToViewportPosition(Offset position) { if (!hasGameRef) return position; - return gameRef.camera.viewport - .globalToLocal(position.toVector2()) - .toOffset(); + return gameRef.globalToViewportPosition(position.toVector2()).toOffset(); } Offset viewportPositionToGlobal(Offset position) { if (!hasGameRef) return position; - return gameRef.camera.viewport - .localToGlobal(position.toVector2()) - .toOffset(); + return gameRef.viewportPositionToGlobal(position.toVector2()).toOffset(); } } diff --git a/lib/util/extensions/viewport_extension.dart b/lib/util/extensions/viewport_extension.dart new file mode 100644 index 000000000..228b5bd74 --- /dev/null +++ b/lib/util/extensions/viewport_extension.dart @@ -0,0 +1,13 @@ +import 'package:bonfire/bonfire.dart'; +import 'package:flame/camera.dart'; + +extension ViewportExt on Viewport { + double get scale { + if (this is FixedResolutionViewport) { + return (this as FixedResolutionViewport).scale.maxValue(); + } else if (this is FixedAspectRatioViewport) { + return (this as FixedAspectRatioViewport).scale; + } + return 1.0; + } +} diff --git a/lib/util/follower_widget.dart b/lib/util/follower_widget.dart index 8a429b839..da5e37ac8 100644 --- a/lib/util/follower_widget.dart +++ b/lib/util/follower_widget.dart @@ -2,6 +2,7 @@ import 'dart:async' as async; import 'package:bonfire/bonfire.dart'; import 'package:bonfire/camera/bonfire_camera.dart'; +import 'package:bonfire/util/extensions/viewport_extension.dart'; import 'package:flutter/widgets.dart'; /// @@ -135,18 +136,15 @@ class FollowerWidgetState extends State { camera.position != lastCameraPosition || camera.canvasSize != gameSize) { gameSize = camera.canvasSize.clone(); - lastZoom = camera.zoom; + lastZoom = camera.zoom * camera.viewport.scale; targetPosition = absolutePosition.clone(); lastCameraPosition = camera.position.clone(); if (mounted) { setState(() { - final globalPosition = widget.target.gameRef.worldToScreen( + widgetPosition = widget.target.gameRef.worldToScreen( targetPosition, ); - widgetPosition = widget.target - .viewportPositionToGlobal(globalPosition.toOffset()) - .toVector2(); }); } } From 4860bb4f366bf458aa1dcbe79282b0bb952e3851 Mon Sep 17 00:00:00 2001 From: Rafael Almeida Barbosa Date: Mon, 10 Jun 2024 09:46:34 -0300 Subject: [PATCH 8/8] code improvement --- lib/base/bonfire_game.dart | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/base/bonfire_game.dart b/lib/base/bonfire_game.dart index 3459de17b..19a25008f 100644 --- a/lib/base/bonfire_game.dart +++ b/lib/base/bonfire_game.dart @@ -8,8 +8,7 @@ import 'package:bonfire/camera/bonfire_camera.dart'; import 'package:bonfire/color_filter/color_filter_component.dart'; import 'package:bonfire/joystick/joystick_map_explorer.dart'; import 'package:bonfire/lighting/lighting_component.dart'; -// ignore: implementation_imports -import 'package:flame/src/camera/viewports/fixed_resolution_viewport.dart'; +import 'package:flame/camera.dart'; // ignore: implementation_imports import 'package:flutter/widgets.dart'; @@ -117,9 +116,7 @@ class BonfireGame extends BaseGame implements BonfireGameInterface { super( camera: BonfireCamera( config: cameraConfig, - viewport: cameraConfig?.resolution != null - ? FixedResolutionViewport(resolution: cameraConfig!.resolution!) - : null, + viewport: _getViewPort(cameraConfig), backdrop: background, hudComponents: [ LightingComponent( @@ -513,4 +510,13 @@ class BonfireGame extends BaseGame implements BonfireGameInterface { FutureOr addHud(Component component) { return camera.viewport.add(component); } + + static _getViewPort(CameraConfig? cameraConfig) { + if (cameraConfig?.resolution != null) { + return FixedResolutionViewport( + resolution: cameraConfig!.resolution!, + ); + } + return null; + } }