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 slider widget #10

Merged
merged 1 commit into from
Apr 1, 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
12 changes: 12 additions & 0 deletions example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function start()

hot_widget = NULL_WIDGET_ID
active_widget = NULL_WIDGET_ID
slider_value = 1

while !GLFW.WindowShouldClose(window)
drawing_time_start = time_ns()
Expand All @@ -159,6 +160,16 @@ function start()
end
SD.draw!(image, button2_shape, 0x0000aa00)

slider_shape = SD.FilledRectangle(SD.Point(600, 600), 50, 100)
slider_id = WidgetID(@__LINE__, @__FILE__)
hot_widget, active_widget, slider_value = widget(hot_widget, active_widget, slider_id, UI_SLIDER, SD.get_i_min(slider_shape), SD.get_j_min(slider_shape), SD.get_i_max(slider_shape), SD.get_j_max(slider_shape), cursor.i, cursor.j, mouse_left.ended_down, mouse_left.half_transition_count, slider_value)
# if slider_value
# text_color = 0x0000aa00
# end
SD.draw!(image, slider_shape, 0x00000000)
slider_value_shape = SD.FilledRectangle(SD.Point(600, 600), 50, slider_value)
SD.draw!(image, slider_value_shape, 0x000000aa)

empty!(lines)
push!(lines, "previous frame number: $(i)")
push!(lines, "average time spent per frame (averaged over previous $(length(time_stamp_buffer)) frames): $(round((last(time_stamp_buffer) - first(time_stamp_buffer)) / (1e6 * length(time_stamp_buffer)), digits = 2)) ms")
Expand All @@ -176,6 +187,7 @@ function start()
push!(lines, "button1_id: $(button1_id)")
push!(lines, "button2_id: $(button2_id)")
push!(lines, "text_color: $(repr(text_color))")
push!(lines, "slider_value: $(slider_value)")
push!(lines, "hot_widget: $(hot_widget)")
push!(lines, "active_widget: $(active_widget)")

Expand Down
30 changes: 30 additions & 0 deletions ui.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ abstract type AbstractWidget end
struct UIButton <: AbstractWidget end
const UI_BUTTON = UIButton()

struct UISlider <: AbstractWidget end
const UI_SLIDER = UISlider()

went_down(ended_down, half_transition_count) = (half_transition_count >= 2) || ((half_transition_count == 1) && ended_down)
went_up(ended_down, half_transition_count) = (half_transition_count >= 2) || ((half_transition_count == 1) && !ended_down)

Expand Down Expand Up @@ -70,3 +73,30 @@ function widget(hot_widget, active_widget, widget, widget_type::UIButton, i_min,

return hot_widget, active_widget, value
end

function get_widget_value(hot_widget, active_widget, widget, ::UISlider, active_value, last_value)
if (active_widget == widget) && (hot_widget == widget)
return active_value
else
return last_value
end
end

function widget(hot_widget, active_widget, widget, widget_type::UISlider, i_min, j_min, i_max, j_max, i_mouse, j_mouse, ended_down, half_transition_count, last_value)
mouse_over_slider = (i_min <= i_mouse <= i_max) && (j_min <= j_mouse <= j_max)
mouse_went_down = went_down(ended_down, half_transition_count)
mouse_went_up = went_up(ended_down, half_transition_count)

hot_widget = try_set_hot_widget(hot_widget, active_widget, widget, mouse_over_slider)

active_widget = try_set_active_widget(hot_widget, active_widget, widget, mouse_over_slider && mouse_went_down)

# value = get_widget_value(hot_widget, active_widget, widget, widget_type, mouse_over_slider && mouse_went_up)
value = get_widget_value(hot_widget, active_widget, widget, widget_type, clamp(j_mouse - j_min + one(j_min), one(j_min), j_max - j_min + one(j_min)), last_value)

active_widget = try_reset_active_widget(hot_widget, active_widget, widget, mouse_went_up)

hot_widget = try_reset_hot_widget(hot_widget, active_widget, widget, !mouse_over_slider)

return hot_widget, active_widget, value
end