From c338b26eea6686375e28cad7901c36c07c84cdff Mon Sep 17 00:00:00 2001 From: Dongdong Kong Date: Sat, 13 Apr 2024 21:52:59 +0800 Subject: [PATCH] improve map_on_mouse --- src/Layers/Base.jl | 6 ++++++ src/Layers/imagesc.jl | 1 + src/events.jl | 37 ++++++++++++++++++++++++------------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Layers/Base.jl b/src/Layers/Base.jl index 1b0da2b..13dd579 100644 --- a/src/Layers/Base.jl +++ b/src/Layers/Base.jl @@ -40,3 +40,9 @@ function add_labels!(axs::Vector, labels::Observable, x, y; end bind_text!(plts, labels) end + +function Base.size(fig::Figure) + m, n = collect(fig.scene.viewport[].widths) + println("size=($m, $n)") + # m, n +end diff --git a/src/Layers/imagesc.jl b/src/Layers/imagesc.jl index e8c7bd6..f88dc90 100644 --- a/src/Layers/imagesc.jl +++ b/src/Layers/imagesc.jl @@ -103,6 +103,7 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition}, push!(axs, ax) push!(plts, plt) end + linkaxes!(axs...) bind_z!(plts, z) # unify the legend diff --git a/src/events.jl b/src/events.jl index fe2c356..ac072fd 100644 --- a/src/events.jl +++ b/src/events.jl @@ -48,25 +48,36 @@ map_on_keyboard = map_on_keyboard_lr """ map_on_mouse(fig, ax, handle_plot, slon, slat) + +- `fun`: 只接受两个参数,其他需要放在`kw...`。 """ -function map_on_mouse(fig, ax, handle_plot, slon, slat) - # handle_plot = get_plot(ax, 1) - on(events(fig).mousebutton, priority=1) do event - # @show event.button, event.action +function map_on_mouse(ax, slon, slat; + verbose=false, (fun!)=nothing, kw...) + + on(events(ax).mousebutton, priority=2) do event if event.button == Mouse.left && event.action == Mouse.press - plt, i = pick(fig) - # @show plt + # plt, i = pick(ax) + pos = mouseposition(ax) + # 如果不在axis范围内 + xlim = ax.xaxis.attributes.limits[] + ylim = ax.yaxis.attributes.limits[] + # xlim, ylim = ax.limits[] + # if !isnothing(xlim) && !isnothing(ylim) - if plt == handle_plot - pos = mouseposition(ax) - slon[] = pos[1] - slat[] = pos[2] - # point = [] + if !((xlim[1] <= pos[1] <= xlim[2]) && (ylim[1] <= pos[2] <= ylim[2])) + return Consume(false) + end + slon[] = pos[1] + slat[] = pos[2] + + verbose && @show slon[], slat[] + if (fun!) !== nothing + fun!(slon[], slat[]; kw...) end end - return Consume(true) + return Consume(false) end end -export map_on_keyboard_lr, map_on_keyboard_ud +export map_on_keyboard_lr, map_on_keyboard_ud, map_on_mouse