Lagrange
Keybinds.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/ui/api.h>
15#include <lagrange/ui/types/GLContext.h>
16
17#include <imgui.h>
18
19#include <array>
20#include <iostream>
21#include <map>
22#include <string>
23#include <vector>
24
25namespace lagrange {
26namespace ui {
27
35class LA_UI_API Keybinds
36{
37public:
38 enum class KeyState { NONE, PRESSED, DOWN, RELEASED };
39
40 Keybinds() {}
41
47 struct LA_UI_API Keybind
48 {
49 Keybind(ImGuiKey btn, const std::vector<ImGuiKey>& modifier_keys = {});
50 ImGuiKey button = ImGuiKey_None;
51 int modifier_count = 0;
52 KeyState previous_state = KeyState::NONE;
53 KeyState current_state = KeyState::NONE;
54 std::array<ImGuiKey, 6> modifiers = {
55 ImGuiKey_None,
56 ImGuiKey_None,
57 ImGuiKey_None,
58 ImGuiKey_None,
59 ImGuiKey_None,
60 ImGuiKey_None,
61 };
62 };
63
64
66 using MapType = std::map<std::string, std::vector<Keybind>>;
67
72 void update();
73
76 void push_context(const std::string& context);
77
80 void pop_context();
81
82 void reset_context();
83
84
92 void
93 add(const std::string& action, ImGuiKey button, const std::vector<ImGuiKey>& modifiers = {});
94
95
101 bool has(
102 const std::string& action,
103 ImGuiKey button,
104 const std::vector<ImGuiKey>& modifiers = {})
105 const;
106
107
114 void add(const std::string& action, const Keybind& keybind);
115
116
119 const MapType& get() const;
120
121
125 bool remove(const std::string& action);
126
127
131 bool unregister_action(const std::string& action);
132
133
137 bool register_action(const std::string& action);
138
139
143 bool is_pressed(const std::string& action) const;
144
148 inline bool is_pressed(ImGuiKey key_code) const { return ImGui::IsKeyPressed(key_code); }
149
153 bool is_down(const std::string& action) const;
154
158 inline bool is_down(ImGuiKey key_code) const { return ImGui::IsKeyDown(key_code); }
159
163 bool is_released(const std::string& action) const;
164
168 inline bool is_released(ImGuiKey key_code) const { return ImGui::IsKeyReleased(key_code); }
169
173 bool save(std::ostream& out) const;
174
175
180 bool load(std::istream& in, bool append = false);
181
187 void enable(bool enabled);
188
190 bool is_enabled() const;
191
192
194 static std::string to_string(const Keybind& keybind);
195
197 static std::string to_string(ImGuiKey key);
198
200 std::string to_string(const std::string& action, int limit = 1) const;
201
202private:
203 MapType m_mapping;
204 bool m_enabled = true;
205 std::vector<std::string> m_context_stack;
206
207 bool is_action_in_state(const std::string& action, KeyState state) const;
208};
209
210} // namespace ui
211} // namespace lagrange
Stores keybinds for actions.
Definition: Keybinds.h:36
bool is_pressed(ImGuiKey key_code) const
Returns true if key was just pressed.
Definition: Keybinds.h:148
bool is_released(ImGuiKey key_code) const
Returns true if key was just released.
Definition: Keybinds.h:168
std::map< std::string, std::vector< Keybind > > MapType
Internal map type.
Definition: Keybinds.h:66
bool is_down(ImGuiKey key_code) const
Returns true if key is held down.
Definition: Keybinds.h:158
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
LA_UI_API void remove(Registry &r, Entity e, bool recursive=false)
Removes entity.
Definition: treenode.cpp:47
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Key/mouse shortcut.
Definition: Keybinds.h:48