redo version checks
[view.love.git] / main.lua
blobaf53451321ba787ccd2cefae4408584281ced2ff
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
92 end
94 function check_love_version_for_tests()
95 if array.find(Supported_versions, Version) == nil then
96 Unsupported_version = true
97 -- warning to include in an error message if any tests failed
98 Warning_before_tests = ("This app hasn't been tested with LÖVE version %s."):format(Version)
99 end
102 function App.love_version_check()
103 if Unsupported_version then
104 Current_app = 'error'
105 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])
106 print(Error_message)
107 -- continue initializing everything; hopefully we won't have errors during initialization
111 function App.initialize(arg)
112 love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
113 love.keyboard.setKeyRepeat(true)
115 love.graphics.setBackgroundColor(1,1,1)
117 if Current_app == 'run' then
118 run.initialize(arg)
119 elseif Current_app == 'source' then
120 source.initialize(arg)
121 elseif Current_app == 'error' then
122 else
123 assert(false, 'unknown app "'..Current_app..'"')
127 function App.resize(w,h)
128 if Current_app == 'run' then
129 if run.resize then run.resize(w,h) end
130 elseif Current_app == 'source' then
131 if source.resize then source.resize(w,h) end
132 elseif Current_app == 'error' then
133 else
134 assert(false, 'unknown app "'..Current_app..'"')
136 Last_resize_time = Current_time
139 function App.filedropped(file)
140 if Current_app == 'run' then
141 if run.file_drop then run.file_drop(file) end
142 elseif Current_app == 'source' then
143 if source.file_drop then source.file_drop(file) end
144 elseif Current_app == 'error' then
145 else
146 assert(false, 'unknown app "'..Current_app..'"')
150 function App.focus(in_focus)
151 if in_focus then
152 Last_focus_time = Current_time
154 if Current_app == 'run' then
155 if run.focus then run.focus(in_focus) end
156 elseif Current_app == 'source' then
157 if source.focus then source.focus(in_focus) end
158 elseif Current_app == 'error' then
159 else
160 assert(false, 'unknown app "'..Current_app..'"')
164 function App.draw()
165 if Current_app == 'run' then
166 run.draw()
167 elseif Current_app == 'source' then
168 source.draw()
169 elseif Current_app == 'error' then
170 love.graphics.setColor(0,0,1)
171 love.graphics.rectangle('fill', 0,0, App.screen.width, App.screen.height)
172 love.graphics.setColor(1,1,1)
173 love.graphics.printf(Error_message, 40,40, 600)
174 else
175 assert(false, 'unknown app "'..Current_app..'"')
179 function App.update(dt)
180 Current_time = Current_time + dt
181 -- some hysteresis while resizing
182 if Current_time < Last_resize_time + 0.1 then
183 return
186 if Current_app == 'run' then
187 run.update(dt)
188 elseif Current_app == 'source' then
189 source.update(dt)
190 elseif Current_app == 'error' then
191 else
192 assert(false, 'unknown app "'..Current_app..'"')
196 function App.keychord_press(chord, key)
197 -- ignore events for some time after window in focus (mostly alt-tab)
198 if Current_time < Last_focus_time + 0.01 then
199 return
202 if Current_app == 'error' then
203 if chord == 'C-c' then
204 love.system.setClipboardText(Error_message)
206 return
208 if chord == 'C-e' then
209 -- carefully save settings
210 if Current_app == 'run' then
211 local source_settings = Settings.source
212 Settings = run.settings()
213 Settings.source = source_settings
214 if run.quit then run.quit() end
215 Current_app = 'source'
216 -- preserve any Error_message when going from run to source
217 elseif Current_app == 'source' then
218 Settings.source = source.settings()
219 if source.quit then source.quit() end
220 Current_app = 'run'
221 Error_message = nil
222 elseif Current_app == 'error' then
223 else
224 assert(false, 'unknown app "'..Current_app..'"')
226 Settings.current_app = Current_app
227 love.filesystem.write('config', json.encode(Settings))
228 -- reboot
229 load_file_from_source_or_save_directory('main.lua')
230 App.undo_initialize()
231 App.run_tests_and_initialize()
232 return
234 if Current_app == 'run' then
235 if run.keychord_press then run.keychord_press(chord, key) end
236 elseif Current_app == 'source' then
237 if source.keychord_press then source.keychord_press(chord, key) end
238 else
239 assert(false, 'unknown app "'..Current_app..'"')
243 function App.textinput(t)
244 if Current_app == 'error' then return end
245 -- ignore events for some time after window in focus (mostly alt-tab)
246 if Current_time < Last_focus_time + 0.01 then
247 return
250 if Current_app == 'run' then
251 if run.text_input then run.text_input(t) end
252 elseif Current_app == 'source' then
253 if source.text_input then source.text_input(t) end
254 else
255 assert(false, 'unknown app "'..Current_app..'"')
259 function App.keyreleased(key, scancode)
260 -- ignore events for some time after window in focus (mostly alt-tab)
261 if Current_time < Last_focus_time + 0.01 then
262 return
265 if Current_app == 'error' then
266 Current_app = 'run'
267 elseif Current_app == 'run' then
268 if run.key_release then run.key_release(key, scancode) end
269 elseif Current_app == 'source' then
270 if source.key_release then source.key_release(key, scancode) end
271 else
272 assert(false, 'unknown app "'..Current_app..'"')
276 function App.mousepressed(x,y, mouse_button)
277 if Current_app == 'error' then return end
278 --? print('mouse press', x,y)
279 if Current_app == 'run' then
280 if run.mouse_press then run.mouse_press(x,y, mouse_button) end
281 elseif Current_app == 'source' then
282 if source.mouse_press then source.mouse_press(x,y, mouse_button) end
283 else
284 assert(false, 'unknown app "'..Current_app..'"')
288 function App.mousereleased(x,y, mouse_button)
289 if Current_app == 'error' then return end
290 if Current_app == 'run' then
291 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
292 elseif Current_app == 'source' then
293 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
294 else
295 assert(false, 'unknown app "'..Current_app..'"')
299 function App.wheelmoved(dx,dy)
300 if Current_app == 'error' then return end
301 if Current_app == 'run' then
302 if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end
303 elseif Current_app == 'source' then
304 if source.mouse_wheel_move then source.mouse_wheel_move(dx,dy) end
305 else
306 assert(false, 'unknown app "'..Current_app..'"')
310 function love.quit()
311 if Current_app == 'error' then return end
312 if Current_app == 'run' then
313 local source_settings = Settings.source
314 Settings = run.settings()
315 Settings.source = source_settings
316 else
317 Settings.source = source.settings()
319 Settings.current_app = Current_app
320 love.filesystem.write('config', json.encode(Settings))
321 if Current_app == 'run' then
322 if run.quit then run.quit() end
323 elseif Current_app == 'source' then
324 if source.quit then source.quit() end
325 else
326 assert(false, 'unknown app "'..Current_app..'"')