vectozavr-shooter/engine/io/Mouse.cpp

46 lines
1.3 KiB
C++
Raw Normal View History

//
// Created by Иван Ильин on 19.09.2021.
//
#include "Mouse.h"
2022-07-11 16:58:05 +03:00
#include "../utils/Time.h"
#include "../Consts.h"
Vec2D Mouse::getMousePosition() const {
2021-10-28 16:58:02 +03:00
sf::Vector2<int> pos = sf::Mouse::getPosition(*_screen->renderWindow());
return Vec2D(pos.x, pos.y);
}
Vec2D Mouse::getMouseDisplacement() const {
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-10-02 20:36:07 +03:00
sf::Vector2<int> displacement = mousePos - center;
2021-10-31 19:01:06 +03:00
setMouseInCenter();
return Vec2D(displacement.x, displacement.y);
}
void Mouse::setMouseInCenter() const {
2022-02-23 07:51:53 +03:00
sf::Vector2<int> center = sf::Vector2<int>(_screen->width() / 2, _screen->height() / 2);
sf::Mouse::setPosition(center,*_screen->renderWindow());
}
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)) {
return false;
2021-10-28 16:58:02 +03:00
}
2021-10-31 11:39:08 +03:00
if (_tappedButtons.count(button) == 0) {
_tappedButtons.emplace(button, Time::time());
return true;
2021-10-31 11:39:08 +03:00
} else if ((Time::time() - _tappedButtons[button]) > Consts::TAP_DELAY) {
_tappedButtons[button] = Time::time();
return true;
}
return false;
}