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:
Lukas Klingsbo
2024-02-05 12:02:02 +01:00
committed by GitHub
parent bf8aecdbfa
commit e27818d872
3 changed files with 29 additions and 5 deletions

View File

@ -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;

View File

@ -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;
} }

View File

@ -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', () {