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 "gui_event.hpp"
9#include "../URL.hpp"
10#include "../architecture.hpp"
11#include "../subsystem.hpp"
12#include <unordered_map>
13#include <tuple>
14
15namespace hi::inline v1 {
16
18 struct commands_t {
21
23 std::vector<gui_event_type> ignored = {};
24
27
29 std::vector<gui_event> cache = {};
30
31 [[nodiscard]] std::vector<gui_event> const& get_events() const noexcept
32 {
33 return cache;
34 }
35
36 void add_system_command(gui_event_type 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(gui_event_type 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(gui_event_type 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.emplace_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.emplace_back(cmd);
85 }
86 }
87 }
88 };
89
93
94public:
95 keyboard_bindings() noexcept : bindings() {}
96
97 void add_system_binding(keyboard_key key, gui_event_type command) noexcept
98 {
99 bindings[key].add_system_command(command);
100 }
101
102 void add_ignored_binding(keyboard_key key, gui_event_type command) noexcept
103 {
104 bindings[key].add_ignored_command(command);
105 }
106
107 void add_user_binding(keyboard_key key, gui_event_type command) noexcept
108 {
109 bindings[key].add_user_command(command);
110 }
111
117 [[nodiscard]] void translate(gui_event event, std::vector<gui_event>& events) const noexcept
118 {
119 if (event == gui_event_type::keyboard_down) {
120 hilet i = bindings.find(keyboard_key{event.keyboard_modifiers, event.key()});
121 if (i != bindings.cend()) {
122 hilet& new_events = i->second.get_events();
123 events.insert(events.cend(), new_events.cbegin(), new_events.cend());
124 }
125 }
126 }
127
133 void clear() noexcept
134 {
135 bindings.clear();
136 }
137
140 void load_bindings(URL url, bool system_binding = false);
141
145 // void save_user_bindings(URL url);
146};
147
148} // 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...
A user interface event.
Definition gui_event.hpp:58
Definition keyboard_bindings.hpp:17
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:117
void load_bindings(URL url, bool system_binding=false)
Load bindings from a JSON file.
void clear() noexcept
Clear all bindings.
Definition keyboard_bindings.hpp:133
A key in combination with modifiers.
Definition keyboard_key.hpp:20
Definition URL.hpp:47
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)