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

Add CanvasPointer callback for node widgets #363

Merged
merged 3 commits into from
Dec 2, 2024
Merged

Conversation

webfiltered
Copy link
Collaborator

CanvasPointer API - Widgets

Adds onPointerDown callback to node widgets. A few benefits of the new API:

  • Simplified usage
  • Exposes callbacks like "double click", removing the need to time / measure multiple pointer events
  • Unified UX - same API as used in the rest of Litegraph
  • Honours the user's click speed and pointer accuracy settings

Usage

// Callbacks for each pointer action can be configured ahead of time
widget.onPointerDown = function (pointer, node, canvas) {
  const e = pointer.eDown
  const offsetFromNode = [
    e.canvasX - node.pos[0],
    e.canvasY - node.pos[1],
  ]

  // Click events - no overlap with drag events
  pointer.onClick = (upEvent) => {
    // Provides access to the whole lifecycle of events in every callback
    console.log(pointer.eDown)
    console.log(pointer.eMove ?? "Pointer didn't move")
    console.log(pointer.eUp)
  }
  pointer.onDoubleClick = (upEvent) => this.customFunction(upEvent)

  // Runs once before the first onDrag event
  pointer.onDragStart = () => {}
  // Receives every movement event
  pointer.onDrag = (moveEvent) => {}
  // The pointerup event of a drag
  pointer.onDragEnd = (upEvent) => {}

  // Semantics of a "finally" block (try/catch).  Once set, the block always executes.
  pointer.finally = () => {}

  // Return true to cancel regular Litegraph handling of this click / drag
  return true
}

TypeScript & JSDoc

Full documentation and typing is available in the IDE. It can be added to your JS projects using the @comfyorg/litegraph npm package and used in mainstream editors.

/** @import { IWidget } from './path/to/@comfyorg/litegraph/litegraph.d.ts' */
/** @type IWidget */
const widget = node.widgets[0]
widget.onPointerDown = function (pointer, node, canvas) { }

VS Code

image

@huchenlei huchenlei merged commit 8cda1ac into master Dec 2, 2024
4 checks passed
@huchenlei huchenlei deleted the pointers-for-widgets branch December 2, 2024 01:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants