Skip to content

Commit

Permalink
feat: Check for global PIXI object
Browse files Browse the repository at this point in the history
Adds a "Patch render engine" button.
Which makes playing with the examples easier.

#98
  • Loading branch information
Bob Fanger committed Mar 18, 2023
1 parent 788d40e commit ad874a6
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 52 deletions.
59 changes: 59 additions & 0 deletions packages/blender-elements/Button.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<script lang="ts">
export let value: boolean | undefined = undefined;
export let location: "ALONE" | "LEFT" | "CENTER" | "RIGHT" = "ALONE";
function onToggle() {
if (typeof value === "boolean") {
value = !value;
}
}
</script>

<button
class="button"
class:pressed={value}
data-location={location}
on:click={onToggle}
on:click|stopPropagation
on:dblclick|stopPropagation={() => {}}><slot /></button
>

<style lang="scss">
.button {
appearance: none;
background: #545454 no-repeat center center;
color: #e6e6e6;
border: 1px solid #3d3d3d;
padding-inline: 8px;
height: 18px;
flex-shrink: 0;
box-shadow: 0 1px 1px rgba(black, 0.6);
cursor: pointer;
&[data-location="ALONE"] {
border-radius: 2px / 3px;
}
&[data-location="LEFT"] {
border-top-left-radius: 2px 3px;
border-bottom-left-radius: 2px 3px;
margin-right: 1px;
}
&[data-location="CENTER"] {
margin-right: 1px;
}
&[data-location="RIGHT"] {
border-top-right-radius: 2px 3px;
border-bottom-right-radius: 2px 3px;
}
&:hover {
background: #656565;
color: #ffffff;
}
&.pressed,
&:active {
background-color: #4772b3;
color: #ffffff;
}
}
</style>
2 changes: 1 addition & 1 deletion packages/blender-elements/Toggle.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
title={hint}
on:click={onToggle}
on:click|stopPropagation
on:dblclick|stopPropagation
on:dblclick|stopPropagation={() => {}}
/>

<style lang="scss">
Expand Down
6 changes: 6 additions & 0 deletions packages/blender-elements/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ const svg = {
<path opacity="0.5" d="M9.51559 1C9.23947 1.00003 9.01563 1.22387 9.01559 1.5V2H7.51559C7.18694 2.005 6.99997 2.25232 6.99997 2.5V3H9.51559H10.5156H13V2.5C13 2.24733 12.8509 2.00474 12.5156 2H11.0156V1.5C11.0156 1.22387 10.7917 1.00003 10.5156 1H9.51559ZM13 3L13.0156 3.5V4H7.01556V3.5L6.99997 3H5.51559C5.23947 3.00003 5.01563 3.22387 5.01559 3.5V7H7.32419C7.37794 6.9925 7.43205 6.98793 7.4863 6.9863C7.70347 6.98018 7.91938 7.02131 8.11909 7.10684C8.31881 7.19237 8.49757 7.32027 8.64301 7.48167C8.78844 7.64308 8.89708 7.83414 8.96141 8.04166C9.02574 8.24917 9.04423 8.4682 9.01559 8.68356V13H14.5156C14.7917 13 15.0156 12.7761 15.0156 12.5V3.5C15.0156 3.22387 14.7917 3.00003 14.5156 3H13Z" fill="white"/>
<path d="M7.50582 7.99609C7.48883 7.99656 7.47188 7.99789 7.45503 8.00009H3.51556C3.4493 7.99915 3.38353 8.01139 3.32205 8.0361C3.26056 8.06081 3.2046 8.09749 3.15742 8.14401C3.11023 8.19053 3.07276 8.24597 3.04719 8.3071C3.02162 8.36822 3.00845 8.43383 3.00845 8.50009C3.00845 8.56635 3.02162 8.63195 3.04719 8.69308C3.07276 8.75421 3.11023 8.80965 3.15742 8.85617C3.2046 8.90269 3.26056 8.93937 3.32205 8.96408C3.38353 8.98878 3.4493 9.00103 3.51556 9.00009H6.30853L1.16206 14.1466C1.11409 14.1926 1.07579 14.2478 1.04942 14.3089C1.02304 14.3699 1.00912 14.4357 1.00845 14.5022C1.00778 14.5687 1.02038 14.6346 1.04553 14.6962C1.07067 14.7578 1.10786 14.8137 1.15489 14.8608C1.20192 14.9078 1.25785 14.945 1.31942 14.9701C1.381 14.9953 1.44697 15.0079 1.51348 15.0072C1.57999 15.0065 1.64571 14.9926 1.70677 14.9662C1.76783 14.9399 1.82301 14.9016 1.86909 14.8536L7.01556 9.70712V12.5C7.01462 12.5663 7.02686 12.632 7.05157 12.6935C7.07628 12.755 7.11296 12.811 7.15948 12.8582C7.206 12.9053 7.26144 12.9428 7.32257 12.9684C7.38369 12.994 7.4493 13.0071 7.51556 13.0071C7.58182 13.0071 7.64742 12.994 7.70855 12.9684C7.76968 12.9428 7.82512 12.9053 7.87164 12.8582C7.91816 12.811 7.95484 12.755 7.97955 12.6935C8.00425 12.632 8.0165 12.5663 8.01556 12.5V8.55664C8.02429 8.48525 8.01752 8.41281 7.99571 8.34428C7.97389 8.27575 7.93754 8.21274 7.88915 8.15953C7.84077 8.10632 7.78148 8.06417 7.71533 8.03596C7.64917 8.00776 7.5777 7.99416 7.50581 7.99609H7.50582Z" fill="white"/>
</svg>`,
warning: `<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M8.01168 0.99997C7.32174 0.99597 6.68595 1.3851 6.37691 2.00192L1.18941 12.373C0.595165 13.5605 1.48251 14.9992 2.81051 15H13.1894C14.5167 14.9992 15.4028 13.5621 14.8105 12.375L9.62301 2.00192C9.31771 1.39255 8.69329 1.00457 8.01168 0.99997ZM6.99996 3.99997H8.99996V9.99997H6.99996V3.99997ZM6.99996 11H8.99996V13H6.99996V11Z" fill="white"/>
</svg>`,
error: `<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M8 1C4.13992 1 1 4.13991 1 8C1 11.8601 4.13992 15 8 15C11.8601 15 15 11.8601 15 8C15 4.13991 11.8601 1 8 1ZM10.9902 3.98633C11.1917 3.98158 11.3898 4.0378 11.5588 4.14758C11.7277 4.25736 11.8595 4.41558 11.937 4.60156C12.0145 4.78755 12.034 4.99263 11.993 5.18988C11.9519 5.38713 11.8523 5.56738 11.707 5.70703L9.41406 8L11.707 10.293C11.803 10.3851 11.8796 10.4955 11.9324 10.6176C11.9852 10.7397 12.0131 10.8712 12.0145 11.0042C12.0158 11.1373 11.9906 11.2693 11.9403 11.3925C11.89 11.5156 11.8157 11.6275 11.7216 11.7216C11.6275 11.8157 11.5156 11.89 11.3924 11.9403C11.2692 11.9906 11.1373 12.0158 11.0042 12.0145C10.8712 12.0131 10.7397 11.9852 10.6176 11.9324C10.4955 11.8796 10.3851 11.803 10.293 11.707L8 9.41406L5.70703 11.707C5.61489 11.803 5.50452 11.8796 5.38239 11.9324C5.26026 11.9852 5.12882 12.0131 4.99577 12.0145C4.86273 12.0158 4.73076 11.9906 4.60757 11.9403C4.48439 11.89 4.37249 11.8157 4.2784 11.7216C4.18432 11.6275 4.10996 11.5156 4.05967 11.3925C4.00938 11.2693 3.98418 11.1373 3.98553 11.0042C3.98689 10.8712 4.01478 10.7397 4.06757 10.6176C4.12036 10.4955 4.19699 10.3851 4.29297 10.293L6.58594 8L4.29297 5.70703C4.15041 5.56828 4.05246 5.3902 4.01166 5.1955C3.97085 5.00079 3.98904 4.79833 4.0639 4.61401C4.13875 4.4297 4.26688 4.27188 4.43189 4.16077C4.5969 4.04965 4.7913 3.99029 4.99023 3.99023C5.12388 3.99024 5.25617 4.01703 5.37928 4.06903C5.5024 4.12103 5.61384 4.19717 5.70703 4.29297L8 6.58594L10.293 4.29297C10.4758 4.10278 10.7265 3.99256 10.9902 3.98633V3.98633Z" fill="white"/>
</svg>`,
};
export type Icon = keyof typeof svg;
export const icons = Object.keys(svg) as Icon[];
Expand Down
23 changes: 0 additions & 23 deletions packages/pixi-panel/src/ErrorMessage.svelte

This file was deleted.

10 changes: 2 additions & 8 deletions packages/pixi-panel/src/Instructions.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import Toggle from "blender-elements/Toggle.svelte";
import { getBridgeContext } from "./bridge-fns";
import ErrorMessage from "./ErrorMessage.svelte";
const bridge = getBridgeContext();
Expand All @@ -11,10 +10,6 @@
</script>

<div class="instructions">
<div class="intro">
<ErrorMessage>No Application or Game configured for debugging.</ErrorMessage
>
</div>
<div class="engines">
<div>
Using <strong class="pixi">PixiJS</strong>?<br />
Expand All @@ -25,6 +20,7 @@
<div class="copy">
<Toggle
icon="copy"
hint="Copy to clipboard"
transparent
on:click={() => onCopy("globalThis.__PIXI_APP__ = app;")}
/>
Expand All @@ -47,6 +43,7 @@
><div class="copy">
<Toggle
icon="copy"
hint="Copy to clipboard"
transparent
on:click={() => onCopy("globalThis.__PHASER_GAME__ = game;")}
/>
Expand All @@ -61,9 +58,6 @@
.instructions {
padding: 12px;
}
.intro {
margin-bottom: 16px;
}
code {
background-color: #202020;
color: rgb(145, 168, 203);
Expand Down
52 changes: 49 additions & 3 deletions packages/pixi-panel/src/PixiPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@
import Base from "blender-elements/Base.svelte";
import Properties from "./Properties.svelte";
import Instructions from "./Instructions.svelte";
import Warning from "./Warning.svelte";
import patchPixi from "./patchPixi";
import Button from "blender-elements/Button.svelte";
export let bridge: BridgeFn;
let refresh: () => void;
const connection = connect(bridge);
const { error } = connection;
setBridgeContext(<T>(code: string) =>
bridge<T>(code).catch((err) => {
connection.retry();
throw err;
})
);
async function applyPatch() {
await patchPixi(bridge);
await connection.retry();
}
</script>

<Base>
{#if $connection}
{#if $connection === "CONNECTED"}
<div class="pixi-panel">
<div class="outliner">
<SceneGraph on:activate={refresh} />
Expand All @@ -31,8 +39,31 @@
<Properties bind:refresh />
</div>
</div>
{:else}
<Instructions />
{:else if $connection !== "INJECT"}
<div class="not-connected">
<div class="instructions">
<Instructions />
</div>
<div class="status">
{#if $connection === "NOT_FOUND"}
<Warning>No Application or Game configured for debugging</Warning>
{:else if $connection === "DISCONNECTED"}
<Warning>Connection lost</Warning>
{:else if $connection === "PATCHABLE"}
<div class="patch">
<Button on:click={applyPatch}>Patch render engine</Button>
</div>
<Warning
>"Patch render engine" is available. This type of Devtools
connection is less reliable</Warning
>
{:else if $connection === "ERROR"}
<Warning icon="error">{$error}</Warning>
{:else}
<Warning icon="error">{$connection}</Warning>
{/if}
</div>
</div>
{/if}
</Base>

Expand Down Expand Up @@ -67,4 +98,19 @@
background: #303030;
overflow: auto;
}
.patch {
margin: 4px 12px;
}
.not-connected {
display: flex;
flex-direction: column;
height: 100%;
}
.instructions {
flex: 1;
overflow: auto;
}
.status {
margin: 0 2px 2px 2px;
}
</style>
5 changes: 2 additions & 3 deletions packages/pixi-panel/src/SceneGraph.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { createEventDispatcher } from "svelte";
import SearchInput from "blender-elements/SearchInput.svelte";
import { getBridgeContext, poll } from "./bridge-fns";
import ReloadButton from "./ReloadButton.svelte";
import Tree from "./Tree.svelte";
import type { OutlinerNode } from "./types";
import Warning from "./Warning.svelte";
const dispatch = createEventDispatcher();
const bridge = getBridgeContext();
Expand Down Expand Up @@ -61,8 +61,7 @@
</div>
<div class="body">
{#if error}
{error.message}
<ReloadButton />
<Warning>{error.message}</Warning>
{/if}
{#if stage}
<Tree
Expand Down
37 changes: 37 additions & 0 deletions packages/pixi-panel/src/Warning.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script lang="ts">
export let icon: "warning" | "error" = "warning";
export let message = "";
</script>

<div class="warning" data-icon={icon}>
{message}<slot />
</div>

<style lang="scss">
.warning {
background: #472722;
color: #e1e1e1;
padding: 2px 4px 4px 30px;
position: relative;
border: 1px solid #3d3d3d;
border-radius: 3px;
overflow: hidden;
min-height: 16px;
&::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 24px;
height: 100%;
background: #c1432a center center no-repeat;
}
&[data-icon="warning"]::before {
background-image: var(--icon-warning);
}
&[data-icon="error"]::before {
background-image: var(--icon-error);
}
}
</style>
37 changes: 26 additions & 11 deletions packages/pixi-panel/src/connect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-underscore-dangle */
import { derived, Readable } from "svelte/store";
import { derived, Readable, writable } from "svelte/store";
import type { BridgeFn } from "./types";
import pixiDevtools from "./pixi-devtools/pixiDevtools";
import pixiDevtoolsViewport from "./pixi-devtools/pixiDevtoolsViewport";
Expand All @@ -14,7 +14,7 @@ function detect() {
const win = window as any;
function hasGlobal(varname: string) {
if (win[varname]) {
return win[varname];
return true;
}
if (win.frames) {
for (let i = 0; i < win.frames.length; i += 1) {
Expand All @@ -33,35 +33,49 @@ function detect() {
hasGlobal("__PIXI_APP__") ||
hasGlobal("__PHASER_GAME__") ||
hasGlobal("__PIXI_STAGE__") ||
hasGlobal("__PIXI_RENDERER__");
hasGlobal("__PIXI_RENDERER__") ||
hasGlobal("__PATCHED_RENDERER__");

if (win.__PIXI_DEVTOOLS__ !== undefined) {
if (detected) {
return "CONNECTED";
}
if (hasGlobal("PIXI")) {
return "PATCHABLE";
}
return "DISCONNECTED";
}
if (detected) {
return "INJECT";
}
if (hasGlobal("PIXI")) {
return "PATCHABLE";
}
return "NOT_FOUND";
}

export default function connect(
bridge: BridgeFn
): Readable<boolean> & { retry: () => void } {
export default function connect(bridge: BridgeFn): Readable<
"NOT_FOUND" | "INJECT" | "CONNECTED" | "DISCONNECTED" | "PATCHABLE" | "ERROR"
> & {
error: Readable<Error | undefined>;
retry: () => void;
} {
const detected = poll<ReturnType<typeof detect>>(
bridge,
`(${detect.toString()}())`,

2500
);
const errorStore = writable<Error | undefined>();
const readable = derived(detected, ({ data, error }) => {
if (error) {
if (error || typeof data === "undefined") {
console.warn(error);
}
if (data === "CONNECTED") {
return true;
if (error?.message === "Operation failed: %s") {
errorStore.set(new Error("Connection failed"));
} else {
errorStore.set(error);
}
return "ERROR";
}
if (data === "INJECT") {
bridge(`(() => {
Expand All @@ -74,10 +88,11 @@ export default function connect(
})();`).then(() => detected.sync());
}

return false;
return data;
});
return {
subscribe: readable.subscribe,
error: { subscribe: errorStore.subscribe },
retry() {
detected.sync();
},
Expand Down
Loading

0 comments on commit ad874a6

Please sign in to comment.