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

Author Topic: Correct mouse coordinates with multiple views.  (Read 3338 times)

0 Members and 1 Guest are viewing this topic.

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Correct mouse coordinates with multiple views.
« on: April 15, 2015, 06:12:49 pm »
Hi there,

is there a way to print the correct map coordinates while using multiple views? Now it only shows the coordinates in relation to the left view, even when I click into the right view. For example if I click into the top left corner of the left view it should show the same coordinates as clicking into the top left corner of the right view. How to do that?

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    sf::RenderWindow mMainWindow(sf::VideoMode(1000,600), "Map", sf::Style::Close);
        sf::View view(sf::Vector2f(500, 300), sf::Vector2f(1000, 600));
        sf::View view2(sf::Vector2f(500, 300), sf::Vector2f(1000, 600));
        view.setViewport(sf::FloatRect(0, 0, 0.5f, 1));
        view2.setViewport(sf::FloatRect(0.5f, 0, 0.5f, 1));
       
    mMainWindow.setFramerateLimit(60);
    mMainWindow.setKeyRepeatEnabled(false);

        sf::Image mapimage;
        mapimage.loadFromFile("world1.jpg");
        sf::Texture maptexture;
        maptexture.loadFromImage(mapimage);
        sf::Sprite mapsprite(maptexture);
        mapsprite.setPosition(0, 0);

    while (mMainWindow.isOpen())
    {
        sf::Event event;
        sf::Vector2i mousePos;
                bool leftclicked = false;

        while (mMainWindow.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                mMainWindow.close();
                break;
                        case sf::Event::MouseButtonPressed:
                if (event.mouseButton.button == sf::Mouse::Left)
                                {
                    leftclicked = true;
                                        sf::Vector2i pixel_pos = sf::Mouse::getPosition(mMainWindow);
                                        sf::Vector2f coord_pos = mMainWindow.mapPixelToCoords(pixel_pos);
                                       
                                        std::cout << coord_pos.x << std::endl;
                                        std::cout << coord_pos.y << std::endl;
                                        break;
                                }
         
                               
            }
        }


        mMainWindow.clear();
                mMainWindow.setView(view2);
                mMainWindow.draw(mapsprite);
                mMainWindow.setView(view);
                mMainWindow.draw(mapsprite);
        mMainWindow.display();
    }
    return 0;
}

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Correct mouse coordinates with multiple views.
« Reply #1 on: April 15, 2015, 06:18:04 pm »
sf::Window::mapPixelToCoords(...) has an overload where you can specify which view to use for the calculation.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Correct mouse coordinates with multiple views.
« Reply #2 on: April 15, 2015, 07:21:21 pm »
                                        sf::Vector2i pixel_pos = sf::Mouse::getPosition(mMainWindow);
                                        sf::Vector2f coord_pos = mMainWindow.mapPixelToCoords(pixel_pos, view2);


Not I`ve found out how to print the coordinates of a specific view, but is it also possible to register into which view I've clicked. I know how to do that in a static way by using fixed coordinates for the check, but if I will be moving the views /view sizes dynamically later on, I need a method to simply detect into which view I've clicked.


zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Correct mouse coordinates with multiple views.
« Reply #3 on: April 15, 2015, 07:30:20 pm »
SFML has no way to determine which view you think the mouse belongs to. So it is your job to figure out what view to use for coordinate conversions.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Bogdan

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Correct mouse coordinates with multiple views.
« Reply #4 on: April 15, 2015, 09:21:24 pm »
Ok, I`ve got it by creating two new rects  for determining if cursor is inside in one of them.