A port can take its type from what is plugged into it (#207)
Until now every port of "First not empty" was grey and typeless, so the
editor could not tell you what actually travels through the node. The
output is now typed as soon as both inputs agree, and every any-input is
painted in the colour of its own edge. Nothing changes at runtime.
- A NodeDef can declare which of its ports carry the same value
(`sharedType: { inputs, outputs }`). The rule itself lives as a pure
function `agreedType` in `src/core/graph/portTypes.ts`.
- Only the output inherits. It becomes the shared type when every input
of the group is connected and they all carry the same type, otherwise
it stays `any`.
- The inputs stay `any` on purpose. `canConnect` checks against the port
type and a busy input is replaced by the new edge, so a typed `a`
could never be re-plugged with a text without disconnecting first.
- Chains do not propagate. When looking up what feeds an input, a node
of the same kind reports the `any` it is registered with, so two of
them in a row stay untyped and a data cycle cannot ask itself in a
circle.
- An edge that the new type makes invalid stays where it is and turns
red. `EdgeLayer` already paints a mismatching edge with `danger`,
nothing is disconnected on its own.
- New `PortDef.paintType`: colour only, never truth. It is set in
`drawnDef` and read in `NodeView.redraw`, so `canConnect`, the
interpreter and the compiler keep seeing `any`. Every connected
any-input is painted, not just members of a `sharedType` group; ghost
ports keep their hollow red look.
- The pin now matches the edge that ends in it, because both take the
colour from the resolved output type of the neighbour. Mixed sources
therefore show two colours, which explains why the output stays grey.
- New hooks `__gm.graph.portTypes(id)`, `__gm.graph.edgeColor(id)` and
`__gm.themeColor(name)`. `portTypes` reports type and paint colour
separately so the type checks do not silently become colour checks.
Findings:
- `resolveDef` only appends derived ports to the registered ones, so a
fixed port cannot be retyped that way -- it would appear twice. The
shared type is therefore resolved before the ports are built.
- Answer 5 of the issue (inline field follows the type) has no case left
under variant A: an unconnected input stays `any` and keeps its text
field.
- `Item at`, `Join` and `Equals` do not profit: `Item at` has no
any-input, the other two have fixed outputs. The next candidates are
the shader arithmetic nodes, which were left alone.
Tests:
- ./check.sh -> 143 files, 2127 tests, exit 0 (was 142 / 2113)
- GM_STEPS=1-6 fresh -> ALL PASSED, 245 checks (was 242), 9 of them new
- 14 new unit tests in tests/core/shared-port-type.test.ts
- 9 new e2e checks in block 6z, one of them a pixel check straight on
the pin via pixelAt + themeColor
- Counter-probes all bite: filtering unconnected inputs out of
`agreedType` fails 3 unit tests, ignoring `inherit` fails 1, killing
the derivation in the bundle fails exactly the 2 expected e2e checks,
and never setting `paintType` fails 4 unit tests plus the 3 pin
checks (#bdbdbd instead of #e6a8ff / #6fcf97)
- Screenshot read: `a` pink, `b` green, output `value` grey, edges in
the same colours
- Check total 1302 (1299 + 3), 1293 was measured by CI at 1a3babf
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1a3babf9c5
commit
d7a2521ca7
14 files changed
+499
-19
No files matched your search
@@ -29,6 +29,7 @@ import {
|
||||
type GraphNode,
|
||||
type GraphLookup,
|
||||
MIN_ANNOTATION_SIZE,
|
||||
type PortDef,
|
||||
type PortSide,
|
||||
type PortType,
|
||||
portTypeOf,
|
||||
@@ -305,7 +306,7 @@ export class GraphCanvas extends Widget {
|
||||
for (const node of asset.graph.nodes) {
|
||||
const resolved = resolveDef(node, asset.graph, lookup);
|
||||
if (!resolved) continue;
|
||||
const def = drawnDef(node, asset.graph, resolved);
|
||||
const def = drawnDef(node, asset.graph, resolved, lookup);
|
||||
seen.add(node.id);
|
||||
let view = this.views.get(node.id);
|
||||
if (!view || view.node !== node) {
|
||||
@@ -1198,6 +1199,32 @@ export class GraphCanvas extends Widget {
|
||||
return this.edgeLayer.wasStroked(edgeId);
|
||||
}
|
||||
|
||||
/** As a hex string, so a test can name the colour it expects: `danger` marks an edge gone invalid. */
|
||||
edgeColor(edgeId: string): string | null {
|
||||
const color = this.edgeLayer.strokeColor(edgeId);
|
||||
return color === null ? null : `#${color.toString(16).padStart(6, '0')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The port types the node is drawn with – what a shared group (#207) makes of its inputs. `paint`
|
||||
* is what an input's pin is *coloured* as, which can differ: the type stays `any`, the pin follows
|
||||
* the edge feeding it.
|
||||
*/
|
||||
portTypes(nodeId: string): {
|
||||
inputs: Record<string, string>;
|
||||
outputs: Record<string, string>;
|
||||
paint: Record<string, string>;
|
||||
} | null {
|
||||
const view = this.views.get(nodeId);
|
||||
if (!view) return null;
|
||||
const side = (ports: readonly PortDef[]) => Object.fromEntries(ports.map((p) => [p.key, p.type]));
|
||||
return {
|
||||
inputs: side(view.def.inputs),
|
||||
outputs: side(view.def.outputs),
|
||||
paint: Object.fromEntries(view.def.inputs.map((p) => [p.key, p.paintType ?? p.type])),
|
||||
};
|
||||
}
|
||||
|
||||
/** The name drawn on a transition, or null when nothing is drawn on it. */
|
||||
transitionLabelDrawn(edgeId: string): string | null {
|
||||
return this.edgeLayer.drawnTransitionLabels().find((l) => l.edgeId === edgeId)?.text ?? null;
|
||||
|
||||
Reference in new issue
Block a user