Merge lines.love
[view.love.git] / main.lua
blobe7d35f403c50e7e73506a40a3650b98321dd339a
1 -- Entrypoint for the app. You can edit this file from within the app if
2 -- you're careful.
4 -- files that come with LÖVE; we can't edit those from within the app
5 utf8 = require 'utf8'
7 function load_file_from_source_or_save_directory(filename)
8 local contents = love.filesystem.read(filename)
9 local code, err = loadstring(contents, filename)
10 if code == nil then
11 error(err)
12 end
13 return code()
14 end
16 json = load_file_from_source_or_save_directory('json.lua')
18 load_file_from_source_or_save_directory('app.lua')
19 load_file_from_source_or_save_directory('test.lua')
21 load_file_from_source_or_save_directory('keychord.lua')
22 load_file_from_source_or_save_directory('button.lua')
24 -- both sides require (different parts of) the logging framework
25 load_file_from_source_or_save_directory('log.lua')
27 -- but some files we want to only load sometimes
28 function App.load()
29 log_new('session')
30 if love.filesystem.getInfo('config') then
31 Settings = json.decode(love.filesystem.read('config'))
32 Current_app = Settings.current_app
33 end
35 -- Current_app =
36 -- | run
37 -- | source
38 -- | {name=warning message='...' next_app = run|source}
40 if Current_app == nil then
41 Current_app = 'run'
42 end
44 if Current_app == 'run' then
45 load_file_from_source_or_save_directory('file.lua')
46 load_file_from_source_or_save_directory('run.lua')
47 load_file_from_source_or_save_directory('edit.lua')
48 load_file_from_source_or_save_directory('text.lua')
49 load_file_from_source_or_save_directory('search.lua')
50 load_file_from_source_or_save_directory('select.lua')
51 load_file_from_source_or_save_directory('undo.lua')
52 load_file_from_source_or_save_directory('text_tests.lua')
53 load_file_from_source_or_save_directory('run_tests.lua')
54 elseif Current_app == 'source' then
55 load_file_from_source_or_save_directory('source_file.lua')
56 load_file_from_source_or_save_directory('source.lua')
57 load_file_from_source_or_save_directory('commands.lua')
58 load_file_from_source_or_save_directory('source_edit.lua')
59 load_file_from_source_or_save_directory('log_browser.lua')
60 load_file_from_source_or_save_directory('source_text.lua')
61 load_file_from_source_or_save_directory('search.lua')
62 load_file_from_source_or_save_directory('source_select.lua')
63 load_file_from_source_or_save_directory('source_undo.lua')
64 load_file_from_source_or_save_directory('colorize.lua')
65 load_file_from_source_or_save_directory('source_text_tests.lua')
66 load_file_from_source_or_save_directory('icons.lua')
67 load_file_from_source_or_save_directory('drawing.lua')
68 load_file_from_source_or_save_directory('geom.lua')
69 load_file_from_source_or_save_directory('help.lua')
70 load_file_from_source_or_save_directory('drawing_tests.lua')
71 load_file_from_source_or_save_directory('source_tests.lua')
72 elseif current_app_is_warning() then
73 else
74 assert(false, 'unknown app "'..Current_app..'"')
75 end
76 end
78 function App.initialize_globals()
79 Supported_versions = {'11.5', '11.4', '12.0'} -- put the recommended version first
80 check_love_version_for_tests()
82 if Current_app == 'run' then
83 run.initialize_globals()
84 elseif Current_app == 'source' then
85 source.initialize_globals()
86 elseif current_app_is_warning() then
87 else
88 assert(false, 'unknown app "'..Current_app..'"')
89 end
91 -- for hysteresis in a few places
92 Current_time = 0
93 Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700
94 Last_resize_time = 0
96 -- Another weird bit for a class of corner cases. E.g.:
97 -- * I press ctrl+e, switch Current_app. I don't want the new app to receive
98 -- text_input and key_release events.
99 -- If I try to avoid text_input events by switching modes on key_release, I
100 -- hit a new problem:
101 -- * I press ctrl+e, am running an untested version, Current_app goes to
102 -- 'warning', and immediately rolls back out of 'warning' in the
103 -- key_release event.
104 -- Skip_rest_of_key_events is ugly, but feels cleaner than creating yet
105 -- another possible value for Current_app.
106 Skip_rest_of_key_events = nil
109 function check_love_version_for_tests()
110 if array.find(Supported_versions, Version) == nil then
111 -- warning to include in an error message if any tests failed
112 Warning_before_tests = ("This app hasn't been tested with LÖVE version %s."):format(Version)
116 function App.initialize(arg)
117 love.keyboard.setKeyRepeat(true)
119 love.graphics.setBackgroundColor(1,1,1)
121 if Current_app == 'run' then
122 run.initialize(arg)
123 elseif Current_app == 'source' then
124 source.initialize(arg)
125 elseif current_app_is_warning() then
126 else
127 assert(false, 'unknown app "'..Current_app..'"')
130 check_love_version()
133 function check_love_version()
134 if array.find(Supported_versions, Version) == nil then
135 show_warning(
136 ("This app hasn't been tested with LÖVE version %s; please switch to version %s if you run into issues. Press any key to continue."):format(Version, Supported_versions[1]))
137 -- continue initializing everything; hopefully we won't have errors during initialization
141 function App.resize(w,h)
142 if current_app_is_warning() then return end
143 if Current_app == 'run' then
144 if run.resize then run.resize(w,h) end
145 elseif Current_app == 'source' then
146 if source.resize then source.resize(w,h) end
147 else
148 assert(false, 'unknown app "'..Current_app..'"')
150 Last_resize_time = Current_time
153 function App.filedropped(file)
154 if current_app_is_warning() then return end
155 if Current_app == 'run' then
156 if run.file_drop then run.file_drop(file) end
157 elseif Current_app == 'source' then
158 if source.file_drop then source.file_drop(file) end
159 else
160 assert(false, 'unknown app "'..Current_app..'"')
164 function App.focus(in_focus)
165 if current_app_is_warning() then return end
166 if in_focus then
167 Last_focus_time = Current_time
169 if Current_app == 'run' then
170 if run.focus then run.focus(in_focus) end
171 elseif Current_app == 'source' then
172 if source.focus then source.focus(in_focus) end
173 else
174 assert(false, 'unknown app "'..Current_app..'"')
178 function App.draw()
179 if Current_app == 'run' then
180 run.draw()
181 elseif Current_app == 'source' then
182 source.draw()
183 elseif current_app_is_warning() then
184 love.graphics.setColor(0,0,1)
185 love.graphics.rectangle('fill', 0,0, App.screen.width, App.screen.height)
186 love.graphics.setColor(1,1,1)
187 love.graphics.printf(Current_app.message, 40,40, 600)
188 else
189 assert(false, 'unknown app "'..Current_app..'"')
193 function App.update(dt)
194 Current_time = Current_time + dt
195 if current_app_is_warning() then return end
196 -- some hysteresis while resizing
197 if Current_time < Last_resize_time + 0.1 then
198 return
201 if Current_app == 'run' then
202 run.update(dt)
203 elseif Current_app == 'source' then
204 source.update(dt)
205 else
206 assert(false, 'unknown app "'..Current_app..'"')
210 function App.keychord_press(chord, key)
211 -- ignore events for some time after window in focus (mostly alt-tab)
212 if Current_time < Last_focus_time + 0.01 then
213 return
216 Skip_rest_of_key_events = nil
217 if current_app_is_warning() then
218 if chord == 'C-c' then
219 love.system.setClipboardText(warning_message())
220 else
221 clear_warning()
222 Skip_rest_of_key_events = true
224 return
226 if chord == 'C-e' then
227 -- carefully save settings
228 if Current_app == 'run' then
229 local source_settings = Settings.source
230 Settings = run.settings()
231 Settings.source = source_settings
232 if run.quit then run.quit() end
233 Current_app = 'source'
234 -- preserve any Error_message when going from run to source
235 elseif Current_app == 'source' then
236 Settings.source = source.settings()
237 if source.quit then source.quit() end
238 Current_app = 'run'
239 Error_message = nil
240 elseif current_app_is_warning() then
241 else
242 assert(false, 'unknown app "'..Current_app..'"')
244 Settings.current_app = Current_app
245 love.filesystem.write('config', json.encode(Settings))
246 -- reboot
247 load_file_from_source_or_save_directory('main.lua')
248 App.undo_initialize()
249 App.run_tests_and_initialize()
250 Skip_rest_of_key_events = true
251 return
253 if Current_app == 'run' then
254 if run.keychord_press then run.keychord_press(chord, key) end
255 elseif Current_app == 'source' then
256 if source.keychord_press then source.keychord_press(chord, key) end
257 else
258 assert(false, 'unknown app "'..Current_app..'"')
262 function App.textinput(t)
263 if current_app_is_warning() then return end
264 -- ignore events for some time after window in focus (mostly alt-tab)
265 if Current_time < Last_focus_time + 0.01 then
266 return
269 if Skip_rest_of_key_events then return end
270 if Current_app == 'run' then
271 if run.text_input then run.text_input(t) end
272 elseif Current_app == 'source' then
273 if source.text_input then source.text_input(t) end
274 else
275 assert(false, 'unknown app "'..Current_app..'"')
279 function App.keyreleased(key, scancode)
280 if current_app_is_warning() then return end
281 -- ignore events for some time after window in focus (mostly alt-tab)
282 if Current_time < Last_focus_time + 0.01 then
283 return
286 if Skip_rest_of_key_events then return end
287 if Current_app == 'run' then
288 if run.key_release then run.key_release(key, scancode) end
289 elseif Current_app == 'source' then
290 if source.key_release then source.key_release(key, scancode) end
291 else
292 assert(false, 'unknown app "'..Current_app..'"')
296 function App.mousepressed(x,y, mouse_button)
297 if current_app_is_warning() then return end
298 --? print('mouse press', x,y)
299 if Current_app == 'run' then
300 if run.mouse_press then run.mouse_press(x,y, mouse_button) end
301 elseif Current_app == 'source' then
302 if source.mouse_press then source.mouse_press(x,y, mouse_button) end
303 else
304 assert(false, 'unknown app "'..Current_app..'"')
308 function App.mousereleased(x,y, mouse_button)
309 if current_app_is_warning() then return end
310 if Current_app == 'run' then
311 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
312 elseif Current_app == 'source' then
313 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
314 else
315 assert(false, 'unknown app "'..Current_app..'"')
319 function App.mousemoved(x,y, dx,dy, is_touch)
320 if current_app_is_warning() then return end
321 if Current_app == 'run' then
322 if run.mouse_move then run.mouse_move(dx,dy) end
323 elseif Current_app == 'source' then
324 if source.mouse_move then source.mouse_move(dx,dy) end
325 else
326 assert(false, 'unknown app "'..Current_app..'"')
330 function App.wheelmoved(dx,dy)
331 if current_app_is_warning() then return end
332 if Current_app == 'run' then
333 if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end
334 elseif Current_app == 'source' then
335 if source.mouse_wheel_move then source.mouse_wheel_move(dx,dy) end
336 else
337 assert(false, 'unknown app "'..Current_app..'"')
341 function App.mousefocus(in_focus)
342 if current_app_is_warning() then return end
343 if Current_app == 'run' then
344 if run.mouse_focus then run.mouse_focus(in_focus) end
345 elseif Current_app == 'source' then
346 if source.mouse_focus then source.mouse_focus(in_focus) end
347 else
348 assert(false, 'unknown app "'..Current_app..'"')
352 function love.quit()
353 if Disable_all_quit_handlers then return end
354 if current_app_is_warning() then return end
355 if Current_app == 'run' then
356 local source_settings = Settings.source
357 Settings = run.settings()
358 Settings.source = source_settings
359 else
360 Settings.source = source.settings()
362 Settings.current_app = Current_app
363 love.filesystem.write('config', json.encode(Settings))
364 if Current_app == 'run' then
365 if run.quit then run.quit() end
366 elseif Current_app == 'source' then
367 if source.quit then source.quit() end
368 else
369 assert(false, 'unknown app "'..Current_app..'"')
373 function current_app_is_warning()
374 return type(Current_app) == 'table' and Current_app.name == 'warning'
377 function show_warning(message)
378 assert(type(Current_app) == 'string')
379 Current_app = {
380 name = 'warning',
381 message = message,
382 next_app = Current_app,
386 function clear_warning()
387 assert(type(Current_app) == 'table')
388 Current_app = Current_app.next_app
391 function warning_message()
392 assert(type(Current_app) == 'table')
393 return Current_app.message