support event handlers

I've agonized over conflicts between editor and script handlers for a
while, but finally the solution occurred to me: to use the script's
handlers, hide the editor. To use the editor's handlers, show the
editor. The menu and settings are always active. This seems nice and
consistent, easy to explain.

Mike Stein would prefer we just supported love.* handlers in scripts to
minimize special cases and cognitive load for people. But I'd rather
err on the side of being transparent about what's going on inside. As a
compromise I'm supporting love.* names at least in addition to my
rephrasings (which came about mostly because love.keychordpressed is
just too much of a mouthful)
This commit is contained in:
Kartik K. Agaram 2023-11-21 19:17:49 -08:00
parent cf286c5ebb
commit fc3cb96171
9 changed files with 75 additions and 31 deletions

View File

@ -1,4 +1,4 @@
on.update = function()
on.update = function(dt)
refresh_debug_animations()
if App.mouse_down(1) then
update_any_sliders(App.mouse_x(), App.mouse_y())
@ -8,4 +8,5 @@ on.update = function()
elseif Current_pane.output_editor_state.scrollbar_drag then
adjust_scrollbar(Current_pane.output_editor_state, App.mouse_y())
end
if car.update then car.update(dt) end
end

View File

@ -2,6 +2,8 @@ on.draw = function()
Global_state.button_handlers = {}
love.graphics.setBackgroundColor(Background_color.r, Background_color.g, Background_color.b)
draw_canvas()
App.color(Foreground_color)
if car.draw then car.draw() end -- off canvas
if Show_code then
draw_editor_border()
edit.draw(Current_pane.editor_state, --[[implicitly use Foreground_color]] nil, --[[hide_cursor]] nil, --[[show_line_numbers]] true)

View File

@ -5,8 +5,14 @@ on.keychord_press = function(chord, key)
update_font_settings(Current_pane.editor_state.font_height-2)
elseif chord == 'C-0' then
update_font_settings(20)
elseif Current_pane.editor_state.cursor_x then
-- send keys to editor if cursor is visible
edit.keychord_press(Current_pane.editor_state, chord, key)
elseif Show_code then
if Current_pane.editor_state.cursor_x then
-- send keys to editor if cursor is visible
edit.keychord_press(Current_pane.editor_state, chord, key)
end
else
-- editors hidden
if car.keychord_press then car.keychord_press(chord, key) end
if car.keypressed then car.keypressed(key) end
end
end

View File

@ -1,5 +1,11 @@
on.text_input = function(t)
if Current_pane.editor_state.cursor_x then
edit.text_input(Current_pane.editor_state, t)
if Show_code then
if Current_pane.editor_state.cursor_x then
edit.text_input(Current_pane.editor_state, t)
end
else
-- editors hidden
if car.text_input then car.text_input(t) end
if car.textinput then car.textinput(t) end
end
end

View File

@ -1,5 +1,11 @@
on.key_release = function(key, scancode)
if Current_pane.editor_state.cursor_x then
edit.key_release(Current_pane.editor_state, key, scancode)
if Show_code then
if Current_pane.editor_state.cursor_x then
edit.key_release(Current_pane.editor_state, key, scancode)
end
else
-- editors hidden
if car.key_release then car.key_release(key, scancode) end
if car.keyreleased then car.keyreleased(key, scancode) end
end
end

View File

@ -8,16 +8,22 @@ on.mouse_press = function(x,y, mouse_button)
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
-- nothing
elseif x < Current_pane.editor_state.right + 15 - 5 and y < Current_pane.editor_state.bottom + 5 + 10 - 5 then
love.keyboard.setTextInput(true)
edit.mouse_press(Current_pane.editor_state, x,y, mouse_button)
elseif on_editor_scrollbar(Current_pane.output_editor_state, x,y) then
Current_pane.output_editor_state.scrollbar_drag = true
elseif on_editor_scrollbar_area(Current_pane.output_editor_state, x,y) then
-- nothing
if Show_code then
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
-- nothing
elseif x < Current_pane.editor_state.right + 15 - 5 and y < Current_pane.editor_state.bottom + 5 + 10 - 5 then
love.keyboard.setTextInput(true)
edit.mouse_press(Current_pane.editor_state, x,y, mouse_button)
elseif on_editor_scrollbar(Current_pane.output_editor_state, x,y) then
Current_pane.output_editor_state.scrollbar_drag = true
elseif on_editor_scrollbar_area(Current_pane.output_editor_state, x,y) then
-- nothing
end
else
-- editors hidden
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

View File

@ -1,16 +1,22 @@
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
elseif on_editor_scrollbar_area(Current_pane.editor_state, x,y) then
adjust_scrollbar(Current_pane.editor_state, y)
elseif x < Current_pane.editor_state.right + 15 - 5 and y < Current_pane.editor_state.bottom + 5 + 10 - 5 then
edit.mouse_release(Current_pane.editor_state, x,y, mouse_button)
elseif Current_pane.output_editor_state.scrollbar_drag then
adjust_scrollbar(Current_pane.output_editor_state, y)
Current_pane.output_editor_state.scrollbar_drag = nil
elseif on_editor_scrollbar_area(Current_pane.output_editor_state, x,y) then
adjust_scrollbar(Current_pane.output_editor_state, y)
if Show_code then
if Current_pane.editor_state.scrollbar_drag then
adjust_scrollbar(Current_pane.editor_state, y)
Current_pane.editor_state.scrollbar_drag = nil
elseif on_editor_scrollbar_area(Current_pane.editor_state, x,y) then
adjust_scrollbar(Current_pane.editor_state, y)
elseif x < Current_pane.editor_state.right + 15 - 5 and y < Current_pane.editor_state.bottom + 5 + 10 - 5 then
edit.mouse_release(Current_pane.editor_state, x,y, mouse_button)
elseif Current_pane.output_editor_state.scrollbar_drag then
adjust_scrollbar(Current_pane.output_editor_state, y)
Current_pane.output_editor_state.scrollbar_drag = nil
elseif on_editor_scrollbar_area(Current_pane.output_editor_state, x,y) then
adjust_scrollbar(Current_pane.output_editor_state, y)
end
else
-- editors hidden
if car.mouse_release then car.mouse_release(x,y, mouse_button) end
if car.mousereleased then car.mousereleased(x,y, mouse_button) end
end
end

2
0100-car Normal file
View File

@ -0,0 +1,2 @@
-- Table where any handlers for the current pane will live.
car = {}

9
0101-on.mouse_wheel_move Normal file
View File

@ -0,0 +1,9 @@
on.mouse_wheel_move = function(dx,dy)
if Show_code then
-- nothing yet
else
-- editors hidden
if car.mouse_wheel_move then car.mouse_wheel_move(dx,dy) end
if car.wheelmoved then car.wheelmoved(dx,dy) end
end
end