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')
69 elseif Current_app
== 'error' then
71 assert(false, 'unknown app "'..Current_app
..'"')
75 function App
.version_check()
76 -- available modes: run, error
79 -- we'll reuse error mode on load for an initial version check
80 local supported_versions
= {'11.5', '11.4', '12.0'} -- put the recommended version first
82 Major_version
, minor_version
= love
.getVersion()
83 Version
= Major_version
..'.'..minor_version
84 if array
.find(supported_versions
, Version
) == nil then
86 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])
88 -- continue initializing everything; hopefully we won't have errors during initialization
92 function App
.initialize_globals()
93 if Current_app
== 'run' then
94 run
.initialize_globals()
95 elseif Current_app
== 'source' then
96 source
.initialize_globals()
97 elseif Current_app
== 'error' then
99 assert(false, 'unknown app "'..Current_app
..'"')
102 -- for hysteresis in a few places
104 Last_focus_time
= 0 -- https://love2d.org/forums/viewtopic.php?p=249700
108 function App
.initialize(arg
)
109 love
.keyboard
.setTextInput(true) -- bring up keyboard on touch screen
110 love
.keyboard
.setKeyRepeat(true)
112 love
.graphics
.setBackgroundColor(1,1,1)
114 if Current_app
== 'run' then
116 elseif Current_app
== 'source' then
117 source
.initialize(arg
)
118 elseif Current_app
== 'error' then
120 assert(false, 'unknown app "'..Current_app
..'"')
124 function App
.resize(w
,h
)
125 if Current_app
== 'run' then
126 if run
.resize
then run
.resize(w
,h
) end
127 elseif Current_app
== 'source' then
128 if source
.resize
then source
.resize(w
,h
) end
129 elseif Current_app
== 'error' then
131 assert(false, 'unknown app "'..Current_app
..'"')
133 Last_resize_time
= Current_time
136 function App
.filedropped(file
)
137 if Current_app
== 'run' then
138 if run
.file_drop
then run
.file_drop(file
) end
139 elseif Current_app
== 'source' then
140 if source
.file_drop
then source
.file_drop(file
) end
141 elseif Current_app
== 'error' then
143 assert(false, 'unknown app "'..Current_app
..'"')
147 function App
.focus(in_focus
)
149 Last_focus_time
= Current_time
151 if Current_app
== 'run' then
152 if run
.focus
then run
.focus(in_focus
) end
153 elseif Current_app
== 'source' then
154 if source
.focus
then source
.focus(in_focus
) end
155 elseif Current_app
== 'error' then
157 assert(false, 'unknown app "'..Current_app
..'"')
162 if Current_app
== 'run' then
164 elseif Current_app
== 'source' then
166 elseif Current_app
== 'error' then
167 love
.graphics
.setColor(0,0,1)
168 love
.graphics
.rectangle('fill', 0,0, App
.screen
.width
, App
.screen
.height
)
169 love
.graphics
.setColor(1,1,1)
170 love
.graphics
.printf(Error_message
, 40,40, 600)
172 assert(false, 'unknown app "'..Current_app
..'"')
176 function App
.update(dt
)
177 Current_time
= Current_time
+ dt
178 -- some hysteresis while resizing
179 if Current_time
< Last_resize_time
+ 0.1 then
183 if Current_app
== 'run' then
185 elseif Current_app
== 'source' then
187 elseif Current_app
== 'error' then
189 assert(false, 'unknown app "'..Current_app
..'"')
193 function App
.keychord_press(chord
, key
)
194 -- ignore events for some time after window in focus (mostly alt-tab)
195 if Current_time
< Last_focus_time
+ 0.01 then
199 if Current_app
== 'error' then
200 if chord
== 'C-c' then
201 love
.system
.setClipboardText(Error_message
)
205 if chord
== 'C-e' then
206 -- carefully save settings
207 if Current_app
== 'run' then
208 local source_settings
= Settings
.source
209 Settings
= run
.settings()
210 Settings
.source
= source_settings
211 if run
.quit
then run
.quit() end
212 Current_app
= 'source'
213 -- preserve any Error_message when going from run to source
214 elseif Current_app
== 'source' then
215 Settings
.source
= source
.settings()
216 if source
.quit
then source
.quit() end
219 elseif Current_app
== 'error' then
221 assert(false, 'unknown app "'..Current_app
..'"')
223 Settings
.current_app
= Current_app
224 love
.filesystem
.write('config', json
.encode(Settings
))
226 load_file_from_source_or_save_directory('main.lua')
227 App
.undo_initialize()
228 App
.run_tests_and_initialize()
231 if Current_app
== 'run' then
232 if run
.keychord_press
then run
.keychord_press(chord
, key
) end
233 elseif Current_app
== 'source' then
234 if source
.keychord_press
then source
.keychord_press(chord
, key
) end
236 assert(false, 'unknown app "'..Current_app
..'"')
240 function App
.textinput(t
)
241 if Current_app
== 'error' then return end
242 -- ignore events for some time after window in focus (mostly alt-tab)
243 if Current_time
< Last_focus_time
+ 0.01 then
247 if Current_app
== 'run' then
248 if run
.text_input
then run
.text_input(t
) end
249 elseif Current_app
== 'source' then
250 if source
.text_input
then source
.text_input(t
) end
252 assert(false, 'unknown app "'..Current_app
..'"')
256 function App
.keyreleased(key
, scancode
)
257 -- ignore events for some time after window in focus (mostly alt-tab)
258 if Current_time
< Last_focus_time
+ 0.01 then
262 if Current_app
== 'error' then
264 elseif Current_app
== 'run' then
265 if run
.key_release
then run
.key_release(key
, scancode
) end
266 elseif Current_app
== 'source' then
267 if source
.key_release
then source
.key_release(key
, scancode
) end
269 assert(false, 'unknown app "'..Current_app
..'"')
273 function App
.mousepressed(x
,y
, mouse_button
)
274 if Current_app
== 'error' then return end
275 --? print('mouse press', x,y)
276 if Current_app
== 'run' then
277 if run
.mouse_press
then run
.mouse_press(x
,y
, mouse_button
) end
278 elseif Current_app
== 'source' then
279 if source
.mouse_press
then source
.mouse_press(x
,y
, mouse_button
) end
281 assert(false, 'unknown app "'..Current_app
..'"')
285 function App
.mousereleased(x
,y
, mouse_button
)
286 if Current_app
== 'error' then return end
287 if Current_app
== 'run' then
288 if run
.mouse_release
then run
.mouse_release(x
,y
, mouse_button
) end
289 elseif Current_app
== 'source' then
290 if source
.mouse_release
then source
.mouse_release(x
,y
, mouse_button
) end
292 assert(false, 'unknown app "'..Current_app
..'"')
296 function App
.wheelmoved(dx
,dy
)
297 if Current_app
== 'error' then return end
298 if Current_app
== 'run' then
299 if run
.mouse_wheel_move
then run
.mouse_wheel_move(dx
,dy
) end
300 elseif Current_app
== 'source' then
301 if source
.mouse_wheel_move
then source
.mouse_wheel_move(dx
,dy
) end
303 assert(false, 'unknown app "'..Current_app
..'"')
308 if Current_app
== 'error' then return end
309 if Current_app
== 'run' then
310 local source_settings
= Settings
.source
311 Settings
= run
.settings()
312 Settings
.source
= source_settings
314 Settings
.source
= source
.settings()
316 Settings
.current_app
= Current_app
317 love
.filesystem
.write('config', json
.encode(Settings
))
318 if Current_app
== 'run' then
319 if run
.quit
then run
.quit() end
320 elseif Current_app
== 'source' then
321 if source
.quit
then source
.quit() end
323 assert(false, 'unknown app "'..Current_app
..'"')