use a helper
[lines.love.git] / main.lua
blob570a7a13d3def3dfa39506830df735a346a30419
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 Last_focus_time = App.getTime() -- https://love2d.org/forums/viewtopic.php?p=249700
85 Last_resize_time = App.getTime()
86 end
88 function App.initialize(arg)
89 if Current_app == 'run' then
90 run.initialize(arg)
91 elseif Current_app == 'source' then
92 source.initialize(arg)
93 else
94 assert(false, 'unknown app "'..Current_app..'"')
95 end
96 end
98 function App.resize(w,h)
99 if Current_app == 'run' then
100 if run.resize then run.resize(w,h) end
101 elseif Current_app == 'source' then
102 if source.resize then source.resize(w,h) end
103 else
104 assert(false, 'unknown app "'..Current_app..'"')
106 Last_resize_time = App.getTime()
109 function App.filedropped(file)
110 if Current_app == 'run' then
111 if run.filedropped then run.filedropped(file) end
112 elseif Current_app == 'source' then
113 if source.filedropped then source.filedropped(file) end
114 else
115 assert(false, 'unknown app "'..Current_app..'"')
119 function App.focus(in_focus)
120 if in_focus then
121 Last_focus_time = App.getTime()
123 if Current_app == 'run' then
124 if run.focus then run.focus(in_focus) end
125 elseif Current_app == 'source' then
126 if source.focus then source.focus(in_focus) end
127 else
128 assert(false, 'unknown app "'..Current_app..'"')
132 function App.draw()
133 if Current_app == 'run' then
134 run.draw()
135 elseif Current_app == 'source' then
136 source.draw()
137 else
138 assert(false, 'unknown app "'..Current_app..'"')
142 function App.update(dt)
143 -- some hysteresis while resizing
144 if App.getTime() < Last_resize_time + 0.1 then
145 return
148 if Current_app == 'run' then
149 run.update(dt)
150 elseif Current_app == 'source' then
151 source.update(dt)
152 else
153 assert(false, 'unknown app "'..Current_app..'"')
157 function App.keychord_pressed(chord, key)
158 -- ignore events for some time after window in focus (mostly alt-tab)
159 if App.getTime() < Last_focus_time + 0.01 then
160 return
163 if chord == 'C-e' then
164 -- carefully save settings
165 if Current_app == 'run' then
166 local source_settings = Settings.source
167 Settings = run.settings()
168 Settings.source = source_settings
169 if run.quit then run.quit() end
170 Current_app = 'source'
171 elseif Current_app == 'source' then
172 Settings.source = source.settings()
173 if source.quit then source.quit() end
174 Current_app = 'run'
175 else
176 assert(false, 'unknown app "'..Current_app..'"')
178 Settings.current_app = Current_app
179 love.filesystem.write('config', json.encode(Settings))
180 -- reboot
181 load_file_from_source_or_save_directory('main.lua')
182 App.undo_initialize()
183 App.run_tests_and_initialize()
184 return
186 if Current_app == 'run' then
187 if run.keychord_pressed then run.keychord_pressed(chord, key) end
188 elseif Current_app == 'source' then
189 if source.keychord_pressed then source.keychord_pressed(chord, key) end
190 else
191 assert(false, 'unknown app "'..Current_app..'"')
195 function App.textinput(t)
196 -- ignore events for some time after window in focus (mostly alt-tab)
197 if App.getTime() < Last_focus_time + 0.01 then
198 return
201 if Current_app == 'run' then
202 if run.textinput then run.textinput(t) end
203 elseif Current_app == 'source' then
204 if source.textinput then source.textinput(t) end
205 else
206 assert(false, 'unknown app "'..Current_app..'"')
210 function App.keyreleased(chord, key)
211 -- ignore events for some time after window in focus (mostly alt-tab)
212 if App.getTime() < Last_focus_time + 0.01 then
213 return
216 if Current_app == 'run' then
217 if run.key_released then run.key_released(chord, key) end
218 elseif Current_app == 'source' then
219 if source.key_released then source.key_released(chord, key) end
220 else
221 assert(false, 'unknown app "'..Current_app..'"')
225 function App.mousepressed(x,y, mouse_button)
226 --? print('mouse press', x,y)
227 if Current_app == 'run' then
228 if run.mouse_pressed then run.mouse_pressed(x,y, mouse_button) end
229 elseif Current_app == 'source' then
230 if source.mouse_pressed then source.mouse_pressed(x,y, mouse_button) end
231 else
232 assert(false, 'unknown app "'..Current_app..'"')
236 function App.mousereleased(x,y, mouse_button)
237 if Current_app == 'run' then
238 if run.mouse_released then run.mouse_released(x,y, mouse_button) end
239 elseif Current_app == 'source' then
240 if source.mouse_released then source.mouse_released(x,y, mouse_button) end
241 else
242 assert(false, 'unknown app "'..Current_app..'"')
246 function love.quit()
247 if Current_app == 'run' then
248 local source_settings = Settings.source
249 Settings = run.settings()
250 Settings.source = source_settings
251 else
252 Settings.source = source.settings()
254 Settings.current_app = Current_app
255 love.filesystem.write('config', json.encode(Settings))
256 if Current_app == 'run' then
257 if run.quit then run.quit() end
258 elseif Current_app == 'source' then
259 if source.quit then source.quit() end
260 else
261 assert(false, 'unknown app "'..Current_app..'"')