├── .gitignore ├── README.md ├── dist ├── AsyncLoading.d.ts ├── AsyncLoading.js ├── AsyncLoading.js.map ├── ReactCanvas.d.ts ├── ReactCanvas.js ├── ReactCanvas.js.map ├── index.d.ts ├── index.js ├── index.js.map └── styles.css ├── example ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── flappy │ │ ├── flappy.js │ │ ├── flappy.pck │ │ ├── flappy.png │ │ └── flappy.wasm │ ├── index.html │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── robots.txt ├── src │ ├── App.tsx │ ├── index.css │ ├── index.tsx │ ├── react-app-env.d.ts │ └── serviceWorker.js └── tsconfig.json ├── package-lock.json ├── package.json ├── src ├── AsyncLoading.tsx ├── ReactCanvas.tsx ├── index.tsx ├── setupTests.js ├── styles.css └── typings.d.ts ├── tsconfig.json └── tsconfig.test.json /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env* 17 | 18 | # debug 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-godot 2 | 3 | > Load a webassembly build of the Godot engine and Bootstrap packed games from within the react component tree 4 | 5 | [![NPM](https://img.shields.io/npm/v/react-godot.svg)](https://www.npmjs.com/package/react-godot) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) 6 | 7 | ## Install 8 | 9 | ```bash 10 | npm install --save react-godot 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```tsx 16 | import * as React from 'react' 17 | 18 | import ReactGodot from 'react-godot' 19 | 20 | class Example extends React.Component { 21 | render () { 22 | return ( 23 | 24 | ) 25 | } 26 | } 27 | ``` 28 | 29 | ## License 30 | 31 | MIT © [d3dc](https://github.com/d3dc) 32 | -------------------------------------------------------------------------------- /dist/AsyncLoading.d.ts: -------------------------------------------------------------------------------- 1 | import { FunctionComponent } from "react"; 2 | export declare type PackLoadingState = { 3 | mode: string; 4 | initializing: boolean; 5 | percent?: number; 6 | msg?: string; 7 | }; 8 | export declare type PackLoadingAction = { 9 | msg?: string; 10 | initialized?: boolean; 11 | percent?: number; 12 | mode: string; 13 | }; 14 | export declare type PackLoadingDispatch = (action: PackLoadingAction) => void; 15 | export declare const useLoading: () => [PackLoadingState, PackLoadingDispatch]; 16 | declare const AsyncLoading: FunctionComponent; 17 | export default AsyncLoading; 18 | -------------------------------------------------------------------------------- /dist/AsyncLoading.js: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { createContext, useContext, useReducer } from "react"; 3 | var packLoadingReducer = function (state, action) { 4 | if (!state.initializing) 5 | return state; 6 | switch (action.mode) { 7 | case "progress": 8 | case "indeterminate": 9 | case "notice": 10 | case "hidden": 11 | break; 12 | default: 13 | throw new Error("Invalid status mode"); 14 | } 15 | var nextState = { 16 | mode: action.mode, 17 | msg: action.msg, 18 | percent: action.percent || 0, 19 | initializing: !action.initialized 20 | }; 21 | return nextState; 22 | }; 23 | var LoadingContext = createContext([ 24 | { mode: "", initializing: true }, 25 | function () { } 26 | ]); 27 | export var useLoading = function () { return useContext(LoadingContext); }; 28 | var Loading = function (_a) { 29 | var notice = _a.notice, _b = _a.percent, percent = _b === void 0 ? 0 : _b, _c = _a.indeterminate, indeterminate = _c === void 0 ? false : _c; 30 | return (React.createElement("div", { id: "status" }, 31 | indeterminate ? (React.createElement("div", { id: "status-indeterminate", onContextMenu: function (e) { return e.preventDefault(); } }, 32 | React.createElement("div", null), 33 | React.createElement("div", null), 34 | React.createElement("div", null), 35 | React.createElement("div", null), 36 | React.createElement("div", null), 37 | React.createElement("div", null), 38 | React.createElement("div", null), 39 | React.createElement("div", null))) : (React.createElement("div", { id: "status-progress", onContextMenu: function (e) { return e.preventDefault(); } }, 40 | React.createElement("div", { id: "status-progress-inner", style: { width: percent + "%" } }))), 41 | notice && (React.createElement("div", { id: "status-notice", className: "godot" }, notice)))); 42 | }; 43 | var AsyncLoading = function (_a) { 44 | var children = _a.children; 45 | var _b = useReducer(packLoadingReducer, { 46 | mode: "indeterminate", 47 | initializing: true 48 | }), loadingState = _b[0], dispatchLoadingAction = _b[1]; 49 | return (React.createElement(LoadingContext.Provider, { value: [loadingState, dispatchLoadingAction] }, 50 | loadingState.mode !== "hidden" && (React.createElement(Loading, { notice: loadingState.msg, percent: loadingState.percent, indeterminate: loadingState.mode === "indeterminate" })), 51 | children)); 52 | }; 53 | export default AsyncLoading; 54 | //# sourceMappingURL=AsyncLoading.js.map -------------------------------------------------------------------------------- /dist/AsyncLoading.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"AsyncLoading.js","sourceRoot":"","sources":["../src/AsyncLoading.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,OAAO,EAAqB,aAAa,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAkBhF,IAAM,kBAAkB,GAAG,UACzB,KAAuB,EACvB,MAAyB;IAEzB,IAAI,CAAC,KAAK,CAAC,YAAY;QAAE,OAAO,KAAK,CAAA;IAErC,QAAQ,MAAM,CAAC,IAAI,EAAE;QACnB,KAAK,UAAU,CAAC;QAChB,KAAK,eAAe,CAAC;QACrB,KAAK,QAAQ,CAAC;QACd,KAAK,QAAQ;YACX,MAAK;QACP;YACE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;KACzC;IAED,IAAM,SAAS,GAAG;QAChB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,GAAG,EAAE,MAAM,CAAC,GAAG;QACf,OAAO,EAAE,MAAM,CAAC,OAAO,IAAI,CAAC;QAC5B,YAAY,EAAE,CAAC,MAAM,CAAC,WAAW;KAClC,CAAA;IAED,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA;AAQD,IAAM,cAAc,GAAG,aAAa,CAA0C;IAC5E,EAAE,IAAI,EAAE,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE;IAChC,cAAO,CAAC;CACT,CAAC,CAAA;AAEF,MAAM,CAAC,IAAM,UAAU,GAAG,cAAM,OAAA,UAAU,CAAC,cAAc,CAAC,EAA1B,CAA0B,CAAA;AAE1D,IAAM,OAAO,GAAoC,UAAC,EAIjD;QAHC,kBAAM,EACN,eAAW,EAAX,gCAAW,EACX,qBAAqB,EAArB,0CAAqB;IAErB,OAAO,CACL,6BAAK,EAAE,EAAC,QAAQ;QACb,aAAa,CAAC,CAAC,CAAC,CACf,6BAAK,EAAE,EAAC,sBAAsB,EAAC,aAAa,EAAE,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,cAAc,EAAE,EAAlB,CAAkB;YACnE,gCAAW;YACX,gCAAW;YACX,gCAAW;YACX,gCAAW;YACX,gCAAW;YACX,gCAAW;YACX,gCAAW;YACX,gCAAW,CACP,CACP,CAAC,CAAC,CAAC,CACF,6BAAK,EAAE,EAAC,iBAAiB,EAAC,aAAa,EAAE,UAAA,CAAC,IAAI,OAAA,CAAC,CAAC,cAAc,EAAE,EAAlB,CAAkB;YAC9D,6BACE,EAAE,EAAC,uBAAuB,EAC1B,KAAK,EAAE,EAAE,KAAK,EAAE,OAAO,GAAG,GAAG,EAAE,GAC1B,CACH,CACP;QACA,MAAM,IAAI,CACT,6BAAK,EAAE,EAAC,eAAe,EAAC,SAAS,EAAC,OAAO,IACtC,MAAM,CACH,CACP,CACG,CACP,CAAA;AACH,CAAC,CAAA;AAED,IAAM,YAAY,GAAsB,UAAC,EAAY;QAAV,sBAAQ;IAC3C,IAAA;;;MAGJ,EAHK,oBAAY,EAAE,6BAGnB,CAAA;IAEF,OAAO,CACL,oBAAC,cAAc,CAAC,QAAQ,IAAC,KAAK,EAAE,CAAC,YAAY,EAAE,qBAAqB,CAAC;QAClE,YAAY,CAAC,IAAI,KAAK,QAAQ,IAAI,CACjC,oBAAC,OAAO,IACN,MAAM,EAAE,YAAY,CAAC,GAAG,EACxB,OAAO,EAAE,YAAY,CAAC,OAAO,EAC7B,aAAa,EAAE,YAAY,CAAC,IAAI,KAAK,eAAe,GACpD,CACH;QACA,QAAQ,CACe,CAC3B,CAAA;AACH,CAAC,CAAA;AAED,eAAe,YAAY,CAAA"} -------------------------------------------------------------------------------- /dist/ReactCanvas.d.ts: -------------------------------------------------------------------------------- 1 | import { FunctionComponent } from "react"; 2 | export declare type ReactEngineProps = { 3 | engine: Engine; 4 | pck: string; 5 | width?: number; 6 | height?: number; 7 | params?: any; 8 | resize?: boolean; 9 | }; 10 | declare const ReactCanvas: FunctionComponent; 11 | export default ReactCanvas; 12 | -------------------------------------------------------------------------------- /dist/ReactCanvas.js: -------------------------------------------------------------------------------- 1 | import * as React from "react"; 2 | import { useEffect, useRef, useState } from "react"; 3 | import { useLoading } from "./AsyncLoading"; 4 | function toFailure(err) { 5 | var msg = err.message || err; 6 | console.error(msg); 7 | return { msg: msg, mode: "notice", initialized: true }; 8 | } 9 | var ReactCanvas = function (_a) { 10 | var engine = _a.engine, pck = _a.pck, _b = _a.width, width = _b === void 0 ? 480 : _b, _c = _a.height, height = _c === void 0 ? 300 : _c; 11 | var canvasRef = useRef(null); 12 | var _d = useState(), instance = _d[0], setInstance = _d[1]; 13 | var _e = useLoading(), loadingState = _e[0], changeLoadingState = _e[1]; 14 | useEffect(function () { 15 | if (engine.isWebGLAvailable()) { 16 | changeLoadingState({ mode: "indeterminate" }); 17 | setInstance(new engine()); 18 | } 19 | else { 20 | changeLoadingState(toFailure("WebGL not available")); 21 | } 22 | }, [engine]); 23 | useEffect(function () { 24 | if (instance) { 25 | instance 26 | .startGame(pck) 27 | .then(function () { 28 | changeLoadingState({ mode: "hidden", initialized: true }); 29 | }) 30 | .catch(function (err) { return changeLoadingState(toFailure(err)); }); 31 | instance.setProgressFunc(function (current, total) { 32 | if (total > 0) { 33 | changeLoadingState({ mode: "progress", percent: current / total }); 34 | } 35 | else { 36 | changeLoadingState({ mode: "indeterminate" }); 37 | } 38 | }); 39 | } 40 | }, [instance, pck, changeLoadingState]); 41 | useEffect(function () { 42 | if (instance) { 43 | instance.setCanvas(canvasRef.current); 44 | } 45 | }, [instance, canvasRef.current]); 46 | return (React.createElement("canvas", { ref: canvasRef, id: "canvas", width: width, height: height, style: { display: loadingState.initializing ? "hidden" : "block" } }, 47 | "HTML5 canvas appears to be unsupported in the current browser.", 48 | React.createElement("br", null), 49 | "Please try updating or use a different browser.")); 50 | }; 51 | export default ReactCanvas; 52 | //# sourceMappingURL=ReactCanvas.js.map -------------------------------------------------------------------------------- /dist/ReactCanvas.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"ReactCanvas.js","sourceRoot":"","sources":["../src/ReactCanvas.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,OAAO,EAAqB,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEtE,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAW3C,SAAS,SAAS,CAAC,GAAG;IACpB,IAAI,GAAG,GAAG,GAAG,CAAC,OAAO,IAAI,GAAG,CAAA;IAC5B,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAClB,OAAO,EAAE,GAAG,KAAA,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,CAAA;AACnD,CAAC;AAED,IAAM,WAAW,GAAwC,UAAC,EAKzD;QAJC,kBAAM,EACN,YAAG,EACH,aAAW,EAAX,gCAAW,EACX,cAAY,EAAZ,iCAAY;IAEZ,IAAM,SAAS,GAAG,MAAM,CAAoB,IAAI,CAAC,CAAA;IAC3C,IAAA,eAAoC,EAAnC,gBAAQ,EAAE,mBAAyB,CAAA;IACpC,IAAA,iBAAiD,EAAhD,oBAAY,EAAE,0BAAkC,CAAA;IAEvD,SAAS,CAAC;QACR,IAAI,MAAM,CAAC,gBAAgB,EAAE,EAAE;YAC7B,kBAAkB,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC,CAAA;YAC7C,WAAW,CAAC,IAAI,MAAM,EAAE,CAAC,CAAA;SAC1B;aAAM;YACL,kBAAkB,CAAC,SAAS,CAAC,qBAAqB,CAAC,CAAC,CAAA;SACrD;IACH,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAA;IAEZ,SAAS,CAAC;QACR,IAAI,QAAQ,EAAE;YACZ,QAAQ;iBACL,SAAS,CAAC,GAAG,CAAC;iBACd,IAAI,CAAC;gBACJ,kBAAkB,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAA;YAC3D,CAAC,CAAC;iBACD,KAAK,CAAC,UAAA,GAAG,IAAI,OAAA,kBAAkB,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,EAAlC,CAAkC,CAAC,CAAA;YAEnD,QAAQ,CAAC,eAAe,CAAC,UAAC,OAAO,EAAE,KAAK;gBACtC,IAAI,KAAK,GAAG,CAAC,EAAE;oBACb,kBAAkB,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,KAAK,EAAE,CAAC,CAAA;iBACnE;qBAAM;oBACL,kBAAkB,CAAC,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC,CAAA;iBAC9C;YACH,CAAC,CAAC,CAAA;SACH;IACH,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,kBAAkB,CAAC,CAAC,CAAA;IAEvC,SAAS,CAAC;QACR,IAAI,QAAQ,EAAE;YACZ,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA;SACtC;IACH,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC,CAAA;IAEjC,OAAO,CACL,gCACE,GAAG,EAAE,SAAS,EACd,EAAE,EAAC,QAAQ,EACX,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,EAAE,OAAO,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,EAAE;;QAGlE,+BAAM;0DAEC,CACV,CAAA;AACH,CAAC,CAAA;AAED,eAAe,WAAW,CAAA"} -------------------------------------------------------------------------------- /dist/index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @function ReactGodot 3 | */ 4 | import "./styles.css"; 5 | import { FunctionComponent } from "react"; 6 | export declare type ReactGodotProps = { 7 | script: EngineLoaderDescription; 8 | pck: string; 9 | resize?: boolean; 10 | width?: number; 11 | height?: number; 12 | params?: any; 13 | }; 14 | declare const ReactGodot: FunctionComponent; 15 | export default ReactGodot; 16 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @function ReactGodot 3 | */ 4 | import "./styles.css"; 5 | import * as React from "react"; 6 | import { useEffect, useRef, useState } from "react"; 7 | import AsyncLoading from "./AsyncLoading"; 8 | import ReactCanvas from "./ReactCanvas"; 9 | var useScript = function (url, onLoad) { 10 | useEffect(function () { 11 | var script = document.createElement("script"); 12 | script.src = url; 13 | script.async = true; 14 | script.onload = onLoad; 15 | document.body.appendChild(script); 16 | return function () { 17 | document.body.removeChild(script); 18 | }; 19 | }, [url]); 20 | }; 21 | var ReactGodot = function (props) { 22 | var script = props.script, pck = props.pck, _a = props.resize, resize = _a === void 0 ? false : _a, width = props.width, height = props.height, params = props.params; 23 | var outerRef = useRef(null); 24 | var _b = useState(null), engine = _b[0], setEngine = _b[1]; 25 | var _c = useState([width, height]), dimensions = _c[0], setDimensions = _c[1]; 26 | useScript(script, function () { 27 | var scope = window; 28 | setEngine(function () { return scope.Engine; }); 29 | }); 30 | useEffect(function () { 31 | if (resize && outerRef.current) { 32 | setDimensions([ 33 | outerRef.current.clientWidth, 34 | outerRef.current.clientHeight 35 | ]); 36 | } 37 | }, [resize, outerRef.current]); 38 | return (React.createElement("div", { id: "wrap", ref: outerRef }, 39 | React.createElement(AsyncLoading, null, engine && (React.createElement(ReactCanvas, { pck: pck, engine: engine, width: dimensions[0], height: dimensions[1], params: params }))))); 40 | }; 41 | export default ReactGodot; 42 | //# sourceMappingURL=index.js.map -------------------------------------------------------------------------------- /dist/index.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,cAAc,CAAA;AAErB,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,OAAO,EAAqB,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEtE,OAAO,YAAY,MAAM,gBAAgB,CAAA;AACzC,OAAO,WAAW,MAAM,eAAe,CAAA;AAEvC,IAAM,SAAS,GAAG,UAAC,GAAG,EAAE,MAAM;IAC5B,SAAS,CAAC;QACR,IAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;QAE/C,MAAM,CAAC,GAAG,GAAG,GAAG,CAAA;QAChB,MAAM,CAAC,KAAK,GAAG,IAAI,CAAA;QACnB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAA;QAEtB,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;QAEjC,OAAO;YACL,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;QACnC,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;AACX,CAAC,CAAA;AAWD,IAAM,UAAU,GAAuC,UAAA,KAAK;IAClD,IAAA,qBAAM,EAAE,eAAG,EAAE,iBAAc,EAAd,mCAAc,EAAE,mBAAK,EAAE,qBAAM,EAAE,qBAAM,CAAU;IACpE,IAAM,QAAQ,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAA;IACvC,IAAA,mBAA4C,EAA3C,cAAM,EAAE,iBAAmC,CAAA;IAC5C,IAAA,8BAAuD,EAAtD,kBAAU,EAAE,qBAA0C,CAAA;IAE7D,SAAS,CAAC,MAAM,EAAE;QAChB,IAAM,KAAK,GAAG,MAAa,CAAA;QAC3B,SAAS,CAAC,cAAM,OAAA,KAAK,CAAC,MAAM,EAAZ,CAAY,CAAC,CAAA;IAC/B,CAAC,CAAC,CAAA;IAEF,SAAS,CAAC;QACR,IAAI,MAAM,IAAI,QAAQ,CAAC,OAAO,EAAE;YAC9B,aAAa,CAAC;gBACZ,QAAQ,CAAC,OAAO,CAAC,WAAW;gBAC5B,QAAQ,CAAC,OAAO,CAAC,YAAY;aAC9B,CAAC,CAAA;SACH;IACH,CAAC,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAA;IAE9B,OAAO,CACL,6BAAK,EAAE,EAAC,MAAM,EAAC,GAAG,EAAE,QAAQ;QAC1B,oBAAC,YAAY,QACV,MAAM,IAAI,CACT,oBAAC,WAAW,IACV,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC,EACpB,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,EACrB,MAAM,EAAE,MAAM,GACd,CACH,CACY,CACX,CACP,CAAA;AACH,CAAC,CAAA;AAED,eAAe,UAAU,CAAA"} -------------------------------------------------------------------------------- /dist/styles.css: -------------------------------------------------------------------------------- 1 | #wrap { 2 | width: 100%; 3 | height: 100%; 4 | display: flex; 5 | align-items: center; 6 | justify-content: center; 7 | } 8 | 9 | #canvas { 10 | display: block; 11 | margin: 0; 12 | color: white; 13 | } 14 | 15 | #canvas:focus { 16 | outline: none; 17 | } 18 | 19 | .godot { 20 | font-family: "Noto Sans", "Droid Sans", Arial, sans-serif; 21 | color: #e0e0e0; 22 | background-color: #3b3943; 23 | background-image: linear-gradient(to bottom, #403e48, #35333c); 24 | border: 1px solid #45434e; 25 | box-shadow: 0 0 1px 1px #2f2d35; 26 | } 27 | 28 | /* Status display 29 | * ============== */ 30 | 31 | #status { 32 | position: absolute; 33 | left: 0; 34 | top: 0; 35 | right: 0; 36 | bottom: 0; 37 | display: flex; 38 | justify-content: center; 39 | align-items: center; 40 | /* don't consume click events - make children visible explicitly */ 41 | visibility: hidden; 42 | } 43 | 44 | #status-progress { 45 | width: 366px; 46 | height: 7px; 47 | background-color: #38363a; 48 | border: 1px solid #444246; 49 | padding: 1px; 50 | box-shadow: 0 0 2px 1px #1b1c22; 51 | border-radius: 2px; 52 | visibility: visible; 53 | } 54 | 55 | @media only screen and (orientation: portrait) { 56 | #status-progress { 57 | width: 61.8%; 58 | } 59 | } 60 | 61 | #status-progress-inner { 62 | height: 100%; 63 | width: 0; 64 | box-sizing: border-box; 65 | transition: width 0.5s linear; 66 | background-color: #202020; 67 | border: 1px solid #222223; 68 | box-shadow: 0 0 1px 1px #27282e; 69 | border-radius: 3px; 70 | } 71 | 72 | #status-indeterminate { 73 | visibility: visible; 74 | position: relative; 75 | } 76 | 77 | #status-indeterminate > div { 78 | width: 4.5px; 79 | height: 0; 80 | border-style: solid; 81 | border-width: 9px 3px 0 3px; 82 | border-color: #2b2b2b transparent transparent transparent; 83 | transform-origin: center 21px; 84 | position: absolute; 85 | animation: indeterminate-blink 800ms infinite; 86 | } 87 | 88 | #status-indeterminate > div:nth-child(1) { 89 | transform: rotate(22.5deg); 90 | animation-delay: 0ms; 91 | } 92 | #status-indeterminate > div:nth-child(2) { 93 | transform: rotate(67.5deg); 94 | animation-delay: 100ms; 95 | } 96 | #status-indeterminate > div:nth-child(3) { 97 | transform: rotate(112.5deg); 98 | animation-delay: 200ms; 99 | } 100 | #status-indeterminate > div:nth-child(4) { 101 | transform: rotate(157.5deg); 102 | animation-delay: 300ms; 103 | } 104 | #status-indeterminate > div:nth-child(5) { 105 | transform: rotate(202.5deg); 106 | animation-delay: 400ms; 107 | } 108 | #status-indeterminate > div:nth-child(6) { 109 | transform: rotate(247.5deg); 110 | animation-delay: 500ms; 111 | } 112 | #status-indeterminate > div:nth-child(7) { 113 | transform: rotate(292.5deg); 114 | animation-delay: 600ms; 115 | } 116 | #status-indeterminate > div:nth-child(8) { 117 | transform: rotate(337.5deg); 118 | animation-delay: 700ms; 119 | } 120 | 121 | @keyframes indeterminate-blink { 122 | 0% { 123 | border-top-color: #dfdfdf; 124 | } 125 | 126 | 12.5% { 127 | border-top-color: none; 128 | } 129 | } 130 | 131 | #status-notice { 132 | margin: 0 100px; 133 | line-height: 1.3; 134 | visibility: visible; 135 | padding: 4px 6px; 136 | visibility: visible; 137 | } 138 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | 46 | ### Code Splitting 47 | 48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 49 | 50 | ### Analyzing the Bundle Size 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 53 | 54 | ### Making a Progressive Web App 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 57 | 58 | ### Advanced Configuration 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 61 | 62 | ### Deployment 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 65 | 66 | ### `npm run build` fails to minify 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 69 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "react": "^16.12.0", 7 | "react-dom": "^16.12.0", 8 | "react-scripts": "3.3.0", 9 | "react-godot": "../" 10 | }, 11 | "devDependencies": { 12 | "typescript": "^3.7.4" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build" 17 | }, 18 | "eslintConfig": { 19 | "extends": "react-app" 20 | }, 21 | "browserslist": { 22 | "production": [ 23 | ">0.2%", 24 | "not dead", 25 | "not op_mini all" 26 | ], 27 | "development": [ 28 | "last 1 chrome version", 29 | "last 1 firefox version", 30 | "last 1 safari version" 31 | ] 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /example/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/d3dc/react-godot/9a628419bc5d71eb6fe503bc48d0431fd2c267f2/example/public/favicon.ico -------------------------------------------------------------------------------- /example/public/flappy/flappy.pck: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/d3dc/react-godot/9a628419bc5d71eb6fe503bc48d0431fd2c267f2/example/public/flappy/flappy.pck -------------------------------------------------------------------------------- /example/public/flappy/flappy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/d3dc/react-godot/9a628419bc5d71eb6fe503bc48d0431fd2c267f2/example/public/flappy/flappy.png -------------------------------------------------------------------------------- /example/public/flappy/flappy.wasm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/d3dc/react-godot/9a628419bc5d71eb6fe503bc48d0431fd2c267f2/example/public/flappy/flappy.wasm -------------------------------------------------------------------------------- /example/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /example/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/d3dc/react-godot/9a628419bc5d71eb6fe503bc48d0431fd2c267f2/example/public/logo192.png -------------------------------------------------------------------------------- /example/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/d3dc/react-godot/9a628419bc5d71eb6fe503bc48d0431fd2c267f2/example/public/logo512.png -------------------------------------------------------------------------------- /example/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /example/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | -------------------------------------------------------------------------------- /example/src/App.tsx: -------------------------------------------------------------------------------- 1 | import "react-godot/dist/styles.css" 2 | 3 | import React from "react" 4 | import ReactGodot from "react-godot" 5 | 6 | const examplePck = "/flappy/flappy.pck" 7 | const exampleEngine = "/flappy/flappy.js" 8 | 9 | function App() { 10 | return 11 | } 12 | 13 | export default App 14 | -------------------------------------------------------------------------------- /example/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 3 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 4 | sans-serif; 5 | -webkit-font-smoothing: antialiased; 6 | -moz-osx-font-smoothing: grayscale; 7 | touch-action: none; 8 | margin: 0; 9 | border: 0 none; 10 | padding: 0; 11 | text-align: center; 12 | background-color: black; 13 | } 14 | 15 | code { 16 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 17 | monospace; 18 | } 19 | -------------------------------------------------------------------------------- /example/src/index.tsx: -------------------------------------------------------------------------------- 1 | import "./index.css" 2 | 3 | import * as serviceWorker from "./serviceWorker" 4 | 5 | import App from "./App" 6 | import React from "react" 7 | import ReactDOM from "react-dom" 8 | 9 | ReactDOM.render(, document.getElementById("root")) 10 | 11 | // If you want your app to work offline and load faster, you can change 12 | // unregister() to register() below. Note this comes with some pitfalls. 13 | // Learn more about service workers: https://bit.ly/CRA-PWA 14 | serviceWorker.unregister() 15 | -------------------------------------------------------------------------------- /example/src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /example/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' } 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready.then(registration => { 134 | registration.unregister(); 135 | }); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "esModuleInterop": true, 8 | "allowSyntheticDefaultImports": true, 9 | "strict": true, 10 | "forceConsistentCasingInFileNames": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "noEmit": true, 16 | "jsx": "react" 17 | }, 18 | "include": ["src"] 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-godot", 3 | "version": "1.0.0", 4 | "description": "Load a webassembly build of the Godot engine and Bootstrap packed games from within the react component tree", 5 | "author": "d3dc", 6 | "license": "MIT", 7 | "repository": "d3dc/react-godot", 8 | "main": "dist/index.js", 9 | "module": "dist/index.es.js", 10 | "jsnext:main": "dist/index.es.js", 11 | "types": "dist/index.d.ts", 12 | "engines": { 13 | "node": ">=8", 14 | "npm": ">=5" 15 | }, 16 | "scripts": { 17 | "test": "react-scripts test", 18 | "build": "NODE_ENV=production tsc", 19 | "build:css": "cp src/styles.css dist/", 20 | "start": "NODE_ENV=development tsc -w", 21 | "prepare": "npm run build && npm run build:css", 22 | "predeploy": "cd example && npm install && npm run build" 23 | }, 24 | "dependencies": {}, 25 | "peerDependencies": { 26 | "react": "^16.12.0", 27 | "react-dom": "^16.12.0" 28 | }, 29 | "devDependencies": { 30 | "@testing-library/jest-dom": "^4.2.4", 31 | "@testing-library/react": "^9.4.0", 32 | "@testing-library/user-event": "^8.0.3", 33 | "@types/jest": "^24.0.25", 34 | "@types/react": "^16.9.17", 35 | "@types/react-dom": "^16.9.4", 36 | "react": "^16.12.0", 37 | "react-dom": "^16.12.0", 38 | "typescript": "^3.7.4" 39 | }, 40 | "files": [ 41 | "dist" 42 | ] 43 | } 44 | -------------------------------------------------------------------------------- /src/AsyncLoading.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react" 2 | 3 | import { FunctionComponent, createContext, useContext, useReducer } from "react" 4 | 5 | export type PackLoadingState = { 6 | mode: string 7 | initializing: boolean 8 | percent?: number 9 | msg?: string 10 | } 11 | 12 | export type PackLoadingAction = { 13 | msg?: string 14 | initialized?: boolean 15 | percent?: number 16 | mode: string 17 | } 18 | 19 | export type PackLoadingDispatch = (action: PackLoadingAction) => void 20 | 21 | const packLoadingReducer = ( 22 | state: PackLoadingState, 23 | action: PackLoadingAction 24 | ) => { 25 | if (!state.initializing) return state 26 | 27 | switch (action.mode) { 28 | case "progress": 29 | case "indeterminate": 30 | case "notice": 31 | case "hidden": 32 | break 33 | default: 34 | throw new Error("Invalid status mode") 35 | } 36 | 37 | const nextState = { 38 | mode: action.mode, 39 | msg: action.msg, 40 | percent: action.percent || 0, 41 | initializing: !action.initialized 42 | } 43 | 44 | return nextState 45 | } 46 | 47 | type LoadingProps = { 48 | notice?: string 49 | percent?: number 50 | indeterminate: boolean 51 | } 52 | 53 | const LoadingContext = createContext<[PackLoadingState, PackLoadingDispatch]>([ 54 | { mode: "", initializing: true }, 55 | () => {} 56 | ]) 57 | 58 | export const useLoading = () => useContext(LoadingContext) 59 | 60 | const Loading: FunctionComponent = ({ 61 | notice, 62 | percent = 0, 63 | indeterminate = false 64 | }) => { 65 | return ( 66 |
67 | {indeterminate ? ( 68 |
e.preventDefault()}> 69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | ) : ( 79 |
e.preventDefault()}> 80 |
84 |
85 | )} 86 | {notice && ( 87 |
88 | {notice} 89 |
90 | )} 91 |
92 | ) 93 | } 94 | 95 | const AsyncLoading: FunctionComponent = ({ children }) => { 96 | const [loadingState, dispatchLoadingAction] = useReducer(packLoadingReducer, { 97 | mode: "indeterminate", 98 | initializing: true 99 | }) 100 | 101 | return ( 102 | 103 | {loadingState.mode !== "hidden" && ( 104 | 109 | )} 110 | {children} 111 | 112 | ) 113 | } 114 | 115 | export default AsyncLoading 116 | -------------------------------------------------------------------------------- /src/ReactCanvas.tsx: -------------------------------------------------------------------------------- 1 | import * as React from "react" 2 | 3 | import { FunctionComponent, useEffect, useRef, useState } from "react" 4 | 5 | import { useLoading } from "./AsyncLoading" 6 | 7 | export type ReactEngineProps = { 8 | engine: Engine 9 | pck: string 10 | width?: number 11 | height?: number 12 | params?: any 13 | resize?: boolean 14 | } 15 | 16 | function toFailure(err) { 17 | var msg = err.message || err 18 | console.error(msg) 19 | return { msg, mode: "notice", initialized: true } 20 | } 21 | 22 | const ReactCanvas: FunctionComponent = ({ 23 | engine, 24 | pck, 25 | width = 480, 26 | height = 300 27 | }) => { 28 | const canvasRef = useRef(null) 29 | const [instance, setInstance] = useState() 30 | const [loadingState, changeLoadingState] = useLoading() 31 | 32 | useEffect(() => { 33 | if (engine.isWebGLAvailable()) { 34 | changeLoadingState({ mode: "indeterminate" }) 35 | setInstance(new engine()) 36 | } else { 37 | changeLoadingState(toFailure("WebGL not available")) 38 | } 39 | }, [engine]) 40 | 41 | useEffect(() => { 42 | if (instance) { 43 | instance 44 | .startGame(pck) 45 | .then(() => { 46 | changeLoadingState({ mode: "hidden", initialized: true }) 47 | }) 48 | .catch(err => changeLoadingState(toFailure(err))) 49 | 50 | instance.setProgressFunc((current, total) => { 51 | if (total > 0) { 52 | changeLoadingState({ mode: "progress", percent: current / total }) 53 | } else { 54 | changeLoadingState({ mode: "indeterminate" }) 55 | } 56 | }) 57 | } 58 | }, [instance, pck, changeLoadingState]) 59 | 60 | useEffect(() => { 61 | if (instance) { 62 | instance.setCanvas(canvasRef.current) 63 | } 64 | }, [instance, canvasRef.current]) 65 | 66 | return ( 67 | 74 | HTML5 canvas appears to be unsupported in the current browser. 75 |
76 | Please try updating or use a different browser. 77 |
78 | ) 79 | } 80 | 81 | export default ReactCanvas 82 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | /** 2 | * @function ReactGodot 3 | */ 4 | 5 | import "./styles.css" 6 | 7 | import * as React from "react" 8 | 9 | import { FunctionComponent, useEffect, useRef, useState } from "react" 10 | 11 | import AsyncLoading from "./AsyncLoading" 12 | import ReactCanvas from "./ReactCanvas" 13 | 14 | const useScript = (url, onLoad) => { 15 | useEffect(() => { 16 | const script = document.createElement("script") 17 | 18 | script.src = url 19 | script.async = true 20 | script.onload = onLoad 21 | 22 | document.body.appendChild(script) 23 | 24 | return () => { 25 | document.body.removeChild(script) 26 | } 27 | }, [url]) 28 | } 29 | 30 | export type ReactGodotProps = { 31 | script: EngineLoaderDescription 32 | pck: string 33 | resize?: boolean 34 | width?: number 35 | height?: number 36 | params?: any 37 | } 38 | 39 | const ReactGodot: FunctionComponent = props => { 40 | const { script, pck, resize = false, width, height, params } = props 41 | const outerRef = useRef(null) 42 | const [engine, setEngine] = useState(null) 43 | const [dimensions, setDimensions] = useState([width, height]) 44 | 45 | useScript(script, () => { 46 | const scope = window as any 47 | setEngine(() => scope.Engine) 48 | }) 49 | 50 | useEffect(() => { 51 | if (resize && outerRef.current) { 52 | setDimensions([ 53 | outerRef.current.clientWidth, 54 | outerRef.current.clientHeight 55 | ]) 56 | } 57 | }, [resize, outerRef.current]) 58 | 59 | return ( 60 |
61 | 62 | {engine && ( 63 | 70 | )} 71 | 72 |
73 | ) 74 | } 75 | 76 | export default ReactGodot 77 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /src/styles.css: -------------------------------------------------------------------------------- 1 | #wrap { 2 | width: 100%; 3 | height: 100%; 4 | display: flex; 5 | align-items: center; 6 | justify-content: center; 7 | } 8 | 9 | #canvas { 10 | display: block; 11 | margin: 0; 12 | color: white; 13 | } 14 | 15 | #canvas:focus { 16 | outline: none; 17 | } 18 | 19 | .godot { 20 | font-family: "Noto Sans", "Droid Sans", Arial, sans-serif; 21 | color: #e0e0e0; 22 | background-color: #3b3943; 23 | background-image: linear-gradient(to bottom, #403e48, #35333c); 24 | border: 1px solid #45434e; 25 | box-shadow: 0 0 1px 1px #2f2d35; 26 | } 27 | 28 | /* Status display 29 | * ============== */ 30 | 31 | #status { 32 | position: absolute; 33 | left: 0; 34 | top: 0; 35 | right: 0; 36 | bottom: 0; 37 | display: flex; 38 | justify-content: center; 39 | align-items: center; 40 | /* don't consume click events - make children visible explicitly */ 41 | visibility: hidden; 42 | } 43 | 44 | #status-progress { 45 | width: 366px; 46 | height: 7px; 47 | background-color: #38363a; 48 | border: 1px solid #444246; 49 | padding: 1px; 50 | box-shadow: 0 0 2px 1px #1b1c22; 51 | border-radius: 2px; 52 | visibility: visible; 53 | } 54 | 55 | @media only screen and (orientation: portrait) { 56 | #status-progress { 57 | width: 61.8%; 58 | } 59 | } 60 | 61 | #status-progress-inner { 62 | height: 100%; 63 | width: 0; 64 | box-sizing: border-box; 65 | transition: width 0.5s linear; 66 | background-color: #202020; 67 | border: 1px solid #222223; 68 | box-shadow: 0 0 1px 1px #27282e; 69 | border-radius: 3px; 70 | } 71 | 72 | #status-indeterminate { 73 | visibility: visible; 74 | position: relative; 75 | } 76 | 77 | #status-indeterminate > div { 78 | width: 4.5px; 79 | height: 0; 80 | border-style: solid; 81 | border-width: 9px 3px 0 3px; 82 | border-color: #2b2b2b transparent transparent transparent; 83 | transform-origin: center 21px; 84 | position: absolute; 85 | animation: indeterminate-blink 800ms infinite; 86 | } 87 | 88 | #status-indeterminate > div:nth-child(1) { 89 | transform: rotate(22.5deg); 90 | animation-delay: 0ms; 91 | } 92 | #status-indeterminate > div:nth-child(2) { 93 | transform: rotate(67.5deg); 94 | animation-delay: 100ms; 95 | } 96 | #status-indeterminate > div:nth-child(3) { 97 | transform: rotate(112.5deg); 98 | animation-delay: 200ms; 99 | } 100 | #status-indeterminate > div:nth-child(4) { 101 | transform: rotate(157.5deg); 102 | animation-delay: 300ms; 103 | } 104 | #status-indeterminate > div:nth-child(5) { 105 | transform: rotate(202.5deg); 106 | animation-delay: 400ms; 107 | } 108 | #status-indeterminate > div:nth-child(6) { 109 | transform: rotate(247.5deg); 110 | animation-delay: 500ms; 111 | } 112 | #status-indeterminate > div:nth-child(7) { 113 | transform: rotate(292.5deg); 114 | animation-delay: 600ms; 115 | } 116 | #status-indeterminate > div:nth-child(8) { 117 | transform: rotate(337.5deg); 118 | animation-delay: 700ms; 119 | } 120 | 121 | @keyframes indeterminate-blink { 122 | 0% { 123 | border-top-color: #dfdfdf; 124 | } 125 | 126 | 12.5% { 127 | border-top-color: none; 128 | } 129 | } 130 | 131 | #status-notice { 132 | margin: 0 100px; 133 | line-height: 1.3; 134 | visibility: visible; 135 | padding: 4px 6px; 136 | visibility: visible; 137 | } 138 | -------------------------------------------------------------------------------- /src/typings.d.ts: -------------------------------------------------------------------------------- 1 | type Engine = any 2 | 3 | type EngineLoaderDescription = string 4 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "dist", 4 | "module": "esnext", 5 | "target": "es5", 6 | "lib": ["es6", "dom", "es2016", "es2017"], 7 | "sourceMap": true, 8 | "allowJs": false, 9 | "jsx": "react", 10 | "declaration": true, 11 | "moduleResolution": "node", 12 | "forceConsistentCasingInFileNames": true, 13 | "noImplicitReturns": true, 14 | "noImplicitThis": true, 15 | "strictNullChecks": true, 16 | "suppressImplicitAnyIndexErrors": true, 17 | "noUnusedLocals": true, 18 | "noUnusedParameters": true 19 | }, 20 | "include": ["src"], 21 | "exclude": ["node_modules", "dist", "example"] 22 | } 23 | -------------------------------------------------------------------------------- /tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "module": "commonjs" 5 | } 6 | } --------------------------------------------------------------------------------