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
.initialize_globals()
75 if Current_app
== 'run' then
76 run
.initialize_globals()
77 elseif Current_app
== 'source' then
78 source
.initialize_globals()
80 assert(false, 'unknown app "'..Current_app
..'"')
83 -- for hysteresis in a few places
84 Last_focus_time
= App
.getTime() -- https://love2d.org/forums/viewtopic.php?p=249700
85 Last_resize_time
= App
.getTime()
88 function App
.initialize(arg
)
89 if Current_app
== 'run' then
91 elseif Current_app
== 'source' then
92 source
.initialize(arg
)
94 assert(false, 'unknown app "'..Current_app
..'"')
98 function App
.resize(w
,h
)
99 if Current_app
== 'run' then
100 if run
.resize
then run
.resize(w
,h
) end
101 elseif Current_app
== 'source' then
102 if source
.resize
then source
.resize(w
,h
) end
104 assert(false, 'unknown app "'..Current_app
..'"')
106 Last_resize_time
= App
.getTime()
109 function App
.filedropped(file
)
110 if Current_app
== 'run' then
111 if run
.filedropped
then run
.filedropped(file
) end
112 elseif Current_app
== 'source' then
113 if source
.filedropped
then source
.filedropped(file
) end
115 assert(false, 'unknown app "'..Current_app
..'"')
119 function App
.focus(in_focus
)
121 Last_focus_time
= App
.getTime()
123 if Current_app
== 'run' then
124 if run
.focus
then run
.focus(in_focus
) end
125 elseif Current_app
== 'source' then
126 if source
.focus
then source
.focus(in_focus
) end
128 assert(false, 'unknown app "'..Current_app
..'"')
133 if Current_app
== 'run' then
135 elseif Current_app
== 'source' then
138 assert(false, 'unknown app "'..Current_app
..'"')
142 function App
.update(dt
)
143 -- some hysteresis while resizing
144 if App
.getTime() < Last_resize_time
+ 0.1 then
148 if Current_app
== 'run' then
150 elseif Current_app
== 'source' then
153 assert(false, 'unknown app "'..Current_app
..'"')
157 function App
.keychord_pressed(chord
, key
)
158 -- ignore events for some time after window in focus (mostly alt-tab)
159 if App
.getTime() < Last_focus_time
+ 0.01 then
163 if chord
== 'C-e' then
164 -- carefully save settings
165 if Current_app
== 'run' then
166 local source_settings
= Settings
.source
167 Settings
= run
.settings()
168 Settings
.source
= source_settings
169 if run
.quit
then run
.quit() end
170 Current_app
= 'source'
171 elseif Current_app
== 'source' then
172 Settings
.source
= source
.settings()
173 if source
.quit
then source
.quit() end
176 assert(false, 'unknown app "'..Current_app
..'"')
178 Settings
.current_app
= Current_app
179 love
.filesystem
.write('config', json
.encode(Settings
))
181 load_file_from_source_or_save_directory('main.lua')
182 App
.undo_initialize()
183 App
.run_tests_and_initialize()
186 if Current_app
== 'run' then
187 if run
.keychord_pressed
then run
.keychord_pressed(chord
, key
) end
188 elseif Current_app
== 'source' then
189 if source
.keychord_pressed
then source
.keychord_pressed(chord
, key
) end
191 assert(false, 'unknown app "'..Current_app
..'"')
195 function App
.textinput(t
)
196 -- ignore events for some time after window in focus (mostly alt-tab)
197 if App
.getTime() < Last_focus_time
+ 0.01 then
201 if Current_app
== 'run' then
202 if run
.textinput
then run
.textinput(t
) end
203 elseif Current_app
== 'source' then
204 if source
.textinput
then source
.textinput(t
) end
206 assert(false, 'unknown app "'..Current_app
..'"')
210 function App
.keyreleased(chord
, key
)
211 -- ignore events for some time after window in focus (mostly alt-tab)
212 if App
.getTime() < Last_focus_time
+ 0.01 then
216 if Current_app
== 'run' then
217 if run
.key_released
then run
.key_released(chord
, key
) end
218 elseif Current_app
== 'source' then
219 if source
.key_released
then source
.key_released(chord
, key
) end
221 assert(false, 'unknown app "'..Current_app
..'"')
225 function App
.mousepressed(x
,y
, mouse_button
)
226 --? print('mouse press', x,y)
227 if Current_app
== 'run' then
228 if run
.mouse_pressed
then run
.mouse_pressed(x
,y
, mouse_button
) end
229 elseif Current_app
== 'source' then
230 if source
.mouse_pressed
then source
.mouse_pressed(x
,y
, mouse_button
) end
232 assert(false, 'unknown app "'..Current_app
..'"')
236 function App
.mousereleased(x
,y
, mouse_button
)
237 if Current_app
== 'run' then
238 if run
.mouse_released
then run
.mouse_released(x
,y
, mouse_button
) end
239 elseif Current_app
== 'source' then
240 if source
.mouse_released
then source
.mouse_released(x
,y
, mouse_button
) end
242 assert(false, 'unknown app "'..Current_app
..'"')
247 if Current_app
== 'run' then
248 local source_settings
= Settings
.source
249 Settings
= run
.settings()
250 Settings
.source
= source_settings
252 Settings
.source
= source
.settings()
254 Settings
.current_app
= Current_app
255 love
.filesystem
.write('config', json
.encode(Settings
))
256 if Current_app
== 'run' then
257 if run
.quit
then run
.quit() end
258 elseif Current_app
== 'source' then
259 if source
.quit
then source
.quit() end
261 assert(false, 'unknown app "'..Current_app
..'"')