Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Global bounds of object inside RenderTexture inside RenderWindow  (Read 2114 times)

0 Members and 1 Guest are viewing this topic.

almog01

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Hello  :)

Usually I like to split my game window (of type RenderWindow) to regions: each region is a RenderTexture which in the end drawn on the main RenderWindow.
By doing this I can split completely the region of the game from the region of the toolbar for instance.

Problem is, when I place a button on the RenderTexture, and I want to check if the button was clicked:

first I'm saving the global coordinates of the mouse click:
const Vector2f mousePos = m_window.mapPixelToCoords({ event.mouseButton.x, event.mouseButton.y });
 
then I check if the button (of type RectangleShape) contains the position of the click:
if (m_button.getGlobalBounds().contains(mousePos))
 

And it doesn't work because the m_button.getGlobalBounds() returns the bounds that are relative to the RenderTexture, while m_window.mapPixelToCoords returns coordinates that are relative to the main RenderWindow.
It works only if the button was drawn directly on the main RenderWindow.

Is there a way to convert the mousePos coordinates to be relative to the RenderTexture. Or other way around, to check the GlobalBounds of the button that are relative to the RenderWindow?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Global bounds of object inside RenderTexture inside RenderWindow
« Reply #1 on: May 18, 2019, 11:50:01 pm »
Hi! :)

Since the button object doesn't actually exist on the window - its visual representation is only a part of the texture used to draw the render texture to the window - no, not directly. You can, of course, calculate where it should be in relation to the render texture.

One other thing you could do is set up different views to represent the render textures and then use each of those views for the co-ordinate conversion.
So, set up - in addition to the normal window view - a view that places the co-ordinates of the render texture aligned with where they would be in the window. You could use a view's viewport to help with framing. Then, convert the mouse position to each view depending on which one you are comparing.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

almog01

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Global bounds of object inside RenderTexture inside RenderWindow
« Reply #2 on: May 21, 2019, 12:57:52 am »
Hi! :)

Since the button object doesn't actually exist on the window - its visual representation is only a part of the texture used to draw the render texture to the window - no, not directly. You can, of course, calculate where it should be in relation to the render texture.

One other thing you could do is set up different views to represent the render textures and then use each of those views for the co-ordinate conversion.
So, set up - in addition to the normal window view - a view that places the co-ordinates of the render texture aligned with where they would be in the window. You could use a view's viewport to help with framing. Then, convert the mouse position to each view depending on which one you are comparing.

I had really short schedule for completing the project so in the end I was forced to just draw the button directly on the main window and live with it ::)
I was hoping that there is easier solution since I don't have much experience with view and viewport so I didn't try your solution yet. Maybe in the future I could try it and report if that works :)

Thanks for the help anyway!

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Global bounds of object inside RenderTexture inside RenderWindow
« Reply #3 on: May 21, 2019, 09:24:56 pm »
You're welcome and I'm glad you managed to find a different approach :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything