bugfix in source editor
[view.love.git] / main.lua
blob4cca3b464793d2687db8ac6adada3358a69fdb6e
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.setKeyRepeat(true)
121 love.graphics.setBackgroundColor(1,1,1)
123 if Current_app == 'run' then
124 run.initialize(arg)
125 elseif Current_app == 'source' then
126 source.initialize(arg)
127 elseif current_app_is_warning() then
128 else
129 assert(false, 'unknown app "'..Current_app..'"')
132 check_love_version()
135 function check_love_version()
136 if array.find(Supported_versions, Version) == nil then
137 show_warning(
138 ("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_is_warning() 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_is_warning() 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_is_warning() 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_is_warning() 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(Current_app.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_is_warning() 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_is_warning() then
220 if chord == 'C-c' then
221 love.system.setClipboardText(warning_message())
222 else
223 clear_warning()
224 Skip_rest_of_key_events = true
226 return
228 if chord == 'C-e' then
229 -- carefully save settings
230 if Current_app == 'run' then
231 local source_settings = Settings.source
232 Settings = run.settings()
233 Settings.source = source_settings
234 if run.quit then run.quit() end
235 Current_app = 'source'
236 -- preserve any Error_message when going from run to source
237 elseif Current_app == 'source' then
238 Settings.source = source.settings()
239 if source.quit then source.quit() end
240 Current_app = 'run'
241 Error_message = nil
242 elseif current_app_is_warning() then
243 else
244 assert(false, 'unknown app "'..Current_app..'"')
246 Settings.current_app = Current_app
247 love.filesystem.write('config', json.encode(Settings))
248 -- reboot
249 load_file_from_source_or_save_directory('main.lua')
250 App.undo_initialize()
251 App.run_tests_and_initialize()
252 Skip_rest_of_key_events = true
253 return
255 if Current_app == 'run' then
256 if run.keychord_press then run.keychord_press(chord, key) end
257 elseif Current_app == 'source' then
258 if source.keychord_press then source.keychord_press(chord, key) end
259 else
260 assert(false, 'unknown app "'..Current_app..'"')
264 function App.textinput(t)
265 if current_app_is_warning() then return end
266 -- ignore events for some time after window in focus (mostly alt-tab)
267 if Current_time < Last_focus_time + 0.01 then
268 return
271 if Skip_rest_of_key_events then return end
272 if Current_app == 'run' then
273 if run.text_input then run.text_input(t) end
274 elseif Current_app == 'source' then
275 if source.text_input then source.text_input(t) end
276 else
277 assert(false, 'unknown app "'..Current_app..'"')
281 function App.keyreleased(key, scancode)
282 if current_app_is_warning() then return end
283 -- ignore events for some time after window in focus (mostly alt-tab)
284 if Current_time < Last_focus_time + 0.01 then
285 return
288 if Skip_rest_of_key_events then return end
289 if Current_app == 'run' then
290 if run.key_release then run.key_release(key, scancode) end
291 elseif Current_app == 'source' then
292 if source.key_release then source.key_release(key, scancode) end
293 else
294 assert(false, 'unknown app "'..Current_app..'"')
298 function App.mousepressed(x,y, mouse_button)
299 if current_app_is_warning() then return end
300 --? print('mouse press', x,y)
301 if Current_app == 'run' then
302 if run.mouse_press then run.mouse_press(x,y, mouse_button) end
303 elseif Current_app == 'source' then
304 if source.mouse_press then source.mouse_press(x,y, mouse_button) end
305 else
306 assert(false, 'unknown app "'..Current_app..'"')
310 function App.mousereleased(x,y, mouse_button)
311 if current_app_is_warning() then return end
312 if Current_app == 'run' then
313 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
314 elseif Current_app == 'source' then
315 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
316 else
317 assert(false, 'unknown app "'..Current_app..'"')
321 function App.mousemoved(x,y, dx,dy, is_touch)
322 if current_app_is_warning() then return end
323 if Current_app == 'run' then
324 if run.mouse_move then run.mouse_move(dx,dy) end
325 elseif Current_app == 'source' then
326 if source.mouse_move then source.mouse_move(dx,dy) end
327 else
328 assert(false, 'unknown app "'..Current_app..'"')
332 function App.wheelmoved(dx,dy)
333 if current_app_is_warning() then return end
334 if Current_app == 'run' then
335 if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end
336 elseif Current_app == 'source' then
337 if source.mouse_wheel_move then source.mouse_wheel_move(dx,dy) end
338 else
339 assert(false, 'unknown app "'..Current_app..'"')
343 function App.mousefocus(in_focus)
344 if current_app_is_warning() then return end
345 if Current_app == 'run' then
346 if run.mouse_focus then run.mouse_focus(in_focus) end
347 elseif Current_app == 'source' then
348 if source.mouse_focus then source.mouse_focus(in_focus) end
349 else
350 assert(false, 'unknown app "'..Current_app..'"')
354 function love.quit()
355 if Disable_all_quit_handlers then return end
356 if current_app_is_warning() then return end
357 if Current_app == 'run' then
358 local source_settings = Settings.source
359 Settings = run.settings()
360 Settings.source = source_settings
361 else
362 Settings.source = source.settings()
364 Settings.current_app = Current_app
365 love.filesystem.write('config', json.encode(Settings))
366 if Current_app == 'run' then
367 if run.quit then run.quit() end
368 elseif Current_app == 'source' then
369 if source.quit then source.quit() end
370 else
371 assert(false, 'unknown app "'..Current_app..'"')
375 function current_app_is_warning()
376 return type(Current_app) == 'table' and Current_app.name == 'warning'
379 function show_warning(message)
380 assert(type(Current_app) == 'string')
381 Current_app = {
382 name = 'warning',
383 message = message,
384 next_app = Current_app,
388 function clear_warning()
389 assert(type(Current_app) == 'table')
390 Current_app = Current_app.next_app
393 function warning_message()
394 assert(type(Current_app) == 'table')
395 return Current_app.message