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 -- but some files we want to only load sometimes
30 if love
.filesystem
.getInfo('config') then
31 Settings
= json
.decode(love
.filesystem
.read('config'))
32 Current_app
= Settings
.current_app
38 -- | {name=warning message='...' next_app = run|source}
40 if Current_app
== nil then
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
74 assert(false, 'unknown app "'..Current_app
..'"')
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
88 assert(false, 'unknown app "'..Current_app
..'"')
91 -- for hysteresis in a few places
93 Last_focus_time
= 0 -- https://love2d.org/forums/viewtopic.php?p=249700
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
.setTextInput(true) -- bring up keyboard on touch screen
118 love
.keyboard
.setKeyRepeat(true)
120 love
.graphics
.setBackgroundColor(1,1,1)
122 if Current_app
== 'run' then
124 elseif Current_app
== 'source' then
125 source
.initialize(arg
)
126 elseif current_app_is_warning() then
128 assert(false, 'unknown app "'..Current_app
..'"')
134 function check_love_version()
135 if array
.find(Supported_versions
, Version
) == nil then
137 ("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]))
138 -- continue initializing everything; hopefully we won't have errors during initialization
142 function App
.resize(w
,h
)
143 if current_app_is_warning() then return end
144 if Current_app
== 'run' then
145 if run
.resize
then run
.resize(w
,h
) end
146 elseif Current_app
== 'source' then
147 if source
.resize
then source
.resize(w
,h
) end
149 assert(false, 'unknown app "'..Current_app
..'"')
151 Last_resize_time
= Current_time
154 function App
.filedropped(file
)
155 if current_app_is_warning() then return end
156 if Current_app
== 'run' then
157 if run
.file_drop
then run
.file_drop(file
) end
158 elseif Current_app
== 'source' then
159 if source
.file_drop
then source
.file_drop(file
) end
161 assert(false, 'unknown app "'..Current_app
..'"')
165 function App
.focus(in_focus
)
166 if current_app_is_warning() then return end
168 Last_focus_time
= Current_time
170 if Current_app
== 'run' then
171 if run
.focus
then run
.focus(in_focus
) end
172 elseif Current_app
== 'source' then
173 if source
.focus
then source
.focus(in_focus
) end
175 assert(false, 'unknown app "'..Current_app
..'"')
180 if Current_app
== 'run' then
182 elseif Current_app
== 'source' then
184 elseif current_app_is_warning() then
185 love
.graphics
.setColor(0,0,1)
186 love
.graphics
.rectangle('fill', 0,0, App
.screen
.width
, App
.screen
.height
)
187 love
.graphics
.setColor(1,1,1)
188 love
.graphics
.printf(Current_app
.message
, 40,40, 600)
190 assert(false, 'unknown app "'..Current_app
..'"')
194 function App
.update(dt
)
195 Current_time
= Current_time
+ dt
196 if current_app_is_warning() then return end
197 -- some hysteresis while resizing
198 if Current_time
< Last_resize_time
+ 0.1 then
202 if Current_app
== 'run' then
204 elseif Current_app
== 'source' then
207 assert(false, 'unknown app "'..Current_app
..'"')
211 function App
.keychord_press(chord
, key
)
212 -- ignore events for some time after window in focus (mostly alt-tab)
213 if Current_time
< Last_focus_time
+ 0.01 then
217 Skip_rest_of_key_events
= nil
218 if current_app_is_warning() then
219 if chord
== 'C-c' then
220 love
.system
.setClipboardText(Error_message
)
223 Skip_rest_of_key_events
= true
227 if chord
== 'C-e' then
228 -- carefully save settings
229 if Current_app
== 'run' then
230 local source_settings
= Settings
.source
231 Settings
= run
.settings()
232 Settings
.source
= source_settings
233 if run
.quit
then run
.quit() end
234 Current_app
= 'source'
235 -- preserve any Error_message when going from run to source
236 elseif Current_app
== 'source' then
237 Settings
.source
= source
.settings()
238 if source
.quit
then source
.quit() end
241 elseif current_app_is_warning() then
243 assert(false, 'unknown app "'..Current_app
..'"')
245 Settings
.current_app
= Current_app
246 love
.filesystem
.write('config', json
.encode(Settings
))
248 load_file_from_source_or_save_directory('main.lua')
249 App
.undo_initialize()
250 App
.run_tests_and_initialize()
251 Skip_rest_of_key_events
= true
254 if Current_app
== 'run' then
255 if run
.keychord_press
then run
.keychord_press(chord
, key
) end
256 elseif Current_app
== 'source' then
257 if source
.keychord_press
then source
.keychord_press(chord
, key
) end
259 assert(false, 'unknown app "'..Current_app
..'"')
263 function App
.textinput(t
)
264 if current_app_is_warning() then return end
265 -- ignore events for some time after window in focus (mostly alt-tab)
266 if Current_time
< Last_focus_time
+ 0.01 then
270 if Skip_rest_of_key_events
then return end
271 if Current_app
== 'run' then
272 if run
.text_input
then run
.text_input(t
) end
273 elseif Current_app
== 'source' then
274 if source
.text_input
then source
.text_input(t
) end
276 assert(false, 'unknown app "'..Current_app
..'"')
280 function App
.keyreleased(key
, scancode
)
281 if current_app_is_warning() then return end
282 -- ignore events for some time after window in focus (mostly alt-tab)
283 if Current_time
< Last_focus_time
+ 0.01 then
287 if Skip_rest_of_key_events
then return end
288 if Current_app
== 'run' then
289 if run
.key_release
then run
.key_release(key
, scancode
) end
290 elseif Current_app
== 'source' then
291 if source
.key_release
then source
.key_release(key
, scancode
) end
293 assert(false, 'unknown app "'..Current_app
..'"')
297 function App
.mousepressed(x
,y
, mouse_button
)
298 if current_app_is_warning() then return end
299 --? print('mouse press', x,y)
300 if Current_app
== 'run' then
301 if run
.mouse_press
then run
.mouse_press(x
,y
, mouse_button
) end
302 elseif Current_app
== 'source' then
303 if source
.mouse_press
then source
.mouse_press(x
,y
, mouse_button
) end
305 assert(false, 'unknown app "'..Current_app
..'"')
309 function App
.mousereleased(x
,y
, mouse_button
)
310 if current_app_is_warning() then return end
311 if Current_app
== 'run' then
312 if run
.mouse_release
then run
.mouse_release(x
,y
, mouse_button
) end
313 elseif Current_app
== 'source' then
314 if source
.mouse_release
then source
.mouse_release(x
,y
, mouse_button
) end
316 assert(false, 'unknown app "'..Current_app
..'"')
320 function App
.wheelmoved(dx
,dy
)
321 if current_app_is_warning() then return end
322 if Current_app
== 'run' then
323 if run
.mouse_wheel_move
then run
.mouse_wheel_move(dx
,dy
) end
324 elseif Current_app
== 'source' then
325 if source
.mouse_wheel_move
then source
.mouse_wheel_move(dx
,dy
) end
327 assert(false, 'unknown app "'..Current_app
..'"')
332 if current_app_is_warning() then return end
333 if Current_app
== 'run' then
334 local source_settings
= Settings
.source
335 Settings
= run
.settings()
336 Settings
.source
= source_settings
338 Settings
.source
= source
.settings()
340 Settings
.current_app
= Current_app
341 love
.filesystem
.write('config', json
.encode(Settings
))
342 if Current_app
== 'run' then
343 if run
.quit
then run
.quit() end
344 elseif Current_app
== 'source' then
345 if source
.quit
then source
.quit() end
347 assert(false, 'unknown app "'..Current_app
..'"')
351 function current_app_is_warning()
352 return type(Current_app
) == 'table' and Current_app
.name
== 'warning'
355 function show_warning(message
)
356 assert(type(Current_app
) == 'string')
360 next_app
= Current_app
,
364 function clear_warning()
365 assert(type(Current_app
) == 'table')
366 Current_app
= Current_app
.next_app