Skip to content

Commit

Permalink
add BoxLayout (#30)
Browse files Browse the repository at this point in the history
* add Point type and add BoundingBox constructor using Point

* add layout.jl

* use BoxLayout top place widgets in example.jl
  • Loading branch information
Sid-Bhatia-0 authored May 6, 2022
1 parent cdbdaec commit 33d821b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
10 changes: 6 additions & 4 deletions examples/example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,12 @@ function start()

compute_time_start = time_ns()

layout = SW.BoxLayout(SW.BoundingBox(577, 1, 576, 0))

SD.draw!(image, SD.Background(), background_color)

button1 = SW.WidgetID(@__LINE__, @__FILE__)
button1_bounding_box = SW.BoundingBox(577, 1, 608, 200)
layout, button1_bounding_box = SW.add_widget(layout, SW.VERTICAL, 32, 200)
button1_value = SW.do_widget!(user_interaction_state, button1, SW.BUTTON, button1_bounding_box, user_input_state.cursor, user_input_state.mouse_left)
if button1_value
text_color = 0x00aa0000
Expand All @@ -190,7 +192,7 @@ function start()
SD.draw!(image, SD.TextLine(button1_rectangle.position, "Button 1", font), text_color)

button2 = SW.WidgetID(@__LINE__, @__FILE__)
button2_bounding_box = SW.BoundingBox(609, 1, 640, 200)
layout, button2_bounding_box = SW.add_widget(layout, SW.VERTICAL, 32, 200)
button2_value = SW.do_widget!(user_interaction_state, button2, SW.BUTTON, button2_bounding_box, user_input_state.cursor, user_input_state.mouse_left)
if button2_value
text_color = 0x00000000
Expand All @@ -200,15 +202,15 @@ function start()
SD.draw!(image, SD.TextLine(button2_rectangle.position, "Button 2", font), text_color)

slider = SW.WidgetID(@__LINE__, @__FILE__)
slider_bounding_box = SW.BoundingBox(641, 1, 672, 200)
layout, slider_bounding_box = SW.add_widget(layout, SW.VERTICAL, 32, 200)
slider_value = SW.do_widget!(user_interaction_state, slider, SW.SLIDER, slider_bounding_box, user_input_state.cursor, user_input_state.mouse_left, slider_value)
slider_rectangle = convert(SD.Rectangle{Int}, slider_bounding_box)
SD.draw!(image, slider_rectangle, text_color)
SD.draw!(image, SD.FilledRectangle(slider_rectangle.position, slider_rectangle.height, slider_value), text_color)
SD.draw!(image, SD.TextLine(slider_rectangle.position, "Slider", font), 0x00ffffff)

text_input = SW.WidgetID(@__LINE__, @__FILE__)
text_input_bounding_box = SW.BoundingBox(673, 1, 704, 200)
layout, text_input_bounding_box = SW.add_widget(layout, SW.VERTICAL, 32, 200)
SW.do_widget!(user_interaction_state, text_input, SW.TEXT_INPUT, text_input_bounding_box, user_input_state.cursor, user_input_state.mouse_left, text_line, user_input_state.characters)
text_input_rectangle = convert(SD.Rectangle{Int}, text_input_bounding_box)
SD.draw!(image, text_input_rectangle, text_color)
Expand Down
1 change: 1 addition & 0 deletions src/SimpleWidgets.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module SimpleWidgets

include("input.jl")
include("layout.jl")
include("utils.jl")
include("widgets.jl")
include("ui_state.jl")
Expand Down
7 changes: 7 additions & 0 deletions src/input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ struct InputButton
half_transition_count::Int
end

struct Point
i::Int
j::Int
end

struct Cursor
i::Int
j::Int
Expand All @@ -15,6 +20,8 @@ struct BoundingBox
j_max::Int
end

BoundingBox(point::Point, height, width) = BoundingBox(point.i, point.j, point.i + height - one(height), point.j + width - one(width))

press_button(button) = InputButton(true, button.half_transition_count + one(button.half_transition_count))
release_button(button) = InputButton(false, button.half_transition_count + one(button.half_transition_count))
reset(button) = InputButton(button.ended_down, zero(button.half_transition_count))
Expand Down
28 changes: 28 additions & 0 deletions src/layout.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
abstract type AbstractLayout end

struct BoxLayout <: AbstractLayout
bounding_box::BoundingBox
end

abstract type AbstractDirection end

struct Vertical <: AbstractDirection end
const VERTICAL = Vertical()

struct Horizontal <: AbstractDirection end
const HORIZONTAL = Horizontal()

update_layout(layout::BoxLayout, ::Vertical, height, width) = BoxLayout(BoundingBox(layout.bounding_box.i_min, layout.bounding_box.j_min, layout.bounding_box.i_max + height, max(layout.bounding_box.j_max, layout.bounding_box.j_min + width - one(width))))

update_layout(layout::BoxLayout, ::Horizontal, height, width) = BoxLayout(BoundingBox(layout.bounding_box.i_min, layout.bounding_box.j_min, max(layout.bounding_box.i_max, layout.bounding_box.i_min + height - one(height)), layout.bounding_box.j_max + width))

get_widget_position(layout::BoxLayout, ::Vertical) = Point(layout.bounding_box.i_max + one(layout.bounding_box.i_max), layout.bounding_box.j_min)

get_widget_position(layout::BoxLayout, ::Horizontal) = Point(layout.bounding_box.i_min, layout.bounding_box.j_max + one(layout.bounding_box.j_max))

function get_widget_bounding_box(layout::BoxLayout, direction::AbstractDirection, height, width)
widget_position = get_widget_position(layout, direction)
return BoundingBox(widget_position, height, width)
end

add_widget(layout::BoxLayout, direction::AbstractDirection, height, width) = update_layout(layout, direction, height, width), get_widget_bounding_box(layout, direction, height, width)

0 comments on commit 33d821b

Please sign in to comment.