-
Notifications
You must be signed in to change notification settings - Fork 137
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
MouseEvents should contain the relative position inside its component #335
Comments
The mouse listener works with any Zircon object, not just |
Without deeper checks of where mouse events are created and consumed... Without breaking the API? Probably by creating a new class that gets new special treatment (new handleXEvent() methods and so on): /**
* A mouse event triggered by a [Component].
*/
data class MouseComponentEvent(
override val type: MouseEventType,
val button: Int,
/**
* Position of this event relative to the top left corner of the tile grid/screen.
*/
val position: Position,
/**
* The [Component] that triggered this event.
*/
val source: Component
) : UIEvent {
/**
* The position of this event inside the [source] component. This position is relative to
* the top left corner of [source]'s absolutePosition.
*
* @see Component.absolutePosition
*/
val positionInSource: Position
get() = position - source.absolutePosition
}
fun MouseEvent.toMouseComponentEvent(source: Component) =
MouseComponentEvent(
type,
button,
position,
source
) But with breaking the API? Just a quick mockup of which I acutally don't know if it woud break the API because of the default value: data class MouseEvent<T: Any>(
override val type: MouseEventType,
val button: Int,
val position: Position,
val source: T? = null
) : UIEvent
val MouseEvent<Component>.positionInComponent: Position
get() = position - (source?.absolutePosition ?: Position.zero()) |
This looks like a good idea, I dig it. I'm gonna take a look after the next release if this can be implemented. In the meantime, you are welcome to give it a try if you're interested. |
Damn, when adding a generic type parameter to |
Oh, yeah...generics is cancer in this case...once I refactored |
Describe the bug
Currently when adding a mouse listener to a component you can get the position of the click (or movement) from it. But this position is absolute. This means it is independent of the component's position. This becomes a problem when you add a handler that has no idea of where the component is located on the screen.
To Reproduce
Expected behavior
When handling a mouse event I would expect to either get the
event.position
inside the component that handles the event. Or have two fields in the event:event.absolutePosition
andevent.position
.The text was updated successfully, but these errors were encountered: