Refactor joystick (#876)

* Refactor joystick

* Fix directional tests

* Joystick example

* Any PositionComponent can be used as knob and background

* Add MarginButtonComponent

* Fix JoystickExample

* Update joystick docs

* Fix joystick direction tests

* Fix effect tests

* Fix analyze issue

* Update docs

* Update docs

* Move joystick to input export

* Update packages/flame/lib/src/geometry/shape.dart

Co-authored-by: Luan Nico <luanpotter27@gmail.com>

* Add test and description for screenAngle

* Update examples/lib/stories/controls/joystick_player.dart

Co-authored-by: Erick <erickzanardoo@gmail.com>

* Update doc/input.md

Co-authored-by: Erick <erickzanardoo@gmail.com>

* controls -> input in examples to align with export file

* controls -> input

* Add simple joystick example

* Fix imports

* velocity -> relativeDelta

Co-authored-by: Luan Nico <luanpotter27@gmail.com>
Co-authored-by: Erick <erickzanardoo@gmail.com>
This commit is contained in:
Lukas Klingsbo
2021-07-15 12:00:41 +02:00
committed by GitHub
parent 750f63ed97
commit 64a40ff641
83 changed files with 797 additions and 1005 deletions

View File

@ -0,0 +1,70 @@
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart' show Colors;
// Note: this component does not consider the possibility of multiple
// simultaneous drags with different pointerIds.
class DraggableSquare extends PositionComponent
with Draggable, HasGameRef<DraggablesGame> {
@override
bool debugMode = true;
DraggableSquare({Vector2? position})
: super(
position: position ?? Vector2.all(100),
size: Vector2.all(100),
);
Vector2? dragDeltaPosition;
bool get isDragging => dragDeltaPosition != null;
@override
void update(double dt) {
super.update(dt);
debugColor = isDragging ? Colors.greenAccent : Colors.purple;
}
@override
bool onDragStart(int pointerId, DragStartInfo info) {
dragDeltaPosition = info.eventPosition.game - position;
return false;
}
@override
bool onDragUpdate(int pointerId, DragUpdateInfo info) {
final dragDeltaPosition = this.dragDeltaPosition;
if (dragDeltaPosition == null) {
return false;
}
position.setFrom(info.eventPosition.game - dragDeltaPosition);
return false;
}
@override
bool onDragEnd(int pointerId, _) {
dragDeltaPosition = null;
return false;
}
@override
bool onDragCancel(int pointerId) {
dragDeltaPosition = null;
return false;
}
}
class DraggablesGame extends BaseGame with HasDraggableComponents {
final double zoom;
DraggablesGame({required this.zoom});
@override
Future<void> onLoad() async {
camera.zoom = zoom;
add(DraggableSquare());
add(DraggableSquare()..y = 350);
}
}