From 3772b9bd6658370ae5c91751d0e7df0df4fe57a6 Mon Sep 17 00:00:00 2001 From: Richard Knoll Date: Mon, 4 Nov 2024 12:43:40 -0800 Subject: [PATCH] add the multiplayer presence bar to the script page (#6532) --- docfiles/script.html | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/docfiles/script.html b/docfiles/script.html index 46385423937..1e91b99955d 100644 --- a/docfiles/script.html +++ b/docfiles/script.html @@ -45,6 +45,14 @@

@title@

loading...
+
@@ -169,6 +177,7 @@

@title@

}); var isGame = true; + var isMultiplayer = false; editCodeButton.addEventListener("click", function() { window.pxtTickEvent('share.editcode', { target: "arcade" }); }) @@ -220,6 +229,13 @@

@title@

}); window.addEventListener('message', function (ev) { + if (ev.data && ev.data.type === "status" && ev.data.state === "running" && isMultiplayer) { + const iframe = document.getElementsByTagName("iframe").item(0); + + if (iframe) { + setActivePlayer(1, iframe); + } + } if (!shareLinkIsApproved) return; var d = ev.data; @@ -240,6 +256,8 @@

@title@

} }); + initPresenceBar(); + function prebuildSimulator(container) { var sims = document.createElement("div"); sims.className = "sim-embed"; @@ -282,6 +300,8 @@

@title@

if (built.parts.indexOf("multiplayer") !== -1) { window.pxtTickEvent('share.isMultiplayerGame', { target: "arcade" }); multiplayerShareButton.style.display = "block"; + isMultiplayer = true; + document.getElementById("presence-bar").style.display = "block"; } }); }); @@ -296,6 +316,35 @@

@title@

iframe.src = src; return iframe; } + + function initPresenceBar() { + for (let i = 0; i < 4; i++) { + const button = document.getElementsByClassName("player-" + (i + 1)).item(0); + button.addEventListener("click", () => { + const iframe = document.getElementsByTagName("iframe").item(0); + + if (iframe) { + setActivePlayer(i + 1, iframe); + iframe.focus(); + } + }); + } + } + + function setActivePlayer(playerNumber, iframe) { + const setSlotMsg = { + type: "setactiveplayer", + playerNumber: playerNumber, + }; + const connectionMsg = { + type: "multiplayer", + content: "Connection", + slot: playerNumber, + connected: true, + }; + iframe.contentWindow.postMessage(setSlotMsg, "*"); + iframe.contentWindow.postMessage(connectionMsg, "*"); + } })();