consistent names in a few more places
[lines.love.git] / main.lua
blob82708587f1c99a628bb0bded8e1eb6aee60104ac
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 if Current_app == 'run' then
91 run.initialize(arg)
92 elseif Current_app == 'source' then
93 source.initialize(arg)
94 else
95 assert(false, 'unknown app "'..Current_app..'"')
96 end
97 end
99 function App.resize(w,h)
100 if Current_app == 'run' then
101 if run.resize then run.resize(w,h) end
102 elseif Current_app == 'source' then
103 if source.resize then source.resize(w,h) end
104 else
105 assert(false, 'unknown app "'..Current_app..'"')
107 Last_resize_time = Current_time
110 function App.filedropped(file)
111 if Current_app == 'run' then
112 if run.file_drop then run.file_drop(file) end
113 elseif Current_app == 'source' then
114 if source.file_drop then source.file_drop(file) end
115 else
116 assert(false, 'unknown app "'..Current_app..'"')
120 function App.focus(in_focus)
121 if in_focus then
122 Last_focus_time = Current_time
124 if Current_app == 'run' then
125 if run.focus then run.focus(in_focus) end
126 elseif Current_app == 'source' then
127 if source.focus then source.focus(in_focus) end
128 else
129 assert(false, 'unknown app "'..Current_app..'"')
133 function App.draw()
134 if Current_app == 'run' then
135 run.draw()
136 elseif Current_app == 'source' then
137 source.draw()
138 else
139 assert(false, 'unknown app "'..Current_app..'"')
143 function App.update(dt)
144 Current_time = Current_time + dt
145 -- some hysteresis while resizing
146 if Current_time < Last_resize_time + 0.1 then
147 return
150 if Current_app == 'run' then
151 run.update(dt)
152 elseif Current_app == 'source' then
153 source.update(dt)
154 else
155 assert(false, 'unknown app "'..Current_app..'"')
159 function App.keychord_press(chord, key)
160 -- ignore events for some time after window in focus (mostly alt-tab)
161 if Current_time < Last_focus_time + 0.01 then
162 return
165 if chord == 'C-e' then
166 -- carefully save settings
167 if Current_app == 'run' then
168 local source_settings = Settings.source
169 Settings = run.settings()
170 Settings.source = source_settings
171 if run.quit then run.quit() end
172 Current_app = 'source'
173 elseif Current_app == 'source' then
174 Settings.source = source.settings()
175 if source.quit then source.quit() end
176 Current_app = 'run'
177 else
178 assert(false, 'unknown app "'..Current_app..'"')
180 Settings.current_app = Current_app
181 love.filesystem.write('config', json.encode(Settings))
182 -- reboot
183 load_file_from_source_or_save_directory('main.lua')
184 App.undo_initialize()
185 App.run_tests_and_initialize()
186 return
188 if Current_app == 'run' then
189 if run.keychord_press then run.keychord_press(chord, key) end
190 elseif Current_app == 'source' then
191 if source.keychord_press then source.keychord_press(chord, key) end
192 else
193 assert(false, 'unknown app "'..Current_app..'"')
197 function App.textinput(t)
198 -- ignore events for some time after window in focus (mostly alt-tab)
199 if Current_time < Last_focus_time + 0.01 then
200 return
203 if Current_app == 'run' then
204 if run.text_input then run.text_input(t) end
205 elseif Current_app == 'source' then
206 if source.text_input then source.text_input(t) end
207 else
208 assert(false, 'unknown app "'..Current_app..'"')
212 function App.keyreleased(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 if Current_app == 'run' then
219 if run.key_release then run.key_release(chord, key) end
220 elseif Current_app == 'source' then
221 if source.key_release then source.key_release(chord, key) end
222 else
223 assert(false, 'unknown app "'..Current_app..'"')
227 function App.mousepressed(x,y, mouse_button)
228 --? print('mouse press', x,y)
229 if Current_app == 'run' then
230 if run.mouse_pressed then run.mouse_press(x,y, mouse_button) end
231 elseif Current_app == 'source' then
232 if source.mouse_pressed then source.mouse_press(x,y, mouse_button) end
233 else
234 assert(false, 'unknown app "'..Current_app..'"')
238 function App.mousereleased(x,y, mouse_button)
239 if Current_app == 'run' then
240 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
241 elseif Current_app == 'source' then
242 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
243 else
244 assert(false, 'unknown app "'..Current_app..'"')
248 function love.quit()
249 if Current_app == 'run' then
250 local source_settings = Settings.source
251 Settings = run.settings()
252 Settings.source = source_settings
253 else
254 Settings.source = source.settings()
256 Settings.current_app = Current_app
257 love.filesystem.write('config', json.encode(Settings))
258 if Current_app == 'run' then
259 if run.quit then run.quit() end
260 elseif Current_app == 'source' then
261 if source.quit then source.quit() end
262 else
263 assert(false, 'unknown app "'..Current_app..'"')