Is there a function to pick the gui control under a Vector2?

I couldn’t find any so I just used modified _processPicking but I’d rather use something in the library. Is there any way to pick a control under an x,y?

import { Container, Control } from "@babylonjs/gui";

declare module '@babylonjs/gui' {
    interface Container {
        pickControl(x: number, y: number, callback: (control: Control) => void);
    }
}
Container.prototype.pickControl = function(x: number, y: number, callback: (control: Control) => void) {
    if (!this._isEnabled || !this.isVisible || this.notRenderable) {
        return false;
    }
    // checks if the picking position is within the container
    const contains = this.contains(x, y);
    // if clipChildren is off, we should still pass picking events to children even if we don't contain the pointer
    if (!contains && this.clipChildren) {
        return false;
    }
    // Checking backwards to pick closest first
    for (let index = this._children.length - 1; index >= 0; index--) {
        const child = this._children[index];
        if (child.pickControl(x, y, callback)) {
            return true;
        }
    }
    if (!contains) {
        return false;
    }
    if (!this.isHitTestVisible) {
        return false;
    }
    callback(this);
    return true;
}
import { Control } from "@babylonjs/gui";

declare module '@babylonjs/gui' {
    interface Control {
        pickControl(x: number, y: number, callback: (control: Control) => void);
    }
}
Control.prototype.pickControl = function(x: number, y: number, callback: (control: Control) => void) {
    if (!this._isEnabled) {
        return false;
    }
    if (!this.isHitTestVisible || !this.isVisible || this._doNotRender) {
        return false;
    }
    if (!this.contains(x, y)) {
        return false;
    }
    callback(this);
    return true;
}

What exactly do you mean by “under a Vector2”? Want to share a playground simulating what you are trying to achieve?

I believe the Vector2 is referring to the mouse position in this case? In this case, we indeed don’t have a method for it on the library right now

1 Like

Yep, that’s what I mean. I also need it to just simulate a mouse position. My rip of _processPicking above works great.