two bugfixes
[lines.love.git] / main.lua
blobc3b5a26061a2035eae42eb170b944789da03787a
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 -- preserve any Error_message when going from run to source
187 elseif Current_app == 'source' then
188 Settings.source = source.settings()
189 if source.quit then source.quit() end
190 Current_app = 'run'
191 Error_message = nil
192 else
193 assert(false, 'unknown app "'..Current_app..'"')
195 Settings.current_app = Current_app
196 love.filesystem.write('config', json.encode(Settings))
197 -- reboot
198 load_file_from_source_or_save_directory('main.lua')
199 App.undo_initialize()
200 App.run_tests_and_initialize()
201 return
203 if Current_app == 'run' then
204 if run.keychord_press then run.keychord_press(chord, key) end
205 elseif Current_app == 'source' then
206 if source.keychord_press then source.keychord_press(chord, key) end
207 else
208 assert(false, 'unknown app "'..Current_app..'"')
212 function App.textinput(t)
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.text_input then run.text_input(t) end
220 elseif Current_app == 'source' then
221 if source.text_input then source.text_input(t) end
222 else
223 assert(false, 'unknown app "'..Current_app..'"')
227 function App.keyreleased(key, scancode)
228 -- ignore events for some time after window in focus (mostly alt-tab)
229 if Current_time < Last_focus_time + 0.01 then
230 return
233 if Current_app == 'run' then
234 if run.key_release then run.key_release(key, scancode) end
235 elseif Current_app == 'source' then
236 if source.key_release then source.key_release(key, scancode) end
237 else
238 assert(false, 'unknown app "'..Current_app..'"')
242 function App.mousepressed(x,y, mouse_button)
243 --? print('mouse press', x,y)
244 if Current_app == 'run' then
245 if run.mouse_press then run.mouse_press(x,y, mouse_button) end
246 elseif Current_app == 'source' then
247 if source.mouse_press then source.mouse_press(x,y, mouse_button) end
248 else
249 assert(false, 'unknown app "'..Current_app..'"')
253 function App.mousereleased(x,y, mouse_button)
254 if Current_app == 'run' then
255 if run.mouse_release then run.mouse_release(x,y, mouse_button) end
256 elseif Current_app == 'source' then
257 if source.mouse_release then source.mouse_release(x,y, mouse_button) end
258 else
259 assert(false, 'unknown app "'..Current_app..'"')
263 function App.wheelmoved(dx,dy)
264 if Current_app == 'run' then
265 if run.mouse_wheel_move then run.mouse_wheel_move(dx,dy) end
266 elseif Current_app == 'source' then
267 if source.mouse_wheel_move then source.mouse_wheel_move(dx,dy) end
268 else
269 assert(false, 'unknown app "'..Current_app..'"')
273 function love.quit()
274 if Current_app == 'run' then
275 local source_settings = Settings.source
276 Settings = run.settings()
277 Settings.source = source_settings
278 else
279 Settings.source = source.settings()
281 Settings.current_app = Current_app
282 love.filesystem.write('config', json.encode(Settings))
283 if Current_app == 'run' then
284 if run.quit then run.quit() end
285 elseif Current_app == 'source' then
286 if source.quit then source.quit() end
287 else
288 assert(false, 'unknown app "'..Current_app..'"')