.
[view.love.git] / main.lua
blob13f2d6a4aa5c13b83a80da1286857f971820617a
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 else
70 assert(false, 'unknown app "'..Current_app..'"')
71 end
72 end
74 function App.initialize_globals()
75 if Current_app == 'run' then
76 run.initialize_globals()
77 elseif Current_app == 'source' then
78 source.initialize_globals()
79 else
80 assert(false, 'unknown app "'..Current_app..'"')
81 end
83 -- for hysteresis in a few places
84 Current_time = 0
85 Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700
86 Last_resize_time = 0
87 end
89 function App.initialize(arg)
90 love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
91 love.keyboard.setKeyRepeat(true)
93 love.graphics.setBackgroundColor(1,1,1)
95 if Current_app == 'run' then
96 run.initialize(arg)
97 elseif Current_app == 'source' then
98 source.initialize(arg)
99 else
100 assert(false, 'unknown app "'..Current_app..'"')
104 function App.resize(w,h)
105 if Current_app == 'run' then
106 if run.resize then run.resize(w,h) end
107 elseif Current_app == 'source' then
108 if source.resize then source.resize(w,h) end
109 else
110 assert(false, 'unknown app "'..Current_app..'"')
112 Last_resize_time = Current_time
115 function App.filedropped(file)
116 if Current_app == 'run' then
117 if run.file_drop then run.file_drop(file) end
118 elseif Current_app == 'source' then
119 if source.file_drop then source.file_drop(file) end
120 else
121 assert(false, 'unknown app "'..Current_app..'"')
125 function App.focus(in_focus)
126 if in_focus then
127 Last_focus_time = Current_time
129 if Current_app == 'run' then
130 if run.focus then run.focus(in_focus) end
131 elseif Current_app == 'source' then
132 if source.focus then source.focus(in_focus) end
133 else
134 assert(false, 'unknown app "'..Current_app..'"')
138 function App.draw()
139 if Current_app == 'run' then
140 run.draw()
141 elseif Current_app == 'source' then
142 source.draw()
143 else
144 assert(false, 'unknown app "'..Current_app..'"')
148 function App.update(dt)
149 Current_time = Current_time + dt
150 -- some hysteresis while resizing
151 if Current_time < Last_resize_time + 0.1 then
152 return
155 if Current_app == 'run' then
156 run.update(dt)
157 elseif Current_app == 'source' then
158 source.update(dt)
159 else
160 assert(false, 'unknown app "'..Current_app..'"')
164 function App.keychord_press(chord, key)
165 -- ignore events for some time after window in focus (mostly alt-tab)
166 if Current_time < Last_focus_time + 0.01 then
167 return
170 if chord == 'C-e' then
171 -- carefully save settings
172 if Current_app == 'run' then
173 local source_settings = Settings.source
174 Settings = run.settings()
175 Settings.source = source_settings
176 if run.quit then run.quit() end
177 Current_app = 'source'
178 elseif Current_app == 'source' then
179 Settings.source = source.settings()
180 if source.quit then source.quit() end
181 Current_app = 'run'
182 else
183 assert(false, 'unknown app "'..Current_app..'"')
185 Settings.current_app = Current_app
186 love.filesystem.write('config', json.encode(Settings))
187 -- reboot
188 load_file_from_source_or_save_directory('main.lua')
189 App.undo_initialize()
190 App.run_tests_and_initialize()
191 return
193 if Current_app == 'run' then
194 if run.keychord_press then run.keychord_press(chord, key) end
195 elseif Current_app == 'source' then
196 if source.keychord_press then source.keychord_press(chord, key) end
197 else
198 assert(false, 'unknown app "'..Current_app..'"')
202 function App.textinput(t)
203 -- ignore events for some time after window in focus (mostly alt-tab)
204 if Current_time < Last_focus_time + 0.01 then
205 return
208 if Current_app == 'run' then
209 if run.text_input then run.text_input(t) end
210 elseif Current_app == 'source' then
211 if source.text_input then source.text_input(t) end
212 else
213 assert(false, 'unknown app "'..Current_app..'"')
217 function App.keyreleased(key, scancode)
218 -- ignore events for some time after window in focus (mostly alt-tab)
219 if Current_time < Last_focus_time + 0.01 then
220 return
223 if Current_app == 'run' then
224 if run.key_release then run.key_release(key, scancode) end
225 elseif Current_app == 'source' then
226 if source.key_release then source.key_release(key, scancode) end
227 else
228 assert(false, 'unknown app "'..Current_app..'"')
232 function App.mousepressed(x,y, mouse_button)
233 --? print('mouse press', x,y)
234 if Current_app == 'run' then
235 if run.mouse_press then run.mouse_press(x,y, mouse_button) end
236 elseif Current_app == 'source' then
237 if source.mouse_press then source.mouse_press(x,y, mouse_button) end
238 else
239 assert(false, 'unknown app "'..Current_app..'"')
243 function App.mousereleased(x,y, mouse_button)
244 if Current_app == 'run' then
245 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
246 elseif Current_app == 'source' then
247 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
248 else
249 assert(false, 'unknown app "'..Current_app..'"')
253 function App.wheelmoved(dx,dy)
254 if Current_app == 'run' then
255 if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end
256 elseif Current_app == 'source' then
257 if source.mouse_wheel_move then source.mouse_wheel_move(dx,dy) end
258 else
259 assert(false, 'unknown app "'..Current_app..'"')
263 function love.quit()
264 if Current_app == 'run' then
265 local source_settings = Settings.source
266 Settings = run.settings()
267 Settings.source = source_settings
268 else
269 Settings.source = source.settings()
271 Settings.current_app = Current_app
272 love.filesystem.write('config', json.encode(Settings))
273 if Current_app == 'run' then
274 if run.quit then run.quit() end
275 elseif Current_app == 'source' then
276 if source.quit then source.quit() end
277 else
278 assert(false, 'unknown app "'..Current_app..'"')