greatly simplify slider implementation

It now looks a lot like button.lua.
This commit is contained in:
Kartik K. Agaram 2023-12-16 23:27:52 -08:00
parent 018c2d09e4
commit 2f09322d49
16 changed files with 52 additions and 119 deletions

View File

@ -5,10 +5,8 @@ on.update = function(dt)
Active_button = nil
end
-- == settings area
if Selected_slider then
update_any_sliders(App.mouse_x(), App.mouse_y())
end
-- main area
update_sliders(Global_state, App.mouse_x())
-- == main area
if Current_pane.editor_state.scrollbar_drag then
adjust_scrollbar(Current_pane.editor_state, App.mouse_y())
elseif Current_pane.output_editor_state.scrollbar_drag then

View File

@ -1,5 +1,6 @@
on.draw = function()
Global_state.button_handlers = {}
Global_state.slider_handlers = {}
-- modal dialog
if Show_file_dialog then
draw_file_dialog()

View File

@ -7,9 +7,12 @@ on.mouse_press = function(x,y, mouse_button)
return
end
-- == settings area
if mouse_press_consumed_by_any_slider(Global_state, x,y) then
return
end
if Show_menu == 'settings' then
if on_area(Settings_menu_area, x,y) then
select_settings_slider(x,y, mouse_button)
-- nothing atm in settings menu that isn't a button or slider
else
Show_menu = nil
-- On mobile devices, we can't depend on on.save_settings() triggering on quit.

View File

@ -1,11 +1,11 @@
on.mouse_release = function(x,y, mouse_button)
-- == menu area
Selected_slider = nil
if Button_pressed then
Button_pressed = nil
return
end
-- == settings area
Global_state.selected_slider = nil
if Show_menu == 'settings' then
if on_area(Settings_menu_area, x,y) then
return
@ -37,4 +37,4 @@ on.mouse_release = function(x,y, mouse_button)
call_protected(car.mousereleased, x,y, mouse_button)
end
end
end
end

View File

@ -9,8 +9,8 @@ draw_settings_menu = function()
love.graphics.print('font size', x+10,y+10)
y = y+10+Line_height
local sx = x+App.width('bg')+50 -- align all sliders
Settings_font_slider = {
name='font',
slider(Global_state, 'font', {
fg=Normal_color,
-- x limits
x0=sx+20, x1=Safe_width-30-10,
-- central y
@ -20,8 +20,8 @@ draw_settings_menu = function()
-- extremes
lo=12, hi=40, -- font sizes
value=Current_pane.editor_state.font_height,
}
draw_slider(Settings_font_slider)
update=update_font_settings,
})
y = y+10
-- colors
love.graphics.print('colors', x+10,y+10)
@ -30,71 +30,34 @@ draw_settings_menu = function()
love.graphics.print('fg', x+20, y)
App.color(Foreground_color)
love.graphics.rectangle('fill', sx-20,y+5, 20,20)
local prev = Settings_font_slider
Settings_foreground_sliders = {
r = {
name='fg/r',
local prev = Global_state.slider_handlers.font
for _,color in ipairs{'r', 'g', 'b'} do
slider(Global_state, 'fg/'..color, {
fg=Normal_color,
x0=prev.x0, x1=prev.x1,
y0=y,
w=prev.w, h=prev.h,
lo=0, hi=1,
value=Foreground_color.r,
},
g = {
name='fg/g',
x0=prev.x0, x1=prev.x1,
y0=y+15,
w=prev.w, h=prev.h,
lo=0, hi=1,
value=Foreground_color.g,
},
b = {
name='fg/b',
x0=prev.x0, x1=prev.x1,
y0=y+30,
w=prev.w, h=prev.h,
lo=0, hi=1,
value=Foreground_color.b,
},
}
App.color(Normal_color)
draw_slider(Settings_foreground_sliders.r)
draw_slider(Settings_foreground_sliders.g)
draw_slider(Settings_foreground_sliders.b)
y = y+50
value=Foreground_color[color],
update=function(v) Foreground_color[color] = v end,
})
y = y+15
end
y = y+5
-- colors/background
love.graphics.print('bg', x+20, y)
App.color(Background_color)
love.graphics.rectangle('fill', sx-20,y+5, 20,20)
App.color(Normal_color)
Settings_background_sliders = {
r = {
name='bg/r',
for _,color in ipairs{'r', 'g', 'b'} do
slider(Global_state, 'bg/'..color, {
fg=Normal_color,
x0=prev.x0, x1=prev.x1,
y0=y,
w=prev.w, h=prev.h,
lo=0, hi=1,
value=Background_color.r
},
g = {
name='bg/g',
x0=prev.x0, x1=prev.x1,
y0=y+15,
w=prev.w, h=prev.h,
lo=0, hi=1,
value=Background_color.g
},
b = {
name='bg/b',
x0 = prev.x0, x1=prev.x1,
y0 = y+30,
w=prev.w, h=prev.h,
lo = 0, hi=1,
value = Background_color.b
},
}
App.color(Normal_color)
draw_slider(Settings_background_sliders.r)
draw_slider(Settings_background_sliders.g)
draw_slider(Settings_background_sliders.b)
value=Background_color[color],
update=function(v) Background_color[color] = v end,
})
y = y+15
end
end

View File

@ -2,6 +2,7 @@
-- position 'value' on the slider
-- the leftmost point on the slider will have value 'lo', and the rightmost will have 'hi'. In between the value will be linearly interpolated.
draw_slider = function(s)
App.color(s.fg)
love.graphics.line(s.x0, s.y0, s.x1, s.y0)
s.x = s.x0 + (s.x1-s.x0)*(s.value-s.lo)/(s.hi-s.lo)
love.graphics.rectangle('fill', s.x-s.h/2, s.y0-s.h/2, s.w,s.h)

View File

@ -1,11 +0,0 @@
Settings_font_slider = {
name='font',
-- left extreme
x0=nil, y0=nil,
-- right extreme
x1=nil,
-- slider itself
x=nil, -- y=y0
w=nil, h=nil,
lo=20, hi=40,
}

4
0083-slider Normal file
View File

@ -0,0 +1,4 @@
slider = function(State, name, params)
draw_slider(params)
State.slider_handlers[name] = params
end

View File

@ -1,18 +0,0 @@
update_any_sliders = function(x,y)
if Selected_slider == Settings_font_slider.name then
update_font_settings(slider_value(Settings_font_slider, x))
return true
end
for color,slider in pairs(Settings_foreground_sliders) do
if Selected_slider == slider.name then
Foreground_color[color] = slider_value(slider, x)
return true
end
end
for color,slider in pairs(Settings_background_sliders) do
if Selected_slider == slider.name then
Background_color[color] = slider_value(slider, x)
return true
end
end
end

8
0084-update_sliders Normal file
View File

@ -0,0 +1,8 @@
update_sliders = function(State, x)
for name, slider in pairs(State.slider_handlers) do
if State.selected_slider == name then
slider.update(slider_value(slider, x))
break
end
end
end

View File

@ -0,0 +1,8 @@
mouse_press_consumed_by_any_slider = function(State, x, y)
for name, slider in pairs(State.slider_handlers) do
if on_slider(slider, x,y) then
State.selected_slider = name -- used by update across frames; slider_handlers is recreated every frame
return true
end
end
end

View File

@ -1,20 +0,0 @@
select_settings_slider = function(x,y, mouse_button)
local result = false
if on_slider(Settings_font_slider, x,y) then
Selected_slider = Settings_font_slider.name
result = true
end
for color,slider in pairs(Settings_foreground_sliders) do
if on_slider(slider, x,y) then
Selected_slider = slider.name
result = true
end
end
for color,slider in pairs(Settings_background_sliders) do
if on_slider(slider, x,y) then
Selected_slider = slider.name
result = true
end
end
return result
end

View File

@ -1,5 +1,4 @@
on_slider = function(slider, x,y)
if slider.x0 == nil then return end -- slider uninitialized
if x < slider.x0-slider.w/2 or x > slider.x1+slider.w/2 then return end
if y < slider.y0-slider.h/2 or y > slider.y0+slider.h/2 then return end
return true

View File

@ -1 +0,0 @@
Settings_foreground_sliders = {}

View File

@ -1 +0,0 @@
Settings_background_sliders = {}

View File

@ -1 +0,0 @@
Selected_slider = nil