Moved some files around

Input manager is ported and just needs to be used by the core
Gui_Panel_Refactor
Joeyrp 4 years ago
parent f8d2df5007
commit 9ad0276d3c

@ -19,6 +19,7 @@ set(LUNARIUM_SRC
"src/core/core.cpp" "src/core/core.cpp"
"src/core/state.cpp" "src/core/state.cpp"
"src/core/version.cpp" "src/core/version.cpp"
"src/core/window.cpp"
"src/utils/types.cpp" "src/utils/types.cpp"
"src/utils/logger.cpp" "src/utils/logger.cpp"
"src/utils/highResTimer.cpp" "src/utils/highResTimer.cpp"
@ -27,12 +28,13 @@ set(LUNARIUM_SRC
"src/utils/args.cpp" "src/utils/args.cpp"
"src/utils/binaryFileBuffer.cpp" "src/utils/binaryFileBuffer.cpp"
"src/utils/frameCounter.cpp" "src/utils/frameCounter.cpp"
"src/graphics/window.cpp" "src/graphics/opengl/glGraphics.cpp"
"src/graphics/glGraphics.cpp" "src/graphics/opengl/glText.cpp"
"src/graphics/glShader.cpp" "src/graphics/opengl/glShader.cpp"
"src/graphics/image.cpp" "src/graphics/image.cpp"
"src/graphics/glText.cpp"
"src/graphics/internalFont.cpp" "src/graphics/internalFont.cpp"
"src/input/keyboard.cpp"
"src/input/inputManager.cpp"
) )
# add the executable # add the executable
@ -85,7 +87,7 @@ add_subdirectory(external/glad/src)
add_subdirectory(external/glm) add_subdirectory(external/glm)
# add dearimgui # add dearimgui
add_subdirectory(src/dearimgui) add_subdirectory(src/graphics/dearimgui)
# add lua -- https://github.com/walterschell/Lua # add lua -- https://github.com/walterschell/Lua
add_subdirectory(external/lua) add_subdirectory(external/lua)
@ -102,7 +104,7 @@ target_include_directories(${PROJECT_NAME}
PUBLIC external/glfw/include PUBLIC external/glfw/include
PUBLIC external/glm PUBLIC external/glm
PUBLIC external/lua/lua5.4.3/include PUBLIC external/lua/lua5.4.3/include
PUBLIC src/dearimgui PUBLIC src/graphics/dearimgui
PUBLIC external/pugixml/src PUBLIC external/pugixml/src
PUBLIC external/glad/include PUBLIC external/glad/include
PUBLIC external/freetype/include PUBLIC external/freetype/include
@ -111,7 +113,7 @@ target_include_directories(${PROJECT_NAME}
target_link_directories(${PROJECT_NAME} target_link_directories(${PROJECT_NAME}
PRIVATE external/glfw/src PRIVATE external/glfw/src
PRIVATE external/glm PRIVATE external/glm
PRIVATE src/dearimgui PRIVATE src/graphics/dearimgui
PRIVATE external/glad/src PRIVATE external/glad/src
PRIVATE external/freetype/src PRIVATE external/freetype/src
) )

@ -14,7 +14,8 @@ Core:
☐ Implement the Image creation methods ☐ Implement the Image creation methods
Input: Input:
☐ Port over the Element2D input system and adjust it to use glfw ✔ Port over the Element2D input system and adjust it to use glfw @done (9/8/2021, 8:20:07 PM)
☐ Add the InputManager to the core
Audio: Audio:
@ -26,15 +27,16 @@ Core:
Interface Class: Interface Class:
☐ Provide Methods that give access to the C++ code ☐ Provide Methods that give access to the C++ code
Resource Managment:
Game: Game:
☐ Load game project data ☐ Load game project data
☐ Manage list of scenes ☐ Manage list of scenes
☐ Manage global scripts ☐ Manage global scripts
☐ Handle Events from the core
Scene: Scene:
☐ Manage scene scripts ☐ Manage scene scripts
☐ Manage game objects in scene
Manage list of Regions: Manage list of Regions:
☐ Track which regions should be loaded ☐ Track which regions should be loaded
@ -55,5 +57,8 @@ Game:
Editor: Editor:
☐ Reference raw asset files in a "content" folder
Asset Pipeline:
☐ Read through the contents folder and generate asset files in a custom format (useable by the engine)

@ -10,8 +10,8 @@
#include "version.h" #include "version.h"
// Sub Systems // Sub Systems
#include <graphics/window.h> #include "window.h"
#include <graphics/glGraphics.h> #include <graphics/opengl/glGraphics.h>
namespace lunarium namespace lunarium
{ {

@ -102,6 +102,23 @@ namespace lunarium
return mpWindow; return mpWindow;
} }
glm::vec2 Window::GetCursorPos() const
{
double x, y;
glfwGetCursorPos(mpWindow, &x, &y);
return { (float)x, (float)y };
}
void Window::SetCursorPos(glm::vec2 pos)
{
glfwSetCursorPos(mpWindow, pos.x, pos.y);
}
bool Window::IsKeyDown(int key) const
{
return glfwGetKey(mpWindow, key) == GLFW_PRESS;
}
void Window::Resize(int width, int height) void Window::Resize(int width, int height)
{ {
glfwSetWindowSize(mpWindow, width, height); glfwSetWindowSize(mpWindow, width, height);

@ -13,6 +13,7 @@
#include <glad/gl.h> #include <glad/gl.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <core/state.h> #include <core/state.h>
#include <utils/opRes.h> #include <utils/opRes.h>
@ -27,7 +28,9 @@ namespace lunarium
bool IsInit() const; bool IsInit() const;
GLFWwindow* GetWindow(); GLFWwindow* GetWindow();
bool IsKeyDown(int key) const;
glm::vec2 GetCursorPos() const;
void SetCursorPos(glm::vec2 pos);
void Resize(int width, int height); void Resize(int width, int height);

@ -1,7 +1,7 @@
add_library(dearimgui imgui.cpp imgui_demo.cpp imgui_widgets.cpp imgui_tables.cpp imgui_draw.cpp imgui_impl_glfw.cpp imgui_impl_opengl3.cpp) add_library(dearimgui imgui.cpp imgui_demo.cpp imgui_widgets.cpp imgui_tables.cpp imgui_draw.cpp imgui_impl_glfw.cpp imgui_impl_opengl3.cpp)
target_include_directories(dearimgui target_include_directories(dearimgui
PUBLIC ../../external/glfw/include PUBLIC ../../../external/glfw/include
) )
# message( " current source dir: ${CMAKE_CURRENT_SOURCE_DIR}" ) # message( " current source dir: ${CMAKE_CURRENT_SOURCE_DIR}" )

@ -8,9 +8,9 @@
#include "glGraphics.h" #include "glGraphics.h"
#include "defaultShaders.h" #include "defaultShaders.h"
#include "window.h" #include <core/window.h>
#include "image.h" #include "../image.h"
#include "internalFont.h" #include "../internalFont.h"
#include <utils/logger.h> #include <utils/logger.h>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>

@ -9,7 +9,7 @@
#ifndef OGLGRAPHICS_H_ #ifndef OGLGRAPHICS_H_
#define OGLGRAPHICS_H_ #define OGLGRAPHICS_H_
#include "igraphics.h" #include "../igraphics.h"
#include <glad/gl.h> #include <glad/gl.h>
#include "glShader.h" #include "glShader.h"
#include "glText.h" #include "glText.h"

@ -0,0 +1,132 @@
/******************************************************************************
* File - InputManager.pp
* Author - Joey Pollack
* Date - 2019/11/14 (y/m/d)
* Mod Date - 2019/11/25 (y/m/d)
* Description - Handles all input for windows systems
*
******************************************************************************/
#include <core/window.h>
#include "inputManager.h"
#include <utils/logger.h>
namespace lunarium
{
InputManager::InputManager()
: mpWindow(nullptr)
{
memset(mKeyboardState, 0, NUM_KEYS);
}
void InputManager::Initialize(Window* pWindow)
{
mpWindow = pWindow;
}
const InputManager::_KeyEvents& InputManager::PollKeys()
{
mKeyEvents.KeysPressed.clear();
mKeyEvents.KeysReleased.clear();
// Check Modifiers first so we only have to do it once
// bool shiftHeld = GetAsyncKeyState(VK_SHIFT);
// bool controlHeld = GetAsyncKeyState(VK_CONTROL);
// bool altHeld = GetAsyncKeyState(VK_MENU);
bool shiftHeld = mpWindow->IsKeyDown(KeyCode::SHIFT);
bool controlHeld = mpWindow->IsKeyDown(KeyCode::CONTROL);
bool altHeld = mpWindow->IsKeyDown(KeyCode::ALT);
for (int i = 0; i < NUM_KEYS; i++)
{
Keyboard::Key key = Keyboard::GetInstance()->GetKey((KeyCode)i);
bool keyIsDown = mpWindow->IsKeyDown(KeyCodeList[i]);
// Mouse Buttons are a special case
if (IsMouseButton(i))
{
if (keyIsDown)
{
mKeyboardState[i] = KEY_DOWN;
}
else
{
mKeyboardState[i] = KEY_UP;
}
continue;
}
// Key is a known keyboard key
if (keyIsDown)
{
// Ignore key presses for unknown keys
if (KeyCode::KEY_UNKNOWN == key.Code)
{
Logger::Log(LogCategory::CORE, LogLevel::INFO_VERBOSE,
"Input Manager skipping unknown key press: idx: %d, code: %d", i, KeyCodeList[i]);
continue;
}
// Key was just pressed this frame
if (KEY_UP == mKeyboardState[i])
{
mKeyboardState[i] = KEY_DOWN;
KeyPress kp;
kp.Key = key;
kp.ShiftHeld = shiftHeld;
kp.ControlHeld = controlHeld;
kp.AltHeld = altHeld;
mKeyEvents.KeysPressed.push_back(kp);
}
}
else
{
// Key was just released this frame
if (KEY_DOWN == mKeyboardState[i])
{
mKeyboardState[i] = KEY_UP;
mKeyEvents.KeysReleased.push_back(key);
}
}
}
return mKeyEvents;
}
bool InputManager::IsMouseButton(int keyCode)
{
return (keyCode == KeyCode::MOUSE_LEFT_BUTTON ||
keyCode == KeyCode::MOUSE_RIGHT_BUTTON ||
keyCode == KeyCode::MOUSE_MIDDLE_BUTTON ||
keyCode == KeyCode::MOUSE_X1_BUTTON ||
keyCode == KeyCode::MOUSE_X2_BUTTON );
}
bool InputManager::IsKeyDown(KeyCode key)
{
return (mKeyboardState[key] == KEY_DOWN);
}
// TODO: FIX WINDOWS SPECIFIC CODE:
glm::vec2 InputManager::GetMousePosition()
{
// POINT p;
// memset(&p, 0, sizeof(POINT));
// GetCursorPos(&p);
// return { (float)p.x, (float)p.y };
return mpWindow->GetCursorPos();
}
void InputManager::SetMousePosition(glm::vec2 p)
{
//SetCursorPos((int)p.X, (int)p.Y);
mpWindow->SetCursorPos(p);
}
///////////////////////////////////////////////////
}

@ -0,0 +1,88 @@
/******************************************************************************
* File - InputManager.h
* Author - Joey Pollack
* Date - 2019/11/14 (y/m/d)
* Mod Date - 2021/09/08 (y/m/d)
* Description - Handles all input for windows systems
*
******************************************************************************/
#ifndef INPUT_MANAGER_H_
#define INPUT_MANAGER_H_
#include <vector>
#include <string>
#include <glm/glm.hpp>
#include "keyboard.h"
namespace lunarium
{
class Window;
class InputManager
{
public:
struct KeyPress
{
Keyboard::Key Key;
// Modifiers
bool ShiftHeld;
bool ControlHeld;
bool AltHeld;
};
struct MouseButton
{
KeyCode code;
std::string Name;
};
private:
friend class Core;
struct _KeyEvents
{
std::vector<KeyPress> KeysPressed;
std::vector<Keyboard::Key> KeysReleased;
} mKeyEvents;
const int KEY_UP = 0;
const int KEY_DOWN = 0xFF;
private: // ENGINE ONLY METHODS
InputManager();
void Initialize(Window* pWindow);
// Returns an array of keys that were pressed
// Keys are only pressed if they were not held on the previous frame
const _KeyEvents& PollKeys();
bool IsMouseButton(int keyCode);
// TODO: Remove these functions
// KeyCode TranslateKey(int k);
// char GetAsciiFromKeyPress(KeyCode k, bool shiftHeld);
public:
bool IsKeyDown(KeyCode key);
// Returns the mouse position in screen coordinates
glm::vec2 GetMousePosition();
void SetMousePosition(glm::vec2 p);
private:
Window* mpWindow;
unsigned char mKeyboardState[NUM_KEYS];
//unsigned char mKeyboardPreviousState[NUM_KEYS];
std::vector<KeyCode> mModifiers;
};
}
#endif // WIN_INPUT_MANAGER_H_

@ -0,0 +1,223 @@
/******************************************************************************
* File - KeyCodes.h
* Author - Joey Pollack
* Date - 2019/11/14 (y/m/d)
* Mod Date - 2021/09/08 (y/m/d)
* Description - Key code definitions using GLFW
*
******************************************************************************/
#ifndef KEY_CODES_H_
#define KEY_CODES_H_
#include <GLFW/glfw3.h>
namespace lunarium
{
enum KeyCode
{
A = 'A',
B = 'B',
C = 'C',
D = 'D',
E = 'E',
F = 'F',
G = 'G',
H = 'H',
I = 'I',
J = 'J',
K = 'K',
L = 'L',
M = 'M',
N = 'N',
O = 'O',
P = 'P',
Q = 'Q',
R = 'R',
S = 'S',
T = 'T',
U = 'U',
V = 'V',
W = 'W',
X = 'X',
Y = 'Y',
Z = 'Z',
NUM_0 = '0',
NUM_1 = '1',
NUM_2 = '2',
NUM_3 = '3',
NUM_4 = '4',
NUM_5 = '5',
NUM_6 = '6',
NUM_7 = '7',
NUM_8 = '8',
NUM_9 = '9',
F1 = GLFW_KEY_F1,
F2 = GLFW_KEY_F2,
F3 = GLFW_KEY_F3,
F4 = GLFW_KEY_F4,
F5 = GLFW_KEY_F5,
F6 = GLFW_KEY_F6,
F7 = GLFW_KEY_F7,
F8 = GLFW_KEY_F8,
F9 = GLFW_KEY_F9,
F10 = GLFW_KEY_F10,
F11 = GLFW_KEY_F11,
F12 = GLFW_KEY_F12,
LSHIFT = GLFW_KEY_LEFT_SHIFT,
RSHIFT = GLFW_KEY_RIGHT_SHIFT,
LCONTROL = GLFW_KEY_LEFT_CONTROL,
RCONTROL = GLFW_KEY_RIGHT_CONTROL,
LALT = GLFW_KEY_LEFT_ALT,
RALT = GLFW_KEY_RIGHT_ALT,
SPACE = GLFW_KEY_SPACE,
// Left Arrow Key
LEFT = GLFW_KEY_LEFT,
// Up Arrow Key
UP = GLFW_KEY_UP,
// Right Arrow Key
RIGHT = GLFW_KEY_RIGHT,
// Down Arrow Key
DOWN = GLFW_KEY_DOWN,
// DELETE
DEL = GLFW_KEY_DELETE,
// ENTER
RETURN = GLFW_KEY_ENTER,
BACKSPACE = GLFW_KEY_BACKSPACE,
TAB = GLFW_KEY_TAB,
ESCAPE = GLFW_KEY_ESCAPE,
// Punctuation
SEMICOLON = GLFW_KEY_SEMICOLON, // ';:' for US
EQUALS = GLFW_KEY_EQUAL, // '+' any country
COMMA = GLFW_KEY_COMMA, // ',' any country
DASH = GLFW_KEY_MINUS, // '-' any country
DOT = GLFW_KEY_PERIOD, // '.' any country
FORWARD_SLASH = GLFW_KEY_SLASH, // '/?' for US
TILDE = GLFW_KEY_GRAVE_ACCENT, // '`~' for US
OPEN_BRACKET = GLFW_KEY_LEFT_BRACKET, // '[{' for US
BACKSLASH = GLFW_KEY_BACKSLASH, // '\|' for US
CLOSE_BRACKET = GLFW_KEY_RIGHT_BRACKET, // ']}' for US
QUOTE = GLFW_KEY_APOSTROPHE, // ''"' for US
// MOUSE BUTTONS
MOUSE_LEFT_BUTTON = GLFW_MOUSE_BUTTON_LEFT,
MOUSE_RIGHT_BUTTON = GLFW_MOUSE_BUTTON_RIGHT,
MOUSE_MIDDLE_BUTTON = GLFW_MOUSE_BUTTON_MIDDLE,
// NOTE: Not sure about these
MOUSE_X1_BUTTON = GLFW_MOUSE_BUTTON_4,
MOUSE_X2_BUTTON = GLFW_MOUSE_BUTTON_5,
NUM_KEYS,
// ALTERNATE NAMES
SHIFT = GLFW_KEY_LEFT_SHIFT,
CONTROL = GLFW_KEY_LEFT_CONTROL,
ALT = GLFW_KEY_LEFT_ALT,
KEY_UNKNOWN = 0xFF
};
static int KeyCodeList[] = {
KeyCode::A,
KeyCode::B,
KeyCode::C,
KeyCode::D,
KeyCode::E,
KeyCode::F,
KeyCode::G,
KeyCode::H,
KeyCode::I,
KeyCode::J,
KeyCode::K,
KeyCode::L,
KeyCode::M,
KeyCode::N,
KeyCode::O,
KeyCode::P,
KeyCode::Q,
KeyCode::R,
KeyCode::S,
KeyCode::T,
KeyCode::U,
KeyCode::V,
KeyCode::W,
KeyCode::X,
KeyCode::Y,
KeyCode::Z,
KeyCode::NUM_0,
KeyCode::NUM_1,
KeyCode::NUM_2,
KeyCode::NUM_3,
KeyCode::NUM_4,
KeyCode::NUM_5,
KeyCode::NUM_6,
KeyCode::NUM_7,
KeyCode::NUM_8,
KeyCode::NUM_9,
KeyCode::F1,
KeyCode::F2,
KeyCode::F3,
KeyCode::F4,
KeyCode::F5,
KeyCode::F6,
KeyCode::F7,
KeyCode::F8,
KeyCode::F9,
KeyCode::F10,
KeyCode::F11,
KeyCode::F12,
KeyCode::LSHIFT,
KeyCode::RSHIFT,
KeyCode::LCONTROL,
KeyCode::RCONTROL,
KeyCode::LALT,
KeyCode::RALT,
KeyCode::SPACE,
KeyCode::LEFT,
KeyCode::UP,
KeyCode::RIGHT,
KeyCode::DOWN,
KeyCode::DEL,
KeyCode::RETURN,
KeyCode::BACKSPACE,
KeyCode::TAB,
KeyCode::ESCAPE,
KeyCode::SEMICOLON,
KeyCode::EQUALS,
KeyCode::COMMA,
KeyCode::DASH,
KeyCode::DOT,
KeyCode::FORWARD_SLASH,
KeyCode::TILDE,
KeyCode::OPEN_BRACKET,
KeyCode::BACKSLASH,
KeyCode::CLOSE_BRACKET,
KeyCode::QUOTE,
KeyCode::MOUSE_LEFT_BUTTON,
KeyCode::MOUSE_RIGHT_BUTTON,
KeyCode::MOUSE_MIDDLE_BUTTON,
KeyCode::MOUSE_X1_BUTTON,
KeyCode::MOUSE_X2_BUTTON
};
}
#endif // KEY_CODES_H_

@ -0,0 +1,188 @@
/******************************************************************************
* File - Keyboard.cpp
* Author - Joey Pollack
* Date - 2019/11/15 (y/m/d)
* Mod Date - 2019/11/15 (y/m/d)
* Description - Stores data about the keys on the keyboard
*
******************************************************************************/
#include "keyboard.h"
namespace lunarium
{
Keyboard* Keyboard::mpInstance = nullptr;
void Keyboard::Shutdown()
{
if (mpInstance)
{
delete mpInstance;
mpInstance = nullptr;
}
}
Keyboard * Keyboard::GetInstance()
{
if (!mpInstance)
{
mpInstance = new Keyboard;
mpInstance->Initialize();
}
return mpInstance;
}
Keyboard::Keyboard()
{
}
void Keyboard::Initialize()
{
// Add letters
for (char letter = 'A'; letter <= 'Z'; letter++)
{
Key k;
k.Code = (KeyCode)letter;
k.Name = std::string() + letter;
k.ShiftName = std::string() + (char)(letter + 32);
k.AsciiValue = letter;
k.ShiftAsciiValue = letter + 32;
mKeyboard.insert(std::pair<KeyCode, Key>(k.Code, k));
}
// Add Numbers
char numShift[10] = { ')', '!', '@', '#', '$', '%', '^', '&', '*', '(' };
for (int i = 0; i <= 9; i++)
{
Key k;
k.Code = (KeyCode)(i + 48);
char name[2];
sprintf_s(name, 2, "%d", i);
k.Name = std::string(name);
k.ShiftName = std::string() + numShift[i];
k.AsciiValue = (char)(i + 48);
k.ShiftAsciiValue = numShift[i];
mKeyboard.insert(std::pair<KeyCode, Key>(k.Code, k));
}
// Punctuation
/*
SEMICOLON = GLFW_KEY_SEMICOLON, // ';:' for US
EQUALS = GLFW_KEY_EQUAL, // '+' any country
COMMA = GLFW_KEY_COMMA, // ',' any country
DASH = GLFW_KEY_MINUS, // '-' any country
DOT = GLFW_KEY_PERIOD, // '.' any country
FORWARD_SLASH = GLFW_KEY_SLASH, // '/?' for US
TILDE = GLFW_KEY_GRAVE_ACCENT, // '`~' for US
OPEN_BRACKET = GLFW_KEY_LEFT_BRACKET, // '[{' for US
BACKSLASH = GLFW_KEY_BACKSLASH, // '\|' for US
CLOSE_BRACKET = GLFW_KEY_RIGHT_BRACKET, // ']}' for US
QUOTE = GLFW_KEY_APOSTROPHE, // ''"' for US
*/
mKeyboard.insert(std::pair<KeyCode, Key>(SEMICOLON, { SEMICOLON, "semi colon", "colon", ';', ';' }));
mKeyboard.insert(std::pair<KeyCode, Key>(EQUALS, { EQUALS, "equals", "plus", '=', '+' }));
mKeyboard.insert(std::pair<KeyCode, Key>(COMMA, { COMMA, "comma", "less than", ',', '<' }));
mKeyboard.insert(std::pair<KeyCode, Key>(DASH, { DASH, "dash", "underscore", '-', '_' }));
mKeyboard.insert(std::pair<KeyCode, Key>(DOT, { DOT, "dot", "greater than", '.', '>' }));
mKeyboard.insert(std::pair<KeyCode, Key>(FORWARD_SLASH, { FORWARD_SLASH, "forward slash", "question mark", '/', '?' }));
mKeyboard.insert(std::pair<KeyCode, Key>(TILDE, { TILDE, "tick", "tilde", '`', '~' }));
mKeyboard.insert(std::pair<KeyCode, Key>(OPEN_BRACKET, { OPEN_BRACKET, "open square bracket", "open curly brace", '[', '{' }));
mKeyboard.insert(std::pair<KeyCode, Key>(BACKSLASH, { BACKSLASH, "back slash", "pipe", '\\', '|' }));
mKeyboard.insert(std::pair<KeyCode, Key>(CLOSE_BRACKET, { CLOSE_BRACKET, "close square bracket", "close curly brace", ']', '}' }));
mKeyboard.insert(std::pair<KeyCode, Key>(QUOTE, { QUOTE, "single quote", "double quote", '\'', '\"' }));
// Modifiers
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::LSHIFT, { KeyCode::LSHIFT, "left shift", "left shift", (char)0, (char)0 }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::RSHIFT, { KeyCode::RSHIFT, "right shift", "right shift", (char)0, (char)0 }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::LCONTROL, { KeyCode::LCONTROL, "left control", "left control", (char)0, (char)0 }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::RCONTROL, { KeyCode::RCONTROL, "right control", "right control", (char)0, (char)0 }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::LALT, { KeyCode::LALT, "left alt", "left alt", (char)0, (char)0 }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::RALT, { KeyCode::RALT, "right alt", "right alt", (char)0, (char)0 }));
// Control characters
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::DEL, { KeyCode::DEL, "delete", "delete", (char)(127), (char)(127) }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::BACKSPACE, { KeyCode::BACKSPACE, "backspace", "backspace", (char)(8), (char)(8) }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::TAB, { KeyCode::TAB, "tab", "tab", (char)(9), (char)(9) }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::RETURN, { KeyCode::RETURN, "return", "return", (char)(10), (char)(10) }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::ESCAPE, { KeyCode::ESCAPE, "escape", "escape", (char)(27), (char)(27) }));
// Arrow Keys
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::LEFT, { KeyCode::LEFT, "Left Arrow", "Left Arrow", (char)(0), (char)(0) }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::UP, { KeyCode::UP, "Up Arrow", "Up Arrow", (char)(0), (char)(0) }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::RIGHT, { KeyCode::RIGHT, "Right Arrow", "Right Arrow", (char)(0), (char)(0) }));
mKeyboard.insert(std::pair<KeyCode, Key>(KeyCode::DOWN, { KeyCode::DOWN, "Down Arrow", "Down Arrow", (char)(0), (char)(0) }));
// Set up the cross references
InitKeysByName();
InitKeysByAscii();
}
Keyboard::Key Keyboard::GetKey(KeyCode code)
{
auto iter = mKeyboard.find(code);
if (mKeyboard.end() == iter)
return Key::Unknown();
return (*iter).second;
}
Keyboard::Key Keyboard::GetKey(std::string name)
{
auto iter = mKeysByName.find(name);
if (mKeysByName.end() == iter)
return Key::Unknown();
return (*iter).second;
}
Keyboard::Key Keyboard::GetKey(char ascii)
{
auto iter = mKeysByAscii.find(ascii);
if (mKeysByAscii.end() == iter)
return Key::Unknown();
return (*iter).second;
}
void Keyboard::InitKeysByName()
{
for (auto iter = mKeyboard.begin(); iter != mKeyboard.end(); iter++)
{
mKeysByName.insert(std::pair<std::string, Key&>((*iter).second.Name, (*iter).second));
}
}
void Keyboard::InitKeysByAscii()
{
for (auto iter = mKeyboard.begin(); iter != mKeyboard.end(); iter++)
{
mKeysByAscii.insert(std::pair<char, Key&>((*iter).second.AsciiValue, (*iter).second));
mKeysByAscii.insert(std::pair<char, Key&>((*iter).second.ShiftAsciiValue, (*iter).second));
}
}
Keyboard::Key Keyboard::Key::Unknown()
{
Key k;
k.Code = KeyCode::KEY_UNKNOWN;
k.Name = "unknown";
k.ShiftName = "unknown";
k.AsciiValue = (char)0;
k.ShiftAsciiValue = (char)0;
return k;
}
}

@ -0,0 +1,69 @@
/******************************************************************************
* File - Keyboard.h
* Author - Joey Pollack
* Date - 2019/11/15 (y/m/d)
* Mod Date - 2019/11/15 (y/m/d)
* Description - Stores data about the keys on the keyboard
*
******************************************************************************/
#ifndef KEYBOARD_H_
#define KEYBOARD_H_
#include <map>
#include <string>
#include "keyCodes.h"
namespace lunarium
{
class Keyboard
{
friend class Core;
public: // Data structures
struct Key
{
KeyCode Code;
std::string Name;
std::string ShiftName;
char AsciiValue;
char ShiftAsciiValue;
static Key Unknown();
};
private: // Internal / Engine Only
Keyboard();
Keyboard(const Keyboard&) = delete;
Keyboard& operator=(const Keyboard&) = delete;
void Initialize();
static void Shutdown();
public: // Interface
static Keyboard* GetInstance();
Key GetKey(KeyCode code);
Key GetKey(std::string name);
Key GetKey(char ascii);
private: // The instance
static Keyboard* mpInstance;
private: // Data
std::map<KeyCode, Key> mKeyboard;
// Cross references
std::map<std::string, Key&> mKeysByName;
std::map<char, Key&> mKeysByAscii;
private: // Helper methods
void InitKeysByName();
void InitKeysByAscii();
};
}
#endif // KEYBOARD_H_
Loading…
Cancel
Save