2021-09-19 15:44:31 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 19.09.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Mouse.h"
|
|
|
|
#include "utils/Time.h"
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec2D Mouse::getMousePosition() const {
|
2021-09-19 15:44:31 +03:00
|
|
|
sf::Vector2<int> pos = sf::Mouse::getPosition(*_window);
|
2021-10-12 17:12:47 +03:00
|
|
|
return Vec2D(pos.x, pos.y);
|
2021-09-19 15:44:31 +03:00
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec2D Mouse::getMouseDisplacement() const {
|
2021-09-19 15:44:31 +03:00
|
|
|
sf::Vector2<int> mousePos = sf::Mouse::getPosition(*_window);
|
|
|
|
sf::Vector2<int> center = sf::Vector2<int>(_window->getSize().x/2, _window->getSize().y/2);
|
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
sf::Vector2<int> displacement = mousePos - center;
|
2021-09-19 15:44:31 +03:00
|
|
|
//setMouseInCenter();
|
2021-10-12 17:12:47 +03:00
|
|
|
return Vec2D(displacement.x, displacement.y);
|
2021-09-19 15:44:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mouse::setMouseInCenter() const {
|
|
|
|
sf::Mouse::setPosition({ static_cast<int>(_window->getSize().x / 2), static_cast<int>(_window->getSize().y / 2) }, *_window);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Mouse::isButtonPressed(sf::Mouse::Button button) {
|
|
|
|
return sf::Mouse::isButtonPressed(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Mouse::isButtonTapped(sf::Mouse::Button button) {
|
|
|
|
if (!Mouse::isButtonPressed(button))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(_tappedButtons.count(button) == 0) {
|
|
|
|
_tappedButtons.emplace(button, Time::time());
|
|
|
|
return true;
|
|
|
|
} else if((Time::time() - _tappedButtons[button]) > 0.2) {
|
|
|
|
_tappedButtons[button] = Time::time();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mouse::setWindow(std::shared_ptr<sf::RenderWindow> window) {
|
|
|
|
_window = window;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mouse::setMouseCursorVisible(bool visible) {
|
|
|
|
_window->setMouseCursorVisible(visible);
|
|
|
|
}
|