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.
		
	
		
			
				
	
	
		
			74 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'package:flame/experimental.dart';
 | |
| import 'package:flame/game.dart';
 | |
| import 'package:flame_rive/flame_rive.dart';
 | |
| import 'package:flutter/material.dart';
 | |
| 
 | |
| void main() {
 | |
|   runApp(const MyApp());
 | |
| }
 | |
| 
 | |
| class MyApp extends StatefulWidget {
 | |
|   const MyApp({super.key});
 | |
| 
 | |
|   @override
 | |
|   State<MyApp> createState() => _MyAppState();
 | |
| }
 | |
| 
 | |
| class _MyAppState extends State<MyApp> {
 | |
|   late final game = RiveExampleGame();
 | |
| 
 | |
|   @override
 | |
|   Widget build(BuildContext context) {
 | |
|     return GameWidget(
 | |
|       game: game,
 | |
|     );
 | |
|   }
 | |
| }
 | |
| 
 | |
| class RiveExampleGame extends FlameGame {
 | |
|   @override
 | |
|   Color backgroundColor() {
 | |
|     return const Color(0xFFFFFFFF);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   Future<void> onLoad() async {
 | |
|     final skillsArtboard =
 | |
|         await loadArtboard(RiveFile.asset('assets/skills.riv'));
 | |
|     add(SkillsAnimationComponent(skillsArtboard));
 | |
|   }
 | |
| }
 | |
| 
 | |
| class SkillsAnimationComponent extends RiveComponent with TapCallbacks {
 | |
|   SkillsAnimationComponent(Artboard artboard)
 | |
|       : super(
 | |
|           artboard: artboard,
 | |
|           size: Vector2.all(550),
 | |
|         );
 | |
| 
 | |
|   SMIInput<double>? _levelInput;
 | |
| 
 | |
|   @override
 | |
|   void onLoad() {
 | |
|     final controller = StateMachineController.fromArtboard(
 | |
|       artboard,
 | |
|       "Designer's Test",
 | |
|     );
 | |
|     if (controller != null) {
 | |
|       artboard.addController(controller);
 | |
|       _levelInput = controller.findInput<double>('Level');
 | |
|       _levelInput?.value = 0;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void onTapDown(TapDownEvent info) {
 | |
|     final levelInput = _levelInput;
 | |
|     if (levelInput == null) {
 | |
|       return;
 | |
|     }
 | |
|     levelInput.value = (levelInput.value + 1) % 3;
 | |
|     info.continuePropagation = true;
 | |
|   }
 | |
| }
 |