|
| 1 | +import React, { memo } from "react"; |
| 2 | +import { StateSetter, assignImm, hasFlag } from "@/src/utils/data"; |
| 3 | +import { Vec3 } from "@/src/utils/vector"; |
| 4 | +import { IComp, IEditorState, IExeComp, IExePort, PortType } from "../CpuModel"; |
| 5 | +import { ICompBuilderArgs, ICompDef } from "./CompBuilder"; |
| 6 | +import { CompRectBase } from "./RenderHelpers"; |
| 7 | +import { editCompConfig, useEditorContext } from "../Editor"; |
| 8 | +import { HexValueEditor, HexValueInputType, clampToSignedWidth } from "../displayTools/HexValueEditor"; |
| 9 | +import { CheckboxMenuTitle, ConfigMenu, MenuRow } from "./InputOutput"; |
| 10 | + |
| 11 | +export interface ICompPortConfig { |
| 12 | + portId: string; |
| 13 | + name: string; |
| 14 | + type: PortType; |
| 15 | + width: number; |
| 16 | + signed: boolean; |
| 17 | + inputOverride: boolean; |
| 18 | + inputValueOverride: number; |
| 19 | + valueMode: HexValueInputType; |
| 20 | +} |
| 21 | + |
| 22 | +export interface ICompPortData { |
| 23 | + port: IExePort; |
| 24 | + value: number; |
| 25 | +} |
| 26 | + |
| 27 | +export function createCompIoComps(args: ICompBuilderArgs) { |
| 28 | + |
| 29 | + let w = 6; |
| 30 | + let h = 6; |
| 31 | + let compPort: ICompDef<ICompPortData, ICompPortConfig> = { |
| 32 | + defId: 'comp/port', |
| 33 | + name: "Port", |
| 34 | + size: new Vec3(w, h), |
| 35 | + ports: (args, compDef) => { |
| 36 | + |
| 37 | + let internalPortDir = switchPortDir(args.type); |
| 38 | + |
| 39 | + return [ |
| 40 | + { id: 'a', name: '', pos: new Vec3(w, 3), type: internalPortDir, width: args.width }, |
| 41 | + ]; |
| 42 | + }, |
| 43 | + initConfig: () => ({ |
| 44 | + portId: '', |
| 45 | + name: '', |
| 46 | + type: PortType.Out, |
| 47 | + width: 1, |
| 48 | + signed: false, |
| 49 | + valueMode: HexValueInputType.Dec, |
| 50 | + inputOverride: false, |
| 51 | + inputValueOverride: 0, |
| 52 | + }), |
| 53 | + applyConfig(comp, args) { |
| 54 | + // let mat = rotateAboutAffineInt(args.rotate, rotateCenter); |
| 55 | + // comp.ports = comp.ports.map(p => { |
| 56 | + // return { ...p, pos: mat.mulVec3(p.pos) } |
| 57 | + // }); |
| 58 | + }, |
| 59 | + build: (builder) => { |
| 60 | + let args = builder.comp.args; |
| 61 | + let isInput = hasFlag(args.type, PortType.In); |
| 62 | + |
| 63 | + let data = builder.addData({ |
| 64 | + port: builder.getPort('a'), |
| 65 | + value: isInput && args.inputOverride ? args.inputValueOverride : 0, |
| 66 | + }); |
| 67 | + |
| 68 | + if (isInput) { |
| 69 | + builder.addPhase(({ data }) => { |
| 70 | + data.port.value = data.value; |
| 71 | + data.port.ioEnabled = true; |
| 72 | + }, [], [data.port]); |
| 73 | + |
| 74 | + } else { |
| 75 | + builder.addPhase(({ data }) => { |
| 76 | + data.value = data.port.value; |
| 77 | + data.port.ioEnabled = true; |
| 78 | + }, [data.port], []); |
| 79 | + } |
| 80 | + |
| 81 | + return builder.build(); |
| 82 | + }, |
| 83 | + renderAll: true, |
| 84 | + render: ({ comp, ctx, cvs, exeComp }) => { |
| 85 | + ctx.save(); |
| 86 | + |
| 87 | + ctx.fillStyle = 'rgb(251, 146, 60)'; |
| 88 | + // let mtx = rotateAboutAffineInt(comp.args.rotate, comp.pos.add(rotateCenter)); |
| 89 | + // ctx.transform(...mtx.toTransformParams()); |
| 90 | + |
| 91 | + ctx.beginPath(); |
| 92 | + // basic structure is a circle |
| 93 | + let p = comp.pos; |
| 94 | + let s = comp.size; |
| 95 | + let center = p.mulAdd(s, 0.5); |
| 96 | + ctx.moveTo(p.x + s.x, center.y); |
| 97 | + ctx.arc(center.x, center.y, s.x / 2, 0, 2 * Math.PI); |
| 98 | + |
| 99 | + ctx.closePath(); |
| 100 | + ctx.fill(); |
| 101 | + ctx.stroke(); |
| 102 | + ctx.restore(); |
| 103 | + }, |
| 104 | + renderDom: ({ comp, exeComp, ctx, styles }) => { |
| 105 | + |
| 106 | + return <PortEditor comp={comp} exeComp={exeComp} styles={styles} />; |
| 107 | + }, |
| 108 | + }; |
| 109 | + |
| 110 | + return [compPort]; |
| 111 | +} |
| 112 | + |
| 113 | +export function switchPortDir(dir: PortType) { |
| 114 | + let newDir = dir & ~(PortType.In | PortType.Out); |
| 115 | + |
| 116 | + if (hasFlag(dir, PortType.In)) { |
| 117 | + newDir |= PortType.Out; |
| 118 | + } |
| 119 | + if (hasFlag(dir, PortType.Out)) { |
| 120 | + newDir |= PortType.In; |
| 121 | + } |
| 122 | + |
| 123 | + return newDir; |
| 124 | +} |
| 125 | + |
| 126 | +function makeEditFunction<T, A>(setEditorState: StateSetter<IEditorState>, comp: IComp<T>, updateFn: (value: A, prev: T) => Partial<T>) { |
| 127 | + return (end: boolean, value: A) => { |
| 128 | + setEditorState(editCompConfig(end, comp, a => assignImm(a, updateFn(value, a)))); |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +const PortEditor: React.FC<{ |
| 133 | + comp: IComp<ICompPortConfig>, |
| 134 | + exeComp: IExeComp<ICompPortData>, styles: any, |
| 135 | +}> = memo(function PortEditor({ comp, exeComp, styles }) { |
| 136 | + let { setEditorState } = useEditorContext(); |
| 137 | + |
| 138 | + let editIsOverriden = makeEditFunction(setEditorState, comp, (value: boolean) => ({ inputOverride: value })); |
| 139 | + let editBitWidth = makeEditFunction(setEditorState, comp, (value: number) => ({ width: value, inputValueOverride: clampToSignedWidth(comp.args.inputValueOverride ?? 0, value, comp.args.signed) })); |
| 140 | + let editSigned = makeEditFunction(setEditorState, comp, (value: boolean) => ({ signed: value, inputValueOverride: clampToSignedWidth(comp.args.inputValueOverride ?? 0, comp.args.width, value) })); |
| 141 | + let editPortType = makeEditFunction(setEditorState, comp, (isInputPort: boolean, prev) => { |
| 142 | + let type = prev.type; |
| 143 | + if (isInputPort) { |
| 144 | + type |= PortType.In; |
| 145 | + type &= ~PortType.Out; |
| 146 | + } else { |
| 147 | + type |= PortType.Out; |
| 148 | + type &= ~PortType.In; |
| 149 | + } |
| 150 | + return { type }; |
| 151 | + }); |
| 152 | + |
| 153 | + function editValueOverride(end: boolean, value: number, valueMode: HexValueInputType) { |
| 154 | + setEditorState(editCompConfig(end, comp, a => assignImm(a, { inputValueOverride: value, valueMode }))); |
| 155 | + } |
| 156 | + |
| 157 | + let isInput = hasFlag(comp.args.type, PortType.In); |
| 158 | + let isInputOverride = comp.args.inputOverride; |
| 159 | + |
| 160 | + return <CompRectBase comp={comp} className={"pr-1"} hideHover={true}> |
| 161 | + {isInput && <HexValueEditor |
| 162 | + className="absolute inset-0 px-2" |
| 163 | + inputType={comp.args.valueMode} |
| 164 | + value={comp.args.inputValueOverride} |
| 165 | + update={editValueOverride} |
| 166 | + minimalBackground |
| 167 | + inputClassName="text-center" |
| 168 | + maxBits={comp.args.width} |
| 169 | + padBits={comp.args.width} |
| 170 | + signed={comp.args.signed} |
| 171 | + hidePrefix |
| 172 | + />} |
| 173 | + {!isInput && <HexValueEditor |
| 174 | + className="absolute inset-0 px-2" |
| 175 | + value={exeComp?.data.value ?? 0} |
| 176 | + minimalBackground |
| 177 | + inputClassName="text-center" |
| 178 | + update={(end, _val, inputType) => editValueOverride(end, comp.args.inputValueOverride, inputType)} |
| 179 | + inputType={comp.args.valueMode} |
| 180 | + padBits={comp.args.width} |
| 181 | + signed={comp.args.signed} |
| 182 | + readonly |
| 183 | + hidePrefix |
| 184 | + />} |
| 185 | + <ConfigMenu className={"absolute top-[12px] right-[12px]"}> |
| 186 | + <MenuRow title={<CheckboxMenuTitle title="Input" value={isInput} update={editPortType} />} /> |
| 187 | + <MenuRow title={<CheckboxMenuTitle title="Override Value" value={isInputOverride} update={editIsOverriden} />} disabled={!isInput}> |
| 188 | + <HexValueEditor |
| 189 | + inputType={comp.args.valueMode} |
| 190 | + value={comp.args.inputValueOverride} |
| 191 | + update={editValueOverride} |
| 192 | + maxBits={comp.args.width} |
| 193 | + padBits={comp.args.width} |
| 194 | + signed={comp.args.signed} |
| 195 | + /> |
| 196 | + </MenuRow> |
| 197 | + <MenuRow title={"Bit Width"}> |
| 198 | + <HexValueEditor inputType={HexValueInputType.Dec} hidePrefix value={comp.args.width} update={editBitWidth} /> |
| 199 | + </MenuRow> |
| 200 | + <MenuRow title={<CheckboxMenuTitle title="Signed" value={comp.args.signed} update={editSigned} />} /> |
| 201 | + </ConfigMenu> |
| 202 | + </CompRectBase>; |
| 203 | +}); |
0 commit comments