Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add RadioButton widget type and reuse Button in CheckBox #51

Merged
merged 3 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function start()
colors = SI.COLORS
sliding_window_size = 30
i = 0
radio_button_value = 1

time_stamp_buffer = DS.CircularBuffer{typeof(time_ns())}(sliding_window_size)
push!(time_stamp_buffer, time_ns())
Expand Down Expand Up @@ -279,6 +280,29 @@ function start()
push!(debug_text, "text_box_value: $(text_box_value)")

layout.reference_bounding_box = reference_bounding_box
for j in Base.OneTo(3)
if SI.do_widget!(
SI.RADIO_BUTTON,
user_interaction_state,
SI.WidgetID(@__FILE__, @__LINE__, j),
radio_button_value == j,
user_input_state.cursor,
user_input_state.mouse_left,
layout,
SI.DOWN2_LEFT1,
padding,
SD.get_height(font),
360,
image,
"radio button $(j)",
font,
colors,
)
radio_button_value = j
end
end
push!(debug_text, "radio_button_value: $(radio_button_value)")

SI.do_widget!(
SI.TEXT,
user_interaction_state,
Expand Down
32 changes: 32 additions & 0 deletions src/color_scheme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@
COLOR_ACTIVE_CHECK_BOX_BORDER
COLOR_ACTIVE_CHECK_BOX_TEXT
COLOR_ACTIVE_CHECK_BOX_BOX

# RadioButton
COLOR_NEUTRAL_RADIO_BUTTON_BACKGROUND
COLOR_NEUTRAL_RADIO_BUTTON_BORDER
COLOR_NEUTRAL_RADIO_BUTTON_TEXT
COLOR_NEUTRAL_RADIO_BUTTON_INDICATOR

COLOR_HOT_RADIO_BUTTON_BACKGROUND
COLOR_HOT_RADIO_BUTTON_BORDER
COLOR_HOT_RADIO_BUTTON_TEXT
COLOR_HOT_RADIO_BUTTON_INDICATOR

COLOR_ACTIVE_RADIO_BUTTON_BACKGROUND
COLOR_ACTIVE_RADIO_BUTTON_BORDER
COLOR_ACTIVE_RADIO_BUTTON_TEXT
COLOR_ACTIVE_RADIO_BUTTON_INDICATOR
end

const COLORS = zeros(UInt32, length(instances(Colorable)))
Expand Down Expand Up @@ -147,3 +163,19 @@ COLORS[Integer(COLOR_ACTIVE_CHECK_BOX_BACKGROUND)] = COLORS[Integer(COLOR_BACKGR
COLORS[Integer(COLOR_ACTIVE_CHECK_BOX_BORDER)] = COLORS[Integer(COLOR_BACKGROUND)]
COLORS[Integer(COLOR_ACTIVE_CHECK_BOX_TEXT)] = 0x00000000
COLORS[Integer(COLOR_ACTIVE_CHECK_BOX_BOX)] = 0x00000000

# RadioButton
COLORS[Integer(COLOR_NEUTRAL_RADIO_BUTTON_BACKGROUND)] = COLORS[Integer(COLOR_BACKGROUND)]
COLORS[Integer(COLOR_NEUTRAL_RADIO_BUTTON_BORDER)] = COLORS[Integer(COLOR_BACKGROUND)]
COLORS[Integer(COLOR_NEUTRAL_RADIO_BUTTON_TEXT)] = 0x00000000
COLORS[Integer(COLOR_NEUTRAL_RADIO_BUTTON_INDICATOR)] = 0x00000000

COLORS[Integer(COLOR_HOT_RADIO_BUTTON_BACKGROUND)] = COLORS[Integer(COLOR_BACKGROUND)]
COLORS[Integer(COLOR_HOT_RADIO_BUTTON_BORDER)] = COLORS[Integer(COLOR_BACKGROUND)]
COLORS[Integer(COLOR_HOT_RADIO_BUTTON_TEXT)] = 0x00000000
COLORS[Integer(COLOR_HOT_RADIO_BUTTON_INDICATOR)] = 0x00000000

COLORS[Integer(COLOR_ACTIVE_RADIO_BUTTON_BACKGROUND)] = COLORS[Integer(COLOR_BACKGROUND)]
COLORS[Integer(COLOR_ACTIVE_RADIO_BUTTON_BORDER)] = COLORS[Integer(COLOR_BACKGROUND)]
COLORS[Integer(COLOR_ACTIVE_RADIO_BUTTON_TEXT)] = 0x00000000
COLORS[Integer(COLOR_ACTIVE_RADIO_BUTTON_INDICATOR)] = 0x00000000
33 changes: 33 additions & 0 deletions src/drawing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,36 @@ function draw_widget!(image, bounding_box, widget_type::CheckBox, user_interacti

return nothing
end

function draw_widget!(image, bounding_box, widget_type::RadioButton, user_interaction_state, this_widget, widget_value, text, font, colors)
alignment = LEFT1
padding = -1

if this_widget == user_interaction_state.active_widget
background_color = colors[Integer(COLOR_ACTIVE_RADIO_BUTTON_BACKGROUND)]
border_color = colors[Integer(COLOR_ACTIVE_RADIO_BUTTON_BORDER)]
text_color = colors[Integer(COLOR_ACTIVE_RADIO_BUTTON_TEXT)]
indicator_color = colors[Integer(COLOR_ACTIVE_RADIO_BUTTON_INDICATOR)]
elseif this_widget == user_interaction_state.hot_widget
background_color = colors[Integer(COLOR_HOT_RADIO_BUTTON_BACKGROUND)]
border_color = colors[Integer(COLOR_HOT_RADIO_BUTTON_BORDER)]
text_color = colors[Integer(COLOR_HOT_RADIO_BUTTON_TEXT)]
indicator_color = colors[Integer(COLOR_HOT_RADIO_BUTTON_INDICATOR)]
else
background_color = colors[Integer(COLOR_NEUTRAL_RADIO_BUTTON_BACKGROUND)]
border_color = colors[Integer(COLOR_NEUTRAL_RADIO_BUTTON_BORDER)]
text_color = colors[Integer(COLOR_NEUTRAL_RADIO_BUTTON_TEXT)]
indicator_color = colors[Integer(COLOR_NEUTRAL_RADIO_BUTTON_INDICATOR)]
end

font_width = SD.get_width(font)
indicator_width = oftype(font_width, 2) * font_width
x = indicator_width ÷ oftype(indicator_width, 8)
SD.draw!(image, SD.ThickCircle(SD.move(bounding_box.position, x, x), oftype(x, 6) * x, x), indicator_color)
if widget_value
SD.draw!(image, SD.FilledCircle(SD.move(bounding_box.position, oftype(x, 3) * x, oftype(x, 3) * x), oftype(x, 2) * x), indicator_color)
end
draw_text_line_in_a_box!(image, SD.Rectangle(SD.move_j(bounding_box.position, indicator_width), bounding_box.height, bounding_box.width - indicator_width), text, font, alignment, padding, background_color, border_color, text_color)

return nothing
end
63 changes: 43 additions & 20 deletions src/widgets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const TEXT = Text()
struct CheckBox <: AbstractWidgetType end
const CHECK_BOX = CheckBox()

struct RadioButton <: AbstractWidgetType end
const RADIO_BUTTON = RadioButton()

#####
##### utils
#####
Expand Down Expand Up @@ -382,28 +385,12 @@ end
##### CheckBox
#####

function get_widget_value(::CheckBox, hot_widget, active_widget, this_widget, widget_value, condition)
if (hot_widget == this_widget) && (active_widget == this_widget) && condition
return !widget_value
else
return widget_value
end
end

function do_widget(widget_type::CheckBox, hot_widget, active_widget, null_widget, this_widget, widget_value, i_mouse, j_mouse, ended_down, num_transitions, i_min, j_min, i_max, j_max)
mouse_over_widget = (i_min <= i_mouse <= i_max) && (j_min <= j_mouse <= j_max)
mouse_went_down = went_down(ended_down, num_transitions)
mouse_went_up = went_up(ended_down, num_transitions)

hot_widget = try_set_hot_widget(hot_widget, active_widget, null_widget, this_widget, mouse_over_widget)

active_widget = try_set_active_widget(hot_widget, active_widget, null_widget, this_widget, mouse_over_widget && mouse_went_down)

widget_value = get_widget_value(widget_type, hot_widget, active_widget, this_widget, widget_value, mouse_over_widget && mouse_went_up)

active_widget = try_reset_active_widget(hot_widget, active_widget, null_widget, this_widget, mouse_went_up)
hot_widget, active_widget, null_widget, button_value = do_widget(BUTTON, hot_widget, active_widget, null_widget, this_widget, i_mouse, j_mouse, ended_down, num_transitions, i_min, j_min, i_max, j_max)

hot_widget = try_reset_hot_widget(hot_widget, active_widget, null_widget, this_widget, !mouse_over_widget)
if button_value
widget_value = !widget_value
end

return hot_widget, active_widget, null_widget, widget_value
end
Expand Down Expand Up @@ -460,3 +447,39 @@ function do_widget!(

return widget_value
end

#####
##### RadioButton
#####

do_widget(widget_type::RadioButton, hot_widget, active_widget, null_widget, this_widget, widget_value, i_mouse, j_mouse, ended_down, num_transitions, i_min, j_min, i_max, j_max) = do_widget(CHECK_BOX, hot_widget, active_widget, null_widget, this_widget, widget_value, i_mouse, j_mouse, ended_down, num_transitions, i_min, j_min, i_max, j_max)

do_widget!(widget_type::RadioButton, user_interaction_state::AbstractUserInteractionState, this_widget, widget_value, cursor, input_button, widget_bounding_box) = do_widget!(CHECK_BOX, user_interaction_state, this_widget, widget_value, cursor, input_button, widget_bounding_box)

function do_widget!(
widget_type::RadioButton,
user_interaction_state::AbstractUserInteractionState,
this_widget,
widget_value,
cursor,
input_button,
layout,
alignment,
padding,
widget_height,
widget_width,
image,
text,
font,
colors,
)

widget_bounding_box = get_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)

SD.draw!(image, widget_bounding_box, widget_type, user_interaction_state, this_widget, widget_value, text, font, colors)

return widget_value
end