Skip to content

Commit

Permalink
fix layout related things and add tests (#60)
Browse files Browse the repository at this point in the history
* use type promotion instead of manual conversion in get_alignment_offset

* change argument order of get_alignment_offset

* use get_enclosing_bounding_box and get_alignment_bounding_box instead of get_bounding_box

* fix get_alignment_offset behavior for UP_IN_LEFT_IN

* add tests
  • Loading branch information
Sid-Bhatia-0 authored Aug 16, 2022
1 parent 72175e4 commit 9301fe0
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 43 deletions.
6 changes: 3 additions & 3 deletions examples/example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ function start()
)
radio_button_value = j
end
reference_bounding_box = SI.get_bounding_box(reference_bounding_box, layout.reference_bounding_box)
reference_bounding_box = SI.get_enclosing_bounding_box(reference_bounding_box, layout.reference_bounding_box)
end

layout.reference_bounding_box = reference_bounding_box
Expand Down Expand Up @@ -396,7 +396,7 @@ function start()
0x00000000,
0x00000000,
)
reference_bounding_box = SI.get_bounding_box(reference_bounding_box, layout.reference_bounding_box)
reference_bounding_box = SI.get_enclosing_bounding_box(reference_bounding_box, layout.reference_bounding_box)

if drop_down_value
for (j, item) in enumerate(drop_down_item_list)
Expand Down Expand Up @@ -424,7 +424,7 @@ function start()
)
drop_down_selected_item = j
end
reference_bounding_box = SI.get_bounding_box(reference_bounding_box, layout.reference_bounding_box)
reference_bounding_box = SI.get_enclosing_bounding_box(reference_bounding_box, layout.reference_bounding_box)
end
end

Expand Down
6 changes: 3 additions & 3 deletions src/drawing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function draw_text_line_in_a_box!(image, bounding_box, text, font, alignment, pa

SD.draw!(image, bounding_box, border_color)

i_offset, j_offset = get_alignment_offset(bounding_box.height, bounding_box.width, SD.get_height(font), SD.get_width(font) * get_num_printable_characters(text), alignment, padding)
i_offset, j_offset = get_alignment_offset(bounding_box.height, bounding_box.width, alignment, padding, SD.get_height(font), SD.get_width(font) * get_num_printable_characters(text))
SD.draw!(image, SD.TextLine(SD.move(bounding_box.position, i_offset, j_offset), text, font), text_color)

return nothing
Expand All @@ -54,7 +54,7 @@ function draw_text_line_with_slider_in_a_box!(image, bounding_box, slider_value,

SD.draw!(image, bounding_box, border_color)

i_offset, j_offset = get_alignment_offset(bounding_box.height, bounding_box.width, SD.get_height(font), SD.get_width(font) * get_num_printable_characters(text), alignment, padding)
i_offset, j_offset = get_alignment_offset(bounding_box.height, bounding_box.width, alignment, padding, SD.get_height(font), SD.get_width(font) * get_num_printable_characters(text))
SD.draw!(image, SD.TextLine(SD.move(bounding_box.position, i_offset, j_offset), text, font), text_color)

return nothing
Expand Down Expand Up @@ -127,7 +127,7 @@ function draw_widget_unclipped!(image, widget_type::TextBox, bounding_box, user_

if this_widget == user_interaction_state.active_widget
num_printable_characters = get_num_printable_characters(text)
_, j_offset = get_alignment_offset(bounding_box.height, bounding_box.width, SD.get_height(font), SD.get_width(font) * num_printable_characters, alignment, padding)
_, j_offset = get_alignment_offset(bounding_box.height, bounding_box.width, alignment, padding, SD.get_height(font), SD.get_width(font) * num_printable_characters)
SD.draw!(image, SD.FilledRectangle(SD.move_j(bounding_box.position, j_offset + num_printable_characters * SD.get_width(font) - one(j_offset)), bounding_box.height, oftype(bounding_box.width, 2)), get_color(user_interaction_state, this_widget, text_color))
end

Expand Down
63 changes: 33 additions & 30 deletions src/layout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,67 +36,70 @@ end
DOWN_OUT_RIGHT_OUT
end

function get_alignment_offset(total_height, total_width, content_height, content_width, alignment, padding)
I = typeof(total_height)
function get_alignment_offset(total_height, total_width, alignment, padding, content_height, content_width)
total_height_promoted, total_width_promoted, content_height_promoted, content_width_promoted, padding_promoted = promote(total_height, total_width, content_height, content_width, padding)
return get_alignment_offset(total_width_promoted, total_width_promoted, alignment, padding_promoted, content_height_promoted, content_width_promoted)
end

function get_alignment_offset(total_height::I, total_width::I, alignment, padding::I, content_height::I, content_width::I) where {I}
if alignment == UP_OUT_LEFT_OUT
return (convert(I, -content_height - padding), convert(I, -content_width - padding))
return (-content_height - padding, -content_width - padding)
elseif alignment == UP_IN_LEFT_OUT
return (zero(I), convert(I, -content_width - padding))
return (zero(I), -content_width - padding)
elseif alignment == LEFT_OUT
return (convert(I, (total_height - content_height) ÷ 2), convert(I, -content_width - padding))
return ((total_height - content_height) ÷ convert(I, 2), -content_width - padding)
elseif alignment == DOWN_IN_LEFT_OUT
return (convert(I, total_height - content_height), convert(I, -content_width - padding))
return (total_height - content_height, -content_width - padding)
elseif alignment == DOWN_OUT_LEFT_OUT
return (convert(I, total_height + padding), convert(I, -content_width - padding))
return (total_height + padding, -content_width - padding)

elseif alignment == UP_OUT_LEFT_IN
return (convert(I, -content_height - padding), zero(I))
return (-content_height - padding, zero(I))
elseif alignment == UP_IN_LEFT_IN
return (convert(I, padding), convert(I, padding + 1))
return (padding + one(I), padding + one(I))
elseif alignment == LEFT_IN
return (convert(I, (total_height - content_height) ÷ 2), convert(I, padding + 1))
return ((total_height - content_height) ÷ convert(I, 2), padding + one(I))
elseif alignment == DOWN_IN_LEFT_IN
return (convert(I, total_height - content_height - padding - 1), convert(I, padding + 1))
return (total_height - content_height - padding - one(I), padding + one(I))
elseif alignment == DOWN_OUT_LEFT_IN
return (convert(I, total_height + padding), zero(I))
return (total_height + padding, zero(I))

elseif alignment == UP_OUT
return (convert(I, -content_height - padding), convert(I, (total_width - content_width) ÷ 2))
return (-content_height - padding, (total_width - content_width) ÷ convert(I, 2))
elseif alignment == UP_IN
return (convert(I, padding + 1), convert(I, (total_width - content_width) ÷ 2))
return (padding + one(I), (total_width - content_width) ÷ convert(I, 2))
elseif alignment == CENTER
return (convert(I, (total_height - content_height) ÷ 2), convert(I, (total_width - content_width) ÷ 2))
return ((total_height - content_height) ÷ convert(I, 2), (total_width - content_width) ÷ convert(I, 2))
elseif alignment == DOWN_IN
return (convert(I, total_height - content_height - padding - 1), convert(I, (total_width - content_width) ÷ 2))
return (total_height - content_height - padding - one(I), (total_width - content_width) ÷ convert(I, 2))
elseif alignment == DOWN_OUT
return (convert(I, total_height + padding), convert(I, (total_width - content_width) ÷ 2))
return (total_height + padding, (total_width - content_width) ÷ convert(I, 2))

elseif alignment == UP_OUT_RIGHT_IN
return (convert(I, -content_height - padding), convert(I, total_width - content_width))
return (-content_height - padding, total_width - content_width)
elseif alignment == UP_IN_RIGHT_IN
return (convert(I, padding + 1), convert(I, total_width - content_width - padding - 1))
return (padding + one(I), total_width - content_width - padding - one(I))
elseif alignment == RIGHT_IN
return (convert(I, (total_height - content_height) ÷ 2), convert(I, total_width - content_width - padding - 1))
return ((total_height - content_height) ÷ convert(I, 2), total_width - content_width - padding - one(I))
elseif alignment == DOWN_IN_RIGHT_IN
return (convert(I, total_height - content_height - padding - 1), convert(I, total_width - content_width - padding - 1))
return (total_height - content_height - padding - one(I), total_width - content_width - padding - one(I))
elseif alignment == DOWN_OUT_RIGHT_IN
return (convert(I, total_height + padding), convert(I, total_width - content_width))
return (total_height + padding, total_width - content_width)

elseif alignment == UP_OUT_RIGHT_OUT
return (convert(I, -content_height - padding), convert(I, total_width + padding))
return (-content_height - padding, total_width + padding)
elseif alignment == UP_IN_RIGHT_OUT
return (zero(I), convert(I, total_width + padding))
return (zero(I), total_width + padding)
elseif alignment == RIGHT_OUT
return (convert(I, (total_height - content_height) ÷ 2), convert(I, total_width + padding))
return ((total_height - content_height) ÷ convert(I, 2), total_width + padding)
elseif alignment == DOWN_IN_RIGHT_OUT
return (convert(I, total_height - content_height), convert(I, total_width + padding))
return (total_height - content_height, total_width + padding)
else
return (convert(I, total_height + padding), convert(I, total_width + padding))
return (total_height + padding, total_width + padding)
end
end

function get_bounding_box(shapes...)
function get_enclosing_bounding_box(shapes...)
shape1 = shapes[1]
i_min = SD.get_i_min(shape1)
j_min = SD.get_j_min(shape1)
Expand All @@ -113,7 +116,7 @@ function get_bounding_box(shapes...)
return SD.Rectangle(SD.Point(i_min, j_min), i_max - i_min + one(i_min), j_max - j_min + one(j_min))
end

function get_bounding_box(bounding_box::SD.Rectangle, alignment::Alignment, padding, height, width)
i_offset, j_offset = get_alignment_offset(bounding_box.height, bounding_box.width, height, width, alignment, padding)
function get_alignment_bounding_box(bounding_box::SD.Rectangle, alignment::Alignment, padding, height, width)
i_offset, j_offset = get_alignment_offset(bounding_box.height, bounding_box.width, alignment, padding, height, width)
return SD.Rectangle(SD.move(bounding_box.position, i_offset, j_offset), height, width)
end
6 changes: 3 additions & 3 deletions src/widgets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ function do_widget!(
text_color,
)

widget_bounding_box = get_bounding_box(layout.reference_bounding_box, alignment, padding, widget_height, widget_width)
widget_bounding_box = get_alignment_bounding_box(layout.reference_bounding_box, alignment, padding, widget_height, widget_width)
layout.reference_bounding_box = widget_bounding_box

widget_value = do_widget!(widget_type, user_interaction_state, this_widget, cursor, input_button, widget_bounding_box)
Expand Down Expand Up @@ -334,7 +334,7 @@ function do_widget!(
indicator_color,
)

widget_bounding_box = get_bounding_box(layout.reference_bounding_box, alignment, padding, widget_height, widget_width)
widget_bounding_box = get_alignment_bounding_box(layout.reference_bounding_box, alignment, padding, widget_height, widget_width)
layout.reference_bounding_box = widget_bounding_box

widget_value = do_widget!(widget_type, user_interaction_state, this_widget, widget_value, cursor, input_button, widget_bounding_box)
Expand Down Expand Up @@ -366,7 +366,7 @@ function do_widget!(
text_color,
)

widget_bounding_box = get_bounding_box(layout.reference_bounding_box, alignment, padding, widget_height, widget_width)
widget_bounding_box = get_alignment_bounding_box(layout.reference_bounding_box, alignment, padding, widget_height, widget_width)
layout.reference_bounding_box = widget_bounding_box

widget_value = do_widget!(widget_type, user_interaction_state, this_widget, widget_value, cursor, input_button, characters, widget_bounding_box)
Expand Down
Loading

0 comments on commit 9301fe0

Please sign in to comment.