-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
432 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef DIOPTRE_KEYBOARD_GLFW_KEYBOARD_H_ | ||
#define DIOPTRE_KEYBOARD_GLFW_KEYBOARD_H_ | ||
|
||
#include <map> | ||
|
||
#include "dioptre/keyboard/keyboard_interface.h" | ||
|
||
namespace dioptre { | ||
namespace keyboard { | ||
namespace glfw { | ||
|
||
/* | ||
* Keyboard class handles keyboard input via GLFW. | ||
*/ | ||
class Keyboard : public dioptre::keyboard::KeyboardInterface { | ||
public: | ||
~Keyboard() {} | ||
|
||
/* | ||
* Binds the keyboard input | ||
*/ | ||
int initialize(); | ||
|
||
/** | ||
* Destroys the keyboard service. | ||
*/ | ||
void destroy(); | ||
|
||
/** | ||
* Map to retrieve the correct Key from the GLFW representation | ||
* of keys. | ||
*/ | ||
static dioptre::keyboard::Key map(int key); | ||
|
||
private: | ||
static std::map<int, dioptre::keyboard::Key> keyMap_; | ||
}; // Keyboard | ||
|
||
/* | ||
* Method to handle key events. | ||
*/ | ||
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); | ||
|
||
} // glfw | ||
} // keyboard | ||
} // dioptre | ||
|
||
#endif // DIOPTRE_KEYBOARD_GLFW_KEYBOARD_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef DIOPTRE_KEYBOARD_KEY_HANDLER_INTERFACE_H_ | ||
#define DIOPTRE_KEYBOARD_KEY_HANDLER_INTERFACE_H_ | ||
|
||
#include "keys.h" | ||
|
||
namespace dioptre { | ||
namespace keyboard { | ||
|
||
/* | ||
* Interface describing key press events. | ||
*/ | ||
class KeyHandlerInterface { | ||
public: | ||
virtual ~KeyHandlerInterface() {} | ||
|
||
/** | ||
* Implementation should return the key it wants to respond to. | ||
*/ | ||
virtual Key handles() = 0; | ||
|
||
/** | ||
* Implementation should execute the code to when the actual key is pressed. | ||
*/ | ||
virtual void update() = 0; | ||
}; // KeyHandlerInterface | ||
|
||
} // keyboard | ||
} // dioptre | ||
|
||
#endif // DIOPTRE_KEYBOARD_KEY_HANDLER_INTERFACE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef DIOPTRE_KEYBOARD_KEYBOARD_INTERFACE_H_ | ||
#define DIOPTRE_KEYBOARD_KEYBOARD_INTERFACE_H_ | ||
|
||
#include <vector> | ||
#include <map> | ||
|
||
#include "dioptre/module.h" | ||
#include "keys.h" | ||
#include "key_handler_interface.h" | ||
|
||
namespace dioptre { | ||
namespace keyboard { | ||
|
||
/* | ||
* Interface to handle keyboard input. | ||
*/ | ||
class KeyboardInterface : public dioptre::Module { | ||
public: | ||
|
||
virtual ~KeyboardInterface() {} | ||
|
||
/** | ||
* Registers a handler which is notified when a particular key is pressed. | ||
*/ | ||
void registerKeyHandler(KeyHandlerInterface* handler); | ||
|
||
/** | ||
* Tells the keyboard service that a button was pressed. | ||
*/ | ||
void press(Key key); | ||
|
||
private: | ||
std::vector<Key> pressed_; | ||
std::map<Key, std::vector<KeyHandlerInterface*>> keyHandlers_; | ||
}; // KeyboardInterface | ||
|
||
} // keyboard | ||
} // dioptre | ||
|
||
#endif // DIOPTRE_KEYBOARD_KEYBOARD_INTERFACE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef DIOPTRE_KEYBOARD_KEYS_H_ | ||
#define DIOPTRE_KEYBOARD_KEYS_H_ | ||
|
||
namespace dioptre { | ||
namespace keyboard { | ||
|
||
/* | ||
* Collection of all available keys. | ||
*/ | ||
enum Key { | ||
KEY_UNKNOWN, | ||
|
||
KEY_ESCAPE, | ||
|
||
KEY_MAX_ENUM | ||
}; // Key | ||
|
||
} // keyboard | ||
} // dioptre | ||
|
||
#endif // DIOPTRE_KEYBOARD_KEYS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef DIOPTRE_KEYBOARD_NULL_KEYBOARD_H_ | ||
#define DIOPTRE_KEYBOARD_NULL_KEYBOARD_H_ | ||
|
||
#include "dioptre/keyboard/keyboard_interface.h" | ||
|
||
namespace dioptre { | ||
namespace keyboard { | ||
namespace null { | ||
|
||
/* | ||
* Null keyboard input. | ||
*/ | ||
class Keyboard : public dioptre::keyboard::KeyboardInterface { | ||
public: | ||
~Keyboard() {} | ||
|
||
/* | ||
* Null initializer. | ||
*/ | ||
int initialize() { return 0; } | ||
|
||
/* | ||
* Null destroy. | ||
*/ | ||
void destroy() {} | ||
}; // Keyboard | ||
|
||
} // null | ||
} // keyboard | ||
} // dioptre | ||
|
||
#endif // DIOPTRE_KEYBOARD_NULL_KEYBOARD_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ class Module { | |
enum ModuleType { | ||
M_WINDOW, | ||
M_GRAPHICS, | ||
M_KEYBOARD, | ||
M_MAX_ENUM | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <iostream> | ||
#include "dioptre/locator.h" | ||
#include "dioptre/window/glfw/window.h" | ||
#include "dioptre/keyboard/glfw/keyboard.h" | ||
|
||
namespace dioptre { | ||
namespace keyboard { | ||
namespace glfw { | ||
|
||
std::map<int, dioptre::keyboard::Key> Keyboard::keyMap_; | ||
|
||
int Keyboard::initialize() { | ||
auto window = dioptre::Locator::getInstance<dioptre::window::glfw::Window>(dioptre::Module::M_WINDOW); | ||
GLFWwindow* glfwWindow = window->GetWindow(); | ||
|
||
// Ensure we can capture the escape key being pressed below | ||
glfwSetInputMode(glfwWindow, GLFW_STICKY_KEYS, GL_TRUE); | ||
glfwSetKeyCallback(glfwWindow, keyCallback); | ||
|
||
Keyboard::keyMap_[GLFW_KEY_ESCAPE] = dioptre::keyboard::KEY_ESCAPE; | ||
|
||
return 0; | ||
} | ||
|
||
void Keyboard::destroy() { | ||
|
||
} | ||
|
||
dioptre::keyboard::Key Keyboard::map(int key) { | ||
return Keyboard::keyMap_[key]; | ||
} | ||
|
||
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { | ||
auto keyboard = dioptre::Locator::getInstance<dioptre::keyboard::glfw::Keyboard>(dioptre::Module::M_KEYBOARD); | ||
auto mappedKey = keyboard->map(key); | ||
|
||
if (action == GLFW_PRESS) { | ||
keyboard->press(mappedKey); | ||
} | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "exit_game.h" | ||
#include "dioptre/locator.h" | ||
#include "dioptre/keyboard/keys.h" | ||
|
||
namespace dioptre { | ||
namespace keyboard { | ||
namespace handlers { | ||
|
||
dioptre::keyboard::Key ExitGame::handles() { | ||
return dioptre::keyboard::KEY_ESCAPE; | ||
} | ||
|
||
void ExitGame::update() { | ||
auto window = dioptre::Locator::getInstance<dioptre::window::WindowInterface>(dioptre::Module::M_WINDOW); | ||
window->setShouldClose(true); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef DIOPTRE_KEYBOARD_HANDLERS_EXIT_GAME_H_ | ||
#define DIOPTRE_KEYBOARD_HANDLERS_EXIT_GAME_H_ | ||
|
||
#include "dioptre/keyboard/keys.h" | ||
#include "dioptre/keyboard/key_handler_interface.h" | ||
|
||
namespace dioptre { | ||
namespace keyboard { | ||
namespace handlers { | ||
|
||
/* | ||
* Keyboard handler to exit the game when the ESC is pressed. | ||
*/ | ||
class ExitGame : public dioptre::keyboard::KeyHandlerInterface { | ||
public: | ||
/* | ||
* handles returns ESC to listen to that key. | ||
*/ | ||
dioptre::keyboard::Key handles(); | ||
|
||
/* | ||
* Sends a signal to the window service that the window should be closed. | ||
*/ | ||
void update(); | ||
}; // ExitGame | ||
|
||
} // handlers | ||
} // keyboard | ||
} // dioptre | ||
|
||
#endif // DIOPTRE_KEYBOARD_HANDLERS_EXIT_GAME_H_ |
Oops, something went wrong.