import React from "react"; import logo from "./logo.svg"; import JSZip from "jszip"; import "./App.css"; import { xml2js } from "xml-js"; const SkinContext = React.createContext(null); function promisify(func) { return function(...args) { return new Promise((resolve, reject) => { func(...args, (err, data) => { if (err != null) { reject(err); return; } resolve(data); }); }); }; } async function getSkin() { const resp = await fetch(process.env.PUBLIC_URL + "/CornerAmp_Redux.wal"); const blob = await resp.blob(); const zip = await JSZip.loadAsync(blob); const player = zip.file("xml/player-elements.xml"); const xml = await player.async("text"); const elementsDoc = await xml2js(xml, { compact: false, elementsKey: "children", }); console.log(zip.files); const images = {}; // TODO: Clearly more complicated than it needed to be. const elements = elementsDoc.children[0].children; for (const element of elements) { switch (element.name) { case "bitmap": { const { file, gammagroup, h, id, w, x, y } = element.attributes; // TODO: Escape file for regex const img = zip.file(new RegExp(file, "i"))[0]; const imgBlob = await img.async("blob"); const imgUrl = URL.createObjectURL(imgBlob); images[id.toLowerCase()] = { file, gammagroup, h, w, x, y, imgUrl }; break; } case "truetypefont": { console.log(element); break; } default: { console.error(`Unknonw node ${element.name}`); } } } return images; } function Layout({ id, background, desktopalpha, drawBackground, minimum_h, maximum_h, minimum_w, maximum_w, droptarget, children, }) { const data = React.useContext(SkinContext); const image = data[background]; return ( <> {children} ); } function Layer({ id, image, children, x, y }) { const data = React.useContext(SkinContext); const img = data[image.toLowerCase()]; return ( <> {children} ); } function Button({ id, image, action, x, y, downImage, tooltip, children }) { const data = React.useContext(SkinContext); const [down, setDown] = React.useState(false); const imgId = down ? downImage : image; // TODO: These seem to be switching too fast const img = data[imgId.toLowerCase()]; return (
{ setDown(true); document.addEventListener("mouseup", () => { // TODO: This could be unmounted setDown(false); }); }} title={tooltip} style={{ position: "absolute", top: Number(y), left: Number(x), backgroundPositionX: -Number(img.x), backgroundPositionx: -Number(img.y), width: Number(img.w), height: Number(img.h), backgroundImage: `url(${img.imgUrl})`, }} > {children}
); } function ToggleButton(props) { return