aggregate global state inside a 'pane' object

We're soon going to start juggling multiple of these.
This commit is contained in:
Kartik K. Agaram 2023-11-19 13:15:06 -08:00
parent 9f21230005
commit e38191fc4f
19 changed files with 79 additions and 76 deletions

View File

@ -1,8 +1,8 @@
on.update = function() on.update = function()
refresh_debug_animations() refresh_debug_animations()
if Editor_state.scrollbar_drag then if Current_pane.editor_state.scrollbar_drag then
adjust_scrollbar(Editor_state, App.mouse_y()) adjust_scrollbar(Current_pane.editor_state, App.mouse_y())
elseif Output_editor_state.scrollbar_drag then elseif Current_pane.output_editor_state.scrollbar_drag then
adjust_scrollbar(Output_editor_state, App.mouse_y()) adjust_scrollbar(Current_pane.output_editor_state, App.mouse_y())
end end
end end

View File

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

View File

@ -5,17 +5,17 @@ on.initialize = function()
Menu_left, Menu_top, Safe_width, Safe_height = love.window.getSafeArea() Menu_left, Menu_top, Safe_width, Safe_height = love.window.getSafeArea()
Menu_height = 5 + Line_height + 5 Menu_height = 5 + Line_height + 5
Menu_bottom = Menu_top + Menu_height Menu_bottom = Menu_top + Menu_height
Editor_state = edit.initialize_state( Current_pane.editor_state = edit.initialize_state(
Menu_bottom + 20, -- top Menu_bottom + 20, -- top
Safe_height/2-Line_height, -- bottom Safe_height/2-Line_height, -- bottom
Menu_left + 50 + Line_number_padding, -- left Menu_left + 50 + Line_number_padding, -- left
math.min(100+30*App.width('m'), Safe_width*2/3), -- right math.min(100+30*App.width('m'), Safe_width*2/3), -- right
love.graphics.getFont():getHeight(), Line_height) love.graphics.getFont():getHeight(), Line_height)
Text.redraw_all(Editor_state) Text.redraw_all(Current_pane.editor_state)
Output_editor_state = edit.initialize_state( Current_pane.output_editor_state = edit.initialize_state(
Editor_state.bottom+5+10+5, -- top Current_pane.editor_state.bottom+5+10+5, -- top
nil, -- buttom nil, -- buttom
Editor_state.left, Editor_state.right, Current_pane.editor_state.left, Current_pane.editor_state.right,
love.graphics.getFont():getHeight(), Line_height) love.graphics.getFont():getHeight(), Line_height)
Text.redraw_all(Output_editor_state) Text.redraw_all(Current_pane.output_editor_state)
end end

View File

@ -1,13 +1,13 @@
on.draw = function() on.draw = function()
Editor_state.button_handlers = {} Global_state.button_handlers = {}
draw_canvas() draw_canvas()
--if Canvas then return end --if Canvas then return end
draw_editor_border() draw_editor_border()
-- love.graphics.rectangle('line', 100-5-Line_number_padding,100-5, 300+Line_number_padding+10, 200+10, 5,5) -- love.graphics.rectangle('line', 100-5-Line_number_padding,100-5, 300+Line_number_padding+10, 200+10, 5,5)
edit.draw(Editor_state) edit.draw(Current_pane.editor_state)
draw_scrollbar(Editor_state) draw_scrollbar(Current_pane.editor_state)
draw_output_border() draw_output_border()
edit.draw(Output_editor_state, Normal_color, --[[hide cursor]] true) edit.draw(Current_pane.output_editor_state, Normal_color, --[[hide cursor]] true)
draw_scrollbar(Output_editor_state) draw_scrollbar(Current_pane.output_editor_state)
draw_menu() draw_menu()
end end

View File

@ -1,11 +1,11 @@
on.keychord_press = function(chord, key) on.keychord_press = function(chord, key)
if chord == 'C-=' then if chord == 'C-=' then
update_font_settings(Editor_state.font_height+2) update_font_settings(Current_pane.editor_state.font_height+2)
elseif chord == 'C--' then elseif chord == 'C--' then
update_font_settings(Editor_state.font_height-2) update_font_settings(Current_pane.editor_state.font_height-2)
elseif chord == 'C-0' then elseif chord == 'C-0' then
update_font_settings(20) update_font_settings(20)
else else
edit.keychord_press(Editor_state, chord, key) edit.keychord_press(Current_pane.editor_state, chord, key)
end end
end end

View File

@ -1,3 +1,3 @@
on.text_input = function(t) on.text_input = function(t)
edit.text_input(Editor_state, t) edit.text_input(Current_pane.editor_state, t)
end end

View File

@ -1,3 +1,3 @@
on.key_release = function(key, scancode) on.key_release = function(key, scancode)
edit.key_release(Editor_state, key, scancode) edit.key_release(Current_pane.editor_state, key, scancode)
end end

View File

@ -1,16 +1,16 @@
on.mouse_press = function(x,y, mouse_button) on.mouse_press = function(x,y, mouse_button)
if mouse_press_consumed_by_any_button_handler(Editor_state, x,y, mouse_button) then if mouse_press_consumed_by_any_button_handler(Global_state, x,y, mouse_button) then
return return
end end
if on_editor_scrollbar(Editor_state, x,y) then if on_editor_scrollbar(Current_pane.editor_state, x,y) then
Editor_state.scrollbar_drag = true Current_pane.editor_state.scrollbar_drag = true
elseif on_editor_scrollbar_area(Editor_state, x,y) then elseif on_editor_scrollbar_area(Current_pane.editor_state, x,y) then
-- nothing -- nothing
elseif x < Editor_state.right + 15 - 5 and y < Editor_state.bottom + 5 + 10 - 5 then elseif x < Current_pane.editor_state.right + 15 - 5 and y < Current_pane.editor_state.bottom + 5 + 10 - 5 then
edit.mouse_press(Editor_state, x,y, mouse_button) edit.mouse_press(Current_pane.editor_state, x,y, mouse_button)
elseif on_editor_scrollbar(Output_editor_state, x,y) then elseif on_editor_scrollbar(Current_pane.output_editor_state, x,y) then
Output_editor_state.scrollbar_drag = true Current_pane.output_editor_state.scrollbar_drag = true
elseif on_editor_scrollbar_area(Output_editor_state, x,y) then elseif on_editor_scrollbar_area(Current_pane.output_editor_state, x,y) then
-- nothing -- nothing
end end
end end

View File

@ -1,14 +1,14 @@
on.mouse_release = function(x,y, mouse_button) on.mouse_release = function(x,y, mouse_button)
if Editor_state.scrollbar_drag then if Current_pane.editor_state.scrollbar_drag then
adjust_scrollbar(Editor_state, y) adjust_scrollbar(Current_pane.editor_state, y)
Editor_state.scrollbar_drag = nil Current_pane.editor_state.scrollbar_drag = nil
elseif on_editor_scrollbar_area(Editor_state, x,y) then elseif on_editor_scrollbar_area(Current_pane.editor_state, x,y) then
adjust_scrollbar(Editor_state, y) adjust_scrollbar(Current_pane.editor_state, y)
elseif x < Editor_state.right + 15 - 5 and y < Editor_state.bottom + 5 + 10 - 5 then elseif x < Current_pane.editor_state.right + 15 - 5 and y < Current_pane.editor_state.bottom + 5 + 10 - 5 then
edit.mouse_release(Editor_state, x,y, mouse_button) edit.mouse_release(Current_pane.editor_state, x,y, mouse_button)
elseif Output_editor_state.scrollbar_drag then elseif Current_pane.output_editor_state.scrollbar_drag then
adjust_scrollbar(Output_editor_state, y) adjust_scrollbar(Current_pane.output_editor_state, y)
elseif on_editor_scrollbar_area(Output_editor_state, x,y) then elseif on_editor_scrollbar_area(Current_pane.output_editor_state, x,y) then
adjust_scrollbar(Output_editor_state, y) adjust_scrollbar(Current_pane.output_editor_state, y)
end end
end end

View File

@ -1,9 +1,9 @@
draw_editor_border = function() draw_editor_border = function()
App.color(Normal_color) App.color(Normal_color)
local x1 = Editor_state.left-5-Line_number_padding local x1 = Current_pane.editor_state.left-5-Line_number_padding
local y1 = Editor_state.top-5 local y1 = Current_pane.editor_state.top-5
local x2 = Editor_state.right+5 local x2 = Current_pane.editor_state.right+5
local y2 = Editor_state.bottom+5 local y2 = Current_pane.editor_state.bottom+5
-- upper border -- upper border
love.graphics.line(x1,y1, x2,y1) love.graphics.line(x1,y1, x2,y1)
love.graphics.line(x1,y1, x1,y1+10) love.graphics.line(x1,y1, x1,y1+10)

View File

@ -2,7 +2,7 @@ draw_menu = function()
App.color(Menu_background) App.color(Menu_background)
love.graphics.rectangle('fill', 0,0, App.screen.width, Menu_bottom) love.graphics.rectangle('fill', 0,0, App.screen.width, Menu_bottom)
-- main buttons -- main buttons
button(Editor_state, 'run', {x=Menu_left+5, y=Menu_top+5, w=App.width('Run')+10, h=Line_height, bg={r=0.6, g=0.8, b=0.6}, button(Global_state, 'run', {x=Menu_left+5, y=Menu_top+5, w=App.width('Run')+10, h=Line_height, bg={r=0.6, g=0.8, b=0.6},
icon = function(p) icon = function(p)
App.color(Normal_color) App.color(Normal_color)
love.graphics.rectangle('line', p.x,p.y, p.w,p.h, 2,2) love.graphics.rectangle('line', p.x,p.y, p.w,p.h, 2,2)
@ -10,9 +10,9 @@ draw_menu = function()
end, end,
onpress1 = function() onpress1 = function()
print('run') print('run')
local buf = table.concat(map(Editor_state.lines, function(line) return line.data end), '\n') local buf = table.concat(map(Current_pane.editor_state.lines, function(line) return line.data end), '\n')
Canvas = love.graphics.newCanvas() Current_pane.canvas = love.graphics.newCanvas()
love.graphics.setCanvas(Canvas) love.graphics.setCanvas(Current_pane.canvas)
love.graphics.push('all') love.graphics.push('all')
love.graphics.setBackgroundColor(1,1,1) love.graphics.setBackgroundColor(1,1,1)
love.graphics.setColor(0,0,0) love.graphics.setColor(0,0,0)
@ -20,10 +20,10 @@ draw_menu = function()
print(status, result) print(status, result)
if result then if result then
-- could be either output or error -- could be either output or error
Output_editor_state.lines = { Current_pane.output_editor_state.lines = {
{data=tostring(result)}, {data=tostring(result)},
} }
Text.redraw_all(Output_editor_state) Text.redraw_all(Current_pane.output_editor_state)
end end
love.graphics.pop() love.graphics.pop()
love.graphics.setCanvas() love.graphics.setCanvas()
@ -31,7 +31,7 @@ draw_menu = function()
}) })
-- settings button on right -- settings button on right
local w = App.width('settings') local w = App.width('settings')
button(Editor_state, 'settings', {x=Safe_width-w-10-5, y=Menu_top+5, w=w+10, h=Line_height, bg={r=0.6, g=0.8, b=0.6}, button(Global_state, 'settings', {x=Safe_width-w-10-5, y=Menu_top+5, w=w+10, h=Line_height, bg={r=0.6, g=0.8, b=0.6},
icon = function(p) icon = function(p)
App.color(Normal_color) App.color(Normal_color)
love.graphics.rectangle('line', p.x,p.y, p.w,p.h, 2,2) love.graphics.rectangle('line', p.x,p.y, p.w,p.h, 2,2)
@ -42,14 +42,14 @@ draw_menu = function()
end, end,
}) })
-- nav buttons along sides -- nav buttons along sides
button(Editor_state, 'left', {x=0, y=Menu_bottom, w=Menu_left+30, h=App.screen.height, bg={r=0.5, g=0.5, b=0.5, a=0.2}, button(Global_state, 'left', {x=0, y=Menu_bottom, w=Menu_left+30, h=App.screen.height, bg={r=0.5, g=0.5, b=0.5, a=0.2},
icon = function(p) icon = function(p)
App.color{r=0.4,g=0.4,b=0.4} App.color{r=0.4,g=0.4,b=0.4}
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) 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, end,
}) })
local r = Menu_left + Safe_width local r = Menu_left + Safe_width
button(Editor_state, 'left', {x=r-30, y=Menu_bottom, w=100, h=App.screen.height, bg={r=0.5, g=0.5, b=0.5, a=0.2}, button(Global_state, 'left', {x=r-30, y=Menu_bottom, w=100, h=App.screen.height, bg={r=0.5, g=0.5, b=0.5, a=0.2},
icon = function(p) icon = function(p)
App.color{r=0.4,g=0.4,b=0.4} App.color{r=0.4,g=0.4,b=0.4}
love.graphics.polygon('fill', r-25, App.screen.height/2-10, r-25, App.screen.height/2+10, r-5, App.screen.height/2) love.graphics.polygon('fill', r-25, App.screen.height/2-10, r-25, App.screen.height/2+10, r-5, App.screen.height/2)

View File

@ -1,9 +1,9 @@
draw_output_border = function() draw_output_border = function()
App.color(Normal_color) App.color(Normal_color)
-- hack: computing based on editor before output exists -- hack: computing based on editor before output exists
local x1 = Editor_state.left-5-Line_number_padding local x1 = Current_pane.editor_state.left-5-Line_number_padding
local x2 = Editor_state.right+5 local x2 = Current_pane.editor_state.right+5
local y1 = Editor_state.bottom+5+10 local y1 = Current_pane.editor_state.bottom+5+10
-- upper border -- upper border
love.graphics.line(x1,y1, x2,y1) love.graphics.line(x1,y1, x2,y1)
love.graphics.line(x1,y1, x1,y1+10) love.graphics.line(x1,y1, x1,y1+10)

View File

@ -1,11 +1,11 @@
on.resize = function() on.resize = function()
_, _, Safe_width, Safe_height = love.window.getSafeArea() _, _, Safe_width, Safe_height = love.window.getSafeArea()
Editor_state.right = math.min(100+30*App.width('m'), Safe_width*2/3) Current_pane.editor_state.right = math.min(100+30*App.width('m'), Safe_width*2/3)
Editor_state.width = Editor_state.right - Editor_state.left Current_pane.editor_state.width = Current_pane.editor_state.right - Current_pane.editor_state.left
Editor_state.bottom = Safe_height/2-5 Current_pane.editor_state.bottom = Safe_height/2-5
Text.redraw_all(Editor_state) Text.redraw_all(Current_pane.editor_state)
Output_editor_state.top = Editor_state.bottom+5+10+5 Current_pane.output_editor_state.top = Current_pane.editor_state.bottom+5+10+5
Output_editor_state.right = Editor_state.right Current_pane.output_editor_state.right = Current_pane.editor_state.right
Output_editor_state.width = Editor_state.width Current_pane.output_editor_state.width = Current_pane.editor_state.width
Output_editor_state.bottom = Safe_height - 5 Current_pane.output_editor_state.bottom = Safe_height - 5
end end

View File

@ -4,10 +4,10 @@ update_font_settings = function(font_height)
Line_number_padding = Line_number_width*App.width('m') Line_number_padding = Line_number_width*App.width('m')
Menu_height = 5 + Line_height + 5 Menu_height = 5 + Line_height + 5
Menu_bottom = Menu_top + Menu_height Menu_bottom = Menu_top + Menu_height
Editor_state.top = Menu_bottom + 20 Current_pane.editor_state.top = Menu_bottom + 20
Editor_state.left = Menu_left + 50 + Line_number_padding Current_pane.editor_state.left = Menu_left + 50 + Line_number_padding
Editor_state.right = math.min(100+30*App.width('m'), Safe_width*2/3) Current_pane.editor_state.right = math.min(100+30*App.width('m'), Safe_width*2/3)
Editor_state.width = Editor_state.right - Editor_state.left Current_pane.editor_state.width = Current_pane.editor_state.right - Current_pane.editor_state.left
edit.update_font_settings(Editor_state, font_height) edit.update_font_settings(Current_pane.editor_state, font_height)
Text.redraw_all(Editor_state) Text.redraw_all(Current_pane.editor_state)
end end

View File

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

View File

@ -1,7 +1,7 @@
draw_canvas = function() draw_canvas = function()
if Canvas == nil then return end if Current_pane.canvas == nil then return end
love.graphics.setBlendMode('alpha', 'premultiplied') love.graphics.setBlendMode('alpha', 'premultiplied')
love.graphics.setColor(1,1,1,1) love.graphics.setColor(1,1,1,1)
love.graphics.draw(Canvas, 0,0) love.graphics.draw(Current_pane.canvas, 0,0)
love.graphics.setBlendMode('alpha') love.graphics.setBlendMode('alpha')
end end

View File

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

5
0045-Current_pane Normal file
View File

@ -0,0 +1,5 @@
Current_pane = {
canvas = nil,
editor_state = nil,
output_editor_state = nil,
}

1
0046-Global_state Normal file
View File

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