summaryrefslogtreecommitdiffstats
path: root/src/keybind.cpp
blob: 8283c4cc9611475e5b9f68bcde3aed7463478cb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "keybind.hpp"
#include "csv.hpp"
#include "input.hpp"

#include <algorithm>

std::unordered_map<std::string, KeyMan::Bind> KeyMan::m_keybinds;
std::unordered_map<std::string, std::string> KeyMan::m_keybindContexts;

KeyMan::Binds KeyMan::binds;

static std::unordered_map<int, std::string> 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];
}