1 -- Wrapper that combines the app with a 'source editor' that allows editing
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.
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
15 function load_file_from_source_or_save_directory(filename
)
16 local contents
= love
.filesystem
.read(filename
)
17 local code
, err
= loadstring(contents
, filename
)
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
45 if love
.filesystem
.getInfo('config') then
46 Settings
= json
.decode(love
.filesystem
.read('config'))
47 Current_app
= Settings
.current_app
50 if Current_app
== nil then
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')
78 assert(false, 'unknown app "'..Current_app
..'"')
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()
88 assert(false, 'unknown app "'..Current_app
..'"')
91 -- for hysteresis in a few places
93 Last_focus_time
= 0 -- https://love2d.org/forums/viewtopic.php?p=249700
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
105 elseif Current_app
== 'source' then
106 source
.initialize(arg
)
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
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
129 assert(false, 'unknown app "'..Current_app
..'"')
133 function App
.focus(in_focus
)
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
142 assert(false, 'unknown app "'..Current_app
..'"')
147 if Current_app
== 'run' then
149 elseif Current_app
== 'source' then
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
163 if Current_app
== 'run' then
165 elseif Current_app
== 'source' then
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
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
193 assert(false, 'unknown app "'..Current_app
..'"')
195 Settings
.current_app
= Current_app
196 love
.filesystem
.write('config', json
.encode(Settings
))
198 load_file_from_source_or_save_directory('main.lua')
199 App
.undo_initialize()
200 App
.run_tests_and_initialize()
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
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
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
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
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
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
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
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
269 assert(false, 'unknown app "'..Current_app
..'"')
274 if Current_app
== 'run' then
275 local source_settings
= Settings
.source
276 Settings
= run
.settings()
277 Settings
.source
= source_settings
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
288 assert(false, 'unknown app "'..Current_app
..'"')