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
85 Last_focus_time
= 0 -- https://love2d.org/forums/viewtopic.php?p=249700
89 function App
.initialize(arg
)
90 if Current_app
== 'run' then
92 elseif Current_app
== 'source' then
93 source
.initialize(arg
)
95 assert(false, 'unknown app "'..Current_app
..'"')
99 function App
.resize(w
,h
)
100 if Current_app
== 'run' then
101 if run
.resize
then run
.resize(w
,h
) end
102 elseif Current_app
== 'source' then
103 if source
.resize
then source
.resize(w
,h
) end
105 assert(false, 'unknown app "'..Current_app
..'"')
107 Last_resize_time
= Current_time
110 function App
.filedropped(file
)
111 if Current_app
== 'run' then
112 if run
.file_drop
then run
.file_drop(file
) end
113 elseif Current_app
== 'source' then
114 if source
.file_drop
then source
.file_drop(file
) end
116 assert(false, 'unknown app "'..Current_app
..'"')
120 function App
.focus(in_focus
)
122 Last_focus_time
= Current_time
124 if Current_app
== 'run' then
125 if run
.focus
then run
.focus(in_focus
) end
126 elseif Current_app
== 'source' then
127 if source
.focus
then source
.focus(in_focus
) end
129 assert(false, 'unknown app "'..Current_app
..'"')
134 if Current_app
== 'run' then
136 elseif Current_app
== 'source' then
139 assert(false, 'unknown app "'..Current_app
..'"')
143 function App
.update(dt
)
144 Current_time
= Current_time
+ dt
145 -- some hysteresis while resizing
146 if Current_time
< Last_resize_time
+ 0.1 then
150 if Current_app
== 'run' then
152 elseif Current_app
== 'source' then
155 assert(false, 'unknown app "'..Current_app
..'"')
159 function App
.keychord_press(chord
, key
)
160 -- ignore events for some time after window in focus (mostly alt-tab)
161 if Current_time
< Last_focus_time
+ 0.01 then
165 if chord
== 'C-e' then
166 -- carefully save settings
167 if Current_app
== 'run' then
168 local source_settings
= Settings
.source
169 Settings
= run
.settings()
170 Settings
.source
= source_settings
171 if run
.quit
then run
.quit() end
172 Current_app
= 'source'
173 elseif Current_app
== 'source' then
174 Settings
.source
= source
.settings()
175 if source
.quit
then source
.quit() end
178 assert(false, 'unknown app "'..Current_app
..'"')
180 Settings
.current_app
= Current_app
181 love
.filesystem
.write('config', json
.encode(Settings
))
183 load_file_from_source_or_save_directory('main.lua')
184 App
.undo_initialize()
185 App
.run_tests_and_initialize()
188 if Current_app
== 'run' then
189 if run
.keychord_press
then run
.keychord_press(chord
, key
) end
190 elseif Current_app
== 'source' then
191 if source
.keychord_press
then source
.keychord_press(chord
, key
) end
193 assert(false, 'unknown app "'..Current_app
..'"')
197 function App
.textinput(t
)
198 -- ignore events for some time after window in focus (mostly alt-tab)
199 if Current_time
< Last_focus_time
+ 0.01 then
203 if Current_app
== 'run' then
204 if run
.text_input
then run
.text_input(t
) end
205 elseif Current_app
== 'source' then
206 if source
.text_input
then source
.text_input(t
) end
208 assert(false, 'unknown app "'..Current_app
..'"')
212 function App
.keyreleased(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 if Current_app
== 'run' then
219 if run
.key_release
then run
.key_release(chord
, key
) end
220 elseif Current_app
== 'source' then
221 if source
.key_release
then source
.key_release(chord
, key
) end
223 assert(false, 'unknown app "'..Current_app
..'"')
227 function App
.mousepressed(x
,y
, mouse_button
)
228 --? print('mouse press', x,y)
229 if Current_app
== 'run' then
230 if run
.mouse_pressed
then run
.mouse_press(x
,y
, mouse_button
) end
231 elseif Current_app
== 'source' then
232 if source
.mouse_pressed
then source
.mouse_press(x
,y
, mouse_button
) end
234 assert(false, 'unknown app "'..Current_app
..'"')
238 function App
.mousereleased(x
,y
, mouse_button
)
239 if Current_app
== 'run' then
240 if run
.mouse_release
then run
.mouse_release(x
,y
, mouse_button
) end
241 elseif Current_app
== 'source' then
242 if source
.mouse_release
then source
.mouse_release(x
,y
, mouse_button
) end
244 assert(false, 'unknown app "'..Current_app
..'"')
249 if Current_app
== 'run' then
250 local source_settings
= Settings
.source
251 Settings
= run
.settings()
252 Settings
.source
= source_settings
254 Settings
.source
= source
.settings()
256 Settings
.current_app
= Current_app
257 love
.filesystem
.write('config', json
.encode(Settings
))
258 if Current_app
== 'run' then
259 if run
.quit
then run
.quit() end
260 elseif Current_app
== 'source' then
261 if source
.quit
then source
.quit() end
263 assert(false, 'unknown app "'..Current_app
..'"')