@stanko/kaplay-inspector 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,6 +14,7 @@ A dev tool for [Kaplay](https://kaplayjs.com/) which allows you to explore and i
14
14
  - Tweak position, text or color
15
15
  - Pause objects
16
16
  - Hide objects
17
+ - Search for tags or comps
17
18
  - Dark theme (is this a feature?)
18
19
 
19
20
  The layout is made with desktop in mind. That said, it is somewhat usable on phones.
@@ -1,19 +1,24 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "preact/jsx-runtime";
2
- import { useEffect, useState } from "preact/hooks";
2
+ import { useEffect, useRef, useState } from "preact/hooks";
3
3
  import { GameObject } from "./game-object";
4
4
  import { useObjectBoolean } from "./boolean-comp";
5
+ import { SearchResults } from "./search-results";
5
6
  const INTERVAL_OPTIONS = [
6
7
  { value: 100, label: "100ms" },
7
8
  { value: 250, label: "250ms" },
8
9
  { value: 500, label: "500ms" },
9
10
  { value: 1000, label: "1s" },
10
11
  ];
12
+ const TYPING_TIMEOUT = 250;
11
13
  export const Inspector = ({ initUpdateTimeout = 250, isVisibleOnLoad = true, initDrawInspectOnHover = true, k, }) => {
12
14
  const [updateTimeout, setUpdateTimeout] = useState(initUpdateTimeout);
13
15
  const [shouldDrawInspect, setShouldDrawInspect] = useState(initDrawInspectOnHover);
14
16
  const [root, setRoot] = useState(k.getTreeRoot());
15
17
  const [renderIndex, setRenderIndex] = useState(0);
16
18
  const [isVisible, setIsVisible] = useState(isVisibleOnLoad);
19
+ const [searchTerm, setSearchTerm] = useState("");
20
+ const [searchResults, setSearchResults] = useState([]);
21
+ const typingTimeout = useRef();
17
22
  const paused = useObjectBoolean(k.getTreeRoot(), "paused");
18
23
  // Force re-render every updateTimeout milliseconds
19
24
  useEffect(() => {
@@ -22,15 +27,22 @@ export const Inspector = ({ initUpdateTimeout = 250, isVisibleOnLoad = true, ini
22
27
  }, updateTimeout);
23
28
  return () => clearInterval(interval);
24
29
  }, [renderIndex, updateTimeout]);
25
- // const handlePauseClick = () => {
26
- // const root = k.getTreeRoot();
27
- // root.paused = !root.paused;
28
- // };
29
30
  const toggleVisibility = () => {
30
31
  setIsVisible(!isVisible);
31
32
  };
33
+ const search = (term) => {
34
+ setSearchResults(k.get(term, { recursive: true, liveUpdate: true }));
35
+ };
36
+ const handleSearchInput = (e) => {
37
+ const term = e.target.value;
38
+ setSearchTerm(term);
39
+ clearTimeout(typingTimeout.current);
40
+ typingTimeout.current = setTimeout(() => {
41
+ search(term);
42
+ }, TYPING_TIMEOUT);
43
+ };
32
44
  if (!isVisible) {
33
45
  return (_jsx("button", { class: "ki-btn k-inspector__show", onClick: toggleVisibility, children: "Show Inspector" }));
34
46
  }
35
- return (_jsxs(_Fragment, { children: [_jsxs("div", { class: "k-inspector__header", children: [_jsx("button", { class: "ki-btn", onClick: () => paused.onChange(!paused.checked), children: paused.checked ? "Resume Game" : "Pause Game" }), "\u2022", _jsxs("div", { children: [k.get("*", { recursive: true }).length, " objects"] }), "\u2022", _jsxs("div", { children: [Math.round(k.debug.fps()), " fps"] }), "\u2022", _jsxs("div", { class: "k-inspector__interval", children: ["Update:", INTERVAL_OPTIONS.map((option) => (_jsxs("label", { children: [_jsx("input", { type: "radio", name: "interval", value: option.value, checked: updateTimeout === option.value, onChange: () => setUpdateTimeout(option.value) }), option.label] }, option.value)))] }), "\u2022", _jsxs("label", { children: [_jsx("input", { type: "checkbox", checked: shouldDrawInspect, onChange: () => setShouldDrawInspect(!shouldDrawInspect) }), "Draw bbox on hover"] }), _jsx("button", { class: "ki-btn k-inspector__hide", onClick: toggleVisibility, children: "Hide" })] }), _jsx("div", { class: "k-inspector__objects", children: _jsx(GameObject, { k: k, className: "game-object--root", obj: root, setRenderRoot: setRoot, shouldDrawInspect: shouldDrawInspect, isExpanded: true, isRenderRoot: true }) })] }));
47
+ return (_jsxs(_Fragment, { children: [_jsxs("div", { class: "k-inspector__header", children: [_jsx("button", { class: "ki-btn", onClick: () => paused.onChange(!paused.checked), children: paused.checked ? "Resume Game" : "Pause Game" }), "\u2022", _jsx("input", { placeholder: "Search tags or comps", type: "text", class: "ki-input", onInput: handleSearchInput }), "\u2022", _jsxs("div", { children: [k.get("*", { recursive: true }).length, " objects"] }), "\u2022", _jsxs("div", { children: [Math.round(k.debug.fps()), " fps"] }), "\u2022", _jsxs("div", { class: "k-inspector__interval", children: ["Update:", INTERVAL_OPTIONS.map((option) => (_jsxs("label", { children: [_jsx("input", { type: "radio", name: "interval", value: option.value, checked: updateTimeout === option.value, onChange: () => setUpdateTimeout(option.value) }), option.label] }, option.value)))] }), "\u2022", _jsxs("label", { children: [_jsx("input", { type: "checkbox", checked: shouldDrawInspect, onChange: () => setShouldDrawInspect(!shouldDrawInspect) }), "Draw bbox on hover"] }), _jsx("button", { class: "ki-btn k-inspector__hide", onClick: toggleVisibility, children: "Hide" })] }), _jsx("div", { class: "k-inspector__objects", children: searchTerm.length > 0 ? (_jsx(SearchResults, { k: k, results: searchResults, setRenderRoot: setRoot, shouldDrawInspect: shouldDrawInspect })) : (_jsx(GameObject, { k: k, className: "game-object--root", obj: root, setRenderRoot: setRoot, shouldDrawInspect: shouldDrawInspect, isExpanded: true, isRenderRoot: true })) })] }));
36
48
  };
@@ -0,0 +1,10 @@
1
+ import type { GameObj } from "kaplay";
2
+ import type { KAPLAYCtxType } from "../init";
3
+ export interface SearchResultsProps {
4
+ k: KAPLAYCtxType;
5
+ className?: string;
6
+ results: GameObj[];
7
+ setRenderRoot: (obj: GameObj) => void;
8
+ shouldDrawInspect: boolean;
9
+ }
10
+ export declare const SearchResults: ({ k, results, setRenderRoot, shouldDrawInspect, }: SearchResultsProps) => import("preact").JSX.Element;
@@ -0,0 +1,10 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
2
+ import { GameObject } from "./game-object";
3
+ export const SearchResults = ({ k, results, setRenderRoot, shouldDrawInspect, }) => {
4
+ if (results.length === 0) {
5
+ return _jsx("div", { children: "No results found." });
6
+ }
7
+ return (_jsxs("div", { children: [results.map((result) => {
8
+ return (_jsx(GameObject, { k: k, className: "game-object--root", obj: result, setRenderRoot: setRenderRoot, shouldDrawInspect: shouldDrawInspect }));
9
+ }), " "] }));
10
+ };
package/dist/styles.css CHANGED
@@ -60,6 +60,10 @@
60
60
  margin: 0;
61
61
  }
62
62
 
63
+ svg {
64
+ display: block;
65
+ }
66
+
63
67
  button,
64
68
  input,
65
69
  select,
@@ -96,8 +100,18 @@
96
100
  border: 1px solid var(--ki-primary);
97
101
  }
98
102
 
99
- svg {
100
- display: block;
103
+ .ki-input {
104
+ background-color: var(--ki-bg);
105
+ border: none;
106
+ padding: 0.25rem 0.375rem;
107
+ border: 1px solid var(--ki-border-light);
108
+ font-size: 0.75rem;
109
+ border-radius: 4px;
110
+ }
111
+
112
+ .ki-input:focus-visible {
113
+ outline: none;
114
+ border-color: var(--ki-primary);
101
115
  }
102
116
 
103
117
  /* Main layout */
@@ -318,20 +332,9 @@
318
332
  /* Text Controls */
319
333
 
320
334
  .text-controls__input {
321
- background-color: var(--ki-bg);
322
- border: none;
323
- padding: 0.25rem 0.375rem;
324
- border: 1px solid var(--ki-border-light);
325
- font-size: 0.75rem;
326
- border-radius: 4px;
327
335
  width: 100%;
328
336
  }
329
337
 
330
- .text-controls__input:focus-visible {
331
- outline: none;
332
- border-color: var(--ki-primary);
333
- }
334
-
335
338
  /* Color */
336
339
 
337
340
  .color-controls {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stanko/kaplay-inspector",
3
3
  "description": "A dev tool for Kaplay which allows you to explore and inspect the game object tree real time.",
4
4
  "private": false,
5
- "version": "0.1.3",
5
+ "version": "0.1.4",
6
6
  "type": "module",
7
7
  "main": "./dist/init.js",
8
8
  "types": "./dist/init.d.ts",