Browse Source

Engine: Add function for finding key names

master
Riyyi 2 years ago
parent
commit
f44c82d06e
  1. 2
      src/inferno/application.cpp
  2. 19
      src/inferno/keycodes.cpp
  3. 7
      src/inferno/keycodes.h

2
src/inferno/application.cpp

@ -199,7 +199,7 @@ bool Application::onKeyPress(KeyPressEvent& e)
// Suppress unused warning
(void)e;
ruc::info("KeyPressEvent {} ({})", Input::getKeyName(e.getKey()), e.getKey());
ruc::info("KeyPressEvent {:3} {}", e.getKey(), keyName(e.getKey()));
// Stop the main loop on 'Escape' keypress
if (e.getKey() == keyCode("GLFW_KEY_ESCAPE")) {

19
src/inferno/keycodes.cpp

@ -1,4 +1,5 @@
#include <string> // std::string
#include <algorithm> // std::find_if
#include <string_view>
#include <unordered_map> // std::unordered_map
#include "GLFW/glfw3.h"
@ -8,7 +9,7 @@
namespace Inferno {
static std::unordered_map<std::string, int> keys({
static std::unordered_map<std::string_view, int> keys({
{ MAP_KEY(GLFW_KEY_UNKNOWN) },
{ MAP_KEY(GLFW_KEY_SPACE) },
{ MAP_KEY(GLFW_KEY_APOSTROPHE) },
@ -134,10 +135,20 @@ static std::unordered_map<std::string, int> keys({
// -----------------------------------------
int keyCode(const char* name)
int keyCode(std::string_view name)
{
VERIFY(keys.find(name) != keys.end(), "keyCode could not find '{}'", name);
VERIFY(keys.find(name) != keys.end(), "could not find key code: {}", name);
return keys.at(name);
}
std::string_view keyName(int key)
{
auto it = std::find_if(keys.begin(), keys.end(), [key](const auto& keybind) {
return keybind.second == key;
});
VERIFY(it != keys.end(), "could not find key name: {}", key);
return it->first.substr(9);
}
} // namespace Inferno

7
src/inferno/keycodes.h

@ -1,9 +1,12 @@
#pragma once
#include <string_view>
#define MAP_KEY(key) #key, key
namespace Inferno {
int keyCode(const char* name);
int keyCode(std::string_view name);
std::string_view keyName(int);
}
} // namespace Inferno

Loading…
Cancel
Save