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
34 changes: 34 additions & 0 deletions src/components/PartDescription.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { describe, expect, it } from "vitest";
import { messagesByLanguage } from "../i18n/messages";
import {
and,
and_n,
broadcast,
decompose,
display,
} from "../store/intrinsics/definitions";
import { pinNotesOf } from "./PartDescription";

const { en, ja } = messagesByLanguage;

describe("pinNotesOf", () => {
it("tells how to set a pin whose bit width the player chooses", () => {
expect(pinNotesOf(en, broadcast)).toEqual([
'The number in the Out pin is its number of bits. To change it, click the pin while editing, change the field and press "Apply".',
]);
});

it("tells how to add pins where a pin can be split", () => {
expect(pinNotesOf(en, and_n)).toEqual([
'The number in the In pin is its number of bits. To change it, click the pin while editing, change the field and press "Apply". Each field added with "Add" becomes another In pin.',
]);
expect(pinNotesOf(ja, decompose)).toEqual([
"Out のピンの中の数字は、そのピンのビット数。編集中にピンをクリックし、出てきた欄で変えて「適用」を押す。「追加」で欄を増やすと、Out のピンも増える。",
]);
});

it("says nothing of pins whose bit widths follow from elsewhere", () => {
expect(pinNotesOf(en, and)).toEqual([]);
expect(pinNotesOf(en, display)).toEqual([]);
});
});
30 changes: 25 additions & 5 deletions src/components/PartDescription.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { Box, Typography } from "@mui/material";
import { type Messages, useMessages } from "../i18n";
import type { CCComponent } from "../store/component";
import { IntrinsicComponentDefinition } from "../store/intrinsics/base";
import { definitionByComponentId } from "../store/intrinsics/definitions";
import { type TruthTable, truthTableOf } from "../store/intrinsics/truthTable";
import { TruthTableView } from "./TruthTableView";

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;
return type ? messages.parts.summaries[type] : null;
}

/** How to set the pins whose bit widths the player chooses, which nothing else tells */
export function pinNotesOf(
messages: Messages,
definition: IntrinsicComponentDefinition,
): string[] {
return definition.allPins.flatMap((pin) => {
const policy = IntrinsicComponentDefinition.getPinAttributesByPinId(
pin.id,
)?.bitWidthPolicy;
return policy?.type === "configurable"
? [messages.parts.pinBitWidth(pin.name, policy.isSplittable)]
: [];
});
}

export function PartDescription({ component }: { component: CCComponent }) {
Expand All @@ -24,6 +36,7 @@ export function PartDescription({ component }: { component: CCComponent }) {
<PartDescriptionView
name={component.name}
summary={partSummary(messages, component)}
notes={definition ? pinNotesOf(messages, definition) : []}
table={definition ? truthTableOf(definition) : null}
/>
);
Expand All @@ -32,10 +45,12 @@ export function PartDescription({ component }: { component: CCComponent }) {
export function PartDescriptionView({
name,
summary,
notes = [],
table,
}: {
name: string;
summary: string | null;
notes?: readonly string[];
table: TruthTable | null;
}) {
return (
Expand All @@ -48,6 +63,11 @@ export function PartDescriptionView({
{name}
</Typography>
{summary && <Typography variant="body2">{summary}</Typography>}
{notes.map((note) => (
<Typography key={note} variant="body2" sx={{ mt: 0.5 }}>
{note}
</Typography>
))}
{table && (
<Box sx={{ mt: 0.5, overflowX: "auto" }}>
<TruthTableView table={table} />
Expand Down
38 changes: 26 additions & 12 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CCIntrinsicComponentType } from "../store/intrinsics/types";
import { englishSimulationErrorTexts } from "../store/simulationErrorTexts";
import type { TutorialPartId, TutorialStageId } from "../tutorial/stages";

Expand Down Expand Up @@ -98,19 +99,32 @@ export const en = {
},
parts: {
summaries: {
AND: "ON only when both inputs are 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",
DECOMPOSE: "Splits a value of several bits, from the highest digit down",
AGGREGATE: "Joins values into one, the upper pins as the higher digits",
AND: "ON only when both inputs are 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.",
DECOMPOSE:
"Splits a value of several bits, the higher digits to the upper pins.",
AGGREGATE: "Joins values into one, the upper pins as the higher digits.",
FLIPFLOP:
"Outputs what it received at the time step before (0 at the first time step)",
CONST: "Always outputs the value written in it",
BROADCAST: "Repeats a one-bit value over several bits",
},
"Outputs what it received at the time step before (0 at the first time step).",
CONST: "Always outputs the value written in it.",
BROADCAST: "Repeats a one-bit value over several bits.",
ANDN: "ON only when every bit of In is 1.",
ORN: "ON when any bit of In is 1.",
SEVEN_SEGMENT:
"Turns the 4-bit value of In into the shape of a hexadecimal character, 0 to F. Out is a 28-bit value of a picture 4 pixels wide and 7 high, with 1 for the character. While editing, the gear button sets its pixel order.",
DISPLAY:
"While playing, shows the value of Pixels as a picture, one bit per pixel (1 is black, 0 is white). From the highest digit, the pixels go from the top left row by row in row-major order, or column by column in column-major order. While editing, the gear button sets the resolution and the pixel order.",
INPUT:
"Adds an input to the component it is placed in. Where that component is placed as a part, the input is a pin on its left. While playing, its value can be changed on the placed Input.",
OUTPUT:
"Adds an output to the component it is placed in. Where that component is placed as a part, the output is a pin on its right. While playing, it shows the value it receives.",
} satisfies Record<CCIntrinsicComponentType, string>,
pinBitWidth: (pinName: string, canAddPins: boolean) =>
`The number in the ${pinName} pin is its number of bits. To change it, click the pin while editing, change the field and press "Apply".${canAddPins ? ` Each field added with "Add" becomes another ${pinName} pin.` : ""}`,
about: (name: string) => `About ${name}`,
ofStage: "Available parts",
},
Expand Down
34 changes: 23 additions & 11 deletions src/i18n/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,30 @@ export const ja: Messages = {
},
parts: {
summaries: {
AND: "両方が ON のときだけ ON",
OR: "少なくとも一方が ON なら ON",
NOT: "ON と OFF を逆にする",
XOR: "片方だけが ON のとき ON",
TRUE: "いつも ON",
FALSE: "いつも OFF",
DECOMPOSE: "何ビットもある値を、上の桁から順に分ける",
AGGREGATE: "上のピンを上の桁として、値を1つにまとめる",
FLIPFLOP: "1つ前のステップで受け取った値を出す(最初のステップでは 0)",
CONST: "中に書いた値をいつも出す",
BROADCAST: "1ビットの値を、同じ値が並んだ何ビットもの値に広げる",
AND: "両方が ON のときだけ ON。",
OR: "少なくとも一方が ON なら ON。",
NOT: "ON と OFF を逆にする。",
XOR: "片方だけが ON のとき ON。",
TRUE: "いつも ON。",
FALSE: "いつも OFF。",
DECOMPOSE: "何ビットもある値を、上の桁から順に、上のピンへ分ける。",
AGGREGATE: "上のピンを上の桁として、値を1つにまとめる。",
FLIPFLOP: "1つ前のステップで受け取った値を出す(最初のステップでは 0)。",
CONST: "中に書いた値をいつも出す。",
BROADCAST: "1ビットの値を、同じ値が並んだ何ビットもの値に広げる。",
ANDN: "In のビットがすべて 1 のときだけ ON。",
ORN: "In のどれかのビットが 1 なら ON。",
SEVEN_SEGMENT:
"In の4ビットの値を、16進数の1文字(0〜F)の形にする。Out は、横4・縦7ピクセルの絵を表す28ビットの値で、文字の部分が 1。ピクセルの並び順は、編集中に歯車のボタンで変えられる。",
DISPLAY:
"再生中、Pixels の値を、1ビットを1ピクセルとして絵にする(1 は黒、0 は白)。上の桁から順に、行優先なら左上から横に1行ずつ、列優先なら縦に1列ずつ並ぶ。解像度とピクセルの並び順は、編集中に歯車のボタンで変えられる。",
INPUT:
"置いた先のコンポーネントに、入力を1つ足す。そのコンポーネントを部品として置くと、左側のピンになる。再生中は、置いた Input の上で値を変えられる。",
OUTPUT:
"置いた先のコンポーネントに、出力を1つ足す。そのコンポーネントを部品として置くと、右側のピンになる。再生中は、受け取った値を見せる。",
},
pinBitWidth: (pinName, canAddPins) =>
`${pinName} のピンの中の数字は、そのピンのビット数。編集中にピンをクリックし、出てきた欄で変えて「適用」を押す。${canAddPins ? `「追加」で欄を増やすと、${pinName} のピンも増える。` : ""}`,
about: (name) => `${name} について`,
ofStage: "使える部品",
},
Expand Down
Loading