Lagrange
Loading...
Searching...
No Matches
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 = {}) const;
105
106
113 void add(const std::string& action, const Keybind& keybind);
114
115
118 const MapType& get() const;
119
120
124 bool remove(const std::string& action);
125
126
130 bool unregister_action(const std::string& action);
131
132
136 bool register_action(const std::string& action);
137
138
142 bool is_pressed(const std::string& action) const;
143
147 inline bool is_pressed(ImGuiKey key_code) const { return ImGui::IsKeyPressed(key_code); }
148
152 bool is_down(const std::string& action) const;
153
157 inline bool is_down(ImGuiKey key_code) const { return ImGui::IsKeyDown(key_code); }
158
162 bool is_released(const std::string& action) const;
163
167 inline bool is_released(ImGuiKey key_code) const { return ImGui::IsKeyReleased(key_code); }
168
172 bool save(std::ostream& out) const;
173
174
179 bool load(std::istream& in, bool append = false);
180
186 void enable(bool enabled);
187
189 bool is_enabled() const;
190
191
193 static std::string to_string(const Keybind& keybind);
194
196 static std::string to_string(ImGuiKey key);
197
199 std::string to_string(const std::string& action, int limit = 1) const;
200
201private:
202 MapType m_mapping;
203 bool m_enabled = true;
204 std::vector<std::string> m_context_stack;
205
206 bool is_action_in_state(const std::string& action, KeyState state) const;
207};
208
209} // namespace ui
210} // namespace lagrange
std::map< std::string, std::vector< Keybind > > MapType
Internal map type.
Definition Keybinds.h:66
void add(const std::string &action, ImGuiKey button, const std::vector< ImGuiKey > &modifiers={})
Adds a key binding for given action.
Definition Keybinds.cpp:143
bool is_pressed(ImGuiKey key_code) const
Returns true if key was just pressed.
Definition Keybinds.h:147
bool is_released(ImGuiKey key_code) const
Returns true if key was just released.
Definition Keybinds.h:167
void update()
Updating key states.
Definition Keybinds.cpp:25
void pop_context()
Pops the last pushed context.
Definition Keybinds.cpp:133
bool is_down(ImGuiKey key_code) const
Returns true if key is held down.
Definition Keybinds.h:157
void push_context(const std::string &context)
Changes current context.
Definition Keybinds.cpp:128
Lagrange UI Viewer and mini 3D engine.
Definition AcceleratedPicking.h:23
Main namespace for Lagrange.