HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
keyboard_bindings.hpp
1// Copyright Take Vos 2020-2022.
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 "gui_event.hpp"
9#include "../utility/module.hpp"
10#include <unordered_map>
11#include <tuple>
12#include <filesystem>
13
14namespace hi::inline v1 {
15
17 struct commands_t {
20
22 std::vector<gui_event_type> ignored = {};
23
26
28 std::vector<gui_event> cache = {};
29
30 [[nodiscard]] std::vector<gui_event> const& get_events() const noexcept
31 {
32 return cache;
33 }
34
35 void add_system_command(gui_event_type cmd) noexcept
36 {
37 hilet i = std::find(system.cbegin(), system.cend(), cmd);
38 if (i == system.cend()) {
39 system.push_back(cmd);
40 update_cache();
41 }
42 }
43
44 void add_ignored_command(gui_event_type cmd) noexcept
45 {
46 hilet i = std::find(ignored.cbegin(), ignored.cend(), cmd);
47 if (i == ignored.cend()) {
48 ignored.push_back(cmd);
49 update_cache();
50 }
51 }
52
53 void add_user_command(gui_event_type cmd) noexcept
54 {
55 hilet i = std::find(user.cbegin(), user.cend(), cmd);
56 if (i == user.cend()) {
57 user.push_back(cmd);
58 update_cache();
59 }
60 }
61
62 void update_cache() noexcept
63 {
64 cache.reserve(ssize(system) + ssize(user));
65
66 for (hilet cmd : system) {
67 hilet i = std::find(cache.cbegin(), cache.cend(), cmd);
68 if (i == cache.cend()) {
69 cache.emplace_back(cmd);
70 }
71 }
72
73 for (hilet cmd : ignored) {
74 hilet i = std::find(cache.cbegin(), cache.cend(), cmd);
75 if (i != cache.cend()) {
76 cache.erase(i);
77 }
78 }
79
80 for (hilet cmd : user) {
81 hilet i = std::find(cache.cbegin(), cache.cend(), cmd);
82 if (i == cache.cend()) {
83 cache.emplace_back(cmd);
84 }
85 }
86 }
87 };
88
92
93public:
94 keyboard_bindings() noexcept : bindings() {}
95
96 void add_system_binding(keyboard_key key, gui_event_type command) noexcept
97 {
98 bindings[key].add_system_command(command);
99 }
100
101 void add_ignored_binding(keyboard_key key, gui_event_type command) noexcept
102 {
103 bindings[key].add_ignored_command(command);
104 }
105
106 void add_user_binding(keyboard_key key, gui_event_type command) noexcept
107 {
108 bindings[key].add_user_command(command);
109 }
110
116 [[nodiscard]] void translate(gui_event event, std::vector<gui_event>& events) const noexcept
117 {
118 if (event == gui_event_type::keyboard_down) {
119 hilet i = bindings.find(keyboard_key{event.keyboard_modifiers, event.key()});
120 if (i != bindings.cend()) {
121 hilet& new_events = i->second.get_events();
122 events.insert(events.cend(), new_events.cbegin(), new_events.cend());
123 }
124 }
125 }
126
132 void clear() noexcept
133 {
134 bindings.clear();
135 }
136
139 void load_bindings(std::filesystem::path const &path, bool system_binding = false);
140
144 // void save_user_bindings(std::filesystem::path const &path);
145};
146
147} // namespace hi::inline v1
Definition of GUI event types.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
Definition keyboard_bindings.hpp:16
void translate(gui_event event, std::vector< gui_event > &events) const noexcept
translate a key press in the empty-context to a command.
Definition keyboard_bindings.hpp:116
void clear() noexcept
Clear all bindings.
Definition keyboard_bindings.hpp:132
void load_bindings(std::filesystem::path const &path, bool system_binding=false)
Load bindings from a JSON file.
A key in combination with modifiers.
Definition keyboard_key.hpp:19
T cbegin(T... args)
T clear(T... args)
T emplace_back(T... args)
T cend(T... args)
T erase(T... args)
T find(T... args)
T push_back(T... args)
T reserve(T... args)