Skip to content

Commit 2a1b25f

Browse files
authored
Merge pull request #405 from M1kep/fix/group-drag
fix: Correct bounding box calculation
2 parents 015547d + a60e26c commit 2a1b25f

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

src/litegraph.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,12 @@ export declare class LGraphNode {
825825

826826
/**
827827
* returns the bounding of the object, used for rendering purposes
828-
* @return [x, y, width, height]
828+
* @method getBounding
829+
* @param out [optional] a place to store the output, to free garbage
830+
* @param compute_outer [optional] set to true to include the shadow and connection points in the bounding calculation
831+
* @return the bounding box in format of [topleft_cornerx, topleft_cornery, width, height]
829832
*/
830-
getBounding(): Vector4;
833+
getBounding(out?: Vector4, compute_outer?: boolean): Vector4;
831834
/** checks if a point is inside the shape of a node */
832835
isPointInside(
833836
x: number,

src/litegraph.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,16 +3773,42 @@
37733773

37743774
/**
37753775
* returns the bounding of the object, used for rendering purposes
3776-
* bounding is: [topleft_cornerx, topleft_cornery, width, height]
37773776
* @method getBounding
3778-
* @return {Float32Array[4]} the total size
3777+
* @param out {Float32Array[4]?} [optional] a place to store the output, to free garbage
3778+
* @param compute_outer {boolean?} [optional] set to true to include the shadow and connection points in the bounding calculation
3779+
* @return {Float32Array[4]} the bounding box in format of [topleft_cornerx, topleft_cornery, width, height]
37793780
*/
3780-
LGraphNode.prototype.getBounding = function(out) {
3781+
LGraphNode.prototype.getBounding = function(out, compute_outer) {
37813782
out = out || new Float32Array(4);
3782-
out[0] = this.pos[0] - 4;
3783-
out[1] = this.pos[1] - LiteGraph.NODE_TITLE_HEIGHT;
3784-
out[2] = this.size[0] + 4;
3785-
out[3] = this.flags.collapsed ? LiteGraph.NODE_TITLE_HEIGHT : this.size[1] + LiteGraph.NODE_TITLE_HEIGHT;
3783+
const nodePos = this.pos;
3784+
const isCollapsed = this.flags.collapsed;
3785+
const nodeSize = this.size;
3786+
3787+
let left_offset = 0;
3788+
// 1 offset due to how nodes are rendered
3789+
let right_offset = 1 ;
3790+
let top_offset = 0;
3791+
let bottom_offset = 0;
3792+
3793+
if (compute_outer) {
3794+
// 4 offset for collapsed node connection points
3795+
left_offset = 4;
3796+
// 6 offset for right shadow and collapsed node connection points
3797+
right_offset = 6 + left_offset;
3798+
// 4 offset for collapsed nodes top connection points
3799+
top_offset = 4;
3800+
// 5 offset for bottom shadow and collapsed node connection points
3801+
bottom_offset = 5 + top_offset;
3802+
}
3803+
3804+
out[0] = nodePos[0] - left_offset;
3805+
out[1] = nodePos[1] - LiteGraph.NODE_TITLE_HEIGHT - top_offset;
3806+
out[2] = isCollapsed ?
3807+
(this._collapsed_width || LiteGraph.NODE_COLLAPSED_WIDTH) + right_offset :
3808+
nodeSize[0] + right_offset;
3809+
out[3] = isCollapsed ?
3810+
LiteGraph.NODE_TITLE_HEIGHT + bottom_offset :
3811+
nodeSize[1] + LiteGraph.NODE_TITLE_HEIGHT + bottom_offset;
37863812

37873813
if (this.onBounding) {
37883814
this.onBounding(out);
@@ -7671,7 +7697,7 @@ LGraphNode.prototype.executeAction = function(action)
76717697
continue;
76727698
}
76737699

7674-
if (!overlapBounding(this.visible_area, n.getBounding(temp))) {
7700+
if (!overlapBounding(this.visible_area, n.getBounding(temp, true))) {
76757701
continue;
76767702
} //out of the visible area
76777703

0 commit comments

Comments
 (0)