reorganize some comments
[view.love.git] / main.lua
bloba59c4837a2b4f3fbbaeb25e6620b3d9324138eb9
1 -- Wrapper that combines the app with a 'source editor' that allows editing
2 -- the app in place.
3 --
4 -- The source editor is a sharp tool. I find it convenient, but I also often
5 -- end up in a bad state that requires dropping down to external tools
6 -- (editor, file manager) to fix.
7 --
8 -- Downstream forks provide a better, "freewheeling" experience for editing
9 -- apps live. The source editor provides a half-baked experience for editing
10 -- some of the primitives used by true freewheeling apps.
12 -- files that come with LÖVE; we can't edit those from within the app
13 utf8 = require 'utf8'
15 function load_file_from_source_or_save_directory(filename)
16 local contents = love.filesystem.read(filename)
17 local code, err = loadstring(contents, filename)
18 if code == nil then
19 error(err)
20 end
21 return code()
22 end
24 json = load_file_from_source_or_save_directory('json.lua')
26 load_file_from_source_or_save_directory('app.lua')
27 load_file_from_source_or_save_directory('test.lua')
29 load_file_from_source_or_save_directory('keychord.lua')
30 load_file_from_source_or_save_directory('button.lua')
32 -- both sides require (different parts of) the logging framework
33 load_file_from_source_or_save_directory('log.lua')
35 -- both sides use drawings
36 load_file_from_source_or_save_directory('icons.lua')
37 load_file_from_source_or_save_directory('drawing.lua')
38 load_file_from_source_or_save_directory('geom.lua')
39 load_file_from_source_or_save_directory('help.lua')
40 load_file_from_source_or_save_directory('drawing_tests.lua')
42 -- but some files we want to only load sometimes
43 function App.load()
44 log_new('session')
45 if love.filesystem.getInfo('config') then
46 Settings = json.decode(love.filesystem.read('config'))
47 Current_app = Settings.current_app
48 end
50 if Current_app == nil then
51 Current_app = 'run'
52 end
54 if Current_app == 'run' then
55 load_file_from_source_or_save_directory('file.lua')
56 load_file_from_source_or_save_directory('run.lua')
57 load_file_from_source_or_save_directory('edit.lua')
58 load_file_from_source_or_save_directory('text.lua')
59 load_file_from_source_or_save_directory('search.lua')
60 load_file_from_source_or_save_directory('select.lua')
61 load_file_from_source_or_save_directory('undo.lua')
62 load_file_from_source_or_save_directory('text_tests.lua')
63 load_file_from_source_or_save_directory('run_tests.lua')
64 elseif Current_app == 'source' then
65 load_file_from_source_or_save_directory('source_file.lua')
66 load_file_from_source_or_save_directory('source.lua')
67 load_file_from_source_or_save_directory('commands.lua')
68 load_file_from_source_or_save_directory('source_edit.lua')
69 load_file_from_source_or_save_directory('log_browser.lua')
70 load_file_from_source_or_save_directory('source_text.lua')
71 load_file_from_source_or_save_directory('search.lua')
72 load_file_from_source_or_save_directory('source_select.lua')
73 load_file_from_source_or_save_directory('source_undo.lua')
74 load_file_from_source_or_save_directory('colorize.lua')
75 load_file_from_source_or_save_directory('source_text_tests.lua')
76 load_file_from_source_or_save_directory('source_tests.lua')
77 else
78 assert(false, 'unknown app "'..Current_app..'"')
79 end
80 end
82 function App.initialize_globals()
83 if Current_app == 'run' then
84 run.initialize_globals()
85 elseif Current_app == 'source' then
86 source.initialize_globals()
87 else
88 assert(false, 'unknown app "'..Current_app..'"')
89 end
91 -- for hysteresis in a few places
92 Current_time = 0
93 Last_focus_time = 0 -- https://love2d.org/forums/viewtopic.php?p=249700
94 Last_resize_time = 0
95 end
97 function App.initialize(arg)
98 love.keyboard.setTextInput(true) -- bring up keyboard on touch screen
99 love.keyboard.setKeyRepeat(true)
101 love.graphics.setBackgroundColor(1,1,1)
103 if Current_app == 'run' then
104 run.initialize(arg)
105 elseif Current_app == 'source' then
106 source.initialize(arg)
107 else
108 assert(false, 'unknown app "'..Current_app..'"')
112 function App.resize(w,h)
113 if Current_app == 'run' then
114 if run.resize then run.resize(w,h) end
115 elseif Current_app == 'source' then
116 if source.resize then source.resize(w,h) end
117 else
118 assert(false, 'unknown app "'..Current_app..'"')
120 Last_resize_time = Current_time
123 function App.filedropped(file)
124 if Current_app == 'run' then
125 if run.file_drop then run.file_drop(file) end
126 elseif Current_app == 'source' then
127 if source.file_drop then source.file_drop(file) end
128 else
129 assert(false, 'unknown app "'..Current_app..'"')
133 function App.focus(in_focus)
134 if in_focus then
135 Last_focus_time = Current_time
137 if Current_app == 'run' then
138 if run.focus then run.focus(in_focus) end
139 elseif Current_app == 'source' then
140 if source.focus then source.focus(in_focus) end
141 else
142 assert(false, 'unknown app "'..Current_app..'"')
146 function App.draw()
147 if Current_app == 'run' then
148 run.draw()
149 elseif Current_app == 'source' then
150 source.draw()
151 else
152 assert(false, 'unknown app "'..Current_app..'"')
156 function App.update(dt)
157 Current_time = Current_time + dt
158 -- some hysteresis while resizing
159 if Current_time < Last_resize_time + 0.1 then
160 return
163 if Current_app == 'run' then
164 run.update(dt)
165 elseif Current_app == 'source' then
166 source.update(dt)
167 else
168 assert(false, 'unknown app "'..Current_app..'"')
172 function App.keychord_press(chord, key)
173 -- ignore events for some time after window in focus (mostly alt-tab)
174 if Current_time < Last_focus_time + 0.01 then
175 return
178 if chord == 'C-e' then
179 -- carefully save settings
180 if Current_app == 'run' then
181 local source_settings = Settings.source
182 Settings = run.settings()
183 Settings.source = source_settings
184 if run.quit then run.quit() end
185 Current_app = 'source'
186 elseif Current_app == 'source' then
187 Settings.source = source.settings()
188 if source.quit then source.quit() end
189 Current_app = 'run'
190 else
191 assert(false, 'unknown app "'..Current_app..'"')
193 Settings.current_app = Current_app
194 love.filesystem.write('config', json.encode(Settings))
195 -- reboot
196 load_file_from_source_or_save_directory('main.lua')
197 App.undo_initialize()
198 App.run_tests_and_initialize()
199 return
201 if Current_app == 'run' then
202 if run.keychord_press then run.keychord_press(chord, key) end
203 elseif Current_app == 'source' then
204 if source.keychord_press then source.keychord_press(chord, key) end
205 else
206 assert(false, 'unknown app "'..Current_app..'"')
210 function App.textinput(t)
211 -- ignore events for some time after window in focus (mostly alt-tab)
212 if Current_time < Last_focus_time + 0.01 then
213 return
216 if Current_app == 'run' then
217 if run.text_input then run.text_input(t) end
218 elseif Current_app == 'source' then
219 if source.text_input then source.text_input(t) end
220 else
221 assert(false, 'unknown app "'..Current_app..'"')
225 function App.keyreleased(key, scancode)
226 -- ignore events for some time after window in focus (mostly alt-tab)
227 if Current_time < Last_focus_time + 0.01 then
228 return
231 if Current_app == 'run' then
232 if run.key_release then run.key_release(key, scancode) end
233 elseif Current_app == 'source' then
234 if source.key_release then source.key_release(key, scancode) end
235 else
236 assert(false, 'unknown app "'..Current_app..'"')
240 function App.mousepressed(x,y, mouse_button)
241 --? print('mouse press', x,y)
242 if Current_app == 'run' then
243 if run.mouse_press then run.mouse_press(x,y, mouse_button) end
244 elseif Current_app == 'source' then
245 if source.mouse_press then source.mouse_press(x,y, mouse_button) end
246 else
247 assert(false, 'unknown app "'..Current_app..'"')
251 function App.mousereleased(x,y, mouse_button)
252 if Current_app == 'run' then
253 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
254 elseif Current_app == 'source' then
255 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
256 else
257 assert(false, 'unknown app "'..Current_app..'"')
261 function App.wheelmoved(dx,dy)
262 if Current_app == 'run' then
263 if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end
264 elseif Current_app == 'source' then
265 if source.mouse_wheel_move then source.mouse_wheel_move(dx,dy) end
266 else
267 assert(false, 'unknown app "'..Current_app..'"')
271 function love.quit()
272 if Current_app == 'run' then
273 local source_settings = Settings.source
274 Settings = run.settings()
275 Settings.source = source_settings
276 else
277 Settings.source = source.settings()
279 Settings.current_app = Current_app
280 love.filesystem.write('config', json.encode(Settings))
281 if Current_app == 'run' then
282 if run.quit then run.quit() end
283 elseif Current_app == 'source' then
284 if source.quit then source.quit() end
285 else
286 assert(false, 'unknown app "'..Current_app..'"')