Skip to content

Commit c18335d

Browse files
committed
visual-lambda: using browser console on itch.io
1 parent 1762dce commit c18335d

6 files changed

Lines changed: 40 additions & 61 deletions

File tree

!run_pygbag.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ xcopy workspaces\library_demo.xml %DEST%\workspaces\
1616
xcopy workspaces\clear.xml %DEST%\workspaces\
1717

1818

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

config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
ALLOW_SYSTEM_CONSOLE = not IS_WEB_PLATFORM
1313

14+
ALLOW_FILE_WRITING = not IS_WEB_PLATFORM
15+
1416

1517
#-----------------------------------------------
1618
# Config file

controls.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11

2-
Run:
3-
4-
python manipulator.py
5-
6-
7-
8-
92
--------------------------------------------------------
103
Controls
114

@@ -79,5 +72,5 @@ Zoom Wheel
7972

8073

8174

82-
Save screen to bmp F12
75+
Save screen to png F12
8376
Mode of export frames Alt+E

localstorage.py

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,7 @@
33

44
# https://github.com/pygame-web/pygame-web.github.io/blob/main/wiki/pygbag-code/README.md
55

6-
# Add this to index.html:
7-
r"""
8-
// localStorage exchange
9-
function setStorageValue(name, newValue, separator = undefined) {
10-
let key = "storage_" + name;
11-
let value = undefined;
12-
if (separator) {
13-
value = window.localStorage.getItem(key);
14-
if (value) {
15-
value += separator + newValue;
16-
} else {
17-
value = newValue;
18-
}
19-
} else {
20-
value = newValue;
21-
}
22-
window.localStorage.setItem(key, value);
23-
}
24-
25-
function addItem(expression) {
26-
setStorageValue("addItem", expression, "|");
27-
}
28-
function clearWorkspace() {
29-
setStorageValue("clearWorkspace", "1");
30-
}
31-
function saveWorkspace(workspaceName) {
32-
setStorageValue("saveWorkspace", workspaceName);
33-
}
34-
function loadWorkspace(workspaceName) {
35-
setStorageValue("loadWorkspace", workspaceName);
36-
}
37-
function help() {
38-
console.log("addItem(expression) - add an item by expression, e.g. addItem('\\x. x x')");
39-
console.log("clearWorkspace() - clear the workspace");
40-
console.log("saveWorkspace(workspaceName) - save current workspace to localStorage, e.g. saveWorkspace('combinators3')");
41-
console.log("loadWorkspace(workspaceName) - load a workspace from localStorage, e.g. loadWorkspace('combinators3')");
42-
}
43-
"""
6+
# See the writing to localStorage in pygbag_index_html.tmpl
447

458

469
import config

main.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ def __init__( self, caption ):
219219
K_v: [ (0, self.eventAddVariable) ],
220220
K_p: [ (KMOD_ALT, self.eventModeLazy) ],
221221

222-
K_e: [ (KMOD_ALT, self.eventExportMode) ],
222+
K_e: [ (KMOD_ALT, config.ALLOW_FILE_WRITING and self.eventExportMode) ],
223223

224224
K_F1: [ (0, self.eventViewHelp) ],
225225
K_F5: [ (0, self.eventRefreshView) ],
226-
K_F12: [ (0, self.eventSaveScreen) ],
226+
K_F12: [ (0, config.ALLOW_FILE_WRITING and self.eventSaveScreen) ],
227227

228228
K_PLUS: [ (0, self.zoomInView) ],
229229
K_KP_PLUS: [ (0, self.zoomInView) ],
@@ -655,12 +655,13 @@ def paint( self ):
655655
surface = self.getSurface()
656656

657657
# Export Frame as bitmap
658-
if Enduring.exportMode:
659-
filename = 'frame_%s_%05d.png' % ( strDate(), Enduring.exportModeFrame )
660-
Enduring.exportModeFrame += 1
661-
662-
pygame.image.save( surface, filename )
663-
debug(1, 'Frame saved:', filename )
658+
if config.ALLOW_FILE_WRITING:
659+
if Enduring.exportMode:
660+
filename = 'frame_%s_%05d.png' % ( strDate(), Enduring.exportModeFrame )
661+
Enduring.exportModeFrame += 1
662+
663+
pygame.image.save( surface, filename )
664+
debug(1, 'Frame saved:', filename )
664665

665666
# Draw Toolbars
666667
for t in self.toolbars:
@@ -1018,8 +1019,9 @@ def onLoadWorkspaceName( self, workspaceName ):
10181019
self.invalidate()
10191020

10201021

1021-
def eventSaveScreen( self ):
1022-
pygame.image.save( self.getSurface(), 'screen_%s.bmp' % strDate() )
1022+
if config.ALLOW_FILE_WRITING:
1023+
def eventSaveScreen( self ):
1024+
pygame.image.save( self.getSurface(), 'screen_%s.png' % strDate() )
10231025

10241026

10251027
def eventRefreshView( self ):
@@ -1061,10 +1063,10 @@ def eventModeQuick( self ):
10611063
self.invalidate()
10621064

10631065

1064-
1065-
def eventExportMode( self ):
1066-
Enduring.exportMode ^= True
1067-
debug( 1, "Export Mode:", Enduring.exportMode )
1066+
if config.ALLOW_FILE_WRITING:
1067+
def eventExportMode( self ):
1068+
Enduring.exportMode ^= True
1069+
debug( 1, "Export Mode:", Enduring.exportMode )
10681070

10691071

10701072

pygbag_index_html.tmpl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,25 @@ frameborder="1"
511511
console.log("saveWorkspace(workspaceName) - save current workspace to localStorage, e.g. saveWorkspace('combinators3')");
512512
console.log("loadWorkspace(workspaceName) - load a workspace from localStorage, e.g. loadWorkspace('combinators3')");
513513
}
514+
515+
window.addEventListener('message', event => {
516+
//console.log(event);
517+
if (event.origin !== "https://bntr.itch.io") return;
518+
const method = event.data[0];
519+
if (method === "addItem") {
520+
addItem(event.data[1]);
521+
} else if (method === "v") {
522+
clearWorkspace();
523+
} else if (method === "saveWorkspace") {
524+
saveWorkspace(event.data[1])
525+
} else if (method === "loadWorkspace") {
526+
loadWorkspace(event.data[1]);
527+
}
528+
});
529+
// Use on itch.io in browser console:
530+
//> var w = document.getElementById("game_drop").contentWindow;
531+
//> w.postMessage(['addItem','\\x.x x'], '*')
532+
//> w.postMessage(['clearWorkspace'], '*')
514533

515534
</script>
516535

0 commit comments

Comments
 (0)