mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-10-30 16:36:57 +08:00 
			
		
		
		
	feat!: The HasTappableComponents mixin is no longer needed (#2450)
				
					
				
			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.
			
			
This commit is contained in:
		| @ -2,16 +2,15 @@ import 'dart:math'; | ||||
|  | ||||
| import 'package:flame/collisions.dart'; | ||||
| import 'package:flame/components.dart'; | ||||
| import 'package:flame/experimental.dart'; | ||||
| import 'package:flame/extensions.dart'; | ||||
| import 'package:flame/game.dart'; | ||||
| import 'package:flame/input.dart'; | ||||
| import 'package:flame/palette.dart'; | ||||
| import 'package:flutter/material.dart' hide Image, Draggable; | ||||
|  | ||||
| enum Shapes { circle, rectangle, polygon } | ||||
|  | ||||
| class MultipleShapesExample extends FlameGame | ||||
|     with HasCollisionDetection, HasDraggables { | ||||
| class MultipleShapesExample extends FlameGame with HasCollisionDetection { | ||||
|   static const description = ''' | ||||
|     An example with many hitboxes that move around on the screen and during | ||||
|     collisions they change color depending on what it is that they have collided | ||||
| @ -81,7 +80,7 @@ class MultipleShapesExample extends FlameGame | ||||
| } | ||||
|  | ||||
| abstract class MyCollidable extends PositionComponent | ||||
|     with Draggable, CollisionCallbacks, GestureHitboxes { | ||||
|     with DragCallbacks, CollisionCallbacks, GestureHitboxes { | ||||
|   double rotationSpeed = 0.0; | ||||
|   final Vector2 velocity; | ||||
|   final delta = Vector2.zero(); | ||||
| @ -91,6 +90,7 @@ abstract class MyCollidable extends PositionComponent | ||||
|   late final Paint _dragIndicatorPaint; | ||||
|   final ScreenHitbox screenHitbox; | ||||
|   ShapeHitbox? hitbox; | ||||
|   bool isDragged = false; | ||||
|  | ||||
|   MyCollidable( | ||||
|     Vector2 position, | ||||
| @ -154,9 +154,14 @@ abstract class MyCollidable extends PositionComponent | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   bool onDragEnd(DragEndInfo info) { | ||||
|   void onDragStart(DragStartEvent info) { | ||||
|     isDragged = true; | ||||
|   } | ||||
|  | ||||
|   @override | ||||
|   void onDragEnd(DragEndEvent info) { | ||||
|     velocity.setFrom(info.velocity / 10); | ||||
|     return true; | ||||
|     isDragged = false; | ||||
|   } | ||||
| } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Lukas Klingsbo
					Lukas Klingsbo