mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-10-31 17:06:50 +08:00 
			
		
		
		
	 63994ebcd8
			
		
	
	63994ebcd8
	
	
	
		
			
			This adds `DisplacementEvent` to fix delta coordinate transformations
for drag events, to be used instead of `PositionEvent`.
Drag Events now expose the start and end position, as well as the delta,
correctly transformed by the camera and zoom.
This also ensures that drag events, once starts, do not get lost if the
drag update leaves the component bounds.
* if you are using `DragUpdateEvent` events, the `devicePosition`,
`canvasPosition`, `localPosition`, and `delta` are deprecated as they
are unclear.
* use `xStartPosition` to get the position at the start of the drag
event ("from")
* use `xEndPosition` to get the position at the end of the drag event
("to")
* if you want the delta, use `localDelta`. it now already considers the
camera zoom. no need to manually account for that
* now you keep receiving drag events for the same component even if the
mouse leaves the component (breaking)
---------
Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
		
	
		
			
				
	
	
		
			45 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:examples/commons/ember.dart';
 | |
| import 'package:flame/events.dart';
 | |
| import 'package:flame/game.dart';
 | |
| import 'package:flutter/material.dart' show Colors;
 | |
| 
 | |
| class DragCallbacksExample extends FlameGame {
 | |
|   static const String description = '''
 | |
|     In this example we show you can use the `DragCallbacks` mixin on
 | |
|     `PositionComponent`s. Drag around the Embers and see their position
 | |
|     changing.
 | |
|   ''';
 | |
| 
 | |
|   DragCallbacksExample({required this.zoom});
 | |
| 
 | |
|   final double zoom;
 | |
|   late final DraggableEmber square;
 | |
| 
 | |
|   @override
 | |
|   Future<void> onLoad() async {
 | |
|     camera.viewfinder.zoom = zoom;
 | |
|     world.add(square = DraggableEmber());
 | |
|     world.add(DraggableEmber()..y = 350);
 | |
|   }
 | |
| }
 | |
| 
 | |
| // Note: this component does not consider the possibility of multiple
 | |
| // simultaneous drags with different pointerIds.
 | |
| class DraggableEmber extends Ember with DragCallbacks {
 | |
|   @override
 | |
|   bool debugMode = true;
 | |
| 
 | |
|   DraggableEmber({super.position}) : super(size: Vector2.all(100));
 | |
| 
 | |
|   @override
 | |
|   void update(double dt) {
 | |
|     super.update(dt);
 | |
|     debugColor = isDragged ? Colors.greenAccent : Colors.purple;
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void onDragUpdate(DragUpdateEvent event) {
 | |
|     position += event.localDelta;
 | |
|   }
 | |
| }
 |