Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored Pin into ComponentPin and NodePin for better modularity #47

Merged
merged 9 commits into from
Jun 8, 2024
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
4 changes: 1 addition & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import { Box } from "@mui/material";
import { useState } from "react";
import "@pixi/math-extras";
import GlobalHeader from "./components/GlobalHeader";
import { useStore } from "./store/react";
import type { CCComponentId } from "./store/component";
import EditPage from "./pages/edit";
import HomePage from "./pages/home";

export default function App() {
const store = useStore();
const [editedComponentId, setEditedComponentId] =
useState<CCComponentId | null>(store.components.rootComponentId);
useState<CCComponentId | null>(null);

return (
<Box
Expand Down
41 changes: 24 additions & 17 deletions src/pages/edit/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
KeyboardDoubleArrowRight,
PlayArrow,
} from "@mui/icons-material";
import nullthrows from "nullthrows";
import { useStore } from "../../../store/react";
import CCComponentEditorRenderer from "./renderer";
import { CCComponentStore, type CCComponentId } from "../../../store/component";
Expand All @@ -44,7 +45,7 @@ function CCComponentEditorContent({
const rendererRef = useRef<CCComponentEditorRenderer>();
const containerRef = useRef<HTMLDivElement>(null);
const overlayAreaRef = useRef<HTMLDivElement>(null);
const store = useStore();
const { store } = useStore();
const componentEditorStore = useComponentEditorStore();
const componentEditorState = componentEditorStore();
const component = store.components.get(componentId);
Expand Down Expand Up @@ -211,6 +212,7 @@ function CCComponentEditorContent({
const newComponent = CCComponentStore.create({
name: "New Component",
});
store.components.register(newComponent);
const oldToNewNodeIdMap = new Map<CCNodeId, CCNodeId>();
const newNodes = oldNodes.map<CCNode>((oldNode) => {
const newNode = CCNodeStore.create({
Expand All @@ -223,31 +225,35 @@ function CCComponentEditorContent({
oldToNewNodeIdMap.set(oldNode.id, newNode.id);
return newNode;
});
for (const node of newNodes) store.nodes.register(node);
const newConnections = oldConnections.flatMap<CCConnection>(
(oldConnection) => {
const fromNodeId = oldToNewNodeIdMap.get(
oldConnection.from.nodeId
const oldFromNodePin = nullthrows(
store.nodePins.get(oldConnection.from)
);
const oldToNodePin = nullthrows(
store.nodePins.get(oldConnection.to)
);
const toNodeId = oldToNewNodeIdMap.get(
oldConnection.to.nodeId
const newFromNodeId = nullthrows(
oldToNewNodeIdMap.get(oldFromNodePin.nodeId)
);
const newToNodeId = nullthrows(
oldToNewNodeIdMap.get(oldToNodePin.nodeId)
);
if (!fromNodeId || !toNodeId) return [];
return CCConnectionStore.create({
parentComponentId: newComponent.id,
from: {
nodeId: fromNodeId,
pinId: oldConnection.from.pinId,
},
to: {
nodeId: toNodeId,
pinId: oldConnection.to.pinId,
},
from: store.nodePins.getByImplementationNodeIdAndPinId(
newFromNodeId,
oldFromNodePin.componentPinId
).id,
to: store.nodePins.getByImplementationNodeIdAndPinId(
newToNodeId,
oldToNodePin.componentPinId
).id,
bentPortion: oldConnection.bentPortion,
});
}
);
store.components.register(newComponent);
for (const node of newNodes) store.nodes.register(node);
for (const connection of newConnections)
store.connections.register(connection);
store.connections.unregister([
Expand Down Expand Up @@ -332,8 +338,9 @@ function CCComponentEditorContent({
}

export default function CCComponentEditor(props: CCComponentEditorProps) {
const { componentId } = props;
return (
<ComponentEditorStoreProvider>
<ComponentEditorStoreProvider componentId={componentId}>
<CCComponentEditorContent {...props} />
</ComponentEditorStoreProvider>
);
Expand Down
90 changes: 42 additions & 48 deletions src/pages/edit/Editor/renderer/componentPin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as PIXI from "pixi.js";
import invariant from "tiny-invariant";
import type { EditorModePlay } from "../store";
import type { CCNodeId } from "../../../../store/node";
import type { CCPinId } from "../../../../store/pin";
import type { CCComponentPinId } from "../../../../store/componentPin";
import {
activeColor,
editorGridColor,
Expand All @@ -18,18 +18,15 @@ type CCComponentEditorRendererComponentPinProps = {
context: CCComponentEditorRendererContext;
pixiParentContainer: PIXI.Container;
nodeId: CCNodeId; // TODO: this might be unnecessary
pinId: CCPinId;
pinId: CCComponentPinId;
position: PIXI.Point;
simulation: () => Map<CCPinId, boolean[]> | null;
};

/**
* Class for rendering component pin
*/
export default class CCComponentEditorRendererComponentPin extends CCComponentEditorRendererBase {
readonly #nodeId: CCNodeId;

readonly #pinId: CCPinId;
readonly #componentPinId: CCComponentPinId;

position: PIXI.Point;

Expand All @@ -45,8 +42,6 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd

readonly #unsubscribeComponentEditorStore: () => void;

readonly #simulation: () => Map<CCPinId, boolean[]> | null;

#valueBoxWidth: number;

private static readonly drawingConstants = {
Expand All @@ -65,15 +60,16 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd
*/
constructor(props: CCComponentEditorRendererComponentPinProps) {
super(props.context);
this.#nodeId = props.nodeId;
this.#pinId = props.pinId;
this.#componentPinId = props.pinId;
this.position = props.position;
this.#simulation = props.simulation;
this.#pixiParentContainer = props.pixiParentContainer;
this.#pixiContainer = new PIXI.Container();
this.#pixiParentContainer.addChild(this.#pixiContainer);
this.#pixiGraphics = new PIXI.Graphics();
if (this.context.store.pins.get(this.#pinId)!.type === "input") {
if (
this.context.store.componentPins.get(this.#componentPinId)!.type ===
"input"
) {
// this.#pixiGraphics.interactive = true;
this.#pixiGraphics.eventMode = "dynamic";
this.#pixiGraphics.cursor = "pointer";
Expand All @@ -85,7 +81,9 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd
pixiParentContainer: this.#pixiContainer,
});
this.#pixiLabelTextBox.onChange = (value) => {
this.context.store.pins.update(this.#pinId, { name: value });
this.context.store.componentPins.update(this.#componentPinId, {
name: value,
});
};
this.registerChildRenderer(this.#pixiLabelTextBox);
this.#pixiLabelTextBox.fontSize =
Expand All @@ -96,7 +94,10 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd
this.#pixiValueText.style.fill =
CCComponentEditorRendererComponentPin.drawingConstants.valueColor;
this.#pixiValueText.anchor.set(0.5, 0.5);
if (this.context.store.pins.get(this.#pinId)!.type === "input") {
if (
this.context.store.componentPins.get(this.#componentPinId)!.type ===
"input"
) {
// this.#pixiValueText.interactive = true;
this.#pixiValueText.eventMode = "dynamic";
this.#pixiValueText.cursor = "pointer";
Expand All @@ -108,8 +109,8 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd
this.context.componentEditorStore.subscribe(this.render);
this.#valueBoxWidth =
CCComponentEditorRendererComponentPin.drawingConstants.valueBoxWidthUnit;
this.context.store.pins.on("didUpdate", (pin) => {
if (pin.id === this.#pinId) this.render();
this.context.store.componentPins.on("didUpdate", (pin) => {
if (pin.id === this.#componentPinId) this.render();
});
this.render();
}
Expand All @@ -119,12 +120,14 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd
* @param e event
*/
onClick = (e: PIXI.FederatedPointerEvent) => {
const editorState = this.context.componentEditorStore.getState();
const previousValue = editorState.getInputValue(
this.#nodeId,
this.#pinId,
this.context.store.pins.get(this.#pinId)!.bits
const componentPin = this.context.store.componentPins.get(
this.#componentPinId
);
invariant(componentPin);
invariant(componentPin.implementation);
const editorState = this.context.componentEditorStore.getState();
const previousValue = editorState.getInputValue(this.#componentPinId);
invariant(previousValue);
const increaseValue = (value: boolean[]) => {
const newValue = [...value];
for (let i = newValue.length - 1; i >= 0; i -= 1) {
Expand All @@ -134,8 +137,7 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd
return newValue;
};
editorState.setInputValue(
this.#nodeId,
this.#pinId,
this.#componentPinId,
increaseValue(previousValue)
);
e.preventDefault();
Expand All @@ -145,7 +147,7 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd
* Render the pin
*/
render = () => {
const pin = this.context.store.pins.get(this.#pinId);
const pin = this.context.store.componentPins.get(this.#componentPinId);
if (!pin) return;

this.#pixiGraphics.clear();
Expand All @@ -171,34 +173,26 @@ export default class CCComponentEditorRendererComponentPin extends CCComponentEd
this.#pixiLabelTextBox.isEditable = false;
if (pin.type === "input") {
this.#valueBoxWidth = c.valueBoxWidthUnit;
const input = editorState.getInputValue(
this.#nodeId,
this.#pinId,
pin.bits
);
const input = editorState.getInputValue(this.#componentPinId);
invariant(input);
this.#pixiValueText.text = input.map((v) => (v ? "1" : "0")).join("");
this.#pixiGraphics.beginFill(activeColor);
} else {
const output = this.#simulation();
if (output) {
const createValueText = (values: boolean[]) => {
let valueText = "";
for (let i = 0; i < values.length; i += 1) {
valueText += values[i] ? "1" : "0";
}
return valueText;
};
invariant(pin.implementation.type === "user");
const implementationPinId = pin.implementation.pinId;
for (const [key, values] of output) {
if (key === implementationPinId) {
this.#pixiValueText.text = createValueText(values);
this.#valueBoxWidth =
c.valueBoxWidthUnit +
((values.length - 1) * c.valueBoxWidthUnit) / 4;
break;
}
const createValueText = (values: boolean[]) => {
let valueText = "";
for (let i = 0; i < values.length; i += 1) {
valueText += values[i] ? "1" : "0";
}
return valueText;
};
const outputValue = editorState.getComponentPinValue(
this.#componentPinId
);
if (outputValue) {
this.#pixiValueText.text = createValueText(outputValue);
this.#valueBoxWidth =
c.valueBoxWidthUnit +
((outputValue.length - 1) * c.valueBoxWidthUnit) / 4;
this.#pixiGraphics.beginFill(grayColor.darken2);
} else {
this.#pixiValueText.text = "";
Expand Down
Loading
Loading