Skip to content

Commit

Permalink
improve imagesc for Obs
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Apr 9, 2024
1 parent d34227b commit 5eafd9b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/Base.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Base.size(x::Observable) = size(x[])
Base.size(x::Observable, i) = size(x[], i)

function gap!(fig, gap=0)
rowgap!(fig.layout, gap)
colgap!(fig.layout, gap)
end

export gap!
22 changes: 17 additions & 5 deletions src/Layers/imagesc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,42 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},

titles === nothing && (titles = fill("", n))
k = 0
ax, plt = nothing, nothing
# ax, plt = nothing, nothing
axs = []
plts = []
for i = 1:nrow, j = 1:ncol
k += 1
k > n && break

title = titles[k]
if isa(z, Observable)
_z = @lift $z[:, :, k]
_z = z[][:, :, k]
else
_z = z[:, :, k]
end
ax, plt = imagesc!(fig[i, j], x, y, _z;
title, colorrange, force_show_legend, colors, kw...)
(fun_axis!) !== nothing && fun_axis!(ax)
push!(axs, ax)
push!(plts, plt)
end


# update plot
if isa(z, Observable)
on(z) do z
for i = 1:n
plts[i][3] = z[:, :, i]
end
end
end

# unify the legend
(colorrange != automatic && !force_show_legend) && Colorbar(fig[:, ncol+1], plt)
(colorrange != automatic && !force_show_legend) &&
Colorbar(fig[1:nrow, ncol+1], plts[1])

rowgap!(fig.layout, gap)
colgap!(fig.layout, gap)
axs
axs, plts
end

function imagesc!(fig, z; kw...)
Expand Down
1 change: 1 addition & 0 deletions src/MakieLayers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export automatic
export map_on_keyboard, map_on_mouse
export my_theme!

include("Base.jl")
include("convert_arguments.jl")
include("Layers/Layers.jl")
include("events.jl")
Expand Down
26 changes: 21 additions & 5 deletions src/events.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
"""
map_on_keyboard(fig, stime)
map_on_keyboard(fig, stime)
"""
function map_on_keyboard(fig, stime)
function map_on_keyboard_lr(fig, stime::Slider, Δ::Integer=1)
on(events(fig).keyboardbutton) do event

if event.action == Keyboard.press || event.action == Keyboard.repeat
if event.key == Keyboard.right
stime[] += 1
set_close_to!(stime, stime.value[] + Δ)
elseif event.key == Keyboard.left
stime[] -= 1
set_close_to!(stime, stime.value[] - Δ)
end
end
return Consume(false)
end
end

function map_on_keyboard_ud(fig, stime::Slider, Δ::Integer=1)
on(events(fig).keyboardbutton) do event
if event.action == Keyboard.press || event.action == Keyboard.repeat
if event.key == Keyboard.down
set_close_to!(stime, stime.value[] + Δ)
elseif event.key == Keyboard.up
set_close_to!(stime, stime.value[] - Δ)
end
end
return Consume(false)
end
end

map_on_keyboard = map_on_keyboard_lr

# @show event.key
# if event.key == Keyboard.up
# slat[] += celly
Expand Down Expand Up @@ -54,3 +68,5 @@ function map_on_mouse(fig, ax, handle_plot, slon, slat)
end
end


export map_on_keyboard_lr, map_on_keyboard_ud

0 comments on commit 5eafd9b

Please sign in to comment.