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
42 if Current_app
== nil then
46 if Current_app
== 'run' then
47 load_file_from_source_or_save_directory('file.lua')
48 load_file_from_source_or_save_directory('run.lua')
49 load_file_from_source_or_save_directory('edit.lua')
50 load_file_from_source_or_save_directory('text.lua')
51 load_file_from_source_or_save_directory('search.lua')
52 load_file_from_source_or_save_directory('select.lua')
53 load_file_from_source_or_save_directory('undo.lua')
54 load_file_from_source_or_save_directory('text_tests.lua')
55 load_file_from_source_or_save_directory('run_tests.lua')
56 elseif Current_app
== 'source' then
57 load_file_from_source_or_save_directory('source_file.lua')
58 load_file_from_source_or_save_directory('source.lua')
59 load_file_from_source_or_save_directory('commands.lua')
60 load_file_from_source_or_save_directory('source_edit.lua')
61 load_file_from_source_or_save_directory('log_browser.lua')
62 load_file_from_source_or_save_directory('source_text.lua')
63 load_file_from_source_or_save_directory('search.lua')
64 load_file_from_source_or_save_directory('source_select.lua')
65 load_file_from_source_or_save_directory('source_undo.lua')
66 load_file_from_source_or_save_directory('colorize.lua')
67 load_file_from_source_or_save_directory('source_text_tests.lua')
68 load_file_from_source_or_save_directory('source_tests.lua')
70 assert(false, 'unknown app "'..Current_app
..'"')
74 function App
.version_check()
75 -- available modes: run, error
78 -- we'll reuse error mode on load for an initial version check
79 local supported_versions
= {'11.4', '12.0'} -- put the recommended version first
81 Major_version
, minor_version
= love
.getVersion()
82 Version
= Major_version
..'.'..minor_version
83 if array
.find(supported_versions
, Version
) == nil then
85 Error_message
= ("This app doesn't support version %s; please use version %s. Press any key to try it with this version anyway."):format(Version
, supported_versions
[1])
87 -- continue initializing everything; hopefully we won't have errors during initialization
91 function App
.initialize_globals()
92 if Current_app
== 'run' then
93 run
.initialize_globals()
94 elseif Current_app
== 'source' then
95 source
.initialize_globals()
97 assert(false, 'unknown app "'..Current_app
..'"')
100 -- for hysteresis in a few places
102 Last_focus_time
= 0 -- https://love2d.org/forums/viewtopic.php?p=249700
106 function App
.initialize(arg
)
107 love
.keyboard
.setTextInput(true) -- bring up keyboard on touch screen
108 love
.keyboard
.setKeyRepeat(true)
110 love
.graphics
.setBackgroundColor(1,1,1)
112 if Current_app
== 'run' then
114 elseif Current_app
== 'source' then
115 source
.initialize(arg
)
117 assert(false, 'unknown app "'..Current_app
..'"')
121 function App
.resize(w
,h
)
122 if Current_app
== 'run' then
123 if run
.resize
then run
.resize(w
,h
) end
124 elseif Current_app
== 'source' then
125 if source
.resize
then source
.resize(w
,h
) end
127 assert(false, 'unknown app "'..Current_app
..'"')
129 Last_resize_time
= Current_time
132 function App
.filedropped(file
)
133 if Current_app
== 'run' then
134 if run
.file_drop
then run
.file_drop(file
) end
135 elseif Current_app
== 'source' then
136 if source
.file_drop
then source
.file_drop(file
) end
138 assert(false, 'unknown app "'..Current_app
..'"')
142 function App
.focus(in_focus
)
144 Last_focus_time
= Current_time
146 if Current_app
== 'run' then
147 if run
.focus
then run
.focus(in_focus
) end
148 elseif Current_app
== 'source' then
149 if source
.focus
then source
.focus(in_focus
) end
151 assert(false, 'unknown app "'..Current_app
..'"')
156 if Current_app
== 'error' then
157 love
.graphics
.setColor(0,0,1)
158 love
.graphics
.rectangle('fill', 0,0, App
.screen
.width
, App
.screen
.height
)
159 love
.graphics
.setColor(1,1,1)
160 love
.graphics
.printf(Error_message
, 40,40, 600)
161 elseif Current_app
== 'run' then
163 elseif Current_app
== 'source' then
166 assert(false, 'unknown app "'..Current_app
..'"')
170 function App
.update(dt
)
171 Current_time
= Current_time
+ dt
172 -- some hysteresis while resizing
173 if Current_time
< Last_resize_time
+ 0.1 then
177 if Current_app
== 'run' then
179 elseif Current_app
== 'source' then
182 assert(false, 'unknown app "'..Current_app
..'"')
186 function App
.keychord_press(chord
, key
)
187 -- ignore events for some time after window in focus (mostly alt-tab)
188 if Current_time
< Last_focus_time
+ 0.01 then
192 if Current_app
== 'error' then
193 if chord
== 'C-c' then
194 love
.system
.setClipboardText(Error_message
)
198 if chord
== 'C-e' then
199 -- carefully save settings
200 if Current_app
== 'run' then
201 local source_settings
= Settings
.source
202 Settings
= run
.settings()
203 Settings
.source
= source_settings
204 if run
.quit
then run
.quit() end
205 Current_app
= 'source'
206 -- preserve any Error_message when going from run to source
207 elseif Current_app
== 'source' then
208 Settings
.source
= source
.settings()
209 if source
.quit
then source
.quit() end
213 assert(false, 'unknown app "'..Current_app
..'"')
215 Settings
.current_app
= Current_app
216 love
.filesystem
.write('config', json
.encode(Settings
))
218 load_file_from_source_or_save_directory('main.lua')
219 App
.undo_initialize()
220 App
.run_tests_and_initialize()
223 if Current_app
== 'run' then
224 if run
.keychord_press
then run
.keychord_press(chord
, key
) end
225 elseif Current_app
== 'source' then
226 if source
.keychord_press
then source
.keychord_press(chord
, key
) end
228 assert(false, 'unknown app "'..Current_app
..'"')
232 function App
.textinput(t
)
233 if Current_app
== 'error' then return end
234 -- ignore events for some time after window in focus (mostly alt-tab)
235 if Current_time
< Last_focus_time
+ 0.01 then
239 if Current_app
== 'run' then
240 if run
.text_input
then run
.text_input(t
) end
241 elseif Current_app
== 'source' then
242 if source
.text_input
then source
.text_input(t
) end
244 assert(false, 'unknown app "'..Current_app
..'"')
248 function App
.keyreleased(key
, scancode
)
249 if Current_app
== 'error' then return end
250 -- ignore events for some time after window in focus (mostly alt-tab)
251 if Current_time
< Last_focus_time
+ 0.01 then
255 if Current_app
== 'run' then
256 if run
.key_release
then run
.key_release(key
, scancode
) end
257 elseif Current_app
== 'source' then
258 if source
.key_release
then source
.key_release(key
, scancode
) end
260 assert(false, 'unknown app "'..Current_app
..'"')
264 function App
.mousepressed(x
,y
, mouse_button
)
265 if Current_app
== 'error' then return end
266 --? print('mouse press', x,y)
267 if Current_app
== 'run' then
268 if run
.mouse_press
then run
.mouse_press(x
,y
, mouse_button
) end
269 elseif Current_app
== 'source' then
270 if source
.mouse_press
then source
.mouse_press(x
,y
, mouse_button
) end
272 assert(false, 'unknown app "'..Current_app
..'"')
276 function App
.mousereleased(x
,y
, mouse_button
)
277 if Current_app
== 'error' then return end
278 if Current_app
== 'run' then
279 if run
.mouse_release
then run
.mouse_release(x
,y
, mouse_button
) end
280 elseif Current_app
== 'source' then
281 if source
.mouse_release
then source
.mouse_release(x
,y
, mouse_button
) end
283 assert(false, 'unknown app "'..Current_app
..'"')
287 function App
.wheelmoved(dx
,dy
)
288 if Current_app
== 'error' then return end
289 if Current_app
== 'run' then
290 if run
.mouse_wheel_move
then run
.mouse_wheel_move(dx
,dy
) end
291 elseif Current_app
== 'source' then
292 if source
.mouse_wheel_move
then source
.mouse_wheel_move(dx
,dy
) end
294 assert(false, 'unknown app "'..Current_app
..'"')
299 if Current_app
== 'error' then return end
300 if Current_app
== 'run' then
301 local source_settings
= Settings
.source
302 Settings
= run
.settings()
303 Settings
.source
= source_settings
305 Settings
.source
= source
.settings()
307 Settings
.current_app
= Current_app
308 love
.filesystem
.write('config', json
.encode(Settings
))
309 if Current_app
== 'run' then
310 if run
.quit
then run
.quit() end
311 elseif Current_app
== 'source' then
312 if source
.quit
then source
.quit() end
314 assert(false, 'unknown app "'..Current_app
..'"')