1 -- Entrypoint for the app. You can edit this file from within the app if
4 -- files that come with LÖVE; we can't edit those from within the app
7 function load_file_from_source_or_save_directory(filename
)
8 local contents
= love
.filesystem
.read(filename
)
9 local code
, err
= loadstring(contents
, filename
)
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 -- both sides use drawings
28 load_file_from_source_or_save_directory('icons.lua')
29 load_file_from_source_or_save_directory('drawing.lua')
30 load_file_from_source_or_save_directory('geom.lua')
31 load_file_from_source_or_save_directory('help.lua')
32 load_file_from_source_or_save_directory('drawing_tests.lua')
34 -- but some files we want to only load sometimes
37 if love
.filesystem
.getInfo('config') then
38 Settings
= json
.decode(love
.filesystem
.read('config'))
39 Current_app
= Settings
.current_app
45 -- | {name=warning message='...' next_app = run|source}
47 if Current_app
== nil then
51 if Current_app
== 'run' then
52 load_file_from_source_or_save_directory('file.lua')
53 load_file_from_source_or_save_directory('run.lua')
54 load_file_from_source_or_save_directory('edit.lua')
55 load_file_from_source_or_save_directory('text.lua')
56 load_file_from_source_or_save_directory('search.lua')
57 load_file_from_source_or_save_directory('select.lua')
58 load_file_from_source_or_save_directory('undo.lua')
59 load_file_from_source_or_save_directory('text_tests.lua')
60 load_file_from_source_or_save_directory('run_tests.lua')
61 elseif Current_app
== 'source' then
62 load_file_from_source_or_save_directory('source_file.lua')
63 load_file_from_source_or_save_directory('source.lua')
64 load_file_from_source_or_save_directory('commands.lua')
65 load_file_from_source_or_save_directory('source_edit.lua')
66 load_file_from_source_or_save_directory('log_browser.lua')
67 load_file_from_source_or_save_directory('source_text.lua')
68 load_file_from_source_or_save_directory('search.lua')
69 load_file_from_source_or_save_directory('source_select.lua')
70 load_file_from_source_or_save_directory('source_undo.lua')
71 load_file_from_source_or_save_directory('colorize.lua')
72 load_file_from_source_or_save_directory('source_text_tests.lua')
73 load_file_from_source_or_save_directory('source_tests.lua')
74 elseif current_app_is_warning() then
76 assert(false, 'unknown app "'..Current_app
..'"')
80 function App
.initialize_globals()
81 Supported_versions
= {'11.5', '11.4', '12.0'} -- put the recommended version first
82 check_love_version_for_tests()
84 if Current_app
== 'run' then
85 run
.initialize_globals()
86 elseif Current_app
== 'source' then
87 source
.initialize_globals()
88 elseif current_app_is_warning() then
90 assert(false, 'unknown app "'..Current_app
..'"')
93 -- for hysteresis in a few places
95 Last_focus_time
= 0 -- https://love2d.org/forums/viewtopic.php?p=249700
98 -- Another weird bit for a class of corner cases. E.g.:
99 -- * I press ctrl+e, switch Current_app. I don't want the new app to receive
100 -- text_input and key_release events.
101 -- If I try to avoid text_input events by switching modes on key_release, I
102 -- hit a new problem:
103 -- * I press ctrl+e, am running an untested version, Current_app goes to
104 -- 'warning', and immediately rolls back out of 'warning' in the
105 -- key_release event.
106 -- Skip_rest_of_key_events is ugly, but feels cleaner than creating yet
107 -- another possible value for Current_app.
108 Skip_rest_of_key_events
= nil
111 function check_love_version_for_tests()
112 if array
.find(Supported_versions
, Version
) == nil then
113 -- warning to include in an error message if any tests failed
114 Warning_before_tests
= ("This app hasn't been tested with LÖVE version %s."):format(Version
)
118 function App
.initialize(arg
)
119 love
.keyboard
.setKeyRepeat(true)
121 love
.graphics
.setBackgroundColor(1,1,1)
123 if Current_app
== 'run' then
125 elseif Current_app
== 'source' then
126 source
.initialize(arg
)
127 elseif current_app_is_warning() then
129 assert(false, 'unknown app "'..Current_app
..'"')
135 function check_love_version()
136 if array
.find(Supported_versions
, Version
) == nil then
138 ("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]))
139 -- continue initializing everything; hopefully we won't have errors during initialization
143 function App
.resize(w
,h
)
144 if current_app_is_warning() then return end
145 if Current_app
== 'run' then
146 if run
.resize
then run
.resize(w
,h
) end
147 elseif Current_app
== 'source' then
148 if source
.resize
then source
.resize(w
,h
) end
150 assert(false, 'unknown app "'..Current_app
..'"')
152 Last_resize_time
= Current_time
155 function App
.filedropped(file
)
156 if current_app_is_warning() then return end
157 if Current_app
== 'run' then
158 if run
.file_drop
then run
.file_drop(file
) end
159 elseif Current_app
== 'source' then
160 if source
.file_drop
then source
.file_drop(file
) end
162 assert(false, 'unknown app "'..Current_app
..'"')
166 function App
.focus(in_focus
)
167 if current_app_is_warning() then return end
169 Last_focus_time
= Current_time
171 if Current_app
== 'run' then
172 if run
.focus
then run
.focus(in_focus
) end
173 elseif Current_app
== 'source' then
174 if source
.focus
then source
.focus(in_focus
) end
176 assert(false, 'unknown app "'..Current_app
..'"')
181 if Current_app
== 'run' then
183 elseif Current_app
== 'source' then
185 elseif current_app_is_warning() then
186 love
.graphics
.setColor(0,0,1)
187 love
.graphics
.rectangle('fill', 0,0, App
.screen
.width
, App
.screen
.height
)
188 love
.graphics
.setColor(1,1,1)
189 love
.graphics
.printf(Current_app
.message
, 40,40, 600)
191 assert(false, 'unknown app "'..Current_app
..'"')
195 function App
.update(dt
)
196 Current_time
= Current_time
+ dt
197 if current_app_is_warning() then return end
198 -- some hysteresis while resizing
199 if Current_time
< Last_resize_time
+ 0.1 then
203 if Current_app
== 'run' then
205 elseif Current_app
== 'source' then
208 assert(false, 'unknown app "'..Current_app
..'"')
212 function App
.keychord_press(chord
, key
)
213 -- ignore events for some time after window in focus (mostly alt-tab)
214 if Current_time
< Last_focus_time
+ 0.01 then
218 Skip_rest_of_key_events
= nil
219 if current_app_is_warning() then
220 if chord
== 'C-c' then
221 love
.system
.setClipboardText(warning_message())
224 Skip_rest_of_key_events
= true
228 if chord
== 'C-e' then
229 -- carefully save settings
230 if Current_app
== 'run' then
231 local source_settings
= Settings
.source
232 Settings
= run
.settings()
233 Settings
.source
= source_settings
234 if run
.quit
then run
.quit() end
235 Current_app
= 'source'
236 -- preserve any Error_message when going from run to source
237 elseif Current_app
== 'source' then
238 Settings
.source
= source
.settings()
239 if source
.quit
then source
.quit() end
242 elseif current_app_is_warning() then
244 assert(false, 'unknown app "'..Current_app
..'"')
246 Settings
.current_app
= Current_app
247 love
.filesystem
.write('config', json
.encode(Settings
))
249 load_file_from_source_or_save_directory('main.lua')
250 App
.undo_initialize()
251 App
.run_tests_and_initialize()
252 Skip_rest_of_key_events
= true
255 if Current_app
== 'run' then
256 if run
.keychord_press
then run
.keychord_press(chord
, key
) end
257 elseif Current_app
== 'source' then
258 if source
.keychord_press
then source
.keychord_press(chord
, key
) end
260 assert(false, 'unknown app "'..Current_app
..'"')
264 function App
.textinput(t
)
265 if current_app_is_warning() then return end
266 -- ignore events for some time after window in focus (mostly alt-tab)
267 if Current_time
< Last_focus_time
+ 0.01 then
271 if Skip_rest_of_key_events
then return end
272 if Current_app
== 'run' then
273 if run
.text_input
then run
.text_input(t
) end
274 elseif Current_app
== 'source' then
275 if source
.text_input
then source
.text_input(t
) end
277 assert(false, 'unknown app "'..Current_app
..'"')
281 function App
.keyreleased(key
, scancode
)
282 if current_app_is_warning() then return end
283 -- ignore events for some time after window in focus (mostly alt-tab)
284 if Current_time
< Last_focus_time
+ 0.01 then
288 if Skip_rest_of_key_events
then return end
289 if Current_app
== 'run' then
290 if run
.key_release
then run
.key_release(key
, scancode
) end
291 elseif Current_app
== 'source' then
292 if source
.key_release
then source
.key_release(key
, scancode
) end
294 assert(false, 'unknown app "'..Current_app
..'"')
298 function App
.mousepressed(x
,y
, mouse_button
)
299 if current_app_is_warning() then return end
300 --? print('mouse press', x,y)
301 if Current_app
== 'run' then
302 if run
.mouse_press
then run
.mouse_press(x
,y
, mouse_button
) end
303 elseif Current_app
== 'source' then
304 if source
.mouse_press
then source
.mouse_press(x
,y
, mouse_button
) end
306 assert(false, 'unknown app "'..Current_app
..'"')
310 function App
.mousereleased(x
,y
, mouse_button
)
311 if current_app_is_warning() then return end
312 if Current_app
== 'run' then
313 if run
.mouse_release
then run
.mouse_release(x
,y
, mouse_button
) end
314 elseif Current_app
== 'source' then
315 if source
.mouse_release
then source
.mouse_release(x
,y
, mouse_button
) end
317 assert(false, 'unknown app "'..Current_app
..'"')
321 function App
.mousemoved(x
,y
, dx
,dy
, is_touch
)
322 if current_app_is_warning() then return end
323 if Current_app
== 'run' then
324 if run
.mouse_move
then run
.mouse_move(dx
,dy
) end
325 elseif Current_app
== 'source' then
326 if source
.mouse_move
then source
.mouse_move(dx
,dy
) end
328 assert(false, 'unknown app "'..Current_app
..'"')
332 function App
.wheelmoved(dx
,dy
)
333 if current_app_is_warning() then return end
334 if Current_app
== 'run' then
335 if run
.mouse_wheel_move
then run
.mouse_wheel_move(dx
,dy
) end
336 elseif Current_app
== 'source' then
337 if source
.mouse_wheel_move
then source
.mouse_wheel_move(dx
,dy
) end
339 assert(false, 'unknown app "'..Current_app
..'"')
343 function App
.mousefocus(in_focus
)
344 if current_app_is_warning() then return end
345 if Current_app
== 'run' then
346 if run
.mouse_focus
then run
.mouse_focus(in_focus
) end
347 elseif Current_app
== 'source' then
348 if source
.mouse_focus
then source
.mouse_focus(in_focus
) end
350 assert(false, 'unknown app "'..Current_app
..'"')
355 if Disable_all_quit_handlers
then return end
356 if current_app_is_warning() then return end
357 if Current_app
== 'run' then
358 local source_settings
= Settings
.source
359 Settings
= run
.settings()
360 Settings
.source
= source_settings
362 Settings
.source
= source
.settings()
364 Settings
.current_app
= Current_app
365 love
.filesystem
.write('config', json
.encode(Settings
))
366 if Current_app
== 'run' then
367 if run
.quit
then run
.quit() end
368 elseif Current_app
== 'source' then
369 if source
.quit
then source
.quit() end
371 assert(false, 'unknown app "'..Current_app
..'"')
375 function current_app_is_warning()
376 return type(Current_app
) == 'table' and Current_app
.name
== 'warning'
379 function show_warning(message
)
380 assert(type(Current_app
) == 'string')
384 next_app
= Current_app
,
388 function clear_warning()
389 assert(type(Current_app
) == 'table')
390 Current_app
= Current_app
.next_app
393 function warning_message()
394 assert(type(Current_app
) == 'table')
395 return Current_app
.message