Files
flame/examples/lib/stories/input/draggables_example.dart
Lukas Klingsbo 5c47d7f6d7 chore: analyze issues introduced from new dart version (#1196)
* Added Component.childrenFactory

* fix some of the lint warnings

* more lint warnings

* remove changelog entry

* more analyzer warnings

* one more warning

* one more warning

* remove more unused imports

* fix more warnings

* another warning

* one more warning

* a lot more warnings

* some more warnings

* fix warnings in flame_svg

* fix warnings in flame_bloc

* Remove OrderedSet override feature

* Remove testRandom change

* Remove unnecessary type checks

* Re-remove deprecated argument in random_test

Co-authored-by: Pasha Stetsenko <stpasha@google.com>
2021-12-09 15:40:43 +01:00

83 lines
2.0 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flutter/material.dart' show Colors;
import '../../commons/ember.dart';
class DraggablesExample extends FlameGame with HasDraggables {
static const String description = '''
In this example we show you can use the `Draggable` mixin on
`PositionComponent`s. Drag around the Embers and see their position
changing.
''';
final double zoom;
late final DraggableSquare square;
DraggablesExample({required this.zoom});
@override
Future<void> onLoad() async {
await super.onLoad();
camera.zoom = zoom;
add(square = DraggableSquare());
add(DraggableSquare()..y = 350);
}
}
// Note: this component does not consider the possibility of multiple
// simultaneous drags with different pointerIds.
class DraggableSquare extends Ember with Draggable {
@override
bool debugMode = true;
DraggableSquare({Vector2? position})
: super(
position: position ?? Vector2.all(100),
size: Vector2.all(100),
);
Vector2? dragDeltaPosition;
@override
void update(double dt) {
super.update(dt);
debugColor = isDragged && parent is DraggablesExample
? 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) {
if (parent is! DraggablesExample) {
return true;
}
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;
}
}