Open Baret opened 4 years ago
The mouse listener works with any Zircon object, not just Component
s, that's why it doesn't have a relative position. You bring up a valid point though. Can you think of a way we could augment MouseEvent
without breaking the API? I'm gonna add this to the backlog.
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 MouseEvent
we would need to add it in almost every place. Usually this would probably simply result in changing MouseEvent
to MouseEvent<*>
.
I will see what else changes with that try...
Oh, yeah...generics is cancer in this case...once I refactored Component
to be generic just to undo it after I was done because it was so horrifying...
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
.