From 966f8a15bf73272303bc6a86e749b2ea12af5eb3 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 4 Dec 2023 22:09:47 -0800 Subject: [PATCH] simplify state management for menus --- 0016-on.mouse_press | 8 ++++---- 0017-on.mouse_release | 4 ++-- 0021-draw_menu | 4 ++-- 0051-run_button | 5 ++--- 0059-previous_pane_button | 5 ++--- 0060-next_pane_button | 5 ++--- 0061-new_pane_button | 5 ++--- 0064-show_code_button | 5 ++--- 0065-hide_code_button | 5 ++--- 0066-copy_button | 5 ++--- 0067-paste_button | 5 ++--- 0072-settings_button | 9 +++++---- 0074-clear_pane_button | 5 ++--- 0076-delete_pane_button | 5 ++--- 0080-Show_menu | 1 + 0080-Show_settings | 1 - 0084-update_any_sliders | 3 +-- 0086-select_settings_slider | 1 - 0106-stop_button | 5 ++--- 0107-Show_overflow | 2 -- 0109-overflow_button | 9 ++++++--- 0116-save_button | 5 ++--- 0117-load_button | 5 ++--- 0118-overflowable_button | 2 +- 24 files changed, 48 insertions(+), 61 deletions(-) create mode 100644 0080-Show_menu delete mode 100644 0080-Show_settings delete mode 100644 0107-Show_overflow diff --git a/0016-on.mouse_press b/0016-on.mouse_press index 1222c49..bd30c37 100644 --- a/0016-on.mouse_press +++ b/0016-on.mouse_press @@ -6,13 +6,12 @@ on.mouse_press = function(x,y, mouse_button) Button_pressed = true return end - Show_overflow = false -- == settings area - if Show_settings then + if Show_menu == 'settings' then if on_area(Settings_menu_area, x,y) then select_settings_slider(x,y, mouse_button) else - Show_settings = false + Show_menu = nil -- On mobile devices, we can't depend on on.save_settings() triggering on quit. -- So save settings every time we close the settings menu. love.filesystem.write('config', json.encode(settings())) @@ -20,6 +19,7 @@ on.mouse_press = function(x,y, mouse_button) return end -- == main area + Show_menu = nil if Show_code then if on_editor_scrollbar(Current_pane.editor_state, x,y) then Current_pane.editor_state.scrollbar_drag = true @@ -42,4 +42,4 @@ on.mouse_press = function(x,y, mouse_button) if car.mouse_press then car.mouse_press(x,y, mouse_button) end if car.mousepressed then car.mousepressed(x,y, mouse_button) end end -end \ No newline at end of file +end diff --git a/0017-on.mouse_release b/0017-on.mouse_release index 9550d23..89d3179 100644 --- a/0017-on.mouse_release +++ b/0017-on.mouse_release @@ -6,7 +6,7 @@ on.mouse_release = function(x,y, mouse_button) return end -- == settings area - if Show_settings then + if Show_menu == 'settings' then if on_area(Settings_menu_area, x,y) then return end @@ -37,4 +37,4 @@ on.mouse_release = function(x,y, mouse_button) call_protected(car.mousereleased, x,y, mouse_button) end end -end \ No newline at end of file +end diff --git a/0021-draw_menu b/0021-draw_menu index 70ea087..328af77 100644 --- a/0021-draw_menu +++ b/0021-draw_menu @@ -49,7 +49,7 @@ draw_menu = function() if Current_pane_index < #Panes then next_pane_button(Menu_left + Safe_width) end - if Show_settings then + if Show_menu == 'settings' then draw_settings_menu() end -end \ No newline at end of file +end diff --git a/0051-run_button b/0051-run_button index fe3afda..0e07664 100644 --- a/0051-run_button +++ b/0051-run_button @@ -1,8 +1,7 @@ run_button = function(x, y) styled_button('run', x,y, function() - Show_overflow = false - Show_settings = false + Show_menu = nil -- ## run: initialize clear_handlers() local buf = table.concat(map(Current_pane.editor_state.lines, function(line) return line.data end), '\n') @@ -30,4 +29,4 @@ run_button = function(x, y) end) local w = App.width('run')+10 return x+w+10, y -end \ No newline at end of file +end diff --git a/0059-previous_pane_button b/0059-previous_pane_button index 6dc2fd5..53f2699 100644 --- a/0059-previous_pane_button +++ b/0059-previous_pane_button @@ -5,12 +5,11 @@ previous_pane_button = function() love.graphics.polygon('fill', Menu_left+5, App.screen.height/2, Menu_left+25, App.screen.height/2-10, Menu_left+25, App.screen.height/2+10) end, onpress1 = function() - Show_overflow = false - Show_settings = false + Show_menu = nil Current_pane.car = car Current_pane_index = Current_pane_index-1 Current_pane = Panes[Current_pane_index] car = Current_pane.car or {} end, }) -end \ No newline at end of file +end diff --git a/0060-next_pane_button b/0060-next_pane_button index e1a2723..ea637ca 100644 --- a/0060-next_pane_button +++ b/0060-next_pane_button @@ -5,12 +5,11 @@ next_pane_button = function(r) love.graphics.polygon('fill', r-25, App.screen.height/2-10, r-25, App.screen.height/2+10, r-5, App.screen.height/2) end, onpress1 = function() - Show_overflow = false - Show_settings = false + Show_menu = nil Current_pane.car = car Current_pane_index = Current_pane_index+1 Current_pane = Panes[Current_pane_index] car = Current_pane.car or {} end, }) -end \ No newline at end of file +end diff --git a/0061-new_pane_button b/0061-new_pane_button index ac01d58..9976150 100644 --- a/0061-new_pane_button +++ b/0061-new_pane_button @@ -1,10 +1,9 @@ new_pane_button = function(x, y, r) return overflowable_button('new', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil Current_pane_index = Current_pane_index+1 table.insert(Panes, Current_pane_index, new_pane()) Current_pane = Panes[Current_pane_index] end) -end \ No newline at end of file +end diff --git a/0064-show_code_button b/0064-show_code_button index 07edbb1..75d8cea 100644 --- a/0064-show_code_button +++ b/0064-show_code_button @@ -1,8 +1,7 @@ show_code_button = function(x, y, r) return overflowable_button('show', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil Show_code = true end) -end \ No newline at end of file +end diff --git a/0065-hide_code_button b/0065-hide_code_button index f0b38ed..162d5be 100644 --- a/0065-hide_code_button +++ b/0065-hide_code_button @@ -1,8 +1,7 @@ hide_code_button = function(x, y, r) return overflowable_button('hide', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil Show_code = false end) -end \ No newline at end of file +end diff --git a/0066-copy_button b/0066-copy_button index 55e2cc0..570bb2a 100644 --- a/0066-copy_button +++ b/0066-copy_button @@ -1,8 +1,7 @@ copy_button = function(x, y, r) return overflowable_button('copy', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil local e = Current_pane.editor_state local s = Text.selection(e) if s == nil then @@ -14,4 +13,4 @@ copy_button = function(x, y, r) end App.set_clipboard(s) end) -end \ No newline at end of file +end diff --git a/0067-paste_button b/0067-paste_button index f542588..0049cef 100644 --- a/0067-paste_button +++ b/0067-paste_button @@ -1,8 +1,7 @@ paste_button = function(x, y, r) return overflowable_button('paste', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil local state = Current_pane.editor_state -- initial state for recording undo operation local before_line = state.cursor1.line @@ -19,4 +18,4 @@ paste_button = function(x, y, r) state.cursor1.line) }) end) -end \ No newline at end of file +end diff --git a/0072-settings_button b/0072-settings_button index 4fb279f..58f8163 100644 --- a/0072-settings_button +++ b/0072-settings_button @@ -1,11 +1,12 @@ settings_button = function(x) return right_justified_button('settings', x, Menu_top+5, function() - Show_overflow = false - Show_settings = not Show_settings - if not Show_settings then + if Show_menu == 'settings' then + Show_menu = nil -- On mobile devices, we can't depend on on.save_settings() triggering on quit love.filesystem.write('config', json.encode(settings())) + else + Show_menu = 'settings' end end) -end \ No newline at end of file +end diff --git a/0074-clear_pane_button b/0074-clear_pane_button index 79edd5b..f986484 100644 --- a/0074-clear_pane_button +++ b/0074-clear_pane_button @@ -1,8 +1,7 @@ clear_pane_button = function(x, y, r) return overflowable_button('clear', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil clear_pane() end) -end \ No newline at end of file +end diff --git a/0076-delete_pane_button b/0076-delete_pane_button index db10840..5de7e3d 100644 --- a/0076-delete_pane_button +++ b/0076-delete_pane_button @@ -1,8 +1,7 @@ delete_pane_button = function(x, y, r) return overflowable_button('delete', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil if Current_pane.example_name then Deleted_example_panes[Current_pane.example_name] = true end @@ -16,4 +15,4 @@ delete_pane_button = function(x, y, r) Current_pane = Panes[Current_pane_index] end, --[[final button]] true) -end \ No newline at end of file +end diff --git a/0080-Show_menu b/0080-Show_menu new file mode 100644 index 0000000..b891d69 --- /dev/null +++ b/0080-Show_menu @@ -0,0 +1 @@ +Show_menu = nil diff --git a/0080-Show_settings b/0080-Show_settings deleted file mode 100644 index 0444cd9..0000000 --- a/0080-Show_settings +++ /dev/null @@ -1 +0,0 @@ -Show_settings =false \ No newline at end of file diff --git a/0084-update_any_sliders b/0084-update_any_sliders index 3321438..f250040 100644 --- a/0084-update_any_sliders +++ b/0084-update_any_sliders @@ -1,5 +1,4 @@ update_any_sliders = function(x,y) - if not Show_settings then return end if Selected_slider == Settings_font_slider.name then update_font_settings(slider_value(Settings_font_slider, x)) return true @@ -16,4 +15,4 @@ update_any_sliders = function(x,y) return true end end -end \ No newline at end of file +end diff --git a/0086-select_settings_slider b/0086-select_settings_slider index 983993b..357e13a 100644 --- a/0086-select_settings_slider +++ b/0086-select_settings_slider @@ -1,5 +1,4 @@ select_settings_slider = function(x,y, mouse_button) - if not Show_settings then return end local result = false if on_slider(Settings_font_slider, x,y) then Selected_slider = Settings_font_slider.name diff --git a/0106-stop_button b/0106-stop_button index 277fcc7..0db6b07 100644 --- a/0106-stop_button +++ b/0106-stop_button @@ -1,10 +1,9 @@ stop_button = function(x, y) styled_button('stop', x,y, function() - Show_overflow = false - Show_settings = false + Show_menu = nil clear_handlers() end) local w = App.width('stop')+10 return x+w+10, y -end \ No newline at end of file +end diff --git a/0107-Show_overflow b/0107-Show_overflow deleted file mode 100644 index 90aef8d..0000000 --- a/0107-Show_overflow +++ /dev/null @@ -1,2 +0,0 @@ --- True if the overflow menu has been opened and we want to render buttons within it. -Show_overflow = false \ No newline at end of file diff --git a/0109-overflow_button b/0109-overflow_button index cfd8d8b..73b9480 100644 --- a/0109-overflow_button +++ b/0109-overflow_button @@ -1,8 +1,11 @@ overflow_button = function(x, y) styled_button('>>', x,y, function() - Show_settings = false - Show_overflow = not Show_overflow + if Show_menu == 'overflow' then + Show_menu = nil + else + Show_menu = 'overflow' + end end) return x, y+Line_height -end \ No newline at end of file +end diff --git a/0116-save_button b/0116-save_button index c9ea047..5309f75 100644 --- a/0116-save_button +++ b/0116-save_button @@ -1,8 +1,7 @@ save_button = function(x, y, r) return overflowable_button('save', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil if Current_pane.filename == nil then Show_file_dialog = true File_dialog_callback = function(filename) @@ -14,4 +13,4 @@ save_button = function(x, y, r) one_time_save() end end) -end \ No newline at end of file +end diff --git a/0117-load_button b/0117-load_button index b1f708a..b3982e5 100644 --- a/0117-load_button +++ b/0117-load_button @@ -1,8 +1,7 @@ load_button = function(x, y, r) return overflowable_button('load', x, y, r, function() - Show_overflow = false - Show_settings = false + Show_menu = nil Show_file_dialog = true File_dialog_callback = function(filename) if filename == '' then @@ -14,4 +13,4 @@ load_button = function(x, y, r) one_time_load() end end) -end \ No newline at end of file +end diff --git a/0118-overflowable_button b/0118-overflowable_button index b876e9e..770e1e3 100644 --- a/0118-overflowable_button +++ b/0118-overflowable_button @@ -5,7 +5,7 @@ overflowable_button = function(name, x, y, r, onpress1, final_button) local x2, y2 = maybe_draw_overflow_button(x, y, w, r, final_button) if Overflow_button then -- overflow - if not Show_overflow then + if Show_menu ~= 'overflow' then return x, y else y = y2