Skip to content

Commit 4d77066

Browse files
committed
keybinds (doing this sucked)
1 parent 2b1d7ae commit 4d77066

11 files changed

Lines changed: 143 additions & 47 deletions

File tree

src/backend/settings/keybind.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,24 @@ ImGuiKey Keybind::transformKeyIdForLayout(ImGuiKey key) const {
2525
std::string Keybind::toString(bool convertForLayoutForceDisable) const {
2626
ImGuiKey keyConvert = convertForLayoutForceDisable ? key : transformKeyIdForLayout(key);
2727
std::string keyString;
28-
keyString += ImGui::GetKeyName((ImGuiKey)(keyConvert & ~ImGuiKey::ImGuiMod_Mask_));
28+
ImGuiKey keyNoMods = (ImGuiKey)(keyConvert & ~ImGuiKey::ImGuiMod_Mask_);
29+
if (keyNoMods >= ImGuiKey_NamedKey_BEGIN && keyNoMods < ImGuiKey_NamedKey_END)
30+
keyString += ImGui::GetKeyName(keyNoMods);
2931
if (keyConvert & ImGuiKey::ImGuiMod_Ctrl) {
3032
if (!keyString.empty()) keyString += " + ";
31-
keyString += ImGui::GetKeyName(ImGuiKey::ImGuiMod_Ctrl);
33+
keyString += "Ctrl";
3234
}
3335
if (keyConvert & ImGuiKey::ImGuiMod_Alt) {
3436
if (!keyString.empty()) keyString += " + ";
35-
keyString += ImGui::GetKeyName(ImGuiKey::ImGuiMod_Alt);
37+
keyString += "Alt";
3638
}
3739
if (keyConvert & ImGuiKey::ImGuiMod_Shift) {
3840
if (!keyString.empty()) keyString += " + ";
39-
keyString += ImGui::GetKeyName(ImGuiKey::ImGuiMod_Shift);
41+
keyString += "Shift";
4042
}
4143
if (keyConvert & ImGuiKey::ImGuiMod_Super) {
4244
if (!keyString.empty()) keyString += " + ";
43-
keyString += ImGui::GetKeyName(ImGuiKey::ImGuiMod_Super);
45+
keyString += "Super";
4446
}
4547
return keyString;
4648
}

src/backend/settings/keybind.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,33 @@
22
#define keybind_h
33

44
#include "imgui/imgui.h"
5+
#include "imgui/imgui_internal.h"
56

67
class Keybind {
78
public:
8-
Keybind() : key(ImGuiKey::ImGuiKey_NamedKey_END) {}
9-
Keybind(ImGuiKey key) : key(key) { }
10-
Keybind(unsigned int key) : Keybind((ImGuiKey)key) { }
9+
Keybind() {}
10+
Keybind(ImGuiKey key) : key(key) {
11+
if (ImGui::IsLRModKey(this->key)) {
12+
this->key = (ImGuiKey)(ImGui::FixupKeyChord((ImGuiKeyChord)this->key) & ImGuiKey::ImGuiMod_Mask_);
13+
}
14+
}
15+
Keybind(unsigned int key) : Keybind((ImGuiKey)key) {
16+
if (ImGui::IsLRModKey(this->key)) {
17+
this->key = (ImGuiKey)(ImGui::FixupKeyChord((ImGuiKeyChord)this->key) & ImGuiKey::ImGuiMod_Mask_);
18+
}
19+
}
1120
Keybind(std::string keyString) {
1221
if (Keybind::stringToKeyId.empty()) {
1322
for (int key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key++) {
1423
ImGuiKey imguiKey = (ImGuiKey)key;
15-
if (key == ImGuiKey_ReservedForModCtrl) imguiKey = ImGuiKey::ImGuiMod_Ctrl;
16-
else if (key == ImGuiKey_ReservedForModShift) imguiKey = ImGuiKey::ImGuiMod_Shift;
17-
else if (key == ImGuiKey_ReservedForModAlt) imguiKey = ImGuiKey::ImGuiMod_Alt;
18-
else if (key == ImGuiKey_ReservedForModSuper) imguiKey = ImGuiKey::ImGuiMod_Super;
19-
const char* name = ImGui::GetKeyName(imguiKey);
20-
if (name && name[0]) Keybind::stringToKeyId[name] = imguiKey;
24+
if (key == ImGuiKey_ReservedForModCtrl || key == ImGuiMod_Ctrl) Keybind::stringToKeyId["Ctrl"] = ImGuiKey::ImGuiMod_Ctrl;
25+
else if (key == ImGuiKey_ReservedForModShift || key == ImGuiMod_Shift) Keybind::stringToKeyId["Shift"] = ImGuiKey::ImGuiMod_Shift;
26+
else if (key == ImGuiKey_ReservedForModAlt || key == ImGuiMod_Alt) Keybind::stringToKeyId["Alt"] = ImGuiKey::ImGuiMod_Alt;
27+
else if (key == ImGuiKey_ReservedForModSuper || key == ImGuiMod_Super) Keybind::stringToKeyId["Super"] = ImGuiKey::ImGuiMod_Super;
28+
else {
29+
const char* name = ImGui::GetKeyName(imguiKey);
30+
if (name && name[0]) Keybind::stringToKeyId[name] = imguiKey;
31+
}
2132
}
2233
}
2334

@@ -34,6 +45,9 @@ class Keybind {
3445
bool operator!=(Keybind keybind) const { return keybind.getKeybind() != key; }
3546

3647
ImGuiKey getKeybind() const { return key; }
48+
bool containsMods() const {
49+
return (ImGuiKey::ImGuiMod_Ctrl & key) || (ImGuiKey::ImGuiMod_Shift & key) || (ImGuiKey::ImGuiMod_Alt & key) || (ImGuiKey::ImGuiMod_Super & key);
50+
}
3751
std::string toString() const { return toString(false); }
3852
std::string toString(bool convertForLayoutForceDisable) const;
3953

src/gui/helper/keybindHelpers.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ std::set<ImGuiKey> getPressedKeys(SDL_Window& sdlWindow) {
77
std::unique_lock lock(ImGuiRenderer::getImGuiRenderer(sdlWindow)->setActiveContext());
88
std::set<ImGuiKey> pressedKeys;
99
for (int key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key++) {
10+
if (
11+
key == ImGuiKey_ReservedForModCtrl ||
12+
key == ImGuiKey_ReservedForModShift ||
13+
key == ImGuiKey_ReservedForModAlt ||
14+
key == ImGuiKey_ReservedForModSuper ||
15+
ImGui::IsLRModKey((ImGuiKey)key)
16+
) continue;
1017
if (ImGui::IsKeyPressed((ImGuiKey)key)) pressedKeys.insert((ImGuiKey)key);
1118
}
1219
if (ImGui::IsKeyDown(ImGuiKey::ImGuiMod_Ctrl)) pressedKeys.insert(ImGuiKey::ImGuiMod_Ctrl);
@@ -16,36 +23,50 @@ std::set<ImGuiKey> getPressedKeys(SDL_Window& sdlWindow) {
1623
return pressedKeys;
1724
}
1825

26+
std::set<ImGuiKey> getHeldKeys(SDL_Window& sdlWindow) {
27+
std::unique_lock lock(ImGuiRenderer::getImGuiRenderer(sdlWindow)->setActiveContext());
28+
std::set<ImGuiKey> pressedKeys;
29+
for (int key = ImGuiKey_NamedKey_BEGIN; key < ImGuiKey_NamedKey_END; key++) {
30+
if (
31+
key == ImGuiKey_ReservedForModCtrl ||
32+
key == ImGuiKey_ReservedForModShift ||
33+
key == ImGuiKey_ReservedForModAlt ||
34+
key == ImGuiKey_ReservedForModSuper ||
35+
ImGui::IsLRModKey((ImGuiKey)key)
36+
) continue;
37+
if (ImGui::IsKeyDown((ImGuiKey)key)) {
38+
pressedKeys.insert((ImGuiKey)key);
39+
}
40+
}
41+
if (ImGui::IsKeyDown(ImGuiKey::ImGuiMod_Ctrl)) pressedKeys.insert(ImGuiKey::ImGuiMod_Ctrl);
42+
if (ImGui::IsKeyDown(ImGuiKey::ImGuiMod_Shift)) pressedKeys.insert(ImGuiKey::ImGuiMod_Shift);
43+
if (ImGui::IsKeyDown(ImGuiKey::ImGuiMod_Alt)) pressedKeys.insert(ImGuiKey::ImGuiMod_Alt);
44+
if (ImGui::IsKeyDown(ImGuiKey::ImGuiMod_Super)) pressedKeys.insert(ImGuiKey::ImGuiMod_Super);
45+
return pressedKeys;
46+
}
47+
1948
bool isPressingKeybind(const Keybind& keybinds, const std::set<ImGuiKey>& pressedKeys) {
2049
if (
2150
((ImGuiKey)(keybinds.getKeybind() & ~ImGuiKey::ImGuiMod_Mask_) != ImGuiKey::ImGuiKey_None) &&
2251
(!pressedKeys.contains((ImGuiKey)(keybinds.getKeybind() & ~ImGuiKey::ImGuiMod_Mask_)))
2352
) return false;
2453
if (((keybinds.getKeybind() & ImGuiKey::ImGuiMod_Ctrl) != 0) != (
25-
pressedKeys.contains(ImGuiKey::ImGuiMod_Ctrl)// ||
26-
// pressedKeys.contains(ImGuiKey::ImGuiKey_RightCtrl) ||
27-
// pressedKeys.contains(ImGuiKey::ImGuiKey_LeftCtrl)
54+
pressedKeys.contains(ImGuiKey::ImGuiMod_Ctrl)
2855
)) {
2956
return false;
3057
}
3158
if (((keybinds.getKeybind() & ImGuiKey::ImGuiMod_Alt) != 0) != (
32-
pressedKeys.contains(ImGuiKey::ImGuiMod_Alt)// ||
33-
// pressedKeys.contains(ImGuiKey::ImGuiKey_RightAlt) ||
34-
// pressedKeys.contains(ImGuiKey::ImGuiKey_LeftAlt)
59+
pressedKeys.contains(ImGuiKey::ImGuiMod_Alt)
3560
)) {
3661
return false;
3762
}
3863
if (((keybinds.getKeybind() & ImGuiKey::ImGuiMod_Shift) != 0) != (
39-
pressedKeys.contains(ImGuiKey::ImGuiMod_Shift)// ||
40-
// pressedKeys.contains(ImGuiKey::ImGuiKey_RightShift) ||
41-
// pressedKeys.contains(ImGuiKey::ImGuiKey_LeftShift)
64+
pressedKeys.contains(ImGuiKey::ImGuiMod_Shift)
4265
)) {
4366
return false;
4467
}
4568
if (((keybinds.getKeybind() & ImGuiKey::ImGuiMod_Super) != 0) != (
46-
pressedKeys.contains(ImGuiKey::ImGuiMod_Super)// ||
47-
// pressedKeys.contains(ImGuiKey::ImGuiKey_RightSuper) ||
48-
// pressedKeys.contains(ImGuiKey::ImGuiKey_LeftSuper)
69+
pressedKeys.contains(ImGuiKey::ImGuiMod_Super)
4970
)) {
5071
return false;
5172
}

src/gui/helper/keybindHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Keybind;
66
struct SDL_Window;
77

88
std::set<ImGuiKey> getPressedKeys(SDL_Window& sdlWindow);
9+
std::set<ImGuiKey> getHeldKeys(SDL_Window& sdlWindow);
910

1011
bool isPressingKeybind(const Keybind& keybinds, const std::set<ImGuiKey>& pressedKeys);
1112

src/gui/mainWindow/mainWindow.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,25 @@ void MainWindow::log(const std::string& message) { ImGui::InsertNotification({ I
4848

4949
void MainWindow::logError(const std::string& message) { ImGui::InsertNotification({ ImGuiToastType_Error, 3000, "%s", message.c_str() }); }
5050

51-
bool MainWindow::isPressingKeybind(const Keybind& keybind, bool repeat) const {
51+
bool MainWindow::isPressingKeybind(const Keybind& keybind) const {
5252
if (lastUpdatedFrame >= frameIndex.load() && (keybind.getKeybind() & (~ImGuiKey::ImGuiMod_Mask_)) != ImGuiKey::ImGuiKey_None) return false;
5353
return ::isPressingKeybind(keybind, pressedKeys);
5454
}
5555

56-
bool MainWindow::isPressingKeybind(const std::string& settingKey, bool repeat) const {
56+
bool MainWindow::isPressingKeybind(const std::string& settingKey) const {
5757
const Keybind* keybind = Settings::get<SettingType::KEYBIND>(settingKey);
5858
if (!keybind) return false;
59-
return isPressingKeybind(*keybind, repeat);
59+
return isPressingKeybind(*keybind);
60+
}
61+
62+
bool MainWindow::isHoldingKeybind(const Keybind& keybind) const {
63+
return ::isPressingKeybind(keybind, heldKeys);
64+
}
65+
66+
bool MainWindow::isHoldingKeybind(const std::string& settingKey) const {
67+
const Keybind* keybind = Settings::get<SettingType::KEYBIND>(settingKey);
68+
if (!keybind) return false;
69+
return isHoldingKeybind(*keybind);
6070
}
6171

6272
void MainWindow::createPopup(const std::string& message, const std::vector<std::pair<std::string, std::function<void()>>>& buttons) {
@@ -149,6 +159,7 @@ void MainWindow::doUpdate() {
149159
widgets.erase(widgetId);
150160
}
151161
pressedKeys = ::getPressedKeys(getHandle());
162+
heldKeys = ::getHeldKeys(getHandle());
152163
if (isPressingKeybind("Keybinds/File/Open")) {
153164
loadDialog();
154165
}

src/gui/mainWindow/mainWindow.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ class MainWindow : public SdlWindow {
7070
logError(message.str());
7171
}
7272

73-
bool isPressingKeybind(const Keybind& keybind, bool repeat = false) const;
74-
bool isPressingKeybind(const std::string& settingKey, bool repeat = false) const;
73+
bool isPressingKeybind(const Keybind& keybind) const;
74+
bool isPressingKeybind(const std::string& settingKey) const;
75+
bool isHoldingKeybind(const Keybind& keybind) const;
76+
bool isHoldingKeybind(const std::string& settingKey) const;
7577
const std::set<ImGuiKey>& getPressedKeys() const { return pressedKeys; }
78+
const std::set<ImGuiKey>& getHeldKeys() const { return heldKeys; }
7679

7780
ImGuiID getDockMainId() const { return dockMainId; }
7881
ImGuiID getDockLeftId() const { return dockLeftId; }
@@ -105,6 +108,7 @@ class MainWindow : public SdlWindow {
105108
Environment environment;
106109

107110
std::set<ImGuiKey> pressedKeys;
111+
std::set<ImGuiKey> heldKeys;
108112
std::atomic<unsigned long long> frameIndex;
109113
unsigned long long lastUpdatedFrame;
110114

src/gui/mainWindow/widgets/blockCreationWidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ BlockCreationWidget::BlockCreationWidget(WidgetId widgetId, MainWindow& mainWind
2424
setupGUIValue<bool>("MouseLeftDown", false, [&](const bool& state) {
2525
if (state) {
2626
if (!getGUIValue<bool>("MouseInView")) return;
27-
if (getMainWindow().isPressingKeybind("Keybinds/Camera/Pan", true)) {
27+
if (getMainWindow().isHoldingKeybind("Keybinds/Camera/Pan")) {
2828
if (circuitView->getEventRegister().doEvent(PositionEvent("View Attach Anchor", circuitView->getViewManager().getPointerPosition()))) {
2929
return;
3030
}
@@ -215,7 +215,7 @@ void BlockCreationWidget::processEvent(SDL_Event& event) {
215215
Vec2 movement(-event.wheel.x * getMainWindow().getWindowScalingSize(), event.wheel.y * getMainWindow().getWindowScalingSize());
216216
if (!Settings::get<SettingType::BOOL>("Keybinds/Camera/Scroll Panning", false)) {
217217
circuitView->getEventRegister().doEvent(DeltaEvent("view zoom", (float)(movement.y) / 15.f));
218-
} else if (getMainWindow().isPressingKeybind("Keybinds/Camera/Zoom", true)) {
218+
} else if (getMainWindow().isHoldingKeybind("Keybinds/Camera/Zoom")) {
219219
// do zoom
220220
circuitView->getEventRegister().doEvent(DeltaEvent("view zoom", (float)(movement.y) / 15.f));
221221
} else {

src/gui/mainWindow/widgets/circuitTestWidget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ CircuitTestWidget::CircuitTestWidget(WidgetId widgetId, MainWindow& mainWindow)
2727
setupGUIValue<bool>("MouseLeftDown", false, [&](const bool& state) { //sets up a value for use with getGUIValue
2828
if (state) {
2929
if (!getGUIValue<bool>("MouseInView")) return;
30-
if (getMainWindow().isPressingKeybind("Keybinds/Camera/Pan", true)) {
30+
if (getMainWindow().isHoldingKeybind("Keybinds/Camera/Pan")) {
3131
if (circuitView->getEventRegister().doEvent(PositionEvent("View Attach Anchor", circuitView->getViewManager().getPointerPosition()))) {
3232
return;
3333
}
@@ -212,7 +212,7 @@ void CircuitTestWidget::processEvent(SDL_Event& event) {
212212
Vec2 movement(-event.wheel.x * getMainWindow().getWindowScalingSize(), event.wheel.y * getMainWindow().getWindowScalingSize());
213213
if (!Settings::get<SettingType::BOOL>("Keybinds/Camera/Scroll Panning", false)) {
214214
circuitView->getEventRegister().doEvent(DeltaEvent("view zoom", (float)(movement.y) / 15.f));
215-
} else if (getMainWindow().isPressingKeybind("Keybinds/Camera/Zoom", true)) {
215+
} else if (getMainWindow().isHoldingKeybind("Keybinds/Camera/Zoom")) {
216216
// do zoom
217217
circuitView->getEventRegister().doEvent(DeltaEvent("view zoom", (float)(movement.y) / 15.f));
218218
} else {
@@ -626,7 +626,7 @@ void CircuitTestWidget::update() {
626626
circuitView->getSimulator()->resetStates();
627627
}
628628
}
629-
if (getMainWindow().isPressingKeybind("Keybinds/Simulation/Transverse Simulation")) {
629+
if (getMainWindow().isHoldingKeybind("Keybinds/Simulation/Transverse Simulation")) {
630630
if (dynamic_cast<const TreeTraversal*>(circuitView->getToolManager().getCircuitTool()) == nullptr) {
631631
lastToolStack = circuitView->getToolManager().getStack();
632632
circuitView->getToolManager().selectTool(std::make_shared<TreeTraversal>(getEnvironment()));

src/gui/mainWindow/widgets/circuitViewWidget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ CircuitViewWidget::CircuitViewWidget(WidgetId widgetId, MainWindow& mainWindow)
1919
setupGUIValue<bool>("MouseLeftDown", false, [&](const bool& state) {
2020
if (state) {
2121
if (!getGUIValue<bool>("MouseInView")) return;
22-
if (getMainWindow().isPressingKeybind("Keybinds/Editing/Pick Block", true)) {
22+
if (getMainWindow().isHoldingKeybind("Keybinds/Editing/Pick Block")) {
2323
std::unique_ptr<CircuitView>& circuitView = this->circuitView;
2424
Circuit* circuit = circuitView->getCircuit();
2525
if (circuit) {
@@ -38,7 +38,7 @@ CircuitViewWidget::CircuitViewWidget(WidgetId widgetId, MainWindow& mainWindow)
3838
}
3939
}
4040
}
41-
if (getMainWindow().isPressingKeybind("Keybinds/Camera/Pan", true)) {
41+
if (getMainWindow().isHoldingKeybind("Keybinds/Camera/Pan")) {
4242
if (circuitView->getEventRegister().doEvent(PositionEvent("View Attach Anchor", circuitView->getViewManager().getPointerPosition()))) {
4343
return;
4444
}
@@ -128,7 +128,7 @@ void CircuitViewWidget::processEvent(SDL_Event& event) {
128128
Vec2 movement(-event.wheel.x * getMainWindow().getWindowScalingSize(), event.wheel.y * getMainWindow().getWindowScalingSize());
129129
if (!Settings::get<SettingType::BOOL>("Keybinds/Camera/Scroll Panning", false)) {
130130
circuitView->getEventRegister().doEvent(DeltaEvent("view zoom", (float)(movement.y) / 15.f));
131-
} else if (getMainWindow().isPressingKeybind("Keybinds/Camera/Zoom", true)) {
131+
} else if (getMainWindow().isHoldingKeybind("Keybinds/Camera/Zoom")) {
132132
// do zoom
133133
circuitView->getEventRegister().doEvent(DeltaEvent("view zoom", (float)(movement.y) / 15.f));
134134
} else {
@@ -461,7 +461,7 @@ void CircuitViewWidget::update() {
461461
if (getMainWindow().isPressingKeybind("Keybinds/Tutorial/DebugForceCompleteStep")) {
462462
circuitView->getTutorialManager().forceCompleteStep();
463463
}
464-
if (getMainWindow().isPressingKeybind("Keybinds/Simulation/Transverse Simulation")) {
464+
if (getMainWindow().isHoldingKeybind("Keybinds/Simulation/Transverse Simulation")) {
465465
if (dynamic_cast<const TreeTraversal*>(circuitView->getToolManager().getCircuitTool()) == nullptr) {
466466
lastToolStack = circuitView->getToolManager().getStack();
467467
circuitView->getToolManager().selectTool(std::make_shared<TreeTraversal>(getEnvironment()));

0 commit comments

Comments
 (0)