refactor input.h and related

This commit is contained in:
A-lex-Ra
2024-02-23 18:49:15 +06:00
parent 9c31c6c8b7
commit 8bb1045e34
12 changed files with 236 additions and 254 deletions
+47 -24
View File
@@ -16,36 +16,48 @@ std::vector<uint> Events::codepoints;
std::vector<int> Events::pressedKeys;
std::unordered_map<std::string, Binding> Events::bindings;
// Returns bool repr. of key state
bool Events::pressed(keycode keycode) {
return pressed(static_cast<int>(keycode));
}
bool Events::pressed(int keycode) {
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
fprintf(stderr, "pressed %i\n", keycode);
fprintf(stderr, "pressed %i\n", keycode); //FIXME: unreasonable cstdio usage
return false;
}
return _keys[keycode];
}
// Returns bool repr. of key state
bool Events::jpressed(int keycode){
bool Events::jpressed(keycode keycode) {
return jpressed(static_cast<int>(keycode));
}
bool Events::jpressed(int keycode) {
return Events::pressed(keycode) && _frames[keycode] == _current;
}
// Returns bool repr. of mouse key state
bool Events::clicked(int button){
bool Events::clicked(mousecode button) {
return clicked(static_cast<int>(button));
}
bool Events::clicked(int button) {
return Events::pressed(_MOUSE_KEYS_OFFSET + button);
}
// Returns bool repr. of mouse key state
bool Events::jclicked(int button){
bool Events::jclicked(mousecode button) {
return jclicked(static_cast<int>(button));
}
bool Events::jclicked(int button) {
return Events::jpressed(_MOUSE_KEYS_OFFSET + button);
}
void Events::toggleCursor(){
void Events::toggleCursor() {
_cursor_locked = !_cursor_locked;
Window::setCursorMode(_cursor_locked ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
}
void Events::pollEvents(){
void Events::pollEvents() {
_current++;
delta.x = 0.f;
delta.y = 0.f;
@@ -60,8 +72,8 @@ void Events::pollEvents(){
bool newstate = false;
switch (binding.type) {
case inputtype::keyboard: newstate = pressed(binding.code); break;
case inputtype::mouse: newstate = clicked(binding.code); break;
case inputtype::keyboard: newstate = pressed(binding.code); break;
case inputtype::mouse: newstate = clicked(binding.code); break;
}
if (newstate) {
@@ -69,7 +81,8 @@ void Events::pollEvents(){
binding.state = true;
binding.justChange = true;
}
} else {
}
else {
if (binding.state) {
binding.state = false;
binding.justChange = true;
@@ -78,8 +91,16 @@ void Events::pollEvents(){
}
}
void Events::bind(std::string name, inputtype type, keycode code) {
bind(name, type, static_cast<int>(code));
}
void Events::bind(std::string name, inputtype type, mousecode code) {
bind(name, type, static_cast<int>(code));
}
void Events::bind(std::string name, inputtype type, int code) {
bindings[name] = {type, code, false, false};
bindings[name] = { type, code, false, false };
}
bool Events::active(std::string name) {
@@ -99,20 +120,22 @@ bool Events::jactive(std::string name) {
}
void Events::setKey(int key, bool b) {
Events::_keys[key] = b;
Events::_frames[key] = Events::_current;
Events::_keys[key] = b;
Events::_frames[key] = Events::_current;
}
void Events::setButton(int button, bool b) {
setKey(_MOUSE_KEYS_OFFSET + button, b);
setKey(_MOUSE_KEYS_OFFSET + button, b);
}
void Events::setPosition(float xpos, float ypos) {
if (Events::cursor_drag) {
Events::delta.x += xpos - Events::cursor.x;
Events::delta.y += ypos - Events::cursor.y;
} else
Events::cursor_drag = true;
Events::cursor.x = xpos;
Events::cursor.y = ypos;
if (Events::cursor_drag) {
Events::delta.x += xpos - Events::cursor.x;
Events::delta.y += ypos - Events::cursor.y;
}
else {
Events::cursor_drag = true;
}
Events::cursor.x = xpos;
Events::cursor.y = ypos;
}