Skip to content

Commit

Permalink
add hovering observables to all blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed Oct 21, 2024
1 parent 7a38a6e commit d6abeaf
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/makielayout/blocks/axis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function register_events!(ax, scene)
setfield!(ax, :keysevents, keysevents)
evs = events(scene)

setfield!(ax, :hovering, Observable(false))
onmouseover(_ -> ax.hovering[] = true, mouseeventhandle)
onmouseout(_ -> ax.hovering[] = false, mouseeventhandle)

on(scene, evs.scroll) do s
if is_mouseinside(scene)
result = setindex!(scrollevents, ScrollEvent(s[1], s[2]))
Expand Down
4 changes: 4 additions & 0 deletions src/makielayout/blocks/axis3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ function initialize_block!(ax::Axis3)
return Consume(false)
end

setfield!(ax, :hovering, Observable(false))
onmouseover(_ -> ax.hovering[] = true, ax.mouseeventhandle)
onmouseout(_ -> ax.hovering[] = false, ax.mouseeventhandle)

ax.interactions = Dict{Symbol, Tuple{Bool, Any}}()

on(scene, ax.limits) do lims
Expand Down
4 changes: 4 additions & 0 deletions src/makielayout/blocks/button.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ function initialize_block!(b::Button)

mouseevents = addmouseevents!(scene, b.layoutobservables.computedbbox)

setfield!(b, :hovering, Observable(false))

onmouseover(mouseevents) do _
mousestate[] = :hover
b.hovering[] = true
return Consume(false)
end

onmouseout(mouseevents) do _
mousestate[] = :out
b.hovering[] = false
return Consume(false)
end

Expand Down
4 changes: 4 additions & 0 deletions src/makielayout/blocks/intervalslider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ function initialize_block!(isl::IntervalSlider)
return Consume(true)
end

setfield!(isl, :hovering, Observable(false))

onmouseover(mouseevents) do event
isl.hovering[] = true
fraction = if isl.horizontal[]
(event.px[1] - endpoints[][1][1]) / (endpoints[][2][1] - endpoints[][1][1])
else
Expand All @@ -250,6 +253,7 @@ function initialize_block!(isl::IntervalSlider)
end

onmouseout(mouseevents) do event
isl.hovering[] = false
state[] = :none
return Consume(false)
end
Expand Down
4 changes: 4 additions & 0 deletions src/makielayout/blocks/menu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ function initialize_block!(m::Menu; default = 1)
return false
end

setfield!(m, :hovering, Observable(false))

onany(blockscene, e.mouseposition, e.mousebutton; priority=64) do position, butt
mp = screen_relative(menuscene, position)
# track if we have been inside menu/options to clean up if we haven't been
Expand Down Expand Up @@ -211,10 +213,12 @@ function initialize_block!(m::Menu; default = 1)
end
return Consume(true)
else # HOVER
m.hovering[] = true
selectionpoly.color = m.cell_color_hover[]
end
else
# If not inside anymore, invalidate was_pressed
m.hovering[] = false
was_pressed_button[] = false
end
end
Expand Down
5 changes: 5 additions & 0 deletions src/makielayout/blocks/scene.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ function initialize_block!(ls::LScene; scenekw = NamedTuple())
scenekw = merge((clear = false, camera=cam3d!), scenekw)
ls.scene = Scene(blockscene, lift(round_to_IRect2D, blockscene, ls.layoutobservables.computedbbox); scenekw...)

setfield!(ls, :hovering, Observable(false))
mouseeventhandle = addmouseevents!(ls.scene)
onmouseover(_ -> ls.hovering[] = true, mouseeventhandle)
onmouseout(_ -> ls.hovering[] = false, mouseeventhandle)

on(blockscene, ls.show_axis) do show_axis
ax = ls.scene[OldAxis]
if show_axis
Expand Down
4 changes: 4 additions & 0 deletions src/makielayout/blocks/slider.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ function initialize_block!(sl::Slider)

mouseevents = addmouseevents!(topscene, sl.layoutobservables.computedbbox)

setfield!(sl, :hovering, Observable(false))
onmouseover(_ -> sl.hovering[] = true, mouseevents)
onmouseout(_ -> sl.hovering[] = false, mouseevents)

onmouseleftdrag(mouseevents) do event
dragging[] = true
dif = event.px - event.prev_px
Expand Down
1 change: 1 addition & 0 deletions src/makielayout/blocks/textbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function initialize_block!(tbox::Textbox)
end

hovering = Observable(false)
setfield!(tbox, :hovering, hovering)
realbordercolor = Observable{RGBAf}()

map!(topscene, realbordercolor, tbox.bordercolor, tbox.bordercolor_focused,
Expand Down
5 changes: 5 additions & 0 deletions src/makielayout/blocks/toggle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function initialize_block!(t::Toggle)
end
end

hovering = Observable(false)
setfield!(t, :hovering, hovering)

buttonfactor = Observable(1.0)
buttonsize = lift(topscene, markersize, t.rimfraction, buttonfactor) do ms, rf, bf
ms * (1 - rf) * bf
Expand Down Expand Up @@ -87,11 +90,13 @@ function initialize_block!(t::Toggle)

onmouseover(mouseevents) do event
buttonfactor[] = 1.15
hovering[] = true
return Consume(false)
end

onmouseout(mouseevents) do event
buttonfactor[] = 1.0
hovering[] = false
return Consume(false)
end

Expand Down
9 changes: 9 additions & 0 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Axis(fig_or_scene; palette = nothing, kwargs...)
xaxis::LineAxis
yaxis::LineAxis
elements::Dict{Symbol, Any}
hovering::Observable{Bool}
@attributes begin
"""
Global state for the x dimension conversion.
Expand Down Expand Up @@ -916,6 +917,7 @@ end

@Block Slider begin
selected_index::Observable{Int}
hovering::Observable{Bool}
@attributes begin
"The horizontal alignment of the element in its suggested bounding box."
halign = :center
Expand Down Expand Up @@ -1016,6 +1018,7 @@ end
@Block IntervalSlider begin
selected_indices::Observable{Tuple{Int, Int}}
displayed_sliderfractions::Observable{Tuple{Float64, Float64}}
hovering::Observable{Bool}
@attributes begin
"The horizontal alignment of the slider in its suggested bounding box."
halign = :center
Expand Down Expand Up @@ -1053,6 +1056,7 @@ end
end

@Block Button begin
hovering::Observable{Bool}
@attributes begin
"The horizontal alignment of the button in its suggested boundingbox"
halign = :center
Expand Down Expand Up @@ -1153,6 +1157,7 @@ const CHECKMARK_BEZIER = scale(BezierPath(
end

@Block Toggle begin
hovering::Observable{Bool}
@attributes begin
"The horizontal alignment of the toggle in its suggested bounding box."
halign = :center
Expand Down Expand Up @@ -1235,6 +1240,7 @@ end
```
"""
@Block Menu begin
hovering::Observable{Bool}
@attributes begin
"The height setting of the menu."
height = Auto()
Expand Down Expand Up @@ -1443,6 +1449,7 @@ end

@Block LScene <: AbstractAxis begin
scene::Scene
hovering::Observable{Bool}
@attributes begin
"""
Global state for the x dimension conversion.
Expand Down Expand Up @@ -1479,6 +1486,7 @@ end
@Block Textbox begin
cursorindex::Observable{Int}
cursoranimtask
hovering::Observable{Bool}
@attributes begin
"The height setting of the textbox."
height = Auto()
Expand Down Expand Up @@ -1554,6 +1562,7 @@ end
scrollevents::Observable{ScrollEvent}
keysevents::Observable{KeysEvent}
interactions::Dict{Symbol, Tuple{Bool, Any}}
hovering::Observable{Bool}
@attributes begin
"""
Global state for the x dimension conversion.
Expand Down

0 comments on commit d6abeaf

Please sign in to comment.