Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export const en = {
next: "Next stage",
backToStages: "Back to the stages",
missingPin: (pinType: "input" | "output", pinName: string) =>
`There is no ${pinType} pin named ${pinName}. "Reset this stage" brings back how the stage started.`,
`The ${pinType} ${pinName} on the ${pinType === "input" ? "left" : "right"} is missing. Reset this stage to bring back how it started.`,
wrongBitWidth: (pinName: string, expected: number, actual: number) =>
`${pinName} should have ${bitCount(expected)}, but it has ${bitCount(actual)}. Check what it is connected to.`,
wrongOutput: (
Expand All @@ -239,8 +239,7 @@ export const en = {
`At time step ${stepNumber}${inputs.length > 0 ? ` (${listOf(inputs.map(({ name, value }) => `${name} is ${value}`))})` : ""}, ${pinName} should be ${expected}, but it ${actual === null ? "has no value" : `is ${actual}`}.`,
checkFailed:
"Something went wrong while checking. Change the circuit a little and press Check again.",
simulationFailed: (detail: string) =>
`The circuit could not be simulated. ${detail}`,
simulationFailed: (detail: string) => `The circuit cannot run. ${detail}`,
resetStage: "Reset this stage",
resetStageDialog: {
title: "Reset this stage?",
Expand Down
16 changes: 7 additions & 9 deletions src/i18n/ja.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { Messages } from "./en";

const bitWidths = (widths: readonly number[]) => `${widths.join("/")} ビット`;
const fixOrFeedBack =
"線をつなぎ直すか、値をフィードバックさせたい場合はループに FlipFlop を入れてください。";

export const ja: Messages = {
languageName: "日本語",
common: {
Expand Down Expand Up @@ -132,7 +129,7 @@ export const ja: Messages = {
next: "次の課題へ",
backToStages: "課題の一覧へ",
missingPin: (pinType, pinName) =>
`${pinName} という名前の${pinType === "input" ? "入力" : "出力"}ピンが見つかりません。「この課題をやり直す」で最初の状態に戻せます。`,
`${pinType === "input" ? "左の入力" : "右の出力"} ${pinName} が見つかりません。この課題を最初の状態に戻してください。`,
wrongBitWidth: (pinName, expected, actual) =>
`${pinName} は ${expected}ビットのはずですが、${actual}ビットになっています。つないだ先を確かめてください。`,
wrongOutput: (inputs, pinName, expected, actual) =>
Expand All @@ -141,8 +138,7 @@ export const ja: Messages = {
`${stepNumber} ステップ目${inputs.length > 0 ? `(${inputs.map(({ name, value }) => `${name} が ${value}`).join("、")})` : ""}に、${pinName} が ${expected} のはずが${actual === null ? "、値がありませんでした" : ` ${actual} でした`}。`,
checkFailed:
"確かめる途中で問題が起きました。回路を少し変えてから、もう一度押してください。",
simulationFailed: (detail) =>
`回路をシミュレーションできませんでした。${detail}`,
simulationFailed: (detail) => `回路を動かせません。${detail}`,
resetStage: "この課題をやり直す",
resetStageDialog: {
title: "この課題をやり直しますか?",
Expand Down Expand Up @@ -293,12 +289,14 @@ export const ja: Messages = {
},
simulation: {
nodeName: (outerToInner) => outerToInner.join(" 内の "),
interfaceNode: (type, pin) =>
`${type === "input" ? "入力" : "出力"} ${pin}`,
sentences: (sentences) => sentences.join(""),
loop: `赤い枠のノードがループになっていて、値が決まりません。ループの${fixOrFeedBack}`,
loop: "赤い枠の部品は、自分の出力が線をたどって自分の入力に戻ってきます。出力を決めるのにその出力自身が必要になるので、このままでは動きません。線をつなぎ直すか、その途中に FlipFlop を入れてください。",
loopInside: (place, component) =>
`${place} の中にループがあり、値が決まりません。${component} を開いて${fixOrFeedBack}`,
`${place} の中に、自分の出力が線をたどって自分の入力に戻ってくる部品があります。出力を決めるのにその出力自身が必要になるので、このままでは動きません。${component} を開いて線をつなぎ直すか、その途中に FlipFlop を入れてください。`,
unconnectedPin: (pin, node) =>
`${node} の入力ピン ${pin} がつながっていません。`,
`${node} の ${pin} のピンがつながっていません。`,
unconnectedNode: (node) => `${node} がつながっていません。`,
openToConnect: (component) => `${component} を開いてつないでください。`,
bitWidthConflict: (component) =>
Expand Down
27 changes: 26 additions & 1 deletion src/i18n/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,32 @@ describe("Japanese messages", () => {
);

expect(error.describe(store, ja.simulation)).toBe(
"Half 内の And の入力ピン B がつながっていません。Half を開いてつないでください。",
"Half 内の And の B のピンがつながっていません。Half を開いてつないでください。",
);
});

it("names an Output node by its role, so that it is not read as a pin of the part", () => {
const store = createTestStore();
const half = registerTestComponent(store, "Half");
registerTestInputNode(store, half.id, "In");
registerTestOutputNode(store, half.id, "S");
const root = registerTestComponent(store, "Root");
const rootIn = registerTestInputNode(store, root.id, "In");
const halfNode = registerTestNode(store, root.id, half.id);
connectTestNodePins(
store,
root.id,
rootIn.out,
testNodePin(store, halfNode, testComponentPin(store, half, "In")),
);

const error = thrownBy(
() => simulateComponent(store, root.id, new Map()),
SimulationUnconnectedPinError,
);

expect(error.describe(store, ja.simulation)).toBe(
"Half 内の 出力 S がつながっていません。Half を開いてつないでください。",
);
});

Expand Down
7 changes: 7 additions & 0 deletions src/pages/edit/Editor/outlinedNodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createContext } from "react";
import type { CCNodeId } from "../../../store/node";

/** Nodes outlined as in a simulation error by something outside the editor, e.g. a check */
export const OutlinedNodeIdsContext = createContext<ReadonlySet<CCNodeId>>(
new Set(),
);
7 changes: 5 additions & 2 deletions src/pages/edit/Editor/renderer/Node/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import clsx from "clsx";
import { useState } from "react";
import { useContext, useState } from "react";
import { vector2 } from "../../../../../common/vector2";
import {
type CCIntrinsicComponentType,
Expand All @@ -8,6 +8,7 @@ import {
import type { CCNodeId } from "../../../../../store/node";
import { useStore } from "../../../../../store/react";
import { useComponent, useNode } from "../../../../../store/react/selectors";
import { OutlinedNodeIdsContext } from "../../outlinedNodes";
import { useComponentEditorStore } from "../../store";
import CCComponentEditorRendererNodePin from "../NodePin";
import { CCComponentEditorRendererNodeConstRenderer } from "./components/Const";
Expand Down Expand Up @@ -49,6 +50,7 @@ export default function CCComponentEditorRendererNode({
const node = useNode(nodeId);
const component = useComponent(node?.componentId);
const componentEditorState = useComponentEditorStore()();
const outlinedNodeIds = useContext(OutlinedNodeIdsContext);
const [dragging, setDragging] = useState(false);
const [dragStartPosition, setDragStartPosition] = useState(vector2.zero);
const [previousNodePosition, setPreviousNodePosition] = useState(
Expand Down Expand Up @@ -108,7 +110,8 @@ export default function CCComponentEditorRendererNode({
className={clsx(
styles.node,
isSelected && styles.selected,
componentEditorState.simulationError?.nodeIds.has(nodeId) &&
(componentEditorState.simulationError?.nodeIds.has(nodeId) ||
outlinedNodeIds.has(nodeId)) &&
styles.simulationError,
)}
onPointerDown={handlePointerDown}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/edit/Editor/store/slices/core/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ describe("simulation errors", () => {
const { editorMode, simulationError } = editorStore.getState();
expect(editorMode).toBe("edit");
expect(simulationError?.messageByLanguage.en).toMatch(
/^The outlined nodes form a loop/,
/^The output of each part outlined in red/,
);
expect(simulationError?.messageByLanguage.ja).toMatch(
/^赤い枠のノードがループになっていて/,
/^赤い枠の部品は、自分の出力が/,
);
expect(simulationError?.nodeIds).toEqual(new Set([first.id, second.id]));
});
Expand Down
7 changes: 3 additions & 4 deletions src/pages/edit/Editor/store/slices/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,10 @@ export const createComponentEditorStoreCoreSlice: ComponentEditorSliceCreator<
? error.message
: String(error),
),
nodeIds: new Set(
nodeIds:
error instanceof SimulationNodeError
? error.nodePaths.flatMap((path) => path.slice(0, 1))
: [],
),
? error.outlinedNodeIds
: new Set(),
},
}));
return;
Expand Down
91 changes: 68 additions & 23 deletions src/pages/tutorial/stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
Expand All @@ -31,6 +32,8 @@ import {
import { TruthTableView } from "../../components/TruthTableView";
import { useMessages } from "../../i18n";
import { definitionByComponentId } from "../../store/intrinsics/definitions";
import { SimulationNodeError } from "../../store/netlist";
import type { CCNodeId } from "../../store/node";
import { StoreScope, useStore } from "../../store/react";
import { useComponents } from "../../store/react/selectors";
import { theme } from "../../style";
Expand Down Expand Up @@ -59,6 +62,7 @@ import {
tutorialStages,
} from "../../tutorial/stages";
import CCComponentEditor from "../edit/Editor";
import { OutlinedNodeIdsContext } from "../edit/Editor/outlinedNodes";
import type { OffersComponent } from "../edit/placement";
import SidePanel from "../edit/SidePanel";

Expand All @@ -77,18 +81,39 @@ function SectionHeading({ children }: { children: ReactNode }) {

function CheckFailure({
result,
onReset,
}: {
result: Exclude<CheckResult, { type: "passed" }>;
onReset: () => void;
}) {
const { store } = useStore();
const messages = useMessages();
// The message may name a part, which is renamed when the language changes
useComponents();
return (
<Alert severity="error">{describeFailure(result, store, messages)}</Alert>
<Alert severity="error">
{describeFailure(result, store, messages)}
{/* The reset button at the end of the stage text may be out of view */}
{result.type === "missingPin" && (
<Box sx={{ mt: 1, textAlign: "end" }}>
<Button color="error" size="small" onClick={onReset}>
{messages.tutorial.resetStage}
</Button>
</Box>
)}
</Alert>
);
}

const noNodeIds: ReadonlySet<CCNodeId> = new Set();

function outlinedNodeIdsOf(result: CheckResult | null): ReadonlySet<CCNodeId> {
return result?.type === "simulationFailed" &&
result.error instanceof SimulationNodeError
? result.error.outlinedNodeIds
: noNodeIds;
}

function StageView({ stage }: { stage: TutorialStage }) {
const messages = useMessages();
const texts = messages.tutorial;
Expand Down Expand Up @@ -137,13 +162,19 @@ function StageView({ stage }: { stage: TutorialStage }) {
};
useEffect(() => () => cancelCheckRef.current?.(), []);

const outlinedNodeIds = useMemo(() => outlinedNodeIdsOf(result), [result]);

const check = () => {
setIsChecking(true);
const run = () => {
cancelCheckRef.current = null;
const checked = checkCircuit(store, stage.componentId, stage.check);
if (checked.type === "checkFailed") console.error(checked.error);
setResult(checked);
// The outline is on the stage's own circuit, which a part opened would hide
if (outlinedNodeIdsOf(checked).size > 0) {
setEditedComponentId(stage.componentId);
}
if (checked.type === "passed") markStageCleared(stage.id);
setIsChecking(false);
};
Expand Down Expand Up @@ -306,7 +337,10 @@ function StageView({ stage }: { stage: TutorialStage }) {
</>
)}
{result && result.type !== "passed" && (
<CheckFailure result={result} />
<CheckFailure
result={result}
onReset={() => setIsConfirmingReset(true)}
/>
)}
</Stack>
</Box>
Expand All @@ -325,27 +359,38 @@ function StageView({ stage }: { stage: TutorialStage }) {
/>
</Box>
<Divider orientation="vertical" />
<CCComponentEditor
key={`${resetCount}:${shownComponentId}`}
componentId={shownComponentId}
options={{
isProtectedNode: (nodeId) => isStagePinNode(store, stage, nodeId),
isOffered,
canCreateComponents: stage.allowsNewComponents,
fitsNodesInView: true,
areaToFit:
shownComponentId === stage.componentId
? stageAreaOf(stage)
: null,
hasProperties: (componentId) =>
!isStageComponent(stage, componentId),
}}
onEditComponent={setEditedComponentId}
onClose={() => {
if (shownComponentId === stage.componentId) navigate("/tutorial");
else setEditedComponentId(stage.componentId);
}}
/>
<OutlinedNodeIdsContext
value={
// The paths of the error start from the stage's own component
shownComponentId === stage.componentId
? outlinedNodeIds
: noNodeIds
}
>
<CCComponentEditor
key={`${resetCount}:${shownComponentId}`}
componentId={shownComponentId}
options={{
isProtectedNode: (nodeId) =>
isStagePinNode(store, stage, nodeId),
isOffered,
canCreateComponents: stage.allowsNewComponents,
fitsNodesInView: true,
areaToFit:
shownComponentId === stage.componentId
? stageAreaOf(stage)
: null,
hasProperties: (componentId) =>
!isStageComponent(stage, componentId),
}}
onEditComponent={setEditedComponentId}
onClose={() => {
if (shownComponentId === stage.componentId)
navigate("/tutorial");
else setEditedComponentId(stage.componentId);
}}
/>
</OutlinedNodeIdsContext>
{isConfirmingReset && (
<ConfirmDialog
title={texts.resetStageDialog.title}
Expand Down
Loading
Loading