From 8dd61977b3690f48f31b8f9f2c0e637fd29a7bf5 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Sat, 21 Nov 2026 13:09:49 +0900 Subject: [PATCH 1/5] feat: let the pale play button be pressed, to tell which input pin is free The play button could not be pressed while an input pin was free, and said nothing of which. It now stays pale then, so that it still tells when every pin is connected, but pressing it tries to play, and the simulation goes back to editing with the free pin named and its component outlined, as it already did for a pin free inside a component. Co-Authored-By: Claude Opus 5.5 --- src/i18n/en.ts | 2 +- src/i18n/ja.ts | 2 +- .../Editor/components/ViewModeSwitcher.tsx | 22 +++++++++++++++--- .../Editor/store/slices/core/index.test.ts | 23 +++++++++++++++++++ src/store/react/selectors.ts | 2 +- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 4c6fe51..ed1500e 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -27,7 +27,7 @@ const stageTexts: Record = { steps: [ "The pieces a circuit is built from are called components. Pick one in the Components list and drag it onto the editor on the right.", "The small squares on the edges of a component are its pins. A pin on the left side is an input, which receives [ON] or [OFF], and a pin on the right side is an output, which sends [ON] or [OFF]. Drag between an output pin and an input pin to connect them with a wire.", - "Once {Out} and every input pin of the components you placed are connected, the play button at the bottom right can be pressed. It cannot be pressed while a component has a free input pin.", + "Once {Out} and every input pin of the components you placed are connected, the play button at the bottom right turns dark. Pressed while a component still has a free input pin, it shows which pin is free.", "After pressing the play button , click the switches {A} and {B} to turn them [ON] and [OFF], and watch the lamp {Out}. To fix the circuit, the pencil button in the same place takes you back to editing.", "When it works as in the table, press .", ], diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index 9ec7642..56659e7 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -173,7 +173,7 @@ export const ja: Messages = { steps: [ "回路を組み立てる部品を「コンポーネント」といいます。「コンポーネント」の一覧から1つ選び、右のエディタにドラッグして置きます。", "コンポーネントの端にある小さな四角を「ピン」といいます。左側のピンは [ON] か [OFF] を受け取る「入力」、右側のピンは [ON] か [OFF] を送り出す「出力」です。出力のピンと入力のピンのあいだをドラッグして、線でつなぎます。", - "{Out} と、置いたコンポーネントの入力のピンがすべてつながると、右下の再生ボタンが押せるようになります。入力のピンが空いているコンポーネントが残っていると押せません。", + "{Out} と、置いたコンポーネントの入力のピンがすべてつながると、右下の再生ボタンの色が濃くなります。入力のピンが空いているコンポーネントが残っているときに押すと、どのピンが空いているかが表示されます。", "再生ボタンを押したら、スイッチ {A} と {B} をクリックして [ON] と [OFF] を切り替え、ランプ {Out} を見ます。回路を直すときは、同じ場所の鉛筆のボタンで編集に戻ります。", "表のとおりに動いたら、を押します。", ], diff --git a/src/pages/edit/Editor/components/ViewModeSwitcher.tsx b/src/pages/edit/Editor/components/ViewModeSwitcher.tsx index 4421f34..4085ce6 100644 --- a/src/pages/edit/Editor/components/ViewModeSwitcher.tsx +++ b/src/pages/edit/Editor/components/ViewModeSwitcher.tsx @@ -1,11 +1,17 @@ import { Edit, PlayArrow } from "@mui/icons-material"; import { Fab } from "@mui/material"; -import { useCanSimulate } from "../../../../store/react/selectors"; +import { useIsEachInputPinConnected } from "../../../../store/react/selectors"; import { useComponentEditorStore } from "../store"; export default function CCComponentEditorViewModeSwitcher() { const componentEditorState = useComponentEditorStore()(); - const canSimulate = useCanSimulate(componentEditorState.componentId); + const isEachInputPinConnected = useIsEachInputPinConnected( + componentEditorState.componentId, + ); + // Pale while a pin is free, yet pressed all the same, so that the simulation can + // tell which pin it is + const looksDisabled = + componentEditorState.editorMode === "edit" && !isEachInputPinConnected; return ( {componentEditorState.editorMode === "edit" ? : } diff --git a/src/pages/edit/Editor/store/slices/core/index.test.ts b/src/pages/edit/Editor/store/slices/core/index.test.ts index 7112cfd..51c7e73 100644 --- a/src/pages/edit/Editor/store/slices/core/index.test.ts +++ b/src/pages/edit/Editor/store/slices/core/index.test.ts @@ -194,6 +194,29 @@ describe("simulation errors", () => { ); expect(simulationError?.nodeIds).toEqual(new Set([first.id, second.id])); }); + + it("tells which pin is free when play is pressed with one", () => { + const store = createTestStore(); + const root = registerTestComponent(store, "Root"); + const source = registerTestNode(store, root.id, true_.component.id); + const gate = registerTestNode(store, root.id, and.component.id); + connectTestNodePins( + store, + root.id, + testNodePin(store, source, true_.outputPin.Out), + testNodePin(store, gate, and.inputPin.A), + ); + const editorStore = createEditorStore(root.id, store); + + editorStore.getState().setEditorMode("play"); + + const { editorMode, simulationError } = editorStore.getState(); + expect(editorMode).toBe("edit"); + expect(simulationError?.messageByLanguage.ja).toBe( + "And の B のピンがつながっていません。", + ); + expect(simulationError?.nodeIds).toEqual(new Set([gate.id])); + }); }); describe("editor mode", () => { diff --git a/src/store/react/selectors.ts b/src/store/react/selectors.ts index c605821..99712ba 100644 --- a/src/store/react/selectors.ts +++ b/src/store/react/selectors.ts @@ -156,7 +156,7 @@ export function useComponentPin(componentPinId: CCComponentPinId | undefined) { return useSyncExternalStore(subscribe, getSnapshot); } -export function useCanSimulate(componentId: CCComponentId) { +export function useIsEachInputPinConnected(componentId: CCComponentId) { const { store } = useStore(); const getSnapshot = useCallback( () => isEachInputPinConnected(store, componentId), From 96e683f6e283fecfb8c928933de13eb4f7c674cc Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Sat, 21 Nov 2026 13:09:49 +0900 Subject: [PATCH 2/5] feat: bring an outlined component into view when an error or a check outlines it A component outlined out of view, e.g. dropped far off, left the player with a message about a pin they could not find. When an error or a failed check outlines one out of view, the view now fits every component, as when the editor opens. Co-Authored-By: Claude Opus 5.5 --- src/pages/edit/Editor/renderer/index.tsx | 10 ++++ .../store/slices/perspective/fit.test.ts | 42 +++++++++++++++- .../Editor/store/slices/perspective/fit.ts | 28 ++++++++++- .../Editor/store/slices/perspective/index.tsx | 48 +++++++++++++------ .../Editor/store/slices/perspective/types.ts | 3 ++ 5 files changed, 114 insertions(+), 17 deletions(-) diff --git a/src/pages/edit/Editor/renderer/index.tsx b/src/pages/edit/Editor/renderer/index.tsx index 39994c1..cf50d9d 100644 --- a/src/pages/edit/Editor/renderer/index.tsx +++ b/src/pages/edit/Editor/renderer/index.tsx @@ -1,3 +1,4 @@ +import { useContext, useEffect } from "react"; import { parseDataTransferAsComponent } from "../../../../common/serialization"; import { vector2 } from "../../../../common/vector2"; import { CCNodeStore } from "../../../../store/node"; @@ -7,6 +8,7 @@ import { useNodeIds, } from "../../../../store/react/selectors"; import { canPlaceComponent } from "../../placement"; +import { OutlinedNodeIdsContext } from "../outlinedNodes"; import { useComponentEditorStore } from "../store"; import CCComponentEditorRendererBackground from "./Background"; import CCComponentEditorRendererConnection from "./Connection"; @@ -19,6 +21,14 @@ export default function CCComponentEditorRenderer() { const nodeIds = useNodeIds(componentEditorState.componentId); const connectionIds = useConnectionIds(componentEditorState.componentId); + // An outlined node out of view leaves the player not knowing what is wrong + const { showNodes, simulationError } = componentEditorState; + const outlinedNodeIds = useContext(OutlinedNodeIdsContext); + useEffect(() => showNodes(outlinedNodeIds), [showNodes, outlinedNodeIds]); + useEffect(() => { + if (simulationError) showNodes(simulationError.nodeIds); + }, [showNodes, simulationError]); + return ( // biome-ignore lint/a11y/noSvgWithoutTitle: This svg is not a graphic { }); }); }); + +describe("isShownInRoom", () => { + const perspective = { center: { x: 0, y: 0 }, scale: 1 }; + + it("tells a rectangle inside the room from one reaching past its edge", () => { + expect( + isShownInRoom( + { position: { x: -50, y: -20 }, size: { x: 100, y: 40 } }, + perspective, + rendererSize, + ), + ).toBe(true); + expect( + isShownInRoom( + { position: { x: 300, y: 0 }, size: { x: 200, y: 40 } }, + perspective, + rendererSize, + ), + ).toBe(false); + }); + + it("counts the padding under the title bar as out of the room", () => { + const underTitleBar = { + position: { x: 0, y: -rendererSize.y / 2 + fitPadding.top - 10 }, + size: { x: 50, y: 30 }, + }; + expect(isShownInRoom(underTitleBar, perspective, rendererSize)).toBe(false); + }); + + it("shows every rectangle in the room once the perspective fits them", () => { + const rects = [ + { position: { x: -2000, y: 50 }, size: { x: 100, y: 30 } }, + { position: { x: 400, y: 900 }, size: { x: 100, y: 30 } }, + ]; + const fitted = perspectiveFitting(rects, rendererSize); + for (const rect of rects) { + expect(isShownInRoom(rect, fitted, rendererSize)).toBe(true); + } + }); +}); diff --git a/src/pages/edit/Editor/store/slices/perspective/fit.ts b/src/pages/edit/Editor/store/slices/perspective/fit.ts index 4748959..e273a7a 100644 --- a/src/pages/edit/Editor/store/slices/perspective/fit.ts +++ b/src/pages/edit/Editor/store/slices/perspective/fit.ts @@ -1,3 +1,4 @@ +import type { Rect } from "../../../../../../common/rect"; import type { Perspective } from "../../../../../../common/types"; import type { Vector2 } from "../../../../../../common/vector2"; @@ -9,7 +10,7 @@ export const fitPadding = { top: 130, right: 30, bottom: 110, left: 30 }; * out only as far as needed, never in */ export function perspectiveFitting( - rects: readonly { position: Vector2; size: Vector2 }[], + rects: readonly Rect[], rendererSize: Vector2, ): Perspective { if (rects.length === 0) return { center: { x: 0, y: 0 }, scale: 1 }; @@ -40,3 +41,28 @@ export function perspectiveFitting( scale, }; } + +/** Whether `rect` lies wholly in the room the padding leaves, as `perspective` shows it */ +export function isShownInRoom( + rect: Rect, + perspective: Perspective, + rendererSize: Vector2, +): boolean { + const toCanvas = (point: Vector2) => ({ + x: + (point.x - perspective.center.x) / perspective.scale + rendererSize.x / 2, + y: + (point.y - perspective.center.y) / perspective.scale + rendererSize.y / 2, + }); + const topLeft = toCanvas(rect.position); + const bottomRight = toCanvas({ + x: rect.position.x + rect.size.x, + y: rect.position.y + rect.size.y, + }); + return ( + topLeft.x >= fitPadding.left && + topLeft.y >= fitPadding.top && + bottomRight.x <= rendererSize.x - fitPadding.right && + bottomRight.y <= rendererSize.y - fitPadding.bottom + ); +} diff --git a/src/pages/edit/Editor/store/slices/perspective/index.tsx b/src/pages/edit/Editor/store/slices/perspective/index.tsx index a2053b5..839ad30 100644 --- a/src/pages/edit/Editor/store/slices/perspective/index.tsx +++ b/src/pages/edit/Editor/store/slices/perspective/index.tsx @@ -1,7 +1,12 @@ +import type { Rect } from "../../../../../../common/rect"; import { vector2 } from "../../../../../../common/vector2"; +import type { CCNodeId } from "../../../../../../store/node"; import { getCCComponentEditorRendererNodeGeometry } from "../../../renderer/Node/geometry"; -import type { ComponentEditorSliceCreator } from "../../types"; -import { perspectiveFitting } from "./fit"; +import type { + ComponentEditorSliceCreator, + ComponentEditorStoreValue, +} from "../../types"; +import { isShownInRoom, perspectiveFitting } from "./fit"; import type { PerspectiveStoreSlice } from "./types"; const createComponentEditorStorePerspectiveSlice: ComponentEditorSliceCreator< @@ -16,6 +21,14 @@ const createComponentEditorStorePerspectiveSlice: ComponentEditorSliceCreator< if (element) resizeObserver.observe(element); rendererElement = element; }; + const rectOf = (nodeId: CCNodeId) => + getCCComponentEditorRendererNodeGeometry(store, nodeId).rect; + const rectsToFit = (state: ComponentEditorStoreValue): Rect[] => [ + ...store.nodes + .getManyByParentComponentId(componentId) + .map((node) => rectOf(node.id)), + ...(state.areaToFit ? [state.areaToFit] : []), + ]; return { define: (set, get) => ({ perspective: { center: vector2.zero, scale: 1 }, @@ -25,6 +38,23 @@ const createComponentEditorStorePerspectiveSlice: ComponentEditorSliceCreator< return rect ? { x: rect.left, y: rect.top } : vector2.zero; }, setPerspective: (perspective) => set((s) => ({ ...s, perspective })), + showNodes: (nodeIds) => { + const { perspective, rendererSize } = get(); + if (rendererSize.x <= 0 || rendererSize.y <= 0) return; + const isEachShown = [...nodeIds] + .filter( + (nodeId) => + store.nodes.get(nodeId)?.parentComponentId === componentId, + ) + .every((nodeId) => + isShownInRoom(rectOf(nodeId), perspective, rendererSize), + ); + if (isEachShown) return; + set((s) => ({ + ...s, + perspective: perspectiveFitting(rectsToFit(get()), rendererSize), + })); + }, registerRendererElement, fromCanvasToStage: (point) => vector2.add( @@ -70,19 +100,7 @@ const createComponentEditorStorePerspectiveSlice: ComponentEditorSliceCreator< editorStore.setState({ rendererSize, ...(fitsNow && { - perspective: perspectiveFitting( - [ - ...store.nodes - .getManyByParentComponentId(componentId) - .map( - (node) => - getCCComponentEditorRendererNodeGeometry(store, node.id) - .rect, - ), - ...(state.areaToFit ? [state.areaToFit] : []), - ], - rendererSize, - ), + perspective: perspectiveFitting(rectsToFit(state), rendererSize), }), }); }); diff --git a/src/pages/edit/Editor/store/slices/perspective/types.ts b/src/pages/edit/Editor/store/slices/perspective/types.ts index 3382c56..25feb60 100644 --- a/src/pages/edit/Editor/store/slices/perspective/types.ts +++ b/src/pages/edit/Editor/store/slices/perspective/types.ts @@ -1,11 +1,14 @@ import type { Perspective } from "../../../../../../common/types"; import type { Vector2 } from "../../../../../../common/vector2"; +import type { CCNodeId } from "../../../../../../store/node"; export type PerspectiveStoreSlice = { perspective: Perspective; rendererSize: Vector2; getRendererPosition: () => Vector2; setPerspective: (perspective: Perspective) => void; + /** Fits every node in view, if any of `nodeIds` in the edited component is out of it */ + showNodes: (nodeIds: ReadonlySet) => void; registerRendererElement: (element: SVGSVGElement | null) => void; fromCanvasToStage: (point: Vector2) => Vector2; fromStageToCanvas: (point: Vector2) => Vector2; From 9dff1f68bf380846b23e631ad146614530d689b7 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Sat, 21 Nov 2026 13:14:11 +0900 Subject: [PATCH 3/5] fix: keep an outlined component clear of the error message, and name the play button - The view fitted for an error leaves room at the bottom for the message about it, which could otherwise cover the component it names. - Stage to canvas is worked out in one place for the view and for the check of what is in view, and showNodes is tested. - The play button is named for screen readers, and stage 1 says it turns green rather than dark. Co-Authored-By: Claude Opus 5.5 --- src/i18n/en.ts | 3 +- src/i18n/ja.ts | 3 +- .../Editor/components/ViewModeSwitcher.tsx | 14 ++-- src/pages/edit/Editor/renderer/index.tsx | 4 +- .../Editor/store/slices/perspective/fit.ts | 70 ++++++++++------- .../store/slices/perspective/index.test.ts | 75 +++++++++++++++++++ .../Editor/store/slices/perspective/index.tsx | 28 ++++--- 7 files changed, 149 insertions(+), 48 deletions(-) create mode 100644 src/pages/edit/Editor/store/slices/perspective/index.test.ts diff --git a/src/i18n/en.ts b/src/i18n/en.ts index ed1500e..7b7e4c2 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -27,7 +27,7 @@ const stageTexts: Record = { steps: [ "The pieces a circuit is built from are called components. Pick one in the Components list and drag it onto the editor on the right.", "The small squares on the edges of a component are its pins. A pin on the left side is an input, which receives [ON] or [OFF], and a pin on the right side is an output, which sends [ON] or [OFF]. Drag between an output pin and an input pin to connect them with a wire.", - "Once {Out} and every input pin of the components you placed are connected, the play button at the bottom right turns dark. Pressed while a component still has a free input pin, it shows which pin is free.", + "Once {Out} and every input pin of the components you placed are connected, the play button at the bottom right turns green. Pressed while a component still has a free input pin, it shows which pin is free.", "After pressing the play button , click the switches {A} and {B} to turn them [ON] and [OFF], and watch the lamp {Out}. To fix the circuit, the pencil button in the same place takes you back to editing.", "When it works as in the table, press .", ], @@ -322,6 +322,7 @@ export const en = { keptOnDelete: (names: readonly string[]) => `${listOf(names)} ${names.length === 1 ? "is" : "are"} kept, as the stage uses ${themOf(names)}`, }, + viewMode: { play: "Play", edit: "Back to editing" }, frameControl: { auto: "Auto", autoPlay: "Auto play", diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index 56659e7..efc0ef3 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -173,7 +173,7 @@ export const ja: Messages = { steps: [ "回路を組み立てる部品を「コンポーネント」といいます。「コンポーネント」の一覧から1つ選び、右のエディタにドラッグして置きます。", "コンポーネントの端にある小さな四角を「ピン」といいます。左側のピンは [ON] か [OFF] を受け取る「入力」、右側のピンは [ON] か [OFF] を送り出す「出力」です。出力のピンと入力のピンのあいだをドラッグして、線でつなぎます。", - "{Out} と、置いたコンポーネントの入力のピンがすべてつながると、右下の再生ボタンの色が濃くなります。入力のピンが空いているコンポーネントが残っているときに押すと、どのピンが空いているかが表示されます。", + "{Out} と、置いたコンポーネントの入力のピンがすべてつながると、右下の再生ボタンが緑色になります。入力のピンが空いているコンポーネントが残っているときに押すと、どのピンが空いているかが表示されます。", "再生ボタンを押したら、スイッチ {A} と {B} をクリックして [ON] と [OFF] を切り替え、ランプ {Out} を見ます。回路を直すときは、同じ場所の鉛筆のボタンで編集に戻ります。", "表のとおりに動いたら、を押します。", ], @@ -286,6 +286,7 @@ export const ja: Messages = { cannotDelete: (names) => `${names.join("、")} は課題で使うので消せません`, keptOnDelete: (names) => `${names.join("、")} は課題で使うので残ります`, }, + viewMode: { play: "再生", edit: "編集に戻る" }, frameControl: { auto: "自動", autoPlay: "自動再生", diff --git a/src/pages/edit/Editor/components/ViewModeSwitcher.tsx b/src/pages/edit/Editor/components/ViewModeSwitcher.tsx index 4085ce6..b50962e 100644 --- a/src/pages/edit/Editor/components/ViewModeSwitcher.tsx +++ b/src/pages/edit/Editor/components/ViewModeSwitcher.tsx @@ -1,28 +1,29 @@ import { Edit, PlayArrow } from "@mui/icons-material"; import { Fab } from "@mui/material"; +import { useMessages } from "../../../../i18n"; import { useIsEachInputPinConnected } from "../../../../store/react/selectors"; import { useComponentEditorStore } from "../store"; export default function CCComponentEditorViewModeSwitcher() { const componentEditorState = useComponentEditorStore()(); + const texts = useMessages().editor.viewMode; + const isEditing = componentEditorState.editorMode === "edit"; const isEachInputPinConnected = useIsEachInputPinConnected( componentEditorState.componentId, ); // Pale while a pin is free, yet pressed all the same, so that the simulation can // tell which pin it is - const looksDisabled = - componentEditorState.editorMode === "edit" && !isEachInputPinConnected; + const looksDisabled = isEditing && !isEachInputPinConnected; return ( { - componentEditorState.setEditorMode( - componentEditorState.editorMode === "edit" ? "play" : "edit", - ); + componentEditorState.setEditorMode(isEditing ? "play" : "edit"); componentEditorState.setTimeStep(0); }} + aria-label={isEditing ? texts.play : texts.edit} aria-disabled={looksDisabled} sx={ looksDisabled @@ -31,11 +32,12 @@ export default function CCComponentEditorViewModeSwitcher() { bgcolor: "action.disabledBackground", boxShadow: "none", "&:hover": { bgcolor: "action.disabledBackground" }, + "&:active": { boxShadow: "none" }, } : {} } > - {componentEditorState.editorMode === "edit" ? : } + {isEditing ? : } ); } diff --git a/src/pages/edit/Editor/renderer/index.tsx b/src/pages/edit/Editor/renderer/index.tsx index cf50d9d..e374ef0 100644 --- a/src/pages/edit/Editor/renderer/index.tsx +++ b/src/pages/edit/Editor/renderer/index.tsx @@ -24,7 +24,9 @@ export default function CCComponentEditorRenderer() { // An outlined node out of view leaves the player not knowing what is wrong const { showNodes, simulationError } = componentEditorState; const outlinedNodeIds = useContext(OutlinedNodeIdsContext); - useEffect(() => showNodes(outlinedNodeIds), [showNodes, outlinedNodeIds]); + useEffect(() => { + showNodes(outlinedNodeIds); + }, [showNodes, outlinedNodeIds]); useEffect(() => { if (simulationError) showNodes(simulationError.nodeIds); }, [showNodes, simulationError]); diff --git a/src/pages/edit/Editor/store/slices/perspective/fit.ts b/src/pages/edit/Editor/store/slices/perspective/fit.ts index e273a7a..bbdef86 100644 --- a/src/pages/edit/Editor/store/slices/perspective/fit.ts +++ b/src/pages/edit/Editor/store/slices/perspective/fit.ts @@ -2,8 +2,31 @@ import type { Rect } from "../../../../../../common/rect"; import type { Perspective } from "../../../../../../common/types"; import type { Vector2 } from "../../../../../../common/vector2"; +type Padding = { top: number; right: number; bottom: number; left: number }; + /** Room kept clear of nodes: the title bar is at the top and the play button at the bottom right */ -export const fitPadding = { top: 130, right: 30, bottom: 110, left: 30 }; +export const fitPadding: Padding = { + top: 130, + right: 30, + bottom: 110, + left: 30, +}; + +/** As `fitPadding`, with room at the bottom for the message about an error */ +export const errorFitPadding: Padding = { ...fitPadding, bottom: 200 }; + +export function stageToCanvas( + point: Vector2, + perspective: Perspective, + rendererSize: Vector2, +): Vector2 { + return { + x: + (point.x - perspective.center.x) / perspective.scale + rendererSize.x / 2, + y: + (point.y - perspective.center.y) / perspective.scale + rendererSize.y / 2, + }; +} /** * The perspective showing every rectangle inside a renderer of `rendererSize`, zoomed @@ -12,6 +35,7 @@ export const fitPadding = { top: 130, right: 30, bottom: 110, left: 30 }; export function perspectiveFitting( rects: readonly Rect[], rendererSize: Vector2, + padding: Padding = fitPadding, ): Perspective { if (rects.length === 0) return { center: { x: 0, y: 0 }, scale: 1 }; const left = Math.min(...rects.map((rect) => rect.position.x)); @@ -21,48 +45,36 @@ export function perspectiveFitting( ...rects.map((rect) => rect.position.y + rect.size.y), ); // A renderer smaller than the padding still gets a finite scale - const roomX = Math.max( - 1, - rendererSize.x - fitPadding.left - fitPadding.right, - ); - const roomY = Math.max( - 1, - rendererSize.y - fitPadding.top - fitPadding.bottom, - ); + const roomX = Math.max(1, rendererSize.x - padding.left - padding.right); + const roomY = Math.max(1, rendererSize.y - padding.top - padding.bottom); const scale = Math.max(1, (right - left) / roomX, (bottom - top) / roomY); // The middle of the room is off the middle of the renderer by half the uneven padding return { center: { - x: - (left + right) / 2 - ((fitPadding.left - fitPadding.right) / 2) * scale, - y: - (top + bottom) / 2 - ((fitPadding.top - fitPadding.bottom) / 2) * scale, + x: (left + right) / 2 - ((padding.left - padding.right) / 2) * scale, + y: (top + bottom) / 2 - ((padding.top - padding.bottom) / 2) * scale, }, scale, }; } -/** Whether `rect` lies wholly in the room the padding leaves, as `perspective` shows it */ +/** Whether `rect` lies wholly in the room `padding` leaves, as `perspective` shows it */ export function isShownInRoom( rect: Rect, perspective: Perspective, rendererSize: Vector2, + padding: Padding = fitPadding, ): boolean { - const toCanvas = (point: Vector2) => ({ - x: - (point.x - perspective.center.x) / perspective.scale + rendererSize.x / 2, - y: - (point.y - perspective.center.y) / perspective.scale + rendererSize.y / 2, - }); - const topLeft = toCanvas(rect.position); - const bottomRight = toCanvas({ - x: rect.position.x + rect.size.x, - y: rect.position.y + rect.size.y, - }); + const topLeft = stageToCanvas(rect.position, perspective, rendererSize); + const bottomRight = stageToCanvas( + { x: rect.position.x + rect.size.x, y: rect.position.y + rect.size.y }, + perspective, + rendererSize, + ); return ( - topLeft.x >= fitPadding.left && - topLeft.y >= fitPadding.top && - bottomRight.x <= rendererSize.x - fitPadding.right && - bottomRight.y <= rendererSize.y - fitPadding.bottom + topLeft.x >= padding.left && + topLeft.y >= padding.top && + bottomRight.x <= rendererSize.x - padding.right && + bottomRight.y <= rendererSize.y - padding.bottom ); } diff --git a/src/pages/edit/Editor/store/slices/perspective/index.test.ts b/src/pages/edit/Editor/store/slices/perspective/index.test.ts new file mode 100644 index 0000000..353ba36 --- /dev/null +++ b/src/pages/edit/Editor/store/slices/perspective/index.test.ts @@ -0,0 +1,75 @@ +import { describe, expect, it } from "vitest"; +import { not } from "../../../../../../store/intrinsics/definitions"; +import { + createTestStore, + registerTestComponent, + registerTestNode, +} from "../../../../../../store/testUtils"; +import { getCCComponentEditorRendererNodeGeometry } from "../../../renderer/Node/geometry"; +import { createEditorStore } from "../.."; +import { stubResizeObserver } from "../../testUtils"; +import { errorFitPadding, isShownInRoom } from "./fit"; + +stubResizeObserver(); + +const rendererSize = { x: 900, y: 800 }; +const perspective = { center: { x: 0, y: 0 }, scale: 1 }; + +function setUp() { + const store = createTestStore(); + const root = registerTestComponent(store, "Root"); + const near = registerTestNode(store, root.id, not.component.id); + store.nodes.update(near.id, { position: { x: 0, y: 0 } }); + const far = registerTestNode(store, root.id, not.component.id); + store.nodes.update(far.id, { position: { x: 5000, y: 0 } }); + const editorStore = createEditorStore(root.id, store); + editorStore.setState({ rendererSize, perspective }); + const isShown = (nodeId: typeof near.id) => + isShownInRoom( + getCCComponentEditorRendererNodeGeometry(store, nodeId).rect, + editorStore.getState().perspective, + rendererSize, + errorFitPadding, + ); + return { store, root, near, far, editorStore, isShown }; +} + +describe("showNodes", () => { + it("leaves the view as it is when the nodes are in it", () => { + const { near, editorStore } = setUp(); + + editorStore.getState().showNodes(new Set([near.id])); + + expect(editorStore.getState().perspective).toBe(perspective); + }); + + it("brings every node into view when one is out of it", () => { + const { near, far, editorStore, isShown } = setUp(); + expect(isShown(far.id)).toBe(false); + + editorStore.getState().showNodes(new Set([far.id])); + + expect(isShown(far.id)).toBe(true); + expect(isShown(near.id)).toBe(true); + }); + + it("ignores nodes of other components", () => { + const { store, editorStore } = setUp(); + const other = registerTestComponent(store, "Other"); + const elsewhere = registerTestNode(store, other.id, not.component.id); + store.nodes.update(elsewhere.id, { position: { x: 9000, y: 0 } }); + + editorStore.getState().showNodes(new Set([elsewhere.id])); + + expect(editorStore.getState().perspective).toBe(perspective); + }); + + it("waits for the size of the renderer", () => { + const { far, editorStore } = setUp(); + editorStore.setState({ rendererSize: { x: 0, y: 0 } }); + + editorStore.getState().showNodes(new Set([far.id])); + + expect(editorStore.getState().perspective).toBe(perspective); + }); +}); diff --git a/src/pages/edit/Editor/store/slices/perspective/index.tsx b/src/pages/edit/Editor/store/slices/perspective/index.tsx index 839ad30..058dbef 100644 --- a/src/pages/edit/Editor/store/slices/perspective/index.tsx +++ b/src/pages/edit/Editor/store/slices/perspective/index.tsx @@ -6,7 +6,12 @@ import type { ComponentEditorSliceCreator, ComponentEditorStoreValue, } from "../../types"; -import { isShownInRoom, perspectiveFitting } from "./fit"; +import { + errorFitPadding, + isShownInRoom, + perspectiveFitting, + stageToCanvas, +} from "./fit"; import type { PerspectiveStoreSlice } from "./types"; const createComponentEditorStorePerspectiveSlice: ComponentEditorSliceCreator< @@ -47,12 +52,21 @@ const createComponentEditorStorePerspectiveSlice: ComponentEditorSliceCreator< store.nodes.get(nodeId)?.parentComponentId === componentId, ) .every((nodeId) => - isShownInRoom(rectOf(nodeId), perspective, rendererSize), + isShownInRoom( + rectOf(nodeId), + perspective, + rendererSize, + errorFitPadding, + ), ); if (isEachShown) return; set((s) => ({ ...s, - perspective: perspectiveFitting(rectsToFit(get()), rendererSize), + perspective: perspectiveFitting( + rectsToFit(get()), + rendererSize, + errorFitPadding, + ), })); }, registerRendererElement, @@ -65,13 +79,7 @@ const createComponentEditorStorePerspectiveSlice: ComponentEditorSliceCreator< get().perspective.center, ), fromStageToCanvas: (point) => - vector2.add( - vector2.div( - vector2.sub(point, get().perspective.center), - get().perspective.scale, - ), - vector2.div(get().rendererSize, 2), - ), + stageToCanvas(point, get().perspective, get().rendererSize), getViewBox: () => { const viewBoxTopLeft = get().fromCanvasToStage(vector2.zero); const viewBoxBottomRight = get().fromCanvasToStage(get().rendererSize); From 3b8e29d10addab2400ad993f7eca102513395764 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Sat, 21 Nov 2026 13:14:42 +0900 Subject: [PATCH 4/5] chore: drop comments that restate the code Co-Authored-By: Claude Opus 5.5 --- src/pages/edit/Editor/components/ViewModeSwitcher.tsx | 3 +-- src/pages/edit/Editor/renderer/index.tsx | 1 - src/pages/edit/Editor/store/slices/perspective/fit.ts | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/pages/edit/Editor/components/ViewModeSwitcher.tsx b/src/pages/edit/Editor/components/ViewModeSwitcher.tsx index b50962e..8645948 100644 --- a/src/pages/edit/Editor/components/ViewModeSwitcher.tsx +++ b/src/pages/edit/Editor/components/ViewModeSwitcher.tsx @@ -11,8 +11,7 @@ export default function CCComponentEditorViewModeSwitcher() { const isEachInputPinConnected = useIsEachInputPinConnected( componentEditorState.componentId, ); - // Pale while a pin is free, yet pressed all the same, so that the simulation can - // tell which pin it is + // Pressed even when pale, so that the simulation names the free pin const looksDisabled = isEditing && !isEachInputPinConnected; return ( diff --git a/src/pages/edit/Editor/renderer/index.tsx b/src/pages/edit/Editor/renderer/index.tsx index e374ef0..598a0c1 100644 --- a/src/pages/edit/Editor/renderer/index.tsx +++ b/src/pages/edit/Editor/renderer/index.tsx @@ -21,7 +21,6 @@ export default function CCComponentEditorRenderer() { const nodeIds = useNodeIds(componentEditorState.componentId); const connectionIds = useConnectionIds(componentEditorState.componentId); - // An outlined node out of view leaves the player not knowing what is wrong const { showNodes, simulationError } = componentEditorState; const outlinedNodeIds = useContext(OutlinedNodeIdsContext); useEffect(() => { diff --git a/src/pages/edit/Editor/store/slices/perspective/fit.ts b/src/pages/edit/Editor/store/slices/perspective/fit.ts index bbdef86..c565cf1 100644 --- a/src/pages/edit/Editor/store/slices/perspective/fit.ts +++ b/src/pages/edit/Editor/store/slices/perspective/fit.ts @@ -58,7 +58,6 @@ export function perspectiveFitting( }; } -/** Whether `rect` lies wholly in the room `padding` leaves, as `perspective` shows it */ export function isShownInRoom( rect: Rect, perspective: Perspective, From 0005a9684d9378c76644174c812fa5139dd939e9 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Sat, 21 Nov 2026 13:23:58 +0900 Subject: [PATCH 5/5] test: check that showNodes brings a node out from under the error message Co-Authored-By: Claude Opus 5.5 --- .../store/slices/perspective/index.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/pages/edit/Editor/store/slices/perspective/index.test.ts b/src/pages/edit/Editor/store/slices/perspective/index.test.ts index 353ba36..2b8050d 100644 --- a/src/pages/edit/Editor/store/slices/perspective/index.test.ts +++ b/src/pages/edit/Editor/store/slices/perspective/index.test.ts @@ -53,6 +53,24 @@ describe("showNodes", () => { expect(isShown(near.id)).toBe(true); }); + it("brings a node out from under the error message", () => { + const { store, near, editorStore, isShown } = setUp(); + const rectOfNear = () => + getCCComponentEditorRendererNodeGeometry(store, near.id).rect; + const bottomUnderMessage = rendererSize.y / 2 - errorFitPadding.bottom + 10; + const { position, size } = rectOfNear(); + const offset = position.y - near.position.y; + store.nodes.update(near.id, { + position: { x: 0, y: bottomUnderMessage - size.y - offset }, + }); + expect(isShownInRoom(rectOfNear(), perspective, rendererSize)).toBe(true); + expect(isShown(near.id)).toBe(false); + + editorStore.getState().showNodes(new Set([near.id])); + + expect(isShown(near.id)).toBe(true); + }); + it("ignores nodes of other components", () => { const { store, editorStore } = setUp(); const other = registerTestComponent(store, "Other");