0 votes

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 :

  • getlocalmouse_position()
  • getviewport().getmouse_position()
  • getglobalmouse_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

in Engine by (14 points)

1 Answer

–1 vote

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
by (190 points)

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.

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://godotengine.org/qa/24621/painting-game-persist-drawing

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.