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

Author Topic: Mouse input with sf::View  (Read 1110 times)

0 Members and 1 Guest are viewing this topic.

M4c!3K

  • Newbie
  • *
  • Posts: 2
    • View Profile
Mouse input with sf::View
« on: January 02, 2015, 05:34:42 pm »
Hello,
I was designing my game menu. I wanted to add slide effect between menu "states", so i used sf::View, and moved it with move() function to show the actual menu state. So for example when you click "options" in menu the view moves until it reaches coordinates where options are drawn. It was a good idea, but then i had to use mouse input. And now mouse is like x: 400 y: 500 and button is like x: 1400 and y: 1500 and because of sf::View it looks like the mouse is over button. Is it possible to make menu like that without moving every single item? Or maybe i can just fix the sf::View thing. Here's the code:

if (this->submenustate == 0 && this->ScreenView.getCenter().x != SCREEN_WIDTH / 2)
{
        if (((SCREEN_WIDTH / 2) - this->ScreenView.getCenter().x) < 15)
                this->ScreenView.move(-((SCREEN_WIDTH / 2) - this->ScreenView.getCenter().x), 0);
        else
                this->ScreenView.move(-15, 0);
}

if (this->submenustate == 1 && this->ScreenView.getCenter().x != (SCREEN_WIDTH / 2) + SCREEN_WIDTH)
{
        if (this->ScreenView.getCenter().x > (SCREEN_WIDTH / 2 + SCREEN_WIDTH))
        {
                if (((SCREEN_WIDTH / 2 + SCREEN_WIDTH) - this->ScreenView.getCenter().x) < 15)
                        this->ScreenView.move(-(((SCREEN_WIDTH / 2) + SCREEN_WIDTH) - this->ScreenView.getCenter().x), 0);
                else
                        this->ScreenView.move(-15, 0);
        }
        else if (this->ScreenView.getCenter().x < (SCREEN_WIDTH / 2 + SCREEN_WIDTH))
        {
                if (this->ScreenView.getCenter().x + 15 >(SCREEN_WIDTH / 2 + SCREEN_WIDTH))
                        this->ScreenView.move((SCREEN_WIDTH / 2 + SCREEN_WIDTH) - this->ScreenView.getCenter().x, 0);
                else
                        this->ScreenView.move(15, 0);
        }
}
If you need more code to help me just write what part do you need. Whole code is like 1300 lines.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Mouse input with sf::View
« Reply #1 on: January 02, 2015, 05:59:54 pm »
The solution is at the end of the official tutorial on views.

Quote
When you use a custom view, or when you resize the window without using the code above, pixels displayed on the target no longer match units in the 2D world. For example, clicking on pixel (10, 50) may hit the point (26.5, -84) of your world. You end up having to use a conversion function to map your pixel coordinates to world coordinates: mapPixelToCoords.

// get the current mouse position in the window
sf::Vector2i pixelPos = sf::Mouse::getPosition(window);

// convert it to world coordinates
sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);

By default, mapPixelToCoords uses the current view. If you want to convert the coordinates using view which is not the active one, you can pass it as an additional argument to the function.

The opposite, converting world coordinates to pixel coordinates, is also possible with the mapCoordsToPixel function.

 

anything