通过左键单击聚焦精灵,然后再次通过左键单击移除焦点。 (C++ 和 SFML 2.2)

Focus sprite by leftclick and then remove focus by leftclick again. (C++ and SFML 2.2)

在下面的代码中,一个精灵可以通过鼠标左键点击然后自由移动。然后你必须再次点击右键 "free" 它。 现在是什么,如果我想使用左键而不是右键来移除焦点。这可能吗?如何实现?我无法想象,如何去做。非常感谢。

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

int main()
{
    sf::RenderWindow mMainWindow(sf::VideoMode(600,600), "Map", sf::Style::Close);
    mMainWindow.setFramerateLimit(60);
    mMainWindow.setKeyRepeatEnabled(false);
    sf::Image image;
    image.create(50, 50, sf::Color::Red);
    sf::Texture texture;
    texture.loadFromImage(image);
    std::vector<sf::Sprite> EnemyVector;
    sf::Sprite* focus = nullptr;

    bool move = false;

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

        sf::Vector2i mousePos;

        while (mMainWindow.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                mMainWindow.close();
                break;
            case sf::Event::KeyPressed:
                creating = (event.key.code == sf::Keyboard::A);
                break;
            case sf::Event::MouseButtonPressed:
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                    leftclicked = true;
                    mousePos = sf::Vector2i(event.mouseButton.x, event.mouseButton.y);
                    break;
                }
                if (event.mouseButton.button == sf::Mouse::Right)
                {
                    rightclicked = true;
                    mousePos = sf::Vector2i(event.mouseButton.x, event.mouseButton.y);
                    break;
                }
            }
        }
        if (creating)
        {
            sf::Sprite sprite;
            mousePos = (mousePos == sf::Vector2i(0, 0) ? sf::Mouse::getPosition(mMainWindow) : mousePos);
            sprite.setTexture(texture);
            sprite.setColor(sf::Color::Red);
            sprite.setOrigin(static_cast<float>(sprite.getTextureRect().width) / 2, static_cast<float>(sprite.getTextureRect().height) / 2);
            sprite.setPosition(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
            focus=nullptr;
            EnemyVector.push_back(sprite);
        }
        if (leftclicked)
        {
            for (auto& enemy = EnemyVector.rbegin(); enemy != EnemyVector.rend(); ++enemy)
            {
                if (enemy->getGlobalBounds().contains(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y)))
                {
                    focus = &(*enemy);
                    move = true;
                    break;
                }
            }
        }
        if(rightclicked)
        {
            focus = nullptr;
        }
        if(move)
        {
            if(focus!=nullptr)
            {
                focus->move((sf::Mouse::getPosition(mMainWindow).x - focus->getPosition().x),(sf::Mouse::getPosition(mMainWindow).y - focus->getPosition().y));
            }
        }
        mMainWindow.clear();
        for (auto& enemy = EnemyVector.rbegin(); enemy != EnemyVector.rend(); ++enemy)
        {
            mMainWindow.draw(*enemy);
        }
        mMainWindow.display();
    }
    return 0;
}

试试这个

   if (leftclicked)
    {
        for (auto& enemy = EnemyVector.rbegin(); enemy != EnemyVector.rend(); ++enemy)
        {
            if (enemy->getGlobalBounds().contains(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y)))
            {
                focus = &(*enemy);
                move = !move;
                break;
            }
        }
    }