Forward Mouse input to Makie
rasmushenningsson opened this issue · comments
It would be useful to forward mouse (and keyboard) input to Makie. E.g. to be able to spin 3d plots using the mouse.
The proper way to do this would probably to adapt the code at https://github.com/jwahlstrand/GtkMakie.jl/blob/main/src/events.jl
At the moment, I have a workaround, by putting a MouseArea
over the Makie viewport.
Here's an example of how it can be done, it might be useful to someone in the meantime. 🙂
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.AllButtons
onPressed: mouse=>Julia.mouse_press_callback(mouse.x,height-mouse.y-1,mouse.button)
onReleased: mouse=>Julia.mouse_release_callback(mouse.x,height-mouse.y-1,mouse.button)
onPositionChanged: mouse=>Julia.mouse_position_callback(mouse.x,height-mouse.y-1)
onWheel: wheel=>Julia.mouse_wheel_callback(wheel.x, wheel.y, wheel.angleDelta.x, wheel.angleDelta.y)
}
and in the callbacks we update the Observables in scene.events
:
function mouse_button_qt2makie(button)
button == 1 && return Makie.Mouse.left
button == 2 && return Makie.Mouse.right
button == 4 && return Makie.Mouse.middle
Makie.Mouse.none
end
function mouse_press_callback(mouseX, mouseY, button)
scene.events.mouseposition[] = (mouseX,mouseY)
b = mouse_button_qt2makie(button)
if b != Makie.Mouse.none
scene.events.mousebutton[] = Makie.MouseButtonEvent(b, Makie.Mouse.press)
end
end
function mouse_release_callback(mouseX, mouseY, button)
scene.events.mouseposition[] = (mouseX,mouseY)
b = mouse_button_qt2makie(button)
if b != Makie.Mouse.none
scene.events.mousebutton[] = Makie.MouseButtonEvent(b, Makie.Mouse.release)
end
end
function mouse_position_callback(mouseX, mouseY)
scene.events.mouseposition[] = (mouseX,mouseY)
end
function mouse_wheel_callback(mouseX, mouseY, angleDeltaX, angleDeltaY)
scene.events.mouseposition[] = (mouseX,mouseY)
scene.events.scroll[] = (angleDeltaX/120,angleDeltaY/120)
end