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 "../os_detect.hpp"
10#include "../command.hpp"
11#include <unordered_map>
12#include <tuple>
13
14namespace tt {
15
17 struct commands_t {
19 std::vector<command> system = {};
20
22 std::vector<command> ignored = {};
23
25 std::vector<command> user = {};
26
28 std::vector<command> cache = {};
29
30 [[nodiscard]] std::vector<command> const &get_commands() const noexcept {
31 return cache;
32 }
33
34 void add_system_command(command cmd) noexcept {
35 ttlet i = std::find(system.cbegin(), system.cend(), cmd);
36 if (i == system.cend()) {
37 system.push_back(cmd);
38 update_cache();
39 }
40 }
41
42 void add_ignored_command(command cmd) noexcept {
43 ttlet i = std::find(ignored.cbegin(), ignored.cend(), cmd);
44 if (i == ignored.cend()) {
45 ignored.push_back(cmd);
46 update_cache();
47 }
48 }
49
50 void add_user_command(command cmd) noexcept {
51 ttlet i = std::find(user.cbegin(), user.cend(), cmd);
52 if (i == user.cend()) {
53 user.push_back(cmd);
54 update_cache();
55 }
56 }
57
58 void update_cache() noexcept {
59 cache.reserve(std::ssize(system) + std::ssize(user));
60
61 for (ttlet cmd: system) {
62 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
63 if (i == cache.cend()) {
64 cache.push_back(cmd);
65 }
66 }
67
68 for (ttlet cmd: ignored) {
69 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
70 if (i != cache.cend()) {
71 cache.erase(i);
72 }
73 }
74
75 for (ttlet cmd: user) {
76 ttlet i = std::find(cache.cbegin(), cache.cend(), cmd);
77 if (i == cache.cend()) {
78 cache.push_back(cmd);
79 }
80 }
81 }
82 };
83
87
88public:
89 keyboard_bindings() noexcept :
90 bindings() {}
91
92 void addSystemBinding(keyboard_key key, command command) noexcept {
93 bindings[key].add_system_command(command);
94 }
95
96 void addIgnoredBinding(keyboard_key key, command command) noexcept {
97 bindings[key].add_ignored_command(command);
98 }
99
100 void addUserBinding(keyboard_key key, command command) noexcept {
101 bindings[key].add_user_command(command);
102 }
103
106 [[nodiscard]] std::vector<command> const &translate(keyboard_key key) const noexcept {
107 static std::vector<command> empty_commands = {};
108
109 ttlet i = bindings.find(key);
110 if (i != bindings.cend()) {
111 return i->second.get_commands();
112 } else {
113 return empty_commands;
114 }
115 }
116
122 void clear() noexcept {
123 bindings.clear();
124 }
125
128 void loadBindings(URL url, bool system_binding);
129
133 if constexpr (OperatingSystem::current == OperatingSystem::Windows) {
134 return loadBindings(URL{"resource:win32.keybinds.json"}, true);
135 } else {
136 tt_no_default();
137 }
138 }
139
140 void loadUserBindings(URL url) {
141 clear();
143 loadBindings(url, false);
144 }
145
150};
151
154inline keyboard_bindings keyboardBindings;
155
156}
Definition keyboard_bindings.hpp:16
void loadBindings(URL url, bool system_binding)
Load bindings from a JSON file.
void saveUserBindings(URL url)
Save user bindings This will save all bindings that are different from the system bindings.
void clear() noexcept
Clear all bindings.
Definition keyboard_bindings.hpp:122
void loadSystemBindings()
Load system bindings.
Definition keyboard_bindings.hpp:132
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:106
A key in combination with modifiers.
Definition keyboard_key.hpp:20
Definition URL.hpp:46
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)