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;
}
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.