mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 19:12:31 +08:00
Adding custom mouse cursors for flame (#935)
* Adding custom mouse cursors for flame * linting and adding fvm to gitignore * PR suggestions * Apply suggestions from code review Co-authored-by: Luan Nico <luanpotter27@gmail.com> Co-authored-by: Luan Nico <luanpotter27@gmail.com>
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import 'package:dashbook/dashbook.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../commons/commons.dart';
|
||||
import 'draggables.dart';
|
||||
@ -7,6 +8,7 @@ import 'hoverables.dart';
|
||||
import 'joystick.dart';
|
||||
import 'joystick_advanced.dart';
|
||||
import 'keyboard.dart';
|
||||
import 'mouse_cursor.dart';
|
||||
import 'mouse_movement.dart';
|
||||
import 'multitap.dart';
|
||||
import 'multitap_advanced.dart';
|
||||
@ -26,6 +28,18 @@ void addInputStories(Dashbook dashbook) {
|
||||
(_) => GameWidget(game: MouseMovementGame()),
|
||||
codeLink: baseLink('input/mouse_movement.dart'),
|
||||
)
|
||||
..add(
|
||||
'Mouse Cursor',
|
||||
(_) => GameWidget(
|
||||
game: MouseCursorGame(),
|
||||
mouseCursor: SystemMouseCursors.move,
|
||||
),
|
||||
codeLink: baseLink('input/mouse_cursor.dart'),
|
||||
info: '''
|
||||
Example showcasing the ability to change the game cursor in runtime
|
||||
hover the little square to see the cursor changing
|
||||
''',
|
||||
)
|
||||
..add(
|
||||
'Scroll',
|
||||
(_) => GameWidget(game: ScrollGame()),
|
||||
|
||||
53
examples/lib/stories/input/mouse_cursor.dart
Normal file
53
examples/lib/stories/input/mouse_cursor.dart
Normal file
@ -0,0 +1,53 @@
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
import 'package:flame/palette.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class MouseCursorGame extends Game with MouseMovementDetector {
|
||||
static const speed = 200;
|
||||
static final Paint _blue = BasicPalette.blue.paint();
|
||||
static final Paint _white = BasicPalette.white.paint();
|
||||
static final Vector2 objSize = Vector2.all(150);
|
||||
|
||||
Vector2 position = Vector2(100, 100);
|
||||
Vector2? target;
|
||||
|
||||
bool onTarget = false;
|
||||
|
||||
@override
|
||||
void onMouseMove(PointerHoverInfo info) {
|
||||
target = info.eventPosition.game;
|
||||
}
|
||||
|
||||
Rect _toRect() => position.toPositionedRect(objSize);
|
||||
|
||||
@override
|
||||
void render(Canvas canvas) {
|
||||
canvas.drawRect(
|
||||
_toRect(),
|
||||
onTarget ? _blue : _white,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double dt) {
|
||||
final target = this.target;
|
||||
if (target != null) {
|
||||
final hovering = _toRect().contains(target.toOffset());
|
||||
if (hovering) {
|
||||
if (!onTarget) {
|
||||
//Entered
|
||||
mouseCursor.value = SystemMouseCursors.grab;
|
||||
}
|
||||
} else {
|
||||
if (onTarget) {
|
||||
// Exited
|
||||
mouseCursor.value = SystemMouseCursors.move;
|
||||
}
|
||||
}
|
||||
onTarget = hovering;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user