From 76bfcba47ee99b877c17dafe3bc6ffee1d831621 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 21 Nov 2023 02:19:56 -0800 Subject: [PATCH] sliders for font size and color settings --- 0004-on.update | 3 + 0012-on.draw | 4 +- 0016-on.mouse_press | 6 ++ 0017-on.mouse_release | 1 + 0021-draw_menu | 3 + 0051-run_button | 3 +- 0072-settings_button | 2 +- 0080-Show_settings | 1 + 0081-draw_settings_menu | 99 ++++++++++++++++++++++++++++++++ 0082-draw_slider | 8 +++ 0083-Settings_font_slider | 11 ++++ 0084-update_any_sliders | 18 ++++++ 0085-slider_value | 8 +++ 0086-mouse_on_any_slider | 20 +++++++ 0087-on_slider | 5 ++ 0088-Settings_foreground_sliders | 1 + 0089-Settings_background_sliders | 1 + 0090-Background_color | 1 + 0091-Foreground_color | 1 + 0092-Settings_menu_area | 1 + 0093-on_area | 7 +++ 0095-Selected_slider | 1 + 22 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 0080-Show_settings create mode 100644 0081-draw_settings_menu create mode 100644 0082-draw_slider create mode 100644 0083-Settings_font_slider create mode 100644 0084-update_any_sliders create mode 100644 0085-slider_value create mode 100644 0086-mouse_on_any_slider create mode 100644 0087-on_slider create mode 100644 0088-Settings_foreground_sliders create mode 100644 0089-Settings_background_sliders create mode 100644 0090-Background_color create mode 100644 0091-Foreground_color create mode 100644 0092-Settings_menu_area create mode 100644 0093-on_area create mode 100644 0095-Selected_slider diff --git a/0004-on.update b/0004-on.update index b2ca719..dceb3da 100644 --- a/0004-on.update +++ b/0004-on.update @@ -1,5 +1,8 @@ on.update = function() refresh_debug_animations() + if App.mouse_down(1) then + update_any_sliders(App.mouse_x(), App.mouse_y()) + end 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 diff --git a/0012-on.draw b/0012-on.draw index 1172467..3c50b0b 100644 --- a/0012-on.draw +++ b/0012-on.draw @@ -3,10 +3,10 @@ on.draw = function() draw_canvas() if Show_code then draw_editor_border() - edit.draw(Current_pane.editor_state, --[[fg]] nil, --[[hide_cursor]] nil, --[[show_line_numbers]] true) + edit.draw(Current_pane.editor_state, --[[fg]] Foreground_color, --[[hide_cursor]] nil, --[[show_line_numbers]] true) draw_scrollbar(Current_pane.editor_state) draw_output_border() - edit.draw(Current_pane.output_editor_state, Normal_color, --[[hide cursor]] true) + edit.draw(Current_pane.output_editor_state, Foreground_color, --[[hide cursor]] true) draw_scrollbar(Current_pane.output_editor_state) end draw_menu() diff --git a/0016-on.mouse_press b/0016-on.mouse_press index 556ec59..49ac544 100644 --- a/0016-on.mouse_press +++ b/0016-on.mouse_press @@ -2,6 +2,12 @@ on.mouse_press = function(x,y, mouse_button) if mouse_press_consumed_by_any_button_handler(Global_state, x,y, mouse_button) then return end + if mouse_on_any_slider(x,y, mouse_button) then + return + end + if not on_area(Settings_menu_area, x,y) then + Show_settings = false + end if on_editor_scrollbar(Current_pane.editor_state, x,y) then Current_pane.editor_state.scrollbar_drag = true elseif on_editor_scrollbar_area(Current_pane.editor_state, x,y) then diff --git a/0017-on.mouse_release b/0017-on.mouse_release index 0bad6cd..b7e0b14 100644 --- a/0017-on.mouse_release +++ b/0017-on.mouse_release @@ -1,4 +1,5 @@ on.mouse_release = function(x,y, mouse_button) + Selected_slider = nil if Current_pane.editor_state.scrollbar_drag then adjust_scrollbar(Current_pane.editor_state, y) Current_pane.editor_state.scrollbar_drag = nil diff --git a/0021-draw_menu b/0021-draw_menu index 99b52a1..246c8ef 100644 --- a/0021-draw_menu +++ b/0021-draw_menu @@ -29,4 +29,7 @@ draw_menu = function() if Current_pane_index < #Panes then next_pane_button(Menu_left + Safe_width) end + if Show_settings then + draw_settings_menu() + end end \ No newline at end of file diff --git a/0051-run_button b/0051-run_button index 60845c5..263760b 100644 --- a/0051-run_button +++ b/0051-run_button @@ -12,8 +12,7 @@ run_button = function(x) Current_pane.canvas = love.graphics.newCanvas() love.graphics.setCanvas(Current_pane.canvas) love.graphics.push('all') - love.graphics.setBackgroundColor(1,1,1) - love.graphics.setColor(0,0,0) +-- love.graphics.setBackgroundColor(Background_color.r, Background_color.g, Background_color.b) Current_pane.output_editor_state.lines = {} Text.redraw_all(Current_pane.output_editor_state) local real_print = print diff --git a/0072-settings_button b/0072-settings_button index 4f4bcd9..5b82606 100644 --- a/0072-settings_button +++ b/0072-settings_button @@ -6,7 +6,7 @@ settings_button = function(x, w) love.graphics.print('settings', p.x+5,p.y+2) end, onpress1 = function() - print('settings') + Show_settings = not Show_settings end, }) return x+w+10 diff --git a/0080-Show_settings b/0080-Show_settings new file mode 100644 index 0000000..0444cd9 --- /dev/null +++ b/0080-Show_settings @@ -0,0 +1 @@ +Show_settings =false \ No newline at end of file diff --git a/0081-draw_settings_menu b/0081-draw_settings_menu new file mode 100644 index 0000000..eee00de --- /dev/null +++ b/0081-draw_settings_menu @@ -0,0 +1,99 @@ +draw_settings_menu = function() + App.color(Menu_background) + local w,h = 200, love.graphics.getFont():getHeight()*8 + local x,y = Safe_width-30-w, Menu_bottom + Settings_menu_area = {x=x, y=y, w=w, h=h} + love.graphics.rectangle('fill', x,y, w,h) + App.color(Normal_color) + -- font size slider + 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', + -- x limits + x0=sx+20, x1=Safe_width-30, + -- central y + y0=y+10, + -- slider knob dimensions + w=10, h=10, + -- extremes + lo=20, hi=40, -- font sizes + value=Current_pane.editor_state.font_height + } + draw_slider(Settings_font_slider) + y = y+10 + -- colors + love.graphics.print('colors', x+10,y+10) + y = y+10+Line_height + -- colors/foreground + love.graphics.print('fg', x+20, y) + App.color(Foreground_color) + love.graphics.rectangle('fill', sx-20,y+5, 20,20) + Settings_foreground_sliders = { + r = { + name='fg/r', + x0 = sx+20, x1=Safe_width-30, + y0 = y, + w=10, h=10, + lo = 0, hi=1, + value = Foreground_color.r + }, + g = { + name='fg/g', + x0 = sx+20, x1=Safe_width-30, + y0 = y+15, + w=10, h=10, + lo = 0, hi=1, + value = Foreground_color.g + }, + b = { + name='fg/b', + x0 = sx+20, x1=Safe_width-30, + y0 = y+30, + w=10, h=10, + 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+10+Line_height+10 + -- 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', + x0 = sx+20, x1=Safe_width-30, + y0 = y, + w=10, h=10, + lo = 0, hi=1, + value = Background_color.r + }, + g = { + name='bg/g', + x0 = sx+20, x1=Safe_width-30, + y0 = y+15, + w=10, h=10, + lo = 0, hi=1, + value = Background_color.g + }, + b = { + name='bg/b', + x0 = sx+20, x1=Safe_width-30, + y0 = y+30, + w=10, h=10, + 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) +end \ No newline at end of file diff --git a/0082-draw_slider b/0082-draw_slider new file mode 100644 index 0000000..b9b5e6b --- /dev/null +++ b/0082-draw_slider @@ -0,0 +1,8 @@ +-- draw a slider widget starting at x,y, extending right w pixels +-- 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) + 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) +end \ No newline at end of file diff --git a/0083-Settings_font_slider b/0083-Settings_font_slider new file mode 100644 index 0000000..861549b --- /dev/null +++ b/0083-Settings_font_slider @@ -0,0 +1,11 @@ +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, +} \ No newline at end of file diff --git a/0084-update_any_sliders b/0084-update_any_sliders new file mode 100644 index 0000000..cb3f57d --- /dev/null +++ b/0084-update_any_sliders @@ -0,0 +1,18 @@ +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 \ No newline at end of file diff --git a/0085-slider_value b/0085-slider_value new file mode 100644 index 0000000..c0b172c --- /dev/null +++ b/0085-slider_value @@ -0,0 +1,8 @@ +slider_value = function(slider, x) + local s = slider + if x < s.x0 then x = s.x0 end + if x > s.x1 then x = s.x1 end + s.x = x + s.value = s.lo + (s.x-s.x0)*(s.hi-s.lo)/(s.x1-s.x0) + return s.value +end \ No newline at end of file diff --git a/0086-mouse_on_any_slider b/0086-mouse_on_any_slider new file mode 100644 index 0000000..e09c890 --- /dev/null +++ b/0086-mouse_on_any_slider @@ -0,0 +1,20 @@ +mouse_on_any_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 \ No newline at end of file diff --git a/0087-on_slider b/0087-on_slider new file mode 100644 index 0000000..583f622 --- /dev/null +++ b/0087-on_slider @@ -0,0 +1,5 @@ +on_slider = function(slider, x,y) + if x < slider.x0 or x > slider.x1 then return end + if y < slider.y0-slider.h/2 or y > slider.y0+slider.h/2 then return end + return true +end \ No newline at end of file diff --git a/0088-Settings_foreground_sliders b/0088-Settings_foreground_sliders new file mode 100644 index 0000000..fbf8811 --- /dev/null +++ b/0088-Settings_foreground_sliders @@ -0,0 +1 @@ +Settings_foreground_sliders = {} \ No newline at end of file diff --git a/0089-Settings_background_sliders b/0089-Settings_background_sliders new file mode 100644 index 0000000..78b44fc --- /dev/null +++ b/0089-Settings_background_sliders @@ -0,0 +1 @@ +Settings_background_sliders = {} \ No newline at end of file diff --git a/0090-Background_color b/0090-Background_color new file mode 100644 index 0000000..be05c46 --- /dev/null +++ b/0090-Background_color @@ -0,0 +1 @@ +Background_color = {r=1, g=1, b=1} \ No newline at end of file diff --git a/0091-Foreground_color b/0091-Foreground_color new file mode 100644 index 0000000..9d4e0b3 --- /dev/null +++ b/0091-Foreground_color @@ -0,0 +1 @@ +Foreground_color = {r=0, g=0, b=0} \ No newline at end of file diff --git a/0092-Settings_menu_area b/0092-Settings_menu_area new file mode 100644 index 0000000..0d8561f --- /dev/null +++ b/0092-Settings_menu_area @@ -0,0 +1 @@ +Settings_menu_area = {} \ No newline at end of file diff --git a/0093-on_area b/0093-on_area new file mode 100644 index 0000000..30f978b --- /dev/null +++ b/0093-on_area @@ -0,0 +1,7 @@ +on_area = function(s, x,y) + if x < s.x then return end + if x > s.x+s.w then return end + if y < s.y then return end + if y > s.y+s.h then return end + return true +end \ No newline at end of file diff --git a/0095-Selected_slider b/0095-Selected_slider new file mode 100644 index 0000000..3b9636d --- /dev/null +++ b/0095-Selected_slider @@ -0,0 +1 @@ +Selected_slider = nil \ No newline at end of file