more realism in one more helper
[view.love.git] / main.lua
blobb40ddf2b6c53582e6cc70a1a66cdf2c0c29a0887
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 -- Current_app =
43 -- | run
44 -- | source
45 -- | {name=warning message='...' next_app = run|source}
47 if Current_app == nil then
48 Current_app = 'run'
49 end
51 if Current_app == 'run' then
52 load_file_from_source_or_save_directory('file.lua')
53 load_file_from_source_or_save_directory('run.lua')
54 load_file_from_source_or_save_directory('edit.lua')
55 load_file_from_source_or_save_directory('text.lua')
56 load_file_from_source_or_save_directory('search.lua')
57 load_file_from_source_or_save_directory('select.lua')
58 load_file_from_source_or_save_directory('undo.lua')
59 load_file_from_source_or_save_directory('text_tests.lua')
60 load_file_from_source_or_save_directory('run_tests.lua')
61 elseif Current_app == 'source' then
62 load_file_from_source_or_save_directory('source_file.lua')
63 load_file_from_source_or_save_directory('source.lua')
64 load_file_from_source_or_save_directory('commands.lua')
65 load_file_from_source_or_save_directory('source_edit.lua')
66 load_file_from_source_or_save_directory('log_browser.lua')
67 load_file_from_source_or_save_directory('source_text.lua')
68 load_file_from_source_or_save_directory('search.lua')
69 load_file_from_source_or_save_directory('source_select.lua')
70 load_file_from_source_or_save_directory('source_undo.lua')
71 load_file_from_source_or_save_directory('colorize.lua')
72 load_file_from_source_or_save_directory('source_text_tests.lua')
73 load_file_from_source_or_save_directory('source_tests.lua')
74 elseif current_app_is_warning() then
75 else
76 assert(false, 'unknown app "'..Current_app..'"')
77 end
78 end
80 function App.initialize_globals()
81 Supported_versions = {'11.5', '11.4', '12.0'} -- put the recommended version first
82 check_love_version_for_tests()
84 if Current_app == 'run' then
85 run.initialize_globals()
86 elseif Current_app == 'source' then
87 source.initialize_globals()
88 elseif current_app_is_warning() then
89 else
90 assert(false, 'unknown app "'..Current_app..'"')
91 end
93 -- for hysteresis in a few places
94 Current_time = 0
95 Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700
96 Last_resize_time = 0
98 -- Another weird bit for a class of corner cases. E.g.:
99 -- * I press ctrl+e, switch Current_app. I don't want the new app to receive
100 -- text_input and key_release events.
101 -- If I try to avoid text_input events by switching modes on key_release, I
102 -- hit a new problem:
103 -- * I press ctrl+e, am running an untested version, Current_app goes to
104 -- 'warning', and immediately rolls back out of 'warning' in the
105 -- key_release event.
106 -- Skip_rest_of_key_events is ugly, but feels cleaner than creating yet
107 -- another possible value for Current_app.
108 Skip_rest_of_key_events = nil
111 function check_love_version_for_tests()
112 if array.find(Supported_versions, Version) == nil then
113 -- warning to include in an error message if any tests failed
114 Warning_before_tests = ("This app hasn't been tested with LÖVE version %s."):format(Version)
118 function App.initialize(arg)
119 love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
120 love.keyboard.setKeyRepeat(true)
122 love.graphics.setBackgroundColor(1,1,1)
124 if Current_app == 'run' then
125 run.initialize(arg)
126 elseif Current_app == 'source' then
127 source.initialize(arg)
128 elseif current_app_is_warning() then
129 else
130 assert(false, 'unknown app "'..Current_app..'"')
133 check_love_version()
136 function check_love_version()
137 if array.find(Supported_versions, Version) == nil then
138 show_warning(
139 ("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]))
140 -- continue initializing everything; hopefully we won't have errors during initialization
144 function App.resize(w,h)
145 if current_app_is_warning() then return end
146 if Current_app == 'run' then
147 if run.resize then run.resize(w,h) end
148 elseif Current_app == 'source' then
149 if source.resize then source.resize(w,h) end
150 else
151 assert(false, 'unknown app "'..Current_app..'"')
153 Last_resize_time = Current_time
156 function App.filedropped(file)
157 if current_app_is_warning() then return end
158 if Current_app == 'run' then
159 if run.file_drop then run.file_drop(file) end
160 elseif Current_app == 'source' then
161 if source.file_drop then source.file_drop(file) end
162 else
163 assert(false, 'unknown app "'..Current_app..'"')
167 function App.focus(in_focus)
168 if current_app_is_warning() then return end
169 if in_focus then
170 Last_focus_time = Current_time
172 if Current_app == 'run' then
173 if run.focus then run.focus(in_focus) end
174 elseif Current_app == 'source' then
175 if source.focus then source.focus(in_focus) end
176 else
177 assert(false, 'unknown app "'..Current_app..'"')
181 function App.draw()
182 if Current_app == 'run' then
183 run.draw()
184 elseif Current_app == 'source' then
185 source.draw()
186 elseif current_app_is_warning() then
187 love.graphics.setColor(0,0,1)
188 love.graphics.rectangle('fill', 0,0, App.screen.width, App.screen.height)
189 love.graphics.setColor(1,1,1)
190 love.graphics.printf(Current_app.message, 40,40, 600)
191 else
192 assert(false, 'unknown app "'..Current_app..'"')
196 function App.update(dt)
197 Current_time = Current_time + dt
198 if current_app_is_warning() then return end
199 -- some hysteresis while resizing
200 if Current_time < Last_resize_time + 0.1 then
201 return
204 if Current_app == 'run' then
205 run.update(dt)
206 elseif Current_app == 'source' then
207 source.update(dt)
208 else
209 assert(false, 'unknown app "'..Current_app..'"')
213 function App.keychord_press(chord, key)
214 -- ignore events for some time after window in focus (mostly alt-tab)
215 if Current_time < Last_focus_time + 0.01 then
216 return
219 Skip_rest_of_key_events = nil
220 if current_app_is_warning() then
221 if chord == 'C-c' then
222 love.system.setClipboardText(warning_message())
223 else
224 clear_warning()
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_is_warning() 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_is_warning() 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_is_warning() 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_is_warning() 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_is_warning() 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_is_warning() 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 Disable_all_quit_handlers then return end
335 if current_app_is_warning() then return end
336 if Current_app == 'run' then
337 local source_settings = Settings.source
338 Settings = run.settings()
339 Settings.source = source_settings
340 else
341 Settings.source = source.settings()
343 Settings.current_app = Current_app
344 love.filesystem.write('config', json.encode(Settings))
345 if Current_app == 'run' then
346 if run.quit then run.quit() end
347 elseif Current_app == 'source' then
348 if source.quit then source.quit() end
349 else
350 assert(false, 'unknown app "'..Current_app..'"')
354 function current_app_is_warning()
355 return type(Current_app) == 'table' and Current_app.name == 'warning'
358 function show_warning(message)
359 assert(type(Current_app) == 'string')
360 Current_app = {
361 name = 'warning',
362 message = message,
363 next_app = Current_app,
367 function clear_warning()
368 assert(type(Current_app) == 'table')
369 Current_app = Current_app.next_app
372 function warning_message()
373 assert(type(Current_app) == 'table')
374 return Current_app.message