mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-01 01:18:38 +08:00 
			
		
		
		
	 b5bdf4ec17
			
		
	
	b5bdf4ec17
	
	
	
		
			
			This PR is the second in a series of refactors that aim to simplify event handling in Flame. The approach is as follows:
    Added the MultiTapDispatcher component, which contains the logic that used to be within the HasTappableComponents mixin. This component is internal; it mounts to a FlameGame directly, and ensures that it is a singleton.
    Whenever any TapCallbacks component is added to a game, it automatically adds the MultiTapDispatcher component (unless there is already one), which in turn registers a tap gesture detector with GestureDetectorBuilder and rebuilds the game widget.
The end result is that now in order to make a component tappable you only need to add the TapCallbacks mixin to that component, everything else will be handled by the framework.
Consequently, the HasTappableComponents mixin is now empty and marked as deprecated.
		
	
		
			
				
	
	
		
			40 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:flame/components.dart';
 | |
| import 'package:flame/experimental.dart';
 | |
| import 'package:flame/extensions.dart';
 | |
| import 'package:flame/game.dart';
 | |
| 
 | |
| class PriorityExample extends FlameGame {
 | |
|   static const String description = '''
 | |
|     On this example, click on the square to bring them to the front by changing
 | |
|     the priority.
 | |
|   ''';
 | |
| 
 | |
|   PriorityExample()
 | |
|       : super(
 | |
|           children: [
 | |
|             Square(Vector2(100, 100)),
 | |
|             Square(Vector2(160, 100)),
 | |
|             Square(Vector2(170, 150)),
 | |
|             Square(Vector2(110, 150)),
 | |
|           ],
 | |
|         );
 | |
| }
 | |
| 
 | |
| class Square extends RectangleComponent
 | |
|     with HasGameRef<PriorityExample>, TapCallbacks {
 | |
|   Square(Vector2 position)
 | |
|       : super(
 | |
|           position: position,
 | |
|           size: Vector2.all(100),
 | |
|           paint: PaintExtension.random(withAlpha: 0.9, base: 100),
 | |
|         );
 | |
| 
 | |
|   @override
 | |
|   void onTapDown(TapDownEvent info) {
 | |
|     final topComponent = gameRef.children.last;
 | |
|     if (topComponent != this) {
 | |
|       priority = topComponent.priority + 1;
 | |
|     }
 | |
|   }
 | |
| }
 |