redo version checks yet again
[view.love.git] / main.lua
blobdbba863350de452a52943defc1d975cefbfaefd5
1 -- Entrypoint for the app. You can edit this file from within the app if
2 -- you're careful.
4 -- files that come with LÖVE; we can't edit those from within the app
5 utf8 = require 'utf8'
7 function load_file_from_source_or_save_directory(filename)
8 local contents = love.filesystem.read(filename)
9 local code, err = loadstring(contents, filename)
10 if code == nil then
11 error(err)
12 end
13 return code()
14 end
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
35 function App.load()
36 log_new('session')
37 if love.filesystem.getInfo('config') then
38 Settings = json.decode(love.filesystem.read('config'))
39 Current_app = Settings.current_app
40 end
42 if Current_app == nil then
43 Current_app = 'run'
44 end
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
70 else
71 assert(false, 'unknown app "'..Current_app..'"')
72 end
73 end
75 function App.initialize_globals()
76 Supported_versions = {'11.5', '11.4', '12.0'} -- put the recommended version first
77 check_love_version_for_tests()
79 if Current_app == 'run' then
80 run.initialize_globals()
81 elseif Current_app == 'source' then
82 source.initialize_globals()
83 elseif Current_app == 'error' then
84 else
85 assert(false, 'unknown app "'..Current_app..'"')
86 end
88 -- for hysteresis in a few places
89 Current_time = 0
90 Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700
91 Last_resize_time = 0
93 -- Another weird bit for a class of corner cases. E.g.:
94 -- * I press ctrl+e, switch Current_app. I don't want the new app to receive
95 -- text_input and key_release events.
96 -- If I try to avoid text_input events by switching modes on key_release, I
97 -- hit a new problem:
98 -- * I press ctrl+e, am running an untested version, Current_app goes to
99 -- 'error', and immediately rolls back out of 'error' in the key_release
100 -- event.
101 -- Skip_rest_of_key_events is ugly, but feels cleaner than creating yet
102 -- another possible value for Current_app.
103 Skip_rest_of_key_events = nil
105 -- Where to go from 'error' app.
106 Next_app = 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
123 run.initialize(arg)
124 elseif Current_app == 'source' then
125 source.initialize(arg)
126 elseif Current_app == 'error' then
127 else
128 assert(false, 'unknown app "'..Current_app..'"')
131 check_love_version()
134 function check_love_version()
135 if array.find(Supported_versions, Version) == nil then
136 Next_app = Current_app
137 Current_app = 'error'
138 Error_message = ("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])
139 -- continue initializing everything; hopefully we won't have errors during initialization
143 function App.resize(w,h)
144 if Current_app == 'error' then return end
145 if Current_app == 'run' then
146 if run.resize then run.resize(w,h) end
147 elseif Current_app == 'source' then
148 if source.resize then source.resize(w,h) end
149 else
150 assert(false, 'unknown app "'..Current_app..'"')
152 Last_resize_time = Current_time
155 function App.filedropped(file)
156 if Current_app == 'error' then return end
157 if Current_app == 'run' then
158 if run.file_drop then run.file_drop(file) end
159 elseif Current_app == 'source' then
160 if source.file_drop then source.file_drop(file) end
161 else
162 assert(false, 'unknown app "'..Current_app..'"')
166 function App.focus(in_focus)
167 if Current_app == 'error' then return end
168 if in_focus then
169 Last_focus_time = Current_time
171 if Current_app == 'run' then
172 if run.focus then run.focus(in_focus) end
173 elseif Current_app == 'source' then
174 if source.focus then source.focus(in_focus) end
175 else
176 assert(false, 'unknown app "'..Current_app..'"')
180 function App.draw()
181 if Current_app == 'run' then
182 run.draw()
183 elseif Current_app == 'source' then
184 source.draw()
185 elseif Current_app == 'error' then
186 love.graphics.setColor(0,0,1)
187 love.graphics.rectangle('fill', 0,0, App.screen.width, App.screen.height)
188 love.graphics.setColor(1,1,1)
189 love.graphics.printf(Error_message, 40,40, 600)
190 else
191 assert(false, 'unknown app "'..Current_app..'"')
195 function App.update(dt)
196 Current_time = Current_time + dt
197 if Current_app == 'error' then return end
198 -- some hysteresis while resizing
199 if Current_time < Last_resize_time + 0.1 then
200 return
203 if Current_app == 'run' then
204 run.update(dt)
205 elseif Current_app == 'source' then
206 source.update(dt)
207 else
208 assert(false, 'unknown app "'..Current_app..'"')
212 function App.keychord_press(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
215 return
218 Skip_rest_of_key_events = nil
219 if Current_app == 'error' then
220 if chord == 'C-c' then
221 love.system.setClipboardText(Error_message)
222 else
223 Current_app = Next_app
224 Next_app = nil
225 Skip_rest_of_key_events = true
227 return
229 if chord == 'C-e' then
230 -- carefully save settings
231 if Current_app == 'run' then
232 local source_settings = Settings.source
233 Settings = run.settings()
234 Settings.source = source_settings
235 if run.quit then run.quit() end
236 Current_app = 'source'
237 -- preserve any Error_message when going from run to source
238 elseif Current_app == 'source' then
239 Settings.source = source.settings()
240 if source.quit then source.quit() end
241 Current_app = 'run'
242 Error_message = nil
243 elseif Current_app == 'error' then
244 else
245 assert(false, 'unknown app "'..Current_app..'"')
247 Settings.current_app = Current_app
248 love.filesystem.write('config', json.encode(Settings))
249 -- reboot
250 load_file_from_source_or_save_directory('main.lua')
251 App.undo_initialize()
252 App.run_tests_and_initialize()
253 Skip_rest_of_key_events = true
254 return
256 if Current_app == 'run' then
257 if run.keychord_press then run.keychord_press(chord, key) end
258 elseif Current_app == 'source' then
259 if source.keychord_press then source.keychord_press(chord, key) end
260 else
261 assert(false, 'unknown app "'..Current_app..'"')
265 function App.textinput(t)
266 if Current_app == 'error' then return end
267 -- ignore events for some time after window in focus (mostly alt-tab)
268 if Current_time < Last_focus_time + 0.01 then
269 return
272 if Skip_rest_of_key_events then return end
273 if Current_app == 'run' then
274 if run.text_input then run.text_input(t) end
275 elseif Current_app == 'source' then
276 if source.text_input then source.text_input(t) end
277 else
278 assert(false, 'unknown app "'..Current_app..'"')
282 function App.keyreleased(key, scancode)
283 if Current_app == 'error' then return end
284 -- ignore events for some time after window in focus (mostly alt-tab)
285 if Current_time < Last_focus_time + 0.01 then
286 return
289 if Skip_rest_of_key_events then return end
290 if Current_app == 'run' then
291 if run.key_release then run.key_release(key, scancode) end
292 elseif Current_app == 'source' then
293 if source.key_release then source.key_release(key, scancode) end
294 else
295 assert(false, 'unknown app "'..Current_app..'"')
299 function App.mousepressed(x,y, mouse_button)
300 if Current_app == 'error' then return end
301 --? print('mouse press', x,y)
302 if Current_app == 'run' then
303 if run.mouse_press then run.mouse_press(x,y, mouse_button) end
304 elseif Current_app == 'source' then
305 if source.mouse_press then source.mouse_press(x,y, mouse_button) end
306 else
307 assert(false, 'unknown app "'..Current_app..'"')
311 function App.mousereleased(x,y, mouse_button)
312 if Current_app == 'error' then return end
313 if Current_app == 'run' then
314 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
315 elseif Current_app == 'source' then
316 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
317 else
318 assert(false, 'unknown app "'..Current_app..'"')
322 function App.wheelmoved(dx,dy)
323 if Current_app == 'error' then return end
324 if Current_app == 'run' then
325 if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end
326 elseif Current_app == 'source' then
327 if source.mouse_wheel_move then source.mouse_wheel_move(dx,dy) end
328 else
329 assert(false, 'unknown app "'..Current_app..'"')
333 function love.quit()
334 if Current_app == 'error' then return end
335 if Current_app == 'run' then
336 local source_settings = Settings.source
337 Settings = run.settings()
338 Settings.source = source_settings
339 else
340 Settings.source = source.settings()
342 Settings.current_app = Current_app
343 love.filesystem.write('config', json.encode(Settings))
344 if Current_app == 'run' then
345 if run.quit then run.quit() end
346 elseif Current_app == 'source' then
347 if source.quit then source.quit() end
348 else
349 assert(false, 'unknown app "'..Current_app..'"')