Skip to content

Commit

Permalink
improve map_on_mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Apr 13, 2024
1 parent fc2b59c commit c338b26
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/Layers/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/Layers/imagesc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 24 additions & 13 deletions src/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c338b26

Please sign in to comment.