-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: Component Mixin to Receive Cursor Hover Events #1834
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
Closed
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
01f3bf4
feat: Add cursor components mixin to Game
garysm 77ac85d
Merge remote-tracking branch 'origin' into cursor-handler-components
garysm ceb61bc
Fix too long lines
spydon 847ba14
Merge branch 'main' into cursor-handler-components
spydon 01d5b83
Merge branch 'main' into cursor-handler-components
garysm 84dc8b3
Add example for CursorHandler components
garysm c70680b
feat: Add cursor components mixin to Game
garysm aab7e10
Fix too long lines
spydon 8a52989
Add example for CursorHandler components
garysm 69af8bf
Merge branch 'cursor-handler-components' of https://github.com/garysm…
garysm 316dd3f
rename example
garysm b229f0c
Add mouse movement to docs
garysm fa4d26a
Merge branch 'main' into cursor-handler-components
garysm 446e80b
Merge branch 'main' into cursor-handler-components
spydon dde9dbb
Merge branch 'main' into cursor-handler-components
spydon 1c19e31
Merge branch 'main' into cursor-handler-components
spydon 1d26c18
update docs for linting
garysm ce40df1
Update doc/flame/inputs/gesture-input.md
garysm cadf718
Merge branch 'main' into cursor-handler-components
garysm 4293baa
update doc for linting and admonition
garysm 0ebb400
Add blank line in doc
garysm eb6a8bd
Merge branch 'main' into cursor-handler-components
garysm fdfbb76
Merge branch 'main' of https://github.com/garysm/flame into cursor-ha…
garysm 27f536a
Merge branch 'main' of https://github.com/garysm/flame into cursor-ha…
garysm 0f0db95
Wip
garysm 135fc39
fix export error
garysm 1f5d375
Merge branch 'main' into cursor-handler-components
garysm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
examples/lib/stories/input/mouse_movement_components_example.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // import 'dart:math'; | ||
|
|
||
| // import 'package:flame/components.dart'; | ||
| // import 'package:flame/events.dart'; | ||
| // import 'package:flame/game.dart'; | ||
| // import 'package:flutter/material.dart'; | ||
|
|
||
| // class MouseMovementComponentsExample extends FlameGame | ||
| // with HasCursorHandlerComponents { | ||
| // static const String description = ''' | ||
| // This example shows how to handle mouse movement using `CursorHandler` Components.\n\n | ||
| // Moving the mouse around the canvas changes the angle of each Component, and causes | ||
| // the line drawn on them to face towards the mouse's position. | ||
| // '''; | ||
|
|
||
| // @override | ||
| // Future<void> onLoad() async { | ||
| // add( | ||
| // CursorFollowerCircle( | ||
| // radius: 20.0, | ||
| // )..position = size / 2, | ||
| // ); | ||
| // add( | ||
| // CursorFollowerCircle( | ||
| // radius: 20.0, | ||
| // )..position = Vector2(100.0, 100.0), | ||
| // ); | ||
| // } | ||
| // } | ||
|
|
||
| // class CursorFollowerCircle extends PositionComponent with CursorHandler { | ||
| // final double _radius; | ||
|
|
||
| // final Paint _paint; | ||
|
|
||
| // CursorFollowerCircle({required double radius}) | ||
| // : _radius = radius, | ||
| // _paint = Paint()..color = const Color(0xFF80C080), | ||
| // super( | ||
| // size: Vector2.all(2 * radius), | ||
| // anchor: Anchor.center, | ||
| // ); | ||
|
|
||
| // @override | ||
| // bool onMouseMove(PointerHoverInfo info) { | ||
| // final gameMousePositionX = info.eventPosition.game.x; | ||
|
|
||
| // final gameMousePositionY = info.eventPosition.game.y; | ||
|
|
||
| // angle = | ||
| // atan2(gameMousePositionY - position.y, gameMousePositionX - position.x); | ||
| // return super.onMouseMove(info); | ||
| // } | ||
|
|
||
| // @override | ||
| // void render(Canvas canvas) { | ||
| // super.render(canvas); | ||
|
|
||
| // canvas.drawCircle( | ||
| // Offset(_radius, _radius), | ||
| // _radius, | ||
| // _paint, | ||
| // ); | ||
| // canvas.drawLine( | ||
| // Offset(_radius, _radius), | ||
| // Offset(_radius * 3.0, _radius), | ||
| // Paint() | ||
| // ..color = const Color(0xFFFFFFFF) | ||
| // ..strokeWidth = 2.0, | ||
| // ); | ||
| // } | ||
| // } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import 'package:flame/components.dart'; | ||
| import 'package:flame/src/gestures/events.dart'; | ||
|
|
||
| mixin CursorHandler on Component { | ||
| bool onMouseMove(PointerHoverInfo info) { | ||
| return true; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
packages/flame/lib/src/events/component_mixins/cursor_hover_callbacks.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import 'package:flame/src/components/core/component.dart'; | ||
| import 'package:flame/src/events/flame_game_mixins/cursor_hover_dispatcher.dart'; | ||
| import 'package:flame/src/events/messages/cursor_hover_event.dart'; | ||
| import 'package:flame/src/game/flame_game.dart'; | ||
| import 'package:meta/meta.dart'; | ||
|
|
||
| mixin CursorHoverCallbacks on Component { | ||
| void onMouseMove(CursorHoverEvent event) {} | ||
|
|
||
| @override | ||
| @mustCallSuper | ||
| void onMount() { | ||
| super.onMount(); | ||
| final game = findGame()! as FlameGame; | ||
| if (game.firstChild<CursorHoverDispatcher>() == null) { | ||
| game.add(CursorHoverDispatcher()); | ||
| } | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
packages/flame/lib/src/events/flame_game_mixins/cursor_hover_dispatcher.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import 'package:flame/src/components/core/component.dart'; | ||
| import 'package:flame/src/events/component_mixins/cursor_hover_callbacks.dart'; | ||
| import 'package:flame/src/events/messages/cursor_hover_event.dart'; | ||
| import 'package:flame/src/game/flame_game.dart'; | ||
| import 'package:flutter/gestures.dart'; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:meta/meta.dart'; | ||
|
|
||
| @internal | ||
| class CursorHoverDispatcher extends Component { | ||
| bool _eventHandlerRegistered = false; | ||
| FlameGame get game => parent! as FlameGame; | ||
|
|
||
| @mustCallSuper | ||
| void onMouseMove(CursorHoverEvent event) { | ||
| event.deliverAtPoint( | ||
| rootComponent: game, | ||
| eventHandler: (CursorHoverCallbacks component) { | ||
| component.onMouseMove(event); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| @internal | ||
| void handleMouseMove(int pointerId, PointerHoverEvent pointerHoverEvent) { | ||
| onMouseMove(CursorHoverEvent(pointerId, pointerHoverEvent)); | ||
| } | ||
|
|
||
| @override | ||
| void onMount() { | ||
| if (game.firstChild<CursorHoverDispatcher>() == null) { | ||
| // TODO: Add detector to game | ||
| // game.gestureDetectors.add( | ||
| // PointerHoverEventListener.new, | ||
| // () {}, | ||
| // ); | ||
| } else { | ||
| removeFromParent(); | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
packages/flame/lib/src/events/messages/cursor_hover_event.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import 'package:flame/image_composition.dart'; | ||
| import 'package:flame/src/events/messages/position_event.dart'; | ||
| import 'package:flame/src/game/game.dart'; | ||
| import 'package:flame/src/gestures/events.dart'; | ||
|
|
||
| import 'package:flutter/gestures.dart'; | ||
|
|
||
| class CursorHoverEvent extends PositionEvent { | ||
| CursorHoverEvent(this.pointerId, PointerHoverEvent pointerHoverEvent) | ||
| : deviceKind = pointerHoverEvent.kind, | ||
| super( | ||
| canvasPosition: pointerHoverEvent.localPosition.toVector2(), | ||
| devicePosition: pointerHoverEvent.position.toVector2(), | ||
| ); | ||
|
|
||
| final int pointerId; | ||
|
|
||
| final PointerDeviceKind deviceKind; | ||
|
|
||
| PointerHoverInfo asInfo(Game game) { | ||
| return PointerHoverInfo.fromDetails( | ||
| game, | ||
| PointerHoverEvent( | ||
| position: devicePosition.toOffset(), | ||
| kind: deviceKind, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| String toString() => 'CursorHoverEvent(canvasPosition: $canvasPosition, ' | ||
| 'devicePosition: $devicePosition, ' | ||
| 'pointerId: $pointerId, deviceKind: $deviceKind)'; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this warning more clear as to what happens?
is the handler ignored?
or will it error?
that is relevant to multi-platform games