yet another bugfix to the version check
[view.love.git] / main.lua
blob034953eaf61b009c55544cea5daf53b89284a5db
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.version_check()
76 -- available modes: run, error
77 Error_message = nil
78 Error_count = 0
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
81 local minor_version
82 Major_version, minor_version = love.getVersion()
83 Version = Major_version..'.'..minor_version
84 if array.find(supported_versions, Version) == nil then
85 Current_app = 'error'
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])
87 print(Error_message)
88 -- continue initializing everything; hopefully we won't have errors during initialization
89 end
90 end
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
98 else
99 assert(false, 'unknown app "'..Current_app..'"')
102 -- for hysteresis in a few places
103 Current_time = 0
104 Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700
105 Last_resize_time = 0
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
115 run.initialize(arg)
116 elseif Current_app == 'source' then
117 source.initialize(arg)
118 elseif Current_app == 'error' then
119 else
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
130 else
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
142 else
143 assert(false, 'unknown app "'..Current_app..'"')
147 function App.focus(in_focus)
148 if in_focus then
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
156 else
157 assert(false, 'unknown app "'..Current_app..'"')
161 function App.draw()
162 if Current_app == 'run' then
163 run.draw()
164 elseif Current_app == 'source' then
165 source.draw()
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)
171 else
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
180 return
183 if Current_app == 'run' then
184 run.update(dt)
185 elseif Current_app == 'source' then
186 source.update(dt)
187 elseif Current_app == 'error' then
188 else
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
196 return
199 if Current_app == 'error' then
200 if chord == 'C-c' then
201 love.system.setClipboardText(Error_message)
203 return
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
217 Current_app = 'run'
218 Error_message = nil
219 elseif Current_app == 'error' then
220 else
221 assert(false, 'unknown app "'..Current_app..'"')
223 Settings.current_app = Current_app
224 love.filesystem.write('config', json.encode(Settings))
225 -- reboot
226 load_file_from_source_or_save_directory('main.lua')
227 App.undo_initialize()
228 App.run_tests_and_initialize()
229 return
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
235 else
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
244 return
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
251 else
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
259 return
262 if Current_app == 'error' then
263 Current_app = 'run'
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
268 else
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
280 else
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
291 else
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
302 else
303 assert(false, 'unknown app "'..Current_app..'"')
307 function love.quit()
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
313 else
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
322 else
323 assert(false, 'unknown app "'..Current_app..'"')