Skip to content

Commit

Permalink
add kwarg to rotate Toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed Oct 10, 2024
1 parent 21a78c8 commit 1b5999b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
11 changes: 10 additions & 1 deletion ReferenceTests/src/tests/figures_and_makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,13 @@ end
Makie.Checkbox(f[2, 4], checked = false, checkboxcolor_unchecked = :yellow)
Makie.Checkbox(f[2, 5], checked = true, checkboxcolor_checked = :orange)
f
end
end

@reference_test "Toggle" begin
f = Figure()
th = Makie.Toggle(f[1,1])
th.orientation[] = pi/2
tv = Makie.Toggle(f[2,1], orientation=pi/2)
tv.orientation[] = 0
f
end
8 changes: 8 additions & 0 deletions src/makielayout/blocks/toggle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ function initialize_block!(t::Toggle)
button = scatter!(topscene, buttonpos, markersize = buttonsize,
color = t.buttoncolor, strokewidth = 0, inspectable = false, marker = Circle)

onany(topscene, t.orientation, t.layoutobservables.computedbbox) do angle_rad, bbox
center = Vec3f(bbox.origin[1]+bbox.widths[1]/2, bbox.origin[2]+bbox.widths[2]/2, 0)
R = Makie.rotationmatrix_z(angle_rad)
T = Makie.translationmatrix(-center)
Tinv = Makie.translationmatrix(center)
topscene.transformation.model[] = Tinv * R * T
end

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

onmouseleftdown(mouseevents) do event
Expand Down
23 changes: 21 additions & 2 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1152,15 +1152,32 @@ const CHECKMARK_BEZIER = scale(BezierPath(
end
end

"""
A switch with two states.
## Constructors
```julia
Toggle(fig_or_scene; kwargs...)
```
## Examples
```julia
t_horizontal = Toggle(fig[1, 1])
t_vertical = Toggle(fig[2, 1], orientation = pi/2)
```
"""
@Block Toggle begin
@attributes begin
"The horizontal alignment of the toggle in its suggested bounding box."
halign = :center
"The vertical alignment of the toggle in its suggested bounding box."
valign = :center
"The width of the toggle."
"The width (ie longitudinal length) of the toggle."
width = 32
"The height of the toggle."
"The height (ie transverse length) of the toggle."
height = 18
"Controls if the parent layout can adjust to this element's width"
tellwidth = true
Expand All @@ -1185,6 +1202,8 @@ end
rimfraction = 0.33
"The align mode of the toggle in its parent GridLayout."
alignmode = Inside()
"The orientation of the toggle (-pi to pi; 0 is horizontal with \"on\" being to the right)."
orientation = 0
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,9 @@ end
end
@test isempty(limits.listeners)
end

@testset "Toggle" begin
f = Figure()
Toggle(f[1,1])
Toggle(f[2,1], orientation=pi/2)
end

0 comments on commit 1b5999b

Please sign in to comment.