feat: Component Mixin to Receive Cursor Hover Events#1834
Conversation
spydon
left a comment
There was a problem hiding this comment.
Lgtm except for the too long lines!
|
@garysm I fixed the too long lines, could you add some docs for the functionality? |
|
@spydon Yes, I will work on docs and provide an example. Thank you for fixing the too long lines. |
|
Would we like to remove this mixin if #1733 gets done? |
|
@alestiago, that would probably make sense. I can finish up with this, then convert to a class once #1733 is implemented. |
…/flame into cursor-handler-components
|
Added docs and example @spydon . Let me know if you'd like a test to be written as well. |
|
Yes, I'll re-review this PR. But first we need to fix up this branch -- currently it shows 54 files changed. |
I performed a rebase, that is probably why. |
GitHub should be able to understand rebases, but maybe not when the branch is outdated, can you press "Update branch" in the UI and see if one of those merges help? |
|
@spydon that suggestion worked, thanks! |
st-pasha
left a comment
There was a problem hiding this comment.
Some general comments:
-
Tests are currently missing. Generally, in order to test mouse movement events in a test, you would
testWidgets(), and thentester.createGesture()+gesture.moveBy(). -
The interaction between this mixin and
HasDraggableComponentsis unclear: would they coexist peacefully, or will one preempt the other? This needs to be documented. Also, if both mixins are present and the user drags the mouse - would both mixins process this event? -
We're slowly trying to move towards more uniform approach for event handling, see the
lib/src/events/folder. I'm not sure whether you'd want to modify your PR to use this newer approach, but that would save us some work in the future.
| 'game also mixed with HasCursorHandlerComponents. Do not mix with both, ' | ||
| 'HasCursorHandlerComponents removes the necessity of ' | ||
| 'MouseMovementDetector.', | ||
| ); |
There was a problem hiding this comment.
This assert is probably unnecessary.
If the user did have MouseMovementDetector on their Game, and then added HasCursorHandlerComponent, then the assert wouldn't trigger since onMouseMove would be overridden anyway.
|
Thanks for the feedback @st-pasha , I'll work on this. |
Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>
| [`PointerHoverEvent`](https://api.flutter.dev/flutter/gestures/PointerHoverEvent-class.html) | ||
|
|
||
| ```{warning} | ||
| This should not be used on platforms that do not support mouse cursors. |
There was a problem hiding this comment.
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
| add( | ||
| CursorFollowerCircle( | ||
| radius: 20.0, | ||
| )..position = Vector2(100.0, 100.0), |
There was a problem hiding this comment.
| )..position = Vector2(100.0, 100.0), | |
| )..position = Vector2.all(100), |
| _radius, | ||
| _paint, | ||
| ); | ||
| canvas.drawLine( |
There was a problem hiding this comment.
(not part of this PR)
we should have a renderLine ext similar to renderPoint
https://github.com/flame-engine/flame/blob/main/packages/flame/lib/src/extensions/canvas.dart#L21
| add( | ||
| CursorFollowerCircle( | ||
| radius: 20.0, | ||
| )..position = Vector2(100.0, 100.0), |
There was a problem hiding this comment.
| )..position = Vector2(100.0, 100.0), | |
| )..position = Vector2.all(100), |
|
@garysm are you interested in continuing working on this? :) |
|
@spydon taking a look at #2450, this makes a lot more sense and aligns to what I'd like to achieve. Since there is no existing cursor component mixin, there shouldn't be a need for a bridge either. I can re-work this PR for "new-style" components and event handling. Or I can close and create a new one for that, whatever works. |
Whatever is the easiest for you :) |
…ndler-components
…ndler-components
|
It seems to me that I cannot add a hover listener directly to the game from my new |
|
@spydon any thoughts on this? |
|
@garysm I missed that you had written! If it is possible (which it should be) it should be done in the same manner as the other ones, i.e. without a mixin in the game, there should be some hints on what to connect the game to in the old hover functionality. |
I don't think that the current class MouseDetectorBuilder {
MouseDetectorBuilder([this._onChange]);
final Map _listeners = {};
final void Function()? _onChange;
Widget build(Widget child) {
// Return mouse listeners
}
}
My question then would be, what should the widget returned from this builder be? I am unsure, since the listeners cannot be passed into a RawGestureDetector, but should still be consolidated into a single widget. What are your thoughts @spydon ? |
Sorry for the late reply, it's been very busy with FlutterCon! |
No problem! The issue would be that there is no way to dynamically add |
Ah right, now I understand the problem. |
Description
This PR adds the
HasCursorHandlerComponentsmixin on FlameGame, and theCursorHandlermixin onComponent. Heavily inspired byHasKeyboardHandlerComponents.HasCursorHandlerComponentspropagates cursor hover events to childCursorHandlerComponents, making these events easier to handle.Checklist
fix:,feat:,docs:etc).docsand added dartdoc comments with///.examples.Breaking Change
Related Issues
Closes #1818