[API] Add convenience methods to LGraphNode (#717)

- getInputOnPos
- getOutputOnPos
- getSlotOnPos
- Follow-up on #716 
- Uses more explicit names for class methods moved to module functions
This commit is contained in:
filtered
2025-03-08 01:38:55 +11:00
committed by GitHub
parent 3e44f6a0c1
commit 20bdc47550
2 changed files with 38 additions and 5 deletions

View File

@@ -26,12 +26,13 @@ import type { CanvasMouseEvent } from "./types/events"
import type { ISerialisedNode } from "./types/serialisation"
import type { IBaseWidget, IWidget, IWidgetOptions, TWidgetType, TWidgetValue } from "./types/widgets"
import { getNodeInputOnPos, getNodeOutputOnPos } from "./canvas/measureSlots"
import { NullGraphError } from "./infrastructure/NullGraphError"
import { BadgePosition, LGraphBadge } from "./LGraphBadge"
import { LGraphCanvas } from "./LGraphCanvas"
import { type LGraphNodeConstructor, LiteGraph } from "./litegraph"
import { LLink } from "./LLink"
import { createBounds, isInRect, isInRectangle, snapPoint } from "./measure"
import { createBounds, isInRect, isInRectangle, isPointInRect, snapPoint } from "./measure"
import { ConnectionColorContext, isINodeInputSlot, isWidgetInputSlot, NodeInputSlot, NodeOutputSlot, serializeSlot, toNodeSlotClass } from "./NodeSlot"
import {
LGraphEventMode,
@@ -1818,6 +1819,38 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
}
/**
* Returns the input slot at the given position. Uses full 20 height, and approximates the label length.
* @param pos The graph co-ordinates to check
* @returns The input slot at the given position if found, otherwise `undefined`.
*/
getInputOnPos(pos: Point): INodeInputSlot | undefined {
return getNodeInputOnPos(this, pos[0], pos[1])?.input
}
/**
* Returns the output slot at the given position. Uses full 20x20 box for the slot.
* @param pos The graph co-ordinates to check
* @returns The output slot at the given position if found, otherwise `undefined`.
*/
getOutputOnPos(pos: Point): INodeOutputSlot | undefined {
return getNodeOutputOnPos(this, pos[0], pos[1])?.output
}
/**
* Returns the input or output slot at the given position.
*
* Tries {@link getNodeInputOnPos} first, then {@link getNodeOutputOnPos}.
* @param pos The graph co-ordinates to check
* @returns The input or output slot at the given position if found, otherwise `undefined`.
*/
getSlotOnPos(pos: Point): INodeInputSlot | INodeOutputSlot | undefined {
if (!isPointInRect(pos, this.boundingRect)) return
return this.getInputOnPos(pos) ?? this.getOutputOnPos(pos)
}
/**
* @deprecated Use {@link getSlotOnPos} instead.
* checks if a point is inside a node slot, and returns info about which slot
* @param x
* @param y

View File

@@ -3,7 +3,7 @@ import type { LGraphNode } from "@/LGraphNode"
import { isInRectangle } from "@/measure"
export function getInputOnPos(node: LGraphNode, x: number, y: number): { index: number, input: INodeInputSlot, pos: Point } | undefined {
export function getNodeInputOnPos(node: LGraphNode, x: number, y: number): { index: number, input: INodeInputSlot, pos: Point } | undefined {
const { inputs } = node
if (!inputs) return
@@ -28,7 +28,7 @@ export function getInputOnPos(node: LGraphNode, x: number, y: number): { index:
}
}
export function getOutputOnPos(node: LGraphNode, x: number, y: number): { index: number, output: INodeOutputSlot, pos: Point } | undefined {
export function getNodeOutputOnPos(node: LGraphNode, x: number, y: number): { index: number, output: INodeOutputSlot, pos: Point } | undefined {
const { outputs } = node
if (!outputs) return
@@ -58,7 +58,7 @@ export function isOverNodeInput(
canvasy: number,
slot_pos?: Point,
): number {
const result = getInputOnPos(node, canvasx, canvasy)
const result = getNodeInputOnPos(node, canvasx, canvasy)
if (!result) return -1
if (slot_pos) {
@@ -78,7 +78,7 @@ export function isOverNodeOutput(
canvasy: number,
slot_pos?: Point,
): number {
const result = getOutputOnPos(node, canvasx, canvasy)
const result = getNodeOutputOnPos(node, canvasx, canvasy)
if (!result) return -1
if (slot_pos) {