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 hi::inline v1 {
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 {
33 return cache;
34 }
35
36 void add_system_command(command cmd) noexcept
37 {
38 hilet i = std::find(system.cbegin(), system.cend(), cmd);
39 if (i == system.cend()) {
40 system.push_back(cmd);
41 update_cache();
42 }
43 }
44
45 void add_ignored_command(command cmd) noexcept
46 {
47 hilet i = std::find(ignored.cbegin(), ignored.cend(), cmd);
48 if (i == ignored.cend()) {
49 ignored.push_back(cmd);
50 update_cache();
51 }
52 }
53
54 void add_user_command(command cmd) noexcept
55 {
56 hilet i = std::find(user.cbegin(), user.cend(), cmd);
57 if (i == user.cend()) {
58 user.push_back(cmd);
59 update_cache();
60 }
61 }
62
63 void update_cache() noexcept
64 {
65 cache.reserve(ssize(system) + ssize(user));
66
67 for (hilet cmd : system) {
68 hilet i = std::find(cache.cbegin(), cache.cend(), cmd);
69 if (i == cache.cend()) {
70 cache.push_back(cmd);
71 }
72 }
73
74 for (hilet cmd : ignored) {
75 hilet i = std::find(cache.cbegin(), cache.cend(), cmd);
76 if (i != cache.cend()) {
77 cache.erase(i);
78 }
79 }
80
81 for (hilet cmd : user) {
82 hilet i = std::find(cache.cbegin(), cache.cend(), cmd);
83 if (i == cache.cend()) {
84 cache.push_back(cmd);
85 }
86 }
87 }
88 };
89
93
94public:
95 keyboard_bindings() noexcept : bindings() {}
96
97 void add_system_binding(keyboard_key key, command command) noexcept
98 {
99 bindings[key].add_system_command(command);
100 }
101
102 void add_ignored_binding(keyboard_key key, command command) noexcept
103 {
104 bindings[key].add_ignored_command(command);
105 }
106
107 void add_user_binding(keyboard_key key, command command) noexcept
108 {
109 bindings[key].add_user_command(command);
110 }
111
114 [[nodiscard]] std::vector<command> const &translate(keyboard_key key) const noexcept
115 {
116 static std::vector<command> empty_commands = {};
117
118 hilet i = bindings.find(key);
119 if (i != bindings.cend()) {
120 return i->second.get_commands();
121 } else {
122 return empty_commands;
123 }
124 }
125
131 void clear() noexcept
132 {
133 bindings.clear();
134 }
135
138 void load_bindings(URL url, bool system_binding = false);
139
143 // void save_user_bindings(URL url);
144};
145
146} // namespace hi::inline v1
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
Definition keyboard_bindings.hpp:17
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:114
void clear() noexcept
Clear all bindings.
Definition keyboard_bindings.hpp:131
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)