HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
keyboard_bindings.hpp
1// Copyright Take Vos 2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "keyboard_key.hpp"
8#include "../URL.hpp"
9#include "../architecture.hpp"
10#include "../command.hpp"
11#include "../subsystem.hpp"
12#include <unordered_map>
13#include <tuple>
14
15namespace tt {
16
18 struct commands_t {
20 std::vector<command> system = {};
21
23 std::vector<command> ignored = {};
24
26 std::vector<command> user = {};
27
29 std::vector<command> cache = {};
30
31 [[nodiscard]] std::vector<command> const &get_commands() const noexcept {
32 return cache;
33 }
34
35 void add_system_command(command cmd) noexcept {
36 ttlet i = std::find(system.cbegin(), system.cend(), cmd);
37 if (i == system.cend()) {
38 system.push_back(cmd);
39 update_cache();
40 }
41 }
42
43 void add_ignored_command(command cmd) noexcept {
44 ttlet i = std::find(ignored.cbegin(), ignored.cend(), cmd);
45 if (i == ignored.cend()) {
46 ignored.push_back(cmd);
47 update_cache();
48 }
49 }
50
51 void add_user_command(command cmd) noexcept {
52 ttlet i = std::find(user.cbegin(), user.cend(), cmd);
53 if (i == user.cend()) {
54 user.push_back(cmd);
55 update_cache();
56 }
57 }
58
59 void update_cache() noexcept {
60 cache.reserve(std::ssize(system) + std::ssize(user));
61
62 for (ttlet cmd: system) {
63 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
64 if (i == cache.cend()) {
65 cache.push_back(cmd);
66 }
67 }
68
69 for (ttlet cmd: ignored) {
70 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
71 if (i != cache.cend()) {
72 cache.erase(i);
73 }
74 }
75
76 for (ttlet cmd: user) {
77 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
78 if (i == cache.cend()) {
79 cache.push_back(cmd);
80 }
81 }
82 }
83 };
84
88
89public:
90 keyboard_bindings() noexcept :
91 bindings() {}
92
93 void add_system_binding(keyboard_key key, command command) noexcept {
94 bindings[key].add_system_command(command);
95 }
96
97 void add_ignored_binding(keyboard_key key, command command) noexcept {
98 bindings[key].add_ignored_command(command);
99 }
100
101 void add_user_binding(keyboard_key key, command command) noexcept {
102 bindings[key].add_user_command(command);
103 }
104
107 [[nodiscard]] std::vector<command> const &translate(keyboard_key key) const noexcept {
108 static std::vector<command> empty_commands = {};
109
110 ttlet i = bindings.find(key);
111 if (i != bindings.cend()) {
112 return i->second.get_commands();
113 } else {
114 return empty_commands;
115 }
116 }
117
123 void clear() noexcept {
124 bindings.clear();
125 }
126
129 void load_bindings(URL url, bool system_binding=false);
130
134 //void save_user_bindings(URL url);
135};
136
137}
Definition keyboard_bindings.hpp:17
void clear() noexcept
Clear all bindings.
Definition keyboard_bindings.hpp:123
void load_bindings(URL url, bool system_binding=false)
Load bindings from a JSON file.
std::vector< command > const & translate(keyboard_key key) const noexcept
translate a key press in the empty-context to a command.
Definition keyboard_bindings.hpp:107
A key in combination with modifiers.
Definition keyboard_key.hpp:20
Definition URL.hpp:47
T cbegin(T... args)
T clear(T... args)
T cend(T... args)
T erase(T... args)
T find(T... args)
T push_back(T... args)
T reserve(T... args)