123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- #include <GLFW/glfw3.h>
- #include "client/input/Keys.h"
- bool Keys::Key::isDown() const
- {
- return down;
- }
- bool Keys::Key::isReleased() const
- {
- return shouldRelease;
- }
- u32 Keys::Key::getDownTime() const
- {
- return downTime;
- }
- std::ostream& operator <<(std::ostream& os, const Keys::Key& k)
- {
- os << "Key(down: " << k.isDown() << ", release: " << k.isReleased() <<
- ", time: " << k.getDownTime() << ")";
- return os;
- }
- Keys::Keys() : left(keys[0]), right(keys[1]), up(keys[2]), down(keys[3]), jump(keys[4]), sneak(keys[5])
- {
- keys[0].glfwKey = GLFW_KEY_A;
- keys[1].glfwKey = GLFW_KEY_D;
- keys[2].glfwKey = GLFW_KEY_W;
- keys[3].glfwKey = GLFW_KEY_S;
- keys[4].glfwKey = GLFW_KEY_SPACE;
- keys[5].glfwKey = GLFW_KEY_LEFT_SHIFT;
- }
- void Keys::release(int key)
- {
- for(Key& k : keys)
- {
- if(k.glfwKey == key)
- {
- k.shouldRelease = true;
- }
- }
- }
- void Keys::press(int key)
- {
- for(Key& k : keys)
- {
- if(k.glfwKey == key)
- {
- k.down = true;
- k.shouldRelease = false;
- }
- }
- }
- void Keys::tick()
- {
- for(Key& k : keys)
- {
- k.downTime += k.down;
- k.down = k.down && !k.shouldRelease;
- k.downTime *= !k.shouldRelease;
- k.shouldRelease = false;
- }
- }
|