From 1b5999b301a331cb80b87fd68583f5a2d43a6b08 Mon Sep 17 00:00:00 2001 From: Ben Arthur Date: Wed, 9 Oct 2024 11:12:04 -0400 Subject: [PATCH] add kwarg to rotate Toggle --- .../src/tests/figures_and_makielayout.jl | 11 ++++++++- src/makielayout/blocks/toggle.jl | 8 +++++++ src/makielayout/types.jl | 23 +++++++++++++++++-- test/makielayout.jl | 6 +++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ReferenceTests/src/tests/figures_and_makielayout.jl b/ReferenceTests/src/tests/figures_and_makielayout.jl index b61da7b1348..12cd7179a6e 100644 --- a/ReferenceTests/src/tests/figures_and_makielayout.jl +++ b/ReferenceTests/src/tests/figures_and_makielayout.jl @@ -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 \ No newline at end of file +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 diff --git a/src/makielayout/blocks/toggle.jl b/src/makielayout/blocks/toggle.jl index 2c37f8d38ba..69ba594cb30 100644 --- a/src/makielayout/blocks/toggle.jl +++ b/src/makielayout/blocks/toggle.jl @@ -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 diff --git a/src/makielayout/types.jl b/src/makielayout/types.jl index 4da455bf423..50dda1ec660 100644 --- a/src/makielayout/types.jl +++ b/src/makielayout/types.jl @@ -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 @@ -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 diff --git a/test/makielayout.jl b/test/makielayout.jl index 6c266f0b2a2..e9b2d55e09d 100644 --- a/test/makielayout.jl +++ b/test/makielayout.jl @@ -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