Merge lines.love
[view.love.git] / main.lua
blob0297c26625ec9f55b760e12b8ead4dd0b843c884
1 -- Wrapper that combines the app with a 'source editor' that allows editing
2 -- the app in place.
3 --
4 -- The source editor is a sharp tool. I find it convenient, but I also often
5 -- end up in a bad state that requires dropping down to external tools
6 -- (editor, file manager) to fix.
7 --
8 -- Downstream forks provide a better, "freewheeling" experience for editing
9 -- apps live. The source editor provides a half-baked experience for editing
10 -- some of the primitives used by true freewheeling apps.
12 -- files that come with LÖVE; we can't edit those from within the app
13 utf8 = require 'utf8'
15 function load_file_from_source_or_save_directory(filename)
16 local contents = love.filesystem.read(filename)
17 local code, err = loadstring(contents, filename)
18 if code == nil then
19 error(err)
20 end
21 return code()
22 end
24 json = load_file_from_source_or_save_directory('json.lua')
26 load_file_from_source_or_save_directory('app.lua')
27 load_file_from_source_or_save_directory('test.lua')
29 load_file_from_source_or_save_directory('keychord.lua')
30 load_file_from_source_or_save_directory('button.lua')
32 -- both sides require (different parts of) the logging framework
33 load_file_from_source_or_save_directory('log.lua')
35 -- but some files we want to only load sometimes
36 function App.load()
37 log_new('session')
38 if love.filesystem.getInfo('config') then
39 Settings = json.decode(love.filesystem.read('config'))
40 Current_app = Settings.current_app
41 end
43 if Current_app == nil then
44 Current_app = 'run'
45 end
47 if Current_app == 'run' then
48 load_file_from_source_or_save_directory('file.lua')
49 load_file_from_source_or_save_directory('run.lua')
50 load_file_from_source_or_save_directory('edit.lua')
51 load_file_from_source_or_save_directory('text.lua')
52 load_file_from_source_or_save_directory('search.lua')
53 load_file_from_source_or_save_directory('select.lua')
54 load_file_from_source_or_save_directory('undo.lua')
55 load_file_from_source_or_save_directory('text_tests.lua')
56 load_file_from_source_or_save_directory('run_tests.lua')
57 elseif Current_app == 'source' then
58 load_file_from_source_or_save_directory('source_file.lua')
59 load_file_from_source_or_save_directory('source.lua')
60 load_file_from_source_or_save_directory('commands.lua')
61 load_file_from_source_or_save_directory('source_edit.lua')
62 load_file_from_source_or_save_directory('log_browser.lua')
63 load_file_from_source_or_save_directory('source_text.lua')
64 load_file_from_source_or_save_directory('search.lua')
65 load_file_from_source_or_save_directory('source_select.lua')
66 load_file_from_source_or_save_directory('source_undo.lua')
67 load_file_from_source_or_save_directory('colorize.lua')
68 load_file_from_source_or_save_directory('source_text_tests.lua')
69 load_file_from_source_or_save_directory('icons.lua')
70 load_file_from_source_or_save_directory('drawing.lua')
71 load_file_from_source_or_save_directory('geom.lua')
72 load_file_from_source_or_save_directory('help.lua')
73 load_file_from_source_or_save_directory('drawing_tests.lua')
74 load_file_from_source_or_save_directory('source_tests.lua')
75 else
76 assert(false, 'unknown app "'..Current_app..'"')
77 end
78 end
80 function App.initialize_globals()
81 if Current_app == 'run' then
82 run.initialize_globals()
83 elseif Current_app == 'source' then
84 source.initialize_globals()
85 else
86 assert(false, 'unknown app "'..Current_app..'"')
87 end
89 -- for hysteresis in a few places
90 Current_time = 0
91 Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700
92 Last_resize_time = 0
93 end
95 function App.initialize(arg)
96 love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
97 love.keyboard.setKeyRepeat(true)
99 love.graphics.setBackgroundColor(1,1,1)
101 if Current_app == 'run' then
102 run.initialize(arg)
103 elseif Current_app == 'source' then
104 source.initialize(arg)
105 else
106 assert(false, 'unknown app "'..Current_app..'"')
110 function App.resize(w,h)
111 if Current_app == 'run' then
112 if run.resize then run.resize(w,h) end
113 elseif Current_app == 'source' then
114 if source.resize then source.resize(w,h) end
115 else
116 assert(false, 'unknown app "'..Current_app..'"')
118 Last_resize_time = Current_time
121 function App.filedropped(file)
122 if Current_app == 'run' then
123 if run.file_drop then run.file_drop(file) end
124 elseif Current_app == 'source' then
125 if source.file_drop then source.file_drop(file) end
126 else
127 assert(false, 'unknown app "'..Current_app..'"')
131 function App.focus(in_focus)
132 if in_focus then
133 Last_focus_time = Current_time
135 if Current_app == 'run' then
136 if run.focus then run.focus(in_focus) end
137 elseif Current_app == 'source' then
138 if source.focus then source.focus(in_focus) end
139 else
140 assert(false, 'unknown app "'..Current_app..'"')
144 function App.draw()
145 if Current_app == 'run' then
146 run.draw()
147 elseif Current_app == 'source' then
148 source.draw()
149 else
150 assert(false, 'unknown app "'..Current_app..'"')
154 function App.update(dt)
155 Current_time = Current_time + dt
156 -- some hysteresis while resizing
157 if Current_time < Last_resize_time + 0.1 then
158 return
161 if Current_app == 'run' then
162 run.update(dt)
163 elseif Current_app == 'source' then
164 source.update(dt)
165 else
166 assert(false, 'unknown app "'..Current_app..'"')
170 function App.keychord_press(chord, key)
171 -- ignore events for some time after window in focus (mostly alt-tab)
172 if Current_time < Last_focus_time + 0.01 then
173 return
176 if chord == 'C-e' then
177 -- carefully save settings
178 if Current_app == 'run' then
179 local source_settings = Settings.source
180 Settings = run.settings()
181 Settings.source = source_settings
182 if run.quit then run.quit() end
183 Current_app = 'source'
184 elseif Current_app == 'source' then
185 Settings.source = source.settings()
186 if source.quit then source.quit() end
187 Current_app = 'run'
188 else
189 assert(false, 'unknown app "'..Current_app..'"')
191 Settings.current_app = Current_app
192 love.filesystem.write('config', json.encode(Settings))
193 -- reboot
194 load_file_from_source_or_save_directory('main.lua')
195 App.undo_initialize()
196 App.run_tests_and_initialize()
197 return
199 if Current_app == 'run' then
200 if run.keychord_press then run.keychord_press(chord, key) end
201 elseif Current_app == 'source' then
202 if source.keychord_press then source.keychord_press(chord, key) end
203 else
204 assert(false, 'unknown app "'..Current_app..'"')
208 function App.textinput(t)
209 -- ignore events for some time after window in focus (mostly alt-tab)
210 if Current_time < Last_focus_time + 0.01 then
211 return
214 if Current_app == 'run' then
215 if run.text_input then run.text_input(t) end
216 elseif Current_app == 'source' then
217 if source.text_input then source.text_input(t) end
218 else
219 assert(false, 'unknown app "'..Current_app..'"')
223 function App.keyreleased(key, scancode)
224 -- ignore events for some time after window in focus (mostly alt-tab)
225 if Current_time < Last_focus_time + 0.01 then
226 return
229 if Current_app == 'run' then
230 if run.key_release then run.key_release(key, scancode) end
231 elseif Current_app == 'source' then
232 if source.key_release then source.key_release(key, scancode) end
233 else
234 assert(false, 'unknown app "'..Current_app..'"')
238 function App.mousepressed(x,y, mouse_button)
239 --? print('mouse press', x,y)
240 if Current_app == 'run' then
241 if run.mouse_press then run.mouse_press(x,y, mouse_button) end
242 elseif Current_app == 'source' then
243 if source.mouse_press then source.mouse_press(x,y, mouse_button) end
244 else
245 assert(false, 'unknown app "'..Current_app..'"')
249 function App.mousereleased(x,y, mouse_button)
250 if Current_app == 'run' then
251 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
252 elseif Current_app == 'source' then
253 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
254 else
255 assert(false, 'unknown app "'..Current_app..'"')
259 function App.wheelmoved(dx,dy)
260 if Current_app == 'run' then
261 if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end
262 elseif Current_app == 'source' then
263 if source.mouse_wheel_move then source.mouse_wheel_move(dx,dy) end
264 else
265 assert(false, 'unknown app "'..Current_app..'"')
269 function love.quit()
270 if Current_app == 'run' then
271 local source_settings = Settings.source
272 Settings = run.settings()
273 Settings.source = source_settings
274 else
275 Settings.source = source.settings()
277 Settings.current_app = Current_app
278 love.filesystem.write('config', json.encode(Settings))
279 if Current_app == 'run' then
280 if run.quit then run.quit() end
281 elseif Current_app == 'source' then
282 if source.quit then source.quit() end
283 else
284 assert(false, 'unknown app "'..Current_app..'"')