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
.setTextInput(true) -- bring up keyboard on touch screen
120 love
.keyboard
.setKeyRepeat(true)
122 love
.graphics
.setBackgroundColor(1,1,1)
124 if Current_app
== 'run' then
126 elseif Current_app
== 'source' then
127 source
.initialize(arg
)
128 elseif current_app_is_warning() then
130 assert(false, 'unknown app "'..Current_app
..'"')
136 function check_love_version()
137 if array
.find(Supported_versions
, Version
) == nil then
139 ("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]))
140 -- continue initializing everything; hopefully we won't have errors during initialization
144 function App
.resize(w
,h
)
145 if current_app_is_warning() then return end
146 if Current_app
== 'run' then
147 if run
.resize
then run
.resize(w
,h
) end
148 elseif Current_app
== 'source' then
149 if source
.resize
then source
.resize(w
,h
) end
151 assert(false, 'unknown app "'..Current_app
..'"')
153 Last_resize_time
= Current_time
156 function App
.filedropped(file
)
157 if current_app_is_warning() then return end
158 if Current_app
== 'run' then
159 if run
.file_drop
then run
.file_drop(file
) end
160 elseif Current_app
== 'source' then
161 if source
.file_drop
then source
.file_drop(file
) end
163 assert(false, 'unknown app "'..Current_app
..'"')
167 function App
.focus(in_focus
)
168 if current_app_is_warning() then return end
170 Last_focus_time
= Current_time
172 if Current_app
== 'run' then
173 if run
.focus
then run
.focus(in_focus
) end
174 elseif Current_app
== 'source' then
175 if source
.focus
then source
.focus(in_focus
) end
177 assert(false, 'unknown app "'..Current_app
..'"')
182 if Current_app
== 'run' then
184 elseif Current_app
== 'source' then
186 elseif current_app_is_warning() then
187 love
.graphics
.setColor(0,0,1)
188 love
.graphics
.rectangle('fill', 0,0, App
.screen
.width
, App
.screen
.height
)
189 love
.graphics
.setColor(1,1,1)
190 love
.graphics
.printf(Current_app
.message
, 40,40, 600)
192 assert(false, 'unknown app "'..Current_app
..'"')
196 function App
.update(dt
)
197 Current_time
= Current_time
+ dt
198 if current_app_is_warning() then return end
199 -- some hysteresis while resizing
200 if Current_time
< Last_resize_time
+ 0.1 then
204 if Current_app
== 'run' then
206 elseif Current_app
== 'source' then
209 assert(false, 'unknown app "'..Current_app
..'"')
213 function App
.keychord_press(chord
, key
)
214 -- ignore events for some time after window in focus (mostly alt-tab)
215 if Current_time
< Last_focus_time
+ 0.01 then
219 Skip_rest_of_key_events
= nil
220 if current_app_is_warning() then
221 if chord
== 'C-c' then
222 love
.system
.setClipboardText(warning_message())
225 Skip_rest_of_key_events
= true
229 if chord
== 'C-e' then
230 -- carefully save settings
231 if Current_app
== 'run' then
232 local source_settings
= Settings
.source
233 Settings
= run
.settings()
234 Settings
.source
= source_settings
235 if run
.quit
then run
.quit() end
236 Current_app
= 'source'
237 -- preserve any Error_message when going from run to source
238 elseif Current_app
== 'source' then
239 Settings
.source
= source
.settings()
240 if source
.quit
then source
.quit() end
243 elseif current_app_is_warning() then
245 assert(false, 'unknown app "'..Current_app
..'"')
247 Settings
.current_app
= Current_app
248 love
.filesystem
.write('config', json
.encode(Settings
))
250 load_file_from_source_or_save_directory('main.lua')
251 App
.undo_initialize()
252 App
.run_tests_and_initialize()
253 Skip_rest_of_key_events
= true
256 if Current_app
== 'run' then
257 if run
.keychord_press
then run
.keychord_press(chord
, key
) end
258 elseif Current_app
== 'source' then
259 if source
.keychord_press
then source
.keychord_press(chord
, key
) end
261 assert(false, 'unknown app "'..Current_app
..'"')
265 function App
.textinput(t
)
266 if current_app_is_warning() then return end
267 -- ignore events for some time after window in focus (mostly alt-tab)
268 if Current_time
< Last_focus_time
+ 0.01 then
272 if Skip_rest_of_key_events
then return end
273 if Current_app
== 'run' then
274 if run
.text_input
then run
.text_input(t
) end
275 elseif Current_app
== 'source' then
276 if source
.text_input
then source
.text_input(t
) end
278 assert(false, 'unknown app "'..Current_app
..'"')
282 function App
.keyreleased(key
, scancode
)
283 if current_app_is_warning() then return end
284 -- ignore events for some time after window in focus (mostly alt-tab)
285 if Current_time
< Last_focus_time
+ 0.01 then
289 if Skip_rest_of_key_events
then return end
290 if Current_app
== 'run' then
291 if run
.key_release
then run
.key_release(key
, scancode
) end
292 elseif Current_app
== 'source' then
293 if source
.key_release
then source
.key_release(key
, scancode
) end
295 assert(false, 'unknown app "'..Current_app
..'"')
299 function App
.mousepressed(x
,y
, mouse_button
)
300 if current_app_is_warning() then return end
301 --? print('mouse press', x,y)
302 if Current_app
== 'run' then
303 if run
.mouse_press
then run
.mouse_press(x
,y
, mouse_button
) end
304 elseif Current_app
== 'source' then
305 if source
.mouse_press
then source
.mouse_press(x
,y
, mouse_button
) end
307 assert(false, 'unknown app "'..Current_app
..'"')
311 function App
.mousereleased(x
,y
, mouse_button
)
312 if current_app_is_warning() then return end
313 if Current_app
== 'run' then
314 if run
.mouse_release
then run
.mouse_release(x
,y
, mouse_button
) end
315 elseif Current_app
== 'source' then
316 if source
.mouse_release
then source
.mouse_release(x
,y
, mouse_button
) end
318 assert(false, 'unknown app "'..Current_app
..'"')
322 function App
.wheelmoved(dx
,dy
)
323 if current_app_is_warning() then return end
324 if Current_app
== 'run' then
325 if run
.mouse_wheel_move
then run
.mouse_wheel_move(dx
,dy
) end
326 elseif Current_app
== 'source' then
327 if source
.mouse_wheel_move
then source
.mouse_wheel_move(dx
,dy
) end
329 assert(false, 'unknown app "'..Current_app
..'"')
334 if current_app_is_warning() then return end
335 if Current_app
== 'run' then
336 local source_settings
= Settings
.source
337 Settings
= run
.settings()
338 Settings
.source
= source_settings
340 Settings
.source
= source
.settings()
342 Settings
.current_app
= Current_app
343 love
.filesystem
.write('config', json
.encode(Settings
))
344 if Current_app
== 'run' then
345 if run
.quit
then run
.quit() end
346 elseif Current_app
== 'source' then
347 if source
.quit
then source
.quit() end
349 assert(false, 'unknown app "'..Current_app
..'"')
353 function current_app_is_warning()
354 return type(Current_app
) == 'table' and Current_app
.name
== 'warning'
357 function show_warning(message
)
358 assert(type(Current_app
) == 'string')
362 next_app
= Current_app
,
366 function clear_warning()
367 assert(type(Current_app
) == 'table')
368 Current_app
= Current_app
.next_app
371 function warning_message()
372 assert(type(Current_app
) == 'table')
373 return Current_app
.message