From 5e3a2492c7bb73daa4e27398daaf490d09980ff3 Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Wed, 22 Jun 2022 17:41:59 -0400 Subject: Base system viewer with data loaded from csv files --- src/keybind.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/keybind.cpp (limited to 'src/keybind.cpp') diff --git a/src/keybind.cpp b/src/keybind.cpp new file mode 100644 index 0000000..8283c4c --- /dev/null +++ b/src/keybind.cpp @@ -0,0 +1,64 @@ +#include "keybind.hpp" +#include "csv.hpp" +#include "input.hpp" + +#include + +std::unordered_map KeyMan::m_keybinds; +std::unordered_map KeyMan::m_keybindContexts; + +KeyMan::Binds KeyMan::binds; + +static std::unordered_map CODENAMES = +{ + { input::CTRL_KEY_ARROWUP, "Up arrow" }, + { input::CTRL_KEY_ARROWDOWN, "Down arrow" }, + { input::CTRL_KEY_ARROWRIGHT, "Right arrow" }, + { input::CTRL_KEY_ARROWLEFT, "Left arrow" } +}; + +void +KeyMan::registerBind(int def, + const std::string &name, + const std::string &context, + const std::string &desc) +{ + auto find = m_keybinds.find(name); + if(find != m_keybinds.end()) { + find->second.ctx = context; + find->second.desc = desc; + }else{ + Bind bind = { def, name, context, desc}; + m_keybinds[name] = bind; + } + m_keybindContexts[name] = context; +} + +void +KeyMan::loadKeybindsFrom(const std::string &csvPath) +{ + csv::CSVFile<',', int, std::string> keybindData(csvPath); + for(auto &bind : keybindData.get()) { + int code = std::get<0>(bind); + std::string name = std::get<1>(bind); + + m_keybinds[name] = { .code = code, .name = name, .ctx = "", .desc = ""}; + } +} + +void +KeyMan::writeKeybindsTo(const std::string &csvPath) +{ + csv::CSVFile<',', int, std::string> keybindData(csvPath, true); + for(Bind &bind : KeyMan::binds()) { + keybindData.put({bind.code, bind.name}); + } + keybindData.write(); +} + +std::string +KeyMan::translateCode(int code) +{ + if(code < 256) return std::string(1, (char)code); + return CODENAMES[code]; +} -- cgit v1.2.1