Skip to content

Commit 5bfdee6

Browse files
committed
update to pygbag 0.9.3
1 parent b8e2b95 commit 5bfdee6

8 files changed

Lines changed: 110 additions & 612 deletions

File tree

!run_pygbag.bat

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
@rem See: https://pygame-web.github.io/wiki/pygbag/
2+
@rem https://pygame-web.github.io/wiki/publishing/itch.io/
13

24

3-
set DEST=visual-lambda-2.0
5+
6+
set DEST=visual-lambda-2.1
47
@rem It will be also the name of .apk file
58

69

@@ -12,8 +15,10 @@ xcopy *.py %DEST%\
1215
xcopy config.cfg %DEST%\
1316
xcopy library.txt %DEST%\
1417
xcopy toolbar_icons.png %DEST%\
15-
xcopy workspaces\library_demo.xml %DEST%\workspaces\
16-
xcopy workspaces\clear.xml %DEST%\workspaces\
18+
xcopy workspaces\default_workspace.xml %DEST%\workspaces\
19+
xcopy workspaces\clear.xml %DEST%\workspaces\
20+
xcopy workspaces\library_demo.xml %DEST%\workspaces\
21+
xcopy workspaces\predecessors.xml %DEST%\workspaces\
1722

1823

19-
python -m pygbag --app_name VisualLambda --package bntr.visuallambda --title "Visual Lambda" --template pygbag_index_html.tmpl --icon favicon.png --archive %DEST%
24+
python -m pygbag --app_name VisualLambda --package bntr.visuallambda --title "Visual Lambda" --template pygbag_0.9.3_index_html.tmpl --icon favicon.png --archive %DEST%

config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Platform
88

99
IS_WEB_PLATFORM = sys.platform == 'emscripten'
10-
#IS_WEB_PLATFORM = True # to test web mode
10+
#IS_WEB_PLATFORM = True # to test web mode locally
1111

1212
ALLOW_SYSTEM_CONSOLE = not IS_WEB_PLATFORM
1313

@@ -40,7 +40,7 @@ def readCfg():
4040

4141
else:
4242
# Read config.cfg
43-
parser.readfp( f )
43+
parser.read_file( f )
4444

4545
# Get Configuration dict
4646
cfg = dict( parser.items('Visual Lambda') )

events.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
SYSTEMCONSOLE_TIMEREVENT = pygame.USEREVENT + 5
2424

2525

26-
LOCALSTORAGE_TIMEREVENT = pygame.USEREVENT + 6
27-
28-
29-
3026
class Enduring: # ?? move class to another module
3127
"""
3228
Enduring Event.

kgrind.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

main.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,8 @@ def __init__( self, caption ):
242242
pygame.time.set_timer(SYSTEMCONSOLE_TIMEREVENT, 100)
243243

244244
if config.IS_WEB_PLATFORM:
245-
pygame.time.set_timer(LOCALSTORAGE_TIMEREVENT, 500)
246-
247-
245+
self._next_localstorage_poll_ms = 0
246+
248247
#-----------------------------------------------------
249248
# Prepare toolbars
250249

@@ -301,12 +300,20 @@ def __init__( self, caption ):
301300

302301
async def run( self ):
303302

304-
sleepSec = 0.02
303+
sleepSec = 0.01
305304
if config.IS_WEB_PLATFORM:
306305
sleepSec = 0 # pygbag asked for 0
307306

308307
while True:
309-
308+
309+
if config.IS_WEB_PLATFORM:
310+
# set_timer is not implemented on WASM yet.
311+
now_ms = pygame.time.get_ticks()
312+
313+
if now_ms >= self._next_localstorage_poll_ms:
314+
self.handleLocalStorageCommands()
315+
self._next_localstorage_poll_ms = now_ms + 500
316+
310317
for e in pygame.event.get():
311318
self.handleEvent( e )
312319
if e.type == pygame.QUIT:
@@ -815,14 +822,6 @@ def handleEvent( self, event ):
815822
self.postEvent( e )
816823
self.invalidate()
817824

818-
if config.IS_WEB_PLATFORM:
819-
if event.type == LOCALSTORAGE_TIMEREVENT:
820-
# Check if user has called a browser console command, e.g. addItem('\\x. x x')
821-
localstorage.handle_storage('addItem', self.onInputItem, "|")
822-
localstorage.handle_storage('clearWorkspace', self.onClearWorkspace)
823-
localstorage.handle_storage('saveWorkspace', self.onSaveWorkspaceName)
824-
localstorage.handle_storage('loadWorkspace', self.onLoadWorkspaceName)
825-
826825
if config.ALLOW_SYSTEM_CONSOLE:
827826
if event.type == SYSTEMCONSOLE_TIMEREVENT:
828827
self.consoleCheck() # it calls console input callbacks, e.g. self.onLoadWorkspaceFileName
@@ -998,6 +997,13 @@ def onSaveWorkspaceFileName( self, workspaceName ):
998997

999998
if config.IS_WEB_PLATFORM: # Save to/Load from localStorage
1000999

1000+
def handleLocalStorageCommands( self ):
1001+
# Check if user has called a browser console command, e.g. addItem('\\x. x x')
1002+
localstorage.handle_storage('addItem', self.onInputItem, "|")
1003+
localstorage.handle_storage('clearWorkspace', self.onClearWorkspace)
1004+
localstorage.handle_storage('saveWorkspace', self.onSaveWorkspaceName)
1005+
localstorage.handle_storage('loadWorkspace', self.onLoadWorkspaceName)
1006+
10011007
def onClearWorkspace( self, _ ):
10021008
if saving.load_from_file( self, "clear.xml" ):
10031009
self.invalidate()
@@ -1307,7 +1313,12 @@ def pickToolbarItem( self, pos ):
13071313

13081314

13091315

1310-
asyncio.run(
1311-
Manipulator('Visual Lambda').run()
1312-
)
1316+
def main():
1317+
asyncio.run(
1318+
Manipulator('Visual Lambda').run()
1319+
)
1320+
1321+
1322+
if __name__ == '__main__':
1323+
main()
13131324

pygbag_0.9.3_index_html.tmpl

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<html lang="en-us"><script src="{{cookiecutter.cdn}}pythons.js" type=module id="site" data-python="python{{cookiecutter.PYBUILD}}" data-LINES=42 data-COLUMNS=132 data-os="vtx,snd,gui" async defer>#<!--
1+
<html lang="en-us"><script src="{{cookiecutter.cdn}}pythons.js" type=module id="site" data-python="python{{cookiecutter.PYBUILD}}" data-LINES=42 data-COLUMNS=132 data-os="vtx,gui" async defer>#<!--
2+
3+
# The template is based on https://github.com/pygame-web/pygbag/blob/main/static/default.tmpl
24

35
print("""
46
Loading {{cookiecutter.title}} from {{cookiecutter.archive}}.apk
@@ -59,25 +61,6 @@ async def custom_site():
5961

6062
main = appdir / "assets" / "main.py"
6163

62-
# TODO: test for window.webkitAudioContext and block aio loop until gesture if accessing media manager play
63-
# before a gesture is recorded ( touch or click )
64-
65-
# test/wait user media interaction
66-
if not platform.window.MM.UME:
67-
68-
# now that apk is mounted we have access to font cache
69-
# but we need to fill __file__ that is not yet set
70-
__import__(__name__).__file__ = main
71-
72-
# now make a prompt
73-
print("Ready to start !")
74-
75-
print("""
76-
* Waiting for media user engagement : please click/touch page *
77-
""")
78-
while not platform.window.MM.UME:
79-
await asyncio.sleep(.1)
80-
8164
# start async top level machinery if not started and add a console in any case if requested.
8265
await TopLevel_async_handler.start_toplevel(platform.shell, console=window.python.config.debug)
8366

@@ -143,7 +126,7 @@ asyncio.run( custom_site() )
143126
config = {
144127
xtermjs : "{{cookiecutter.xtermjs}}" ,
145128
_sdl2 : "canvas",
146-
user_canvas : 0,
129+
user_canvas : 1,
147130
user_canvas_managed : 0,
148131
gui_divider : 2,
149132
ume_block : {{cookiecutter.ume_block}},
@@ -353,6 +336,8 @@ frameborder="1"
353336
// make your js customization here
354337
console.log(__FILE__, "custom_onload")
355338

339+
//debug_hidden = false //!!! use this for debug
340+
356341
pyconsole.hidden = debug_hidden
357342
system.hidden = debug_hidden
358343
transfer.hidden = debug_hidden
@@ -401,6 +386,72 @@ frameborder="1"
401386
window.frames["iframe"].location = url;
402387
}
403388

389+
390+
391+
392+
// Visual Lambda stuff
393+
// localStorage exchange
394+
function setStorageValue(name, newValue, separator = undefined) {
395+
let key = "storage_" + name;
396+
let value = undefined;
397+
if (separator) {
398+
value = window.localStorage.getItem(key);
399+
if (value) {
400+
value += separator + newValue;
401+
} else {
402+
value = newValue;
403+
}
404+
} else {
405+
value = newValue;
406+
}
407+
window.localStorage.setItem(key, value);
408+
}
409+
410+
function addItem(expression) {
411+
setStorageValue("addItem", expression, "|");
412+
}
413+
function clearWorkspace() {
414+
setStorageValue("clearWorkspace", "1");
415+
}
416+
function saveWorkspace(workspaceName) {
417+
setStorageValue("saveWorkspace", workspaceName);
418+
}
419+
function loadWorkspace(workspaceName) {
420+
setStorageValue("loadWorkspace", workspaceName);
421+
}
422+
function help() {
423+
console.log("addItem(expression) - add an item by expression, e.g. addItem('\\x. x x')");
424+
console.log("clearWorkspace() - clear the workspace");
425+
console.log("saveWorkspace(workspaceName) - save current workspace to localStorage, e.g. saveWorkspace('combinators3')");
426+
console.log("loadWorkspace(workspaceName) - load a workspace from localStorage, e.g. loadWorkspace('combinators3')");
427+
}
428+
429+
// Allow using browser console through an iframe on itch.io
430+
window.addEventListener('message', event => {
431+
//console.log(event);
432+
if (event.origin !== "https://bntr.itch.io") return;
433+
const method = event.data[0];
434+
if (method === "addItem") {
435+
addItem(event.data[1]);
436+
} else if (method === "clearWorkspace") {
437+
clearWorkspace();
438+
} else if (method === "saveWorkspace") {
439+
saveWorkspace(event.data[1])
440+
} else if (method === "loadWorkspace") {
441+
loadWorkspace(event.data[1]);
442+
}
443+
});
444+
445+
// Then in browser console:
446+
//> function vl(...args) { document.getElementById("game_drop").contentWindow.postMessage(args, "*") }
447+
//> vl('addItem','MULT 2 (\\f x. f (f x))')
448+
//> vl('clearWorkspace')
449+
//> vl('saveWorkspace', 'combinators3')
450+
//> vl('loadWorkspace', 'combinators3')
451+
452+
453+
454+
404455
</script>
405456

406457
</body>

0 commit comments

Comments
 (0)