FuncDoodle
Loading...
Searching...
No Matches
Keybinds.h
Go to the documentation of this file.
1
21
22#pragma once
23
24#include <vector>
25
26#include <cstring>
27#include <filesystem>
28#include <imgui.h>
29#include <map>
30#include <optional>
31
32namespace FuncDoodle {
37 constexpr int KEY_MASK_SIZE = (ImGuiKey_NamedKey_END + 63) / 64;
38
51 class KeyMask {
52 public:
56 KeyMask();
62 KeyMask(ImGuiKey key);
68 KeyMask(const KeyMask& other) = default;
69
76 [[nodiscard]] bool IsPressed() const;
82 operator char*() const;
83
88 void Reset() {
89 for (unsigned long long& m_Key : m_Keys) {
90 m_Key = 0;
91 }
92 }
93
100 [[nodiscard]] std::vector<ImGuiKey> All() const {
101 std::vector<ImGuiKey> keys;
102 for (int i = 0; i < KEY_MASK_SIZE; i++) {
103 if (m_Keys[i] == 0)
104 continue;
105 for (int j = 0; j < 64; j++) {
106 if (m_Keys[i] & (1ull << j)) {
107 auto key = (ImGuiKey)((i * 64) + j);
108 keys.push_back(key);
109 }
110 }
111 }
112 return keys;
113 }
114
121 KeyMask operator|(const KeyMask& other) const;
128 bool operator==(const KeyMask& other) const {
129 for (int i = 0; i < KEY_MASK_SIZE; i++) {
130 if (m_Keys[i] != other.m_Keys[i])
131 return false;
132 }
133 return true;
134 }
135
136 private:
137 unsigned long long m_Keys[KEY_MASK_SIZE];
138 };
139
149 struct Shortcut {
153 Shortcut();
159 Shortcut(const char* str);
168 Shortcut(bool requiresCtrl, bool requiresShift, bool requiresSuper,
169 KeyMask key);
170
191
197 operator char*() const;
204 [[nodiscard]] bool IsPressed() const;
211 bool operator==(const Shortcut& other) const {
212 return RequiresCtrl == other.RequiresCtrl &&
213 RequiresShift == other.RequiresShift &&
214 RequiresSuper == other.RequiresSuper && Key == other.Key;
215 }
216 };
217
238 std::optional<Shortcut> User;
239 };
240
251 public:
259 KeybindsRegistry(std::filesystem::path rootPath);
268 Shortcut Get(const char* id);
276 void Register(const char* id, Shortcut shortcut);
281 void End();
286 void Write();
293 std::vector<std::pair<const char*, ShortcutWithUser>>& GetAll();
294
295 private:
296 std::vector<std::pair<const char*, ShortcutWithUser>> m_Reg;
297 std::filesystem::path m_RootPath;
298 };
299} // namespace FuncDoodle
Bitmask representing a set of ImGui keys.
Definition Keybinds.h:51
bool operator==(const KeyMask &other) const
Returns whether two key masks contain the same keys.
Definition Keybinds.h:128
KeyMask()
Creates an empty key mask.
Definition Keybinds.cc:15
std::vector< ImGuiKey > All() const
Returns all keys currently stored in the mask.
Definition Keybinds.h:100
KeyMask operator|(const KeyMask &other) const
Returns a key mask containing keys from both operands.
Definition Keybinds.cc:76
bool IsPressed() const
Returns whether every key in the mask is currently pressed.
Definition Keybinds.cc:31
KeyMask(const KeyMask &other)=default
Copies another key mask.
void Reset()
Clears all keys from the mask.
Definition Keybinds.h:88
unsigned long long m_Keys[KEY_MASK_SIZE]
Definition Keybinds.h:137
KeybindsRegistry(std::filesystem::path rootPath)
Creates a keybind registry rooted at a configuration directory.
Definition Keybinds.cc:185
~KeybindsRegistry()
Definition Keybinds.cc:187
void End()
Finalizes registration and loads any user overrides.
Definition Keybinds.cc:205
Shortcut Get(const char *id)
Returns the effective shortcut registered for an identifier.
Definition Keybinds.cc:191
std::filesystem::path m_RootPath
Definition Keybinds.h:297
void Register(const char *id, Shortcut shortcut)
Registers a default shortcut for an identifier.
Definition Keybinds.cc:200
std::vector< std::pair< const char *, ShortcutWithUser > > m_Reg
Definition Keybinds.h:296
std::vector< std::pair< const char *, ShortcutWithUser > > & GetAll()
Returns all registered keybind entries.
Definition Keybinds.cc:256
void Write()
Persists user keybind overrides to disk.
Definition Keybinds.cc:234
The FuncDoodle C++ namespace.
Definition Common.h:12
constexpr int KEY_MASK_SIZE
Number of 64-bit slots required to store all named ImGui keys.
Definition Keybinds.h:37
Stores a default shortcut and an optional user-defined one.
Definition Keybinds.h:228
Shortcut Default
Built-in shortcut shipped by the application.
Definition Keybinds.h:233
std::optional< Shortcut > User
Optional user override loaded from configuration.
Definition Keybinds.h:238
Keyboard shortcut definition (key + mods).
Definition Keybinds.h:149
bool RequiresSuper
Whether Super/Meta must be held.
Definition Keybinds.h:185
Shortcut()
Creates an empty shortcut.
Definition Keybinds.cc:84
bool operator==(const Shortcut &other) const
Returns whether two shortcuts are identical.
Definition Keybinds.h:211
KeyMask Key
Primary key mask for the shortcut.
Definition Keybinds.h:190
bool RequiresShift
Whether Shift must be held.
Definition Keybinds.h:180
bool IsPressed() const
Returns whether the full shortcut is active.
Definition Keybinds.cc:178
bool RequiresCtrl
Whether Ctrl must be held.
Definition Keybinds.h:175