From 6f236b5fc1b2fbe1986ee563e624a7fc31586d92 Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Thu, 19 Nov 2026 22:26:04 +0900 Subject: [PATCH 1/5] feat: describe the basic parts, with a truth table, in the stage and the side panel A visitor who knows no logic cannot tell what And or Xor does from the name. Each gate now has a one-line summary in English and Japanese, and a truth table computed by running its definition, so it always matches how the part behaves. A stage lists its parts this way below Check, and the side panel opens the same description from an info button on each part that has one. Co-Authored-By: Claude Opus 5.5 --- src/components/PartDescription.tsx | 84 +++++++++++++++++++++++++ src/i18n/en.ts | 16 ++++- src/i18n/ja.ts | 16 ++++- src/pages/edit/SidePanel.tsx | 43 +++++++++++-- src/pages/tutorial/stage.tsx | 26 ++++++-- src/store/intrinsics/truthTable.test.ts | 37 +++++++++++ src/store/intrinsics/truthTable.ts | 61 ++++++++++++++++++ 7 files changed, 271 insertions(+), 12 deletions(-) create mode 100644 src/components/PartDescription.tsx create mode 100644 src/store/intrinsics/truthTable.test.ts create mode 100644 src/store/intrinsics/truthTable.ts diff --git a/src/components/PartDescription.tsx b/src/components/PartDescription.tsx new file mode 100644 index 0000000..4606c46 --- /dev/null +++ b/src/components/PartDescription.tsx @@ -0,0 +1,84 @@ +import { Box, Typography } from "@mui/material"; +import { type Messages, useMessages } from "../i18n"; +import type { CCComponent } from "../store/component"; +import { definitionByComponentId } from "../store/intrinsics/definitions"; +import { truthTableOf } from "../store/intrinsics/truthTable"; +import { theme } from "../style"; + +type Summaries = Messages["parts"]["summaries"]; + +export function partSummary( + messages: Messages, + component: CCComponent, +): string | null { + const type = component.intrinsicType; + return type && type in messages.parts.summaries + ? messages.parts.summaries[type as keyof Summaries] + : null; +} + +export function PartDescription({ component }: { component: CCComponent }) { + const messages = useMessages(); + const summary = partSummary(messages, component); + const definition = definitionByComponentId.get(component.id); + const table = definition && truthTableOf(definition); + const columns = table ? [...table.inputs, ...table.outputs] : []; + const cellSx = (column: number) => + ({ + px: 1, + py: 0.25, + textAlign: "center", + ...(column === table?.inputs.length && { + borderLeft: 1, + borderColor: "divider", + }), + }) as const; + + return ( +
+ {component.name} + {summary && {summary}} + {table && ( + + + + {columns.map((name, i) => ( + + {name} + + ))} + + + + {table.rows.map((row) => ( + + {[...row.inputs, ...row.outputs].map((value, i) => ( + + {value ? messages.common.on : messages.common.off} + + ))} + + ))} + + + )} +
+ ); +} diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 66e0a7d..6a4b701 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -16,6 +16,20 @@ export const en = { cancel: "Cancel", apply: "Apply", delete: "Delete", + on: "on", + off: "off", + }, + parts: { + summaries: { + AND: "On only when both inputs are on", + OR: "On when either input is on", + NOT: "Turns on into off, and off into on", + XOR: "On when exactly one input is on", + TRUE: "Always on", + FALSE: "Always off", + }, + about: (name: string) => `About ${name}`, + ofStage: "Parts for this stage", }, header: { language: "Language", @@ -95,8 +109,6 @@ export const en = { stages: "Stages", parts: "Parts", check: "Check", - on: "on", - off: "off", passed: "Cleared! The circuit works for every combination of the inputs.", next: "Next stage", backToStages: "Back to the stages", diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index d0a09c7..5a35021 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -12,6 +12,20 @@ export const ja: Messages = { cancel: "キャンセル", apply: "適用", delete: "削除", + on: "オン", + off: "オフ", + }, + parts: { + summaries: { + AND: "両方オンのときだけオン", + OR: "どちらかがオンならオン", + NOT: "オンとオフを逆にする", + XOR: "片方だけがオンのときオン", + TRUE: "いつもオン", + FALSE: "いつもオフ", + }, + about: (name) => `${name} について`, + ofStage: "この課題で使う部品", }, header: { language: "言語", @@ -90,8 +104,6 @@ export const ja: Messages = { stages: "課題の一覧", parts: "部品", check: "確かめる", - on: "オン", - off: "オフ", passed: "クリア!どの入力の組み合わせでも正しく動きます。", next: "次の課題へ", backToStages: "課題の一覧へ", diff --git a/src/pages/edit/SidePanel.tsx b/src/pages/edit/SidePanel.tsx index fa9c0a2..538e14a 100644 --- a/src/pages/edit/SidePanel.tsx +++ b/src/pages/edit/SidePanel.tsx @@ -1,10 +1,17 @@ -import { Search } from "@mui/icons-material"; -import { Box, InputAdornment, TextField } from "@mui/material"; +import { InfoOutlined, Search } from "@mui/icons-material"; +import { + Box, + IconButton, + InputAdornment, + Popover, + TextField, +} from "@mui/material"; import { useState } from "react"; import invariant from "tiny-invariant"; import { setDataTransferAsComponent } from "../../common/serialization"; +import { PartDescription, partSummary } from "../../components/PartDescription"; import { useMessages } from "../../i18n"; -import type { CCComponentId } from "../../store/component"; +import type { CCComponent, CCComponentId } from "../../store/component"; import { ccPinTypes } from "../../store/componentPin"; import { useStore } from "../../store/react"; import { useComponents } from "../../store/react/selectors"; @@ -95,6 +102,10 @@ export default function SidePanel(sidePanelProps: SidePanelProps) { const messages = useMessages(); const components = useComponents(); const [searchText, setSearchText] = useState(""); + const [describedPart, setDescribedPart] = useState<{ + component: CCComponent; + anchorEl: HTMLElement; + } | null>(null); return ( @@ -133,15 +144,39 @@ export default function SidePanel(sidePanelProps: SidePanelProps) { key={component.id} component="li" draggable - sx={{ cursor: "grab", p: 2 }} + sx={{ cursor: "grab", p: 2, position: "relative" }} onDragStart={(e) => { setDataTransferAsComponent(e.dataTransfer, component.id); }} > + {partSummary(messages, component) && ( + + setDescribedPart({ component, anchorEl: e.currentTarget }) + } + > + + + )} ))} + setDescribedPart(null)} + > + {describedPart && ( + + + + )} + ); } diff --git a/src/pages/tutorial/stage.tsx b/src/pages/tutorial/stage.tsx index 15e67d7..4cf75eb 100644 --- a/src/pages/tutorial/stage.tsx +++ b/src/pages/tutorial/stage.tsx @@ -8,6 +8,7 @@ import { Stack, Typography, } from "@mui/material"; +import nullthrows from "nullthrows"; import { useEffect, useState } from "react"; import { Navigate, @@ -16,8 +17,10 @@ import { useParams, } from "react-router"; import { ConfirmDialog } from "../../components/ConfirmDialog"; +import { PartDescription } from "../../components/PartDescription"; import { type Messages, useMessages } from "../../i18n"; import type CCStore from "../../store"; +import { definitionByComponentId } from "../../store/intrinsics/definitions"; import { SimulationNodeError } from "../../store/netlist"; import { StoreScope } from "../../store/react"; import type { SimulationValue } from "../../store/simulation"; @@ -39,7 +42,7 @@ import CCComponentEditor from "../edit/Editor"; import type { OffersComponent } from "../edit/placement"; import SidePanel from "../edit/SidePanel"; -function formatValue(value: SimulationValue, texts: Messages["tutorial"]) { +function formatValue(value: SimulationValue, texts: Messages["common"]) { if (value.length === 1) return value[0] ? texts.on : texts.off; return value.map((bit) => (bit ? "1" : "0")).join(""); } @@ -57,11 +60,11 @@ function describeFailure( return texts.wrongOutput( result.inputs.map(({ name, value }) => ({ name, - value: formatValue(value, texts), + value: formatValue(value, messages.common), })), result.pinName, - formatValue(result.expected, texts), - result.actual ? formatValue(result.actual, texts) : null, + formatValue(result.expected, messages.common), + result.actual ? formatValue(result.actual, messages.common) : null, ); case "simulationFailed": return texts.simulationFailed( @@ -181,6 +184,21 @@ function StageView({ stage }: { stage: TutorialStage }) { {describeFailure(result, store, messages)} )} +
+ + {messages.parts.ofStage} + + + {stage.parts.map((partId) => ( + + ))} + +
{hasResetFailed && ( {texts.resetStageFailed} diff --git a/src/store/intrinsics/truthTable.test.ts b/src/store/intrinsics/truthTable.test.ts new file mode 100644 index 0000000..9322630 --- /dev/null +++ b/src/store/intrinsics/truthTable.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, it } from "vitest"; +import { and, display, flipflop, input, not, true_, xor } from "./definitions"; +import { truthTableOf } from "./truthTable"; + +describe("truthTableOf", () => { + it("tells the output of a gate for every combination of its inputs", () => { + expect(truthTableOf(and)).toEqual({ + inputs: ["A", "B"], + outputs: ["Out"], + rows: [ + { inputs: [false, false], outputs: [false] }, + { inputs: [false, true], outputs: [false] }, + { inputs: [true, false], outputs: [false] }, + { inputs: [true, true], outputs: [true] }, + ], + }); + expect(truthTableOf(xor)?.rows.map((row) => row.outputs[0])).toEqual([ + false, + true, + true, + false, + ]); + expect(truthTableOf(not)?.rows).toEqual([ + { inputs: [false], outputs: [true] }, + { inputs: [true], outputs: [false] }, + ]); + expect(truthTableOf(true_)?.rows).toEqual([ + { inputs: [], outputs: [true] }, + ]); + }); + + it("gives none for components a table would not show", () => { + expect(truthTableOf(input)).toBeNull(); + expect(truthTableOf(flipflop)).toBeNull(); + expect(truthTableOf(display)).toBeNull(); + }); +}); diff --git a/src/store/intrinsics/truthTable.ts b/src/store/intrinsics/truthTable.ts new file mode 100644 index 0000000..cd9cd38 --- /dev/null +++ b/src/store/intrinsics/truthTable.ts @@ -0,0 +1,61 @@ +import { mapValues } from "es-toolkit"; +import nullthrows from "nullthrows"; +import { IntrinsicComponentDefinition } from "./base"; + +export type TruthTable = { + inputs: string[]; + outputs: string[]; + rows: { inputs: boolean[]; outputs: boolean[] }[]; +}; + +const maxInputCount = 2; + +/** null for a part a small table cannot show, e.g. one with settings or a state */ +export function truthTableOf( + definition: IntrinsicComponentDefinition, +): TruthTable | null { + const { behavior } = definition; + if (behavior?.kind !== "combinational") return null; + const inputKeys = Object.keys(definition.inputPin); + const outputKeys = Object.keys(definition.outputPin); + const isEachPinInferred = definition.allPins.every( + (pin) => + IntrinsicComponentDefinition.getPinAttributesByPinId(pin.id) + ?.bitWidthPolicy.type === "inferred", + ); + if ( + !isEachPinInferred || + inputKeys.length > maxInputCount || + outputKeys.length === 0 + ) { + return null; + } + + const rows = Array.from({ length: 2 ** inputKeys.length }, (_, n) => { + const inputs = inputKeys.map( + (_, i) => ((n >> (inputKeys.length - 1 - i)) & 1) === 1, + ); + const values = behavior.evaluate( + Object.fromEntries( + inputKeys.map((key, i) => [key, [[nullthrows(inputs[i])]]]), + ), + { + outputBitWidths: mapValues(definition.outputPin, () => [1]), + config: definition.initialConfig, + }, + ); + return { + inputs, + outputs: outputKeys.map((key) => + nullthrows(values[key]?.[0]?.[0], `No value for ${key}`), + ), + }; + }); + const nameOf = (pins: Record, key: string) => + nullthrows(pins[key]).name; + return { + inputs: inputKeys.map((key) => nameOf(definition.inputPin, key)), + outputs: outputKeys.map((key) => nameOf(definition.outputPin, key)), + rows, + }; +} From 84b47fef68ccdccb97f2ef6195a9811c508163ca Mon Sep 17 00:00:00 2001 From: Rn86222 Date: Thu, 19 Nov 2026 22:30:20 +0900 Subject: [PATCH 2/5] fix: write a bit as ON or OFF everywhere, and address the review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The truth tables and the check's messages said オン and オフ in Japanese while the nodes in play mode said ON and OFF. One function now labels a bit for all of them. From the review: OR is described as on when at least one input is on, so it is not read as XOR; a stage lists the parts it lets the player use; a part with no inputs gets no divider; the info button and its popover are announced as a dialog; and tests cover parts that get no table and asking any part for one. Co-Authored-By: Claude Opus 5.5 --- src/common/bitLabel.ts | 11 +++++++++ src/components/PartDescription.tsx | 24 ++++++++++++------- src/i18n/en.ts | 6 ++--- src/i18n/ja.ts | 8 +++---- .../Node/components/Interface/Value/index.tsx | 3 ++- src/pages/edit/SidePanel.tsx | 9 +++++++ src/pages/tutorial/stage.tsx | 13 ++++------ src/store/intrinsics/truthTable.test.ts | 24 ++++++++++++++++++- 8 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 src/common/bitLabel.ts diff --git a/src/common/bitLabel.ts b/src/common/bitLabel.ts new file mode 100644 index 0000000..b40e885 --- /dev/null +++ b/src/common/bitLabel.ts @@ -0,0 +1,11 @@ +import type { SimulationValue } from "../store/simulation"; + +export function bitLabel(bit: boolean): string { + return bit ? "ON" : "OFF"; +} + +export function valueLabel(value: SimulationValue): string { + const [bit] = value; + if (value.length === 1 && bit !== undefined) return bitLabel(bit); + return value.map((b) => (b ? "1" : "0")).join(""); +} diff --git a/src/components/PartDescription.tsx b/src/components/PartDescription.tsx index 4606c46..b05af72 100644 --- a/src/components/PartDescription.tsx +++ b/src/components/PartDescription.tsx @@ -1,4 +1,5 @@ import { Box, Typography } from "@mui/material"; +import { bitLabel } from "../common/bitLabel"; import { type Messages, useMessages } from "../i18n"; import type { CCComponent } from "../store/component"; import { definitionByComponentId } from "../store/intrinsics/definitions"; @@ -22,16 +23,23 @@ export function PartDescription({ component }: { component: CCComponent }) { const summary = partSummary(messages, component); const definition = definitionByComponentId.get(component.id); const table = definition && truthTableOf(definition); - const columns = table ? [...table.inputs, ...table.outputs] : []; + const columns = table + ? [ + ...table.inputs.map((name) => `in-${name}`), + ...table.outputs.map((name) => `out-${name}`), + ] + : []; const cellSx = (column: number) => ({ px: 1, py: 0.25, textAlign: "center", - ...(column === table?.inputs.length && { - borderLeft: 1, - borderColor: "divider", - }), + ...(table && + table.inputs.length > 0 && + column === table.inputs.length && { + borderLeft: 1, + borderColor: "divider", + }), }) as const; return ( @@ -45,9 +53,9 @@ export function PartDescription({ component }: { component: CCComponent }) { > - {columns.map((name, i) => ( + {[...table.inputs, ...table.outputs].map((name, i) => ( @@ -71,7 +79,7 @@ export function PartDescription({ component }: { component: CCComponent }) { }), }} > - {value ? messages.common.on : messages.common.off} + {bitLabel(value)} ))} diff --git a/src/i18n/en.ts b/src/i18n/en.ts index 6a4b701..a0c2634 100644 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -16,20 +16,18 @@ export const en = { cancel: "Cancel", apply: "Apply", delete: "Delete", - on: "on", - off: "off", }, parts: { summaries: { AND: "On only when both inputs are on", - OR: "On when either input is on", + OR: "On when at least one input is on", NOT: "Turns on into off, and off into on", XOR: "On when exactly one input is on", TRUE: "Always on", FALSE: "Always off", }, about: (name: string) => `About ${name}`, - ofStage: "Parts for this stage", + ofStage: "Parts you can use in this stage", }, header: { language: "Language", diff --git a/src/i18n/ja.ts b/src/i18n/ja.ts index 5a35021..2ed1664 100644 --- a/src/i18n/ja.ts +++ b/src/i18n/ja.ts @@ -12,20 +12,18 @@ export const ja: Messages = { cancel: "キャンセル", apply: "適用", delete: "削除", - on: "オン", - off: "オフ", }, parts: { summaries: { - AND: "両方オンのときだけオン", - OR: "どちらかがオンならオン", + AND: "両方がオンのときだけオン", + OR: "少なくとも一方がオンならオン", NOT: "オンとオフを逆にする", XOR: "片方だけがオンのときオン", TRUE: "いつもオン", FALSE: "いつもオフ", }, about: (name) => `${name} について`, - ofStage: "この課題で使う部品", + ofStage: "この課題で使える部品", }, header: { language: "言語", diff --git a/src/pages/edit/Editor/renderer/Node/components/Interface/Value/index.tsx b/src/pages/edit/Editor/renderer/Node/components/Interface/Value/index.tsx index e9de385..2fac409 100644 --- a/src/pages/edit/Editor/renderer/Node/components/Interface/Value/index.tsx +++ b/src/pages/edit/Editor/renderer/Node/components/Interface/Value/index.tsx @@ -1,4 +1,5 @@ import clsx from "clsx"; +import { bitLabel } from "../../../../../../../../common/bitLabel"; import type { SimulationValue } from "../../../../../../../../store/simulation"; import { stringifySimulationValue } from "../../../../../store/slices/core"; import { CCComponentEditorRendererNodeInterfaceRendererValueEditButton } from "./EditButton"; @@ -45,7 +46,7 @@ export function CCComponentEditorRendererNodeInterfaceRendererValue({ > {value.map((bit, index) => { const className = clsx(styles.bit, bit && styles.on); - const label = isSingleBit ? (bit ? "ON" : "OFF") : bit ? "1" : "0"; + const label = isSingleBit ? bitLabel(bit) : bit ? "1" : "0"; return onChange ? (