Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Set margins of JoystickComponent properly #3019

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading