HitTest area that is larger than Component?

Is it possible to make the Hit test area of a component larger than the actual bounds of the component? I’ve tried this in the past with getLocalBounds().expanded().contains() in hitTest(), but did not have any luck.

Thanks!

I’m pretty sure all that hitTest/contains logic is scoped to the component’s bounds.

Probably the easiest thing to do is to wrap the component in a wrapper component with some “padding” and setup your hitTest there.

Alternatively you could register a mouse listener on the parent component, but be sure not to have the listener be the component in question. Otherwise you’ll get “feeds” of mouse events from 2 sources (with different coordinate systems) coming into the component. I just wrote up a post about mouse and keyboard listeners, maybe it’s helpful.

1 Like

My usual way to do it is to create two components, hosted inside a larger component.

One handles the drawing only and not capturing any mouse events, and the other captures mouse events but never drawing anything.

That pattern is useful for two main scenarios:
One like you mention, when you need the “hitbox” of the component to be different than what’s drawn, and the other is when you want to customize the mouse handling without touching the drawing.

For example if you want to be able to switch a vertical to a horizontal slider but have the UI show some visualizer that doesn’t directly care how the mouse event was handled.

2 Likes

This is great guys. Thanks for the tips. I’m going to begin implementing a system like this.

Thanks so much!

-Cameron Clark

1 Like