mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-10-31 08:56:01 +08:00 
			
		
		
		
	fix: Set margins of JoystickComponent properly (#3019)
				
					
				
			The margins of the `JoystickComponent` weren't set properly, this PR fixes that.
This commit is contained in:
		| @ -20,6 +20,11 @@ class JoystickAdvancedExample extends FlameGame with HasCollisionDetection { | |||||||
|     the buttons. |     the buttons. | ||||||
|   '''; |   '''; | ||||||
|  |  | ||||||
|  |   JoystickAdvancedExample() | ||||||
|  |       : super( | ||||||
|  |           camera: CameraComponent.withFixedResolution(width: 1200, height: 800), | ||||||
|  |         ); | ||||||
|  |  | ||||||
|   late final JoystickPlayer player; |   late final JoystickPlayer player; | ||||||
|   late final JoystickComponent joystick; |   late final JoystickComponent joystick; | ||||||
|   late final TextComponent speedText; |   late final TextComponent speedText; | ||||||
|  | |||||||
| @ -2,8 +2,7 @@ import 'dart:math'; | |||||||
|  |  | ||||||
| import 'package:flame/components.dart'; | import 'package:flame/components.dart'; | ||||||
| import 'package:flame/events.dart'; | import 'package:flame/events.dart'; | ||||||
| import 'package:flame/src/components/input/hud_margin_component.dart'; | import 'package:flutter/widgets.dart'; | ||||||
| import 'package:meta/meta.dart'; |  | ||||||
|  |  | ||||||
| enum JoystickDirection { | enum JoystickDirection { | ||||||
|   up, |   up, | ||||||
| @ -17,7 +16,8 @@ enum JoystickDirection { | |||||||
|   idle, |   idle, | ||||||
| } | } | ||||||
|  |  | ||||||
| class JoystickComponent extends HudMarginComponent with DragCallbacks { | class JoystickComponent extends PositionComponent | ||||||
|  |     with HasGameReference, ComponentViewportMargin, DragCallbacks { | ||||||
|   late final PositionComponent? knob; |   late final PositionComponent? knob; | ||||||
|   late final PositionComponent? background; |   late final PositionComponent? background; | ||||||
|  |  | ||||||
| @ -46,8 +46,8 @@ class JoystickComponent extends HudMarginComponent with DragCallbacks { | |||||||
|   JoystickComponent({ |   JoystickComponent({ | ||||||
|     this.knob, |     this.knob, | ||||||
|     this.background, |     this.background, | ||||||
|     super.margin, |  | ||||||
|     super.position, |     super.position, | ||||||
|  |     EdgeInsets? margin, | ||||||
|     double? size, |     double? size, | ||||||
|     double? knobRadius, |     double? knobRadius, | ||||||
|     Anchor super.anchor = Anchor.center, |     Anchor super.anchor = Anchor.center, | ||||||
| @ -66,6 +66,7 @@ class JoystickComponent extends HudMarginComponent with DragCallbacks { | |||||||
|         super( |         super( | ||||||
|           size: background?.size ?? Vector2.all(size ?? 0), |           size: background?.size ?? Vector2.all(size ?? 0), | ||||||
|         ) { |         ) { | ||||||
|  |     this.margin = margin; | ||||||
|     this.knobRadius = knobRadius ?? this.size.x / 2; |     this.knobRadius = knobRadius ?? this.size.x / 2; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  | |||||||
| @ -45,11 +45,29 @@ void main() { | |||||||
|           size: 20, |           size: 20, | ||||||
|           margin: const EdgeInsets.only(left: 20, bottom: 20), |           margin: const EdgeInsets.only(left: 20, bottom: 20), | ||||||
|         ); |         ); | ||||||
|         joystick.loaded.then((_) => game.onGameResize(Vector2(200, 100))); |         joystick.mounted.then((_) => game.onGameResize(Vector2(200, 100))); | ||||||
|         await game.ensureAdd(joystick); |         await game.ensureAdd(joystick); | ||||||
|         expect(joystick.position, Vector2(30, 70)); |         expect(joystick.position, Vector2(30, 70)); | ||||||
|       }, |       }, | ||||||
|     ); |     ); | ||||||
|  |  | ||||||
|  |     testWithFlameGame( | ||||||
|  |       'properly re-positions with FixedResolutionViewport', | ||||||
|  |       (game) async { | ||||||
|  |         game.camera = | ||||||
|  |             CameraComponent.withFixedResolution(width: 100, height: 200); | ||||||
|  |         game.onGameResize(Vector2(100, 200)); | ||||||
|  |         final joystick = JoystickComponent( | ||||||
|  |           knob: CircleComponent(radius: 5.0), | ||||||
|  |           size: 20, | ||||||
|  |           margin: const EdgeInsets.only(left: 20, bottom: 20), | ||||||
|  |         ); | ||||||
|  |         await game.camera.viewport.ensureAdd(joystick); | ||||||
|  |         expect(joystick.position, Vector2(30, 170)); | ||||||
|  |         game.onGameResize(Vector2(200, 100)); | ||||||
|  |         expect(joystick.position, Vector2(30, 170)); | ||||||
|  |       }, | ||||||
|  |     ); | ||||||
|   }); |   }); | ||||||
|  |  | ||||||
|   group('Joystick input tests', () { |   group('Joystick input tests', () { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Lukas Klingsbo
					Lukas Klingsbo