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

Author Topic: Different Window Sizes  (Read 2693 times)

0 Members and 1 Guest are viewing this topic.

Drysteven

  • Newbie
  • *
  • Posts: 3
    • View Profile
Different Window Sizes
« on: August 17, 2016, 06:35:56 am »
I am currently trying to make text in the window and then make the text clickable by the user and I have successfully done this, but if I change the window size then it only detects a click when it is in the top left corner and not on the right or bottom of the text. I made it so that the text relative to the background which placed it in the right spot I need to know how to change the size relative to the resolution or if there is a much easier way of doing text click detection on text. I am using a small box set to the mouse's coordinates and then testing if it is on the text. Thank you for reading.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Different Window Sizes
« Reply #1 on: August 17, 2016, 10:13:03 am »
http://www.sfml-dev.org/tutorials/2.4/graphics-view.php#coordinates-conversions
You resized your window and your view has been stretched (whether you explicity use one or not, there is always a view), thus the coordinates inside your world (your text) aren't equivalent to those of your window (your mouse) and you have to convert your mouse coordinates to your world coordinates with mapPixelToCoords.

Hapax

  • Hero Member
  • *****
  • Posts: 3362
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Different Window Sizes
« Reply #2 on: August 17, 2016, 07:42:36 pm »
As well as the co-ordinate conversion advice given by G, be aware that a similar symptom could be caused by mistakingly using local bounds instead of global bounds.

Use global bounds and convert them :)

Note: don't forget the left and top values of the bounds. This is especially important for text.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Drysteven

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Different Window Sizes
« Reply #3 on: August 18, 2016, 07:22:48 am »
As well as the co-ordinate conversion advice given by G, be aware that a similar symptom could be caused by mistakingly using local bounds instead of global bounds.

Use global bounds and convert them :)

Note: don't forget the left and top values of the bounds. This is especially important for text.

This is the line I am using to test for the box over the mouse, mouse, intersects the play text, play.

if (mouse.intersects(play.getGlobalBounds()) == true)

How would I add top and left in this. Would I have to make something with if play.getGlobalBounds().x, play.getGlobalBounds().y, play.getGlobalBounds().top, play.getGlobalBounds().left? If so can you please give me an example of how to do this. Thank you.

Hapax

  • Hero Member
  • *****
  • Posts: 3362
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Different Window Sizes
« Reply #4 on: August 18, 2016, 12:38:14 pm »
.intersects takes a rect so will use all four members (including left and top).
The question here is what is "mouse"? Is it a rectangle?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Drysteven

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Different Window Sizes
« Reply #5 on: August 18, 2016, 04:41:30 pm »
.intersects takes a rect so will use all four members (including left and top).
The question here is what is "mouse"? Is it a rectangle?

Yes, mouse is a 1 pixel by 1 pixel rectangle that I set to always be at the mouse coordinates.

Hapax

  • Hero Member
  • *****
  • Posts: 3362
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Different Window Sizes
« Reply #6 on: August 18, 2016, 08:03:51 pm »
That's odd; I've not seen that before. Is the rectangle the hotspot + a pixel, the hotspot dead centre of the rectangle or is the rectangle locked to actual pixels?

You may want to consider using the mouse co-ordinate as a single point and comparing that with the rectangle you are colliding with (remember to map co-ordinates):
const sf::FloatRect play(50.f, 50.f, 200.f, 100.f); // play is rectangle of size 200x100 with top-left at (50, 50)
const sf::Vector2f mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window)); // gets mouse position using your current view
if (play.contains(mousePosition))
    mouseIsInsidePlayRectangle = true;
where window is the variable name of your window.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*