-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputManager.cpp
145 lines (126 loc) · 3.03 KB
/
InputManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "InputManager.hpp"
#include "globals.hpp"
using namespace std;
InputManager::InputManager(){
isLocked = false;
isFocused = true;
inputString = "";
}
InputManager::InputManager(sf::Window *window){
this->window = window;
isLocked = false;
isFocused = true;
inputString = "";
}
void InputManager::setWindow(sf::Window *window){
this->window = window;
}
bool InputManager::isGuiLocked(){
return isLocked;
}
void InputManager::lockToGui(bool yes){
isLocked = yes;
}
void InputManager::setFocus(bool yes){
isFocused = yes;
}
bool InputManager::isKeyDown(sf::Keyboard::Key key){
if(!isLocked && isFocused && sf::Keyboard::isKeyPressed(key)){
return true;
}
return false;
}
bool InputManager::isMouseDown(sf::Mouse::Button button){
if(!isLocked && isFocused && sf::Mouse::isButtonPressed(button)){
return true;
}
return false;
}
sf::Vector2i InputManager::getMousePos(){
if(!isLocked && isFocused){
return sf::Mouse::getPosition(*window);
}
return sf::Vector2i(width/2,height/2);
}
void InputManager::setMousePos(sf::Vector2i pos){
if(!isLocked && isFocused){
sf::Mouse::setPosition(pos, *window);
}
}
bool InputManager::isGuiKeyDown(sf::Keyboard::Key key){
if(isLocked && isFocused && sf::Keyboard::isKeyPressed(key)){
return true;
}
return false;
}
bool InputManager::isGuiMouseDown(sf::Mouse::Button button){
if(isLocked && isFocused && sf::Mouse::isButtonPressed(button)){
return true;
}
return false;
}
sf::Vector2i InputManager::getGuiMousePos(){
if(isLocked && isFocused){
return sf::Mouse::getPosition(*window);
}
return sf::Vector2i(0,0);
}
void InputManager::setGuiMousePos(sf::Vector2i pos){
if(isLocked && isFocused){
sf::Mouse::setPosition(pos, *window);
}
}
void InputManager::addInput(string input){
if(inputString.size() > 512){
inputString = "";
}
if(input != "\r"){
inputString += input;
}
}
bool InputManager::isJoystickConnected(){
if(sf::Joystick::isConnected(0))
return true;
return false;
}
bool InputManager::isJoystickButtonDown(JoyButton b){
if(sf::Joystick::isButtonPressed(0,b)){
return true;
}
return false;
}
sf::Vector2f InputManager::getLeftAnalog(){
sf::Vector2f out;
out.x = sf::Joystick::getAxisPosition(0,sf::Joystick::X);
out.y = sf::Joystick::getAxisPosition(0,sf::Joystick::Y);
out.x /= 100;
out.y /= 100;
return out;
}
sf::Vector2f InputManager::getRightAnalog(){
sf::Vector2f out;
out.x = sf::Joystick::getAxisPosition(0,sf::Joystick::U);
out.y = sf::Joystick::getAxisPosition(0,sf::Joystick::V);
out.x /= 100;
out.y /= 100;
return out;
}
sf::Vector2i InputManager::getDpad(){
sf::Vector2i out;
out.x = sf::Joystick::getAxisPosition(0,sf::Joystick::PovX);
out.y = sf::Joystick::getAxisPosition(0,sf::Joystick::PovY);
out.x /= 100;
out.y /= 100;
return out;
}
float InputManager::getLeftTrigger(){
return sf::Joystick::getAxisPosition(0, sf::Joystick::Z)/100;
}
float InputManager::getRightTrigger(){
return sf::Joystick::getAxisPosition(0, sf::Joystick::R)/100;
}
string InputManager::getString(){
string out = inputString;
inputString = "";
return out;
}