Getting the position within a texture in a texturebox where the mouse pointer is over

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By livingstone

Hi all,

I have check all previous questions I came across around mouse positioning with no luck.

I need to know the exact position within a texture (in order to get the Color, for reasons not relevant at this point) that the mouse is over.

My scene is a Control with only a TextureRect disposed to full rect with a slighly bigger that it should texture that is scaled down by setting the Expand property to true.

Control 
  \-> TextureRect

In order to get the exact pixel I’ve tried :

  • get_local_mouse_position()
  • get_viewport().get_mouse_position()
  • get_global_mouse_position()

At this point I’m assuming that the TextureRect has applied a transformation that I need to apply too, but can’t quite find it.

How could I get the exact position within a texture in a texturebox where the mouse pointer is over

A project to replicate the problen can be found here:
https://drive.google.com/open?id=1EevyFCUTfsg2z1gB0tca69ieXb6YW7V4

:bust_in_silhouette: Reply From: null_digital

With the size and position of the texturerect, you can find where in the texture the player clicked.

#assuming layout is top left
func _on_textrect_click():
	var mousepos = get_global_mouse_position()
	var ctrlo = TextureRect.new()
	var rectpos = ctrlo.rect_global_position
	var rectsize = ctrlo.rect_size
	var maxpos = rectpos + rectsize
	
	if mousepos.x >= rectpos.x && mousepos.x <= maxpos.x && \
		mousepos.y >= rectpos.y && mousepos.y <= maxpos.y:
		var selectedpos = mousepos - rectpos # the result
		
	pass

Thanks for the help. Sadly it doesn’t work there is a non linear delta between where I move the mouse and the pixels I’m painting.

I suspect there is a great deal of logic hidden behind the scenes if expand is activated that is also depending on the stretch mode.

livingstone | 2019-12-07 12:24

Are you talking about stretch on window resize? Is it possible to keep aspect and implement zoom instead? And what do you mean non-linear-delta?

Sounds like you’re doing a paint canvas of sorts. This might help:
https://forum.godotengine.org/24621/painting-game-persist-drawing

null_digital | 2019-12-07 15:42