Emit on mouse enter only when it's over non-empty pixels in Control

Godot Version

4.3 linux

Question

Hi, I need to detect an on mouse enter signal when the mouse enter’s a non empty pixel in a TextureRect control node.

Is there a way to do this?

i think you either put something above the texture that takes all the inputs away from the texturerect where its not supposed to react or you use an empty buttons-bitmask or you create a custom collisionshape with an area and polygonshape or you handle it by code by checking mouseposition and going through your texture and see if the pixel value is not empty basically

Thanks for your reply, I don’t think it’s possible to use collisions, I would opt for the texture filtering the input, do you have any idea on how it could be achieved?

im not sure if it works but maybe you can use a polygonshape2d and use clip-children. I dont know if it clips input too

thank you, i’ll try it

Use a TextureButton instead. You can assign it a BitMap in its TextureButton.texture_click_mask to mask where you want the button to react.

You can generate the BitMap from a texture in code like:

extends TextureButton


func _ready() -> void:
	if texture_normal:
		# Get the image from the texture normal
		var image = texture_normal.get_image()
		# Create the BitMap
		var bitmap = BitMap.new()
		# Fill it from the image alpha
		bitmap.create_from_image_alpha(image)
		# Assign it to the mask
		texture_click_mask = bitmap

If you are forced to use a TextureRect then you can implement the Control._has_point() method and filter it somehow (like using a BitMap like TextureButton does) but it’s not straightforward. Here’s how the TextureButton implements it godot/scene/gui/texture_button.cpp at 826de7976a6add282c7b14d4be2a7e6d775821d8 · godotengine/godot · GitHub

2 Likes