summaryrefslogtreecommitdiff
path: root/engine/xe_input.cpp
blob: 91b9cab4da0dcc7b153e8300e3769821356f5ae8 (plain)
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
#include "xe_input.hpp"

#include <GLFW/glfw3.h>

namespace xe {

static XeInput* _instance;

XeInput::XeInput(XeWindow& window) : window{window} {
  glfwSetKeyCallback(window.getGLFWwindow(), XeInput::key_callback);
  glfwSetMouseButtonCallback(window.getGLFWwindow(), XeInput::mouse_callback);
  _instance = this;
}

bool XeInput::isKeyPressed(int key) {
  return glfwGetKey(window.getGLFWwindow(), key) == GLFW_PRESS;
}

bool XeInput::wasKeyPressed(int key) {
  if(_pressed[key] == true) {
    _pressed[key] = false;
    return true;
  }
  return false;
}

bool XeInput::wasKeyReleased(int key) {
  if(_released[key] == true) {
    _released[key] = false;
    return true;
  }
  return false;
}

void XeInput::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
  if(action == GLFW_PRESS) _instance->_pressed[key] = true;
  if(action == GLFW_RELEASE) _instance->_released[key] = true;
}

void XeInput::mouse_callback(GLFWwindow* window, int key, int action, int mods) {
  if(action == GLFW_PRESS) _instance->_pressed[key] = true;
  if(action == GLFW_RELEASE) _instance->_released[key] = true;
}


}