2021-09-19 15:44:31 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 19.09.2021.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Mouse.h"
|
|
|
|
#include "utils/Time.h"
|
2021-10-28 16:58:02 +03:00
|
|
|
#include "Consts.h"
|
2021-09-19 15:44:31 +03:00
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
Vec2D Mouse::getMousePosition() const {
|
2021-10-28 16:58:02 +03:00
|
|
|
sf::Vector2<int> pos = sf::Mouse::getPosition(*_screen->renderWindow());
|
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-10-31 11:39:08 +03:00
|
|
|
// TODO: getMouseDisplacement() should return displacement from the previous position but not from the center
|
2021-10-28 16:58:02 +03:00
|
|
|
sf::Vector2<int> mousePos = sf::Mouse::getPosition(*_screen->renderWindow());
|
2021-10-31 11:39:08 +03:00
|
|
|
sf::Vector2<int> center = sf::Vector2<int>(_screen->width() / 2, _screen->height() / 2);
|
2021-09-19 15:44:31 +03:00
|
|
|
|
2021-10-02 20:36:07 +03:00
|
|
|
sf::Vector2<int> displacement = mousePos - center;
|
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 {
|
2021-10-31 11:39:08 +03:00
|
|
|
sf::Mouse::setPosition({static_cast<int>(_screen->width() / 2), static_cast<int>(_screen->height() / 2)},
|
|
|
|
*_screen->renderWindow());
|
2021-09-19 15:44:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Mouse::isButtonPressed(sf::Mouse::Button button) {
|
|
|
|
return sf::Mouse::isButtonPressed(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Mouse::isButtonTapped(sf::Mouse::Button button) {
|
2021-10-28 16:58:02 +03:00
|
|
|
if (!Mouse::isButtonPressed(button)) {
|
2021-09-19 15:44:31 +03:00
|
|
|
return false;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-09-19 15:44:31 +03:00
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
if (_tappedButtons.count(button) == 0) {
|
2021-09-19 15:44:31 +03:00
|
|
|
_tappedButtons.emplace(button, Time::time());
|
|
|
|
return true;
|
2021-10-31 11:39:08 +03:00
|
|
|
} else if ((Time::time() - _tappedButtons[button]) > Consts::TAP_DELAY) {
|
2021-09-19 15:44:31 +03:00
|
|
|
_tappedButtons[button] = Time::time();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|