Skip to content

Commit

Permalink
refactor to rotate manually, thereby fixing alignment problem
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed Nov 4, 2024
1 parent 4968834 commit c2394c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
37 changes: 18 additions & 19 deletions src/makielayout/blocks/toggle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ function initialize_block!(t::Toggle)

topscene = t.blockscene

markersize = lift(topscene, t.layoutobservables.computedbbox) do bbox
min(width(bbox), height(bbox))
onany(topscene, t.orientation, t.length, t.markersize) do or, len, ms
theta = or == :horizontal ? 0 : or == :vertical ? pi/2 : or
t.width[] = (len - ms) * cos(theta) + ms
t.height[] = (len - ms) * sin(theta) + ms

Check warning on line 8 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L5-L8

Added lines #L5 - L8 were not covered by tests
end

button_endpoint_inactive = lift(topscene, markersize) do ms
bbox = t.layoutobservables.computedbbox[]
button_endpoint_inactive = lift(topscene, t.markersize, t.layoutobservables.computedbbox) do ms, bbox

Check warning on line 11 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L11

Added line #L11 was not covered by tests

Point2f(left(bbox) + ms / 2, bottom(bbox) + ms / 2)
end

button_endpoint_active = lift(topscene, markersize) do ms
bbox = t.layoutobservables.computedbbox[]
Point2f(right(bbox) - ms / 2, bottom(bbox) + ms / 2)
button_endpoint_active = lift(topscene, t.markersize, t.layoutobservables.computedbbox) do ms, bbox
Point2f(right(bbox) - ms / 2, top(bbox) - ms / 2)

Check warning on line 17 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L16-L17

Added lines #L16 - L17 were not covered by tests
end

buttonvertices = lift(topscene, markersize, t.cornersegments) do ms, cs
roundedrectvertices(t.layoutobservables.computedbbox[], ms * 0.499, cs)
buttonvertices = lift(topscene, t.length, t.markersize, t.cornersegments, t.orientation, t.layoutobservables.computedbbox) do len, ms, cs, or, bbox
rect0 = GeometryBasics.HyperRectangle(-ms/2, -ms/2, len, ms)
rrv = roundedrectvertices(rect0, ms * 0.499, cs)
theta = or == :horizontal ? 0 : or == :vertical ? pi/2 : or
costheta, sintheta = cos(theta), sin(theta)
rotmatrix = GeometryBasics.@SMatrix [costheta -sintheta

Check warning on line 25 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L20-L25

Added lines #L20 - L25 were not covered by tests
sintheta costheta]
Ref(rotmatrix) .* rrv .+ Ref(GeometryBasics.@SVector [left(bbox)+ms/2, bottom(bbox)+ms/2])

Check warning on line 27 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L27

Added line #L27 was not covered by tests
end

# trigger bbox
notify(t.length)

Check warning on line 31 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L31

Added line #L31 was not covered by tests
notify(t.layoutobservables.suggestedbbox)

framecolor = Observable{Any}(t.active[] ? t.framecolor_active[] : t.framecolor_inactive[])
Expand All @@ -37,22 +45,13 @@ function initialize_block!(t::Toggle)
end

buttonfactor = Observable(1.0)
buttonsize = lift(topscene, markersize, t.rimfraction, buttonfactor) do ms, rf, bf
buttonsize = lift(topscene, t.markersize, t.rimfraction, buttonfactor) do ms, rf, bf

Check warning on line 48 in src/makielayout/blocks/toggle.jl

View check run for this annotation

Codecov / codecov/patch

src/makielayout/blocks/toggle.jl#L48

Added line #L48 was not covered by tests
ms * (1 - rf) * bf
end

button = scatter!(topscene, buttonpos, markersize = buttonsize,
color = t.buttoncolor, strokewidth = 0, inspectable = false, marker = Circle)

onany(topscene, t.orientation, t.layoutobservables.computedbbox) do or, bbox
angle_rad = or == :horizontal ? 0 : or == :vertical ? pi/2 : or
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)

updatefunc = Ref{Any}(nothing)
Expand Down
12 changes: 8 additions & 4 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1179,10 +1179,10 @@ end
halign = :center
"The vertical alignment of the toggle in its suggested bounding box."
valign = :center
"The width (ie longitudinal length) of the toggle."
width = 32
"The height (ie transverse length) of the toggle."
height = 18
"The width of the bounding box. Use `length` and `markersize` to set the dimensions of the toggle."
width = Auto()
"The height of the bounding box. Use `length` and `markersize` to set the dimensions of the toggle."
height = Auto()
"Controls if the parent layout can adjust to this element's width"
tellwidth = true
"Controls if the parent layout can adjust to this element's height"
Expand All @@ -1208,6 +1208,10 @@ end
alignmode = Inside()
"The orientation of the toggle. Can be :horizontal, :vertical, or -pi to pi. 0 is horizontal with \"on\" being to the right."
orientation = :horizontal
"The length of the toggle."
length = 32
"The size of the button."
markersize = 18
end
end

Expand Down

0 comments on commit c2394c9

Please sign in to comment.