2021-01-10 03:36:05 +00:00
|
|
|
import "./index.css";
|
|
|
|
|
2020-11-16 23:18:48 +00:00
|
|
|
const { SourceAPI, PublicAPI } = require("ruffle-core");
|
2019-09-05 02:11:01 +00:00
|
|
|
|
2020-05-12 22:24:41 +00:00
|
|
|
window.RufflePlayer = PublicAPI.negotiate(
|
|
|
|
window.RufflePlayer,
|
|
|
|
"local",
|
|
|
|
new SourceAPI("local")
|
|
|
|
);
|
2019-04-25 17:52:22 +00:00
|
|
|
|
2020-03-29 09:23:17 +00:00
|
|
|
let ruffle;
|
|
|
|
let player;
|
2019-04-25 17:52:22 +00:00
|
|
|
|
2021-01-10 03:36:05 +00:00
|
|
|
const container = document.getElementById("main");
|
2021-01-22 02:16:39 +00:00
|
|
|
const overlay = document.getElementById("overlay");
|
2021-01-10 03:36:05 +00:00
|
|
|
const authorContainer = document.getElementById("author-container");
|
|
|
|
const author = document.getElementById("author");
|
|
|
|
const sampleFileInputContainer = document.getElementById(
|
|
|
|
"sample-swfs-container"
|
|
|
|
);
|
|
|
|
const sampleFileInput = document.getElementById("sample-swfs");
|
|
|
|
const localFileInput = document.getElementById("local-file");
|
|
|
|
const animOptGroup = document.getElementById("anim-optgroup");
|
|
|
|
const gamesOptGroup = document.getElementById("games-optgroup");
|
2020-03-29 09:23:17 +00:00
|
|
|
|
2021-01-06 07:32:18 +00:00
|
|
|
// Default config used by the player.
|
2021-01-10 03:36:05 +00:00
|
|
|
const config = {
|
2021-01-06 07:32:18 +00:00
|
|
|
letterbox: "on",
|
2021-01-20 02:10:35 +00:00
|
|
|
logLevel: "warn",
|
2021-01-06 07:32:18 +00:00
|
|
|
};
|
|
|
|
|
2021-01-10 03:36:05 +00:00
|
|
|
window.addEventListener("DOMContentLoaded", async () => {
|
2020-05-12 22:24:41 +00:00
|
|
|
ruffle = window.RufflePlayer.newest();
|
2020-11-14 21:22:28 +00:00
|
|
|
player = ruffle.createPlayer();
|
2020-05-12 22:24:41 +00:00
|
|
|
player.id = "player";
|
2021-01-10 03:36:05 +00:00
|
|
|
container.append(player);
|
|
|
|
|
|
|
|
const response = await fetch("swfs.json");
|
|
|
|
if (!response.ok) {
|
|
|
|
sampleFileInputContainer.style.display = "none";
|
|
|
|
return;
|
|
|
|
}
|
2020-11-24 16:54:03 +00:00
|
|
|
|
2021-01-10 03:36:05 +00:00
|
|
|
const data = await response.json();
|
|
|
|
for (const swfData of data.swfs) {
|
|
|
|
const option = document.createElement("option");
|
|
|
|
option.textContent = swfData.title;
|
|
|
|
option.value = swfData.location;
|
|
|
|
option.swfData = swfData;
|
|
|
|
switch (swfData.type) {
|
|
|
|
case "Animation":
|
|
|
|
animOptGroup.append(option);
|
|
|
|
break;
|
|
|
|
case "Game":
|
|
|
|
gamesOptGroup.append(option);
|
|
|
|
break;
|
2020-05-12 22:24:41 +00:00
|
|
|
}
|
2021-01-10 03:36:05 +00:00
|
|
|
}
|
|
|
|
sampleFileInputContainer.style.display = "inline-block";
|
2020-03-29 09:23:17 +00:00
|
|
|
|
2021-01-10 03:36:05 +00:00
|
|
|
const initialFile = new URLSearchParams(window.location.search).get("file");
|
|
|
|
if (initialFile) {
|
|
|
|
const options = Array.from(sampleFileInput.options);
|
|
|
|
sampleFileInput.selectedIndex = Math.max(
|
|
|
|
options.findIndex((swfData) => swfData.value.endsWith(initialFile)),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Load a random file.
|
|
|
|
sampleFileInput.selectedIndex =
|
|
|
|
Math.floor(Math.random() * data.swfs.length) + 1;
|
|
|
|
}
|
|
|
|
sampleFileSelected();
|
|
|
|
});
|
2020-03-29 09:23:17 +00:00
|
|
|
|
2021-01-22 02:16:39 +00:00
|
|
|
window.addEventListener("load", () => {
|
|
|
|
overlay.style.display = "block";
|
|
|
|
});
|
|
|
|
|
2021-01-10 03:36:05 +00:00
|
|
|
sampleFileInput.addEventListener("change", sampleFileSelected);
|
2021-01-22 02:25:11 +00:00
|
|
|
localFileInput.addEventListener("change", (event) => {
|
|
|
|
loadFile(event.target.files[0]);
|
|
|
|
});
|
|
|
|
container.addEventListener("dragenter", () => {
|
2021-01-22 02:16:39 +00:00
|
|
|
overlay.classList.add("drag");
|
|
|
|
});
|
2021-01-22 02:25:11 +00:00
|
|
|
container.addEventListener("dragleave", () => {
|
2021-01-22 02:16:39 +00:00
|
|
|
overlay.classList.remove("drag");
|
|
|
|
});
|
2021-01-22 02:25:11 +00:00
|
|
|
container.addEventListener("dragover", (event) => {
|
2021-01-22 02:16:39 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
});
|
2021-01-22 02:25:11 +00:00
|
|
|
container.addEventListener("drop", (event) => {
|
2021-01-22 02:16:39 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
event.preventDefault();
|
|
|
|
overlay.classList.remove("drag");
|
|
|
|
loadFile(event.dataTransfer.files[0]);
|
|
|
|
});
|
2019-04-29 20:24:29 +00:00
|
|
|
|
2020-03-29 09:23:17 +00:00
|
|
|
function sampleFileSelected() {
|
2021-01-10 03:36:05 +00:00
|
|
|
const swfData = sampleFileInput[sampleFileInput.selectedIndex].swfData;
|
2020-06-05 21:47:05 +00:00
|
|
|
if (swfData) {
|
2021-01-10 03:36:05 +00:00
|
|
|
authorContainer.style.display = "block";
|
|
|
|
author.textContent = swfData.author;
|
2020-06-05 21:47:05 +00:00
|
|
|
author.href = swfData.authorLink;
|
2020-03-29 09:23:17 +00:00
|
|
|
localFileInput.value = null;
|
2021-01-10 03:36:05 +00:00
|
|
|
player.load({ url: swfData.location, ...config });
|
2020-05-12 22:24:41 +00:00
|
|
|
} else {
|
2021-01-23 03:16:37 +00:00
|
|
|
if (player) {
|
|
|
|
player.remove();
|
|
|
|
}
|
2021-01-21 23:53:01 +00:00
|
|
|
player = ruffle.createPlayer();
|
2021-01-10 03:36:05 +00:00
|
|
|
player.id = "player";
|
|
|
|
container.append(player);
|
|
|
|
authorContainer.style.display = "none";
|
|
|
|
author.textContent = "";
|
|
|
|
author.href = "";
|
2020-03-29 09:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 02:16:39 +00:00
|
|
|
async function loadFile(file) {
|
|
|
|
if (!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-29 09:23:17 +00:00
|
|
|
sampleFileInput.selectedIndex = 0;
|
2021-01-10 03:36:05 +00:00
|
|
|
authorContainer.style.display = "none";
|
|
|
|
author.textContent = "";
|
2020-03-29 09:23:17 +00:00
|
|
|
author.href = "";
|
2020-05-12 22:24:41 +00:00
|
|
|
|
2021-01-22 02:25:11 +00:00
|
|
|
const data = await new Response(file).arrayBuffer();
|
2021-01-22 02:16:39 +00:00
|
|
|
player.load({ data, ...config });
|
2020-05-12 22:24:41 +00:00
|
|
|
}
|