Skip to content

Commit

Permalink
fix: Set margins of JoystickComponent properly (#3019)
Browse files Browse the repository at this point in the history
The margins of the `JoystickComponent` weren't set properly, this PR
fixes that.
  • Loading branch information
spydon committed Feb 5, 2024
1 parent bf8aecd commit e27818d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/lib/stories/input/joystick_advanced_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class JoystickAdvancedExample extends FlameGame with HasCollisionDetection {
the buttons.
''';

JoystickAdvancedExample()
: super(
camera: CameraComponent.withFixedResolution(width: 1200, height: 800),
);

late final JoystickPlayer player;
late final JoystickComponent joystick;
late final TextComponent speedText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import 'dart:math';

import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/src/components/input/hud_margin_component.dart';
import 'package:meta/meta.dart';
import 'package:flutter/widgets.dart';

enum JoystickDirection {
up,
Expand All @@ -17,7 +16,8 @@ enum JoystickDirection {
idle,
}

class JoystickComponent extends HudMarginComponent with DragCallbacks {
class JoystickComponent extends PositionComponent
with HasGameReference, ComponentViewportMargin, DragCallbacks {
late final PositionComponent? knob;
late final PositionComponent? background;

Expand Down Expand Up @@ -46,8 +46,8 @@ class JoystickComponent extends HudMarginComponent with DragCallbacks {
JoystickComponent({
this.knob,
this.background,
super.margin,
super.position,
EdgeInsets? margin,
double? size,
double? knobRadius,
Anchor super.anchor = Anchor.center,
Expand All @@ -66,6 +66,7 @@ class JoystickComponent extends HudMarginComponent with DragCallbacks {
super(
size: background?.size ?? Vector2.all(size ?? 0),
) {
this.margin = margin;
this.knobRadius = knobRadius ?? this.size.x / 2;
}

Expand Down
20 changes: 19 additions & 1 deletion packages/flame/test/components/joystick_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,29 @@ void main() {
size: 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);
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', () {
Expand Down

0 comments on commit e27818d

Please sign in to comment.