mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
* some initial refactoring * Fixing examples and joystick component * Fixing examples * Addressing comments from PR * Fixing tutorials * Updating docs * Fixing tests * PR followup * A big follow up * linting * doc nit * Changelog and fix tutorial * Addressing comments * Fixing BaseGame project and scale offset methods * Formatting * doc suggestions * Update packages/flame/lib/src/gestures/events.dart Co-authored-by: Luan Nico <luanpotter27@gmail.com> * Hopefully, the last follow up * Linting and adding dart-code-metrics again * fixing tutorial Co-authored-by: Luan Nico <luanpotter27@gmail.com>
66 lines
1.6 KiB
Dart
66 lines
1.6 KiB
Dart
import 'package:flame/components.dart';
|
|
import 'package:flame/extensions.dart';
|
|
import 'package:flame/game.dart';
|
|
import 'package:flame/gestures.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, Vector2 startPosition) {
|
|
dragDeltaPosition = startPosition - position;
|
|
return false;
|
|
}
|
|
|
|
@override
|
|
bool onDragUpdate(int pointerId, DragUpdateInfo event) {
|
|
final dragDeltaPosition = this.dragDeltaPosition;
|
|
if (dragDeltaPosition == null) {
|
|
return false;
|
|
}
|
|
|
|
position.setFrom(event.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 {
|
|
@override
|
|
Future<void> onLoad() async {
|
|
add(DraggableSquare());
|
|
add(DraggableSquare()..y = 350);
|
|
}
|
|
}
|