mirror of https://github.com/WerWolv/ImHex.git
sys: Improve shortcut api
This commit is contained in:
parent
59dd372ec8
commit
771bb22962
|
@ -24,7 +24,7 @@ namespace hex {
|
||||||
bool isAvailable() override { return true; }
|
bool isAvailable() override { return true; }
|
||||||
bool shouldProcess() override { return true; }
|
bool shouldProcess() override { return true; }
|
||||||
|
|
||||||
bool handleShortcut(int key, int mods) override;
|
bool handleShortcut(bool keys[512], bool ctrl, bool shift, bool alt) override;
|
||||||
|
|
||||||
bool hasViewMenuItemEntry() override { return false; }
|
bool hasViewMenuItemEntry() override { return false; }
|
||||||
ImVec2 getMinSize() override { return ImVec2(400, 100); }
|
ImVec2 getMinSize() override { return ImVec2(400, 100); }
|
||||||
|
|
|
@ -27,7 +27,7 @@ namespace hex {
|
||||||
void drawContent() override;
|
void drawContent() override;
|
||||||
void drawAlwaysVisible() override;
|
void drawAlwaysVisible() override;
|
||||||
void drawMenu() override;
|
void drawMenu() override;
|
||||||
bool handleShortcut(int key, int mods) override;
|
bool handleShortcut(bool keys[512], bool ctrl, bool shift, bool alt) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MemoryEditor m_memoryEditor;
|
MemoryEditor m_memoryEditor;
|
||||||
|
|
|
@ -52,6 +52,8 @@ namespace hex {
|
||||||
|
|
||||||
double m_lastFrameTime;
|
double m_lastFrameTime;
|
||||||
|
|
||||||
|
bool m_prevKeysDown[512];
|
||||||
|
|
||||||
static inline std::tuple<int, int> s_currShortcut = { -1, -1 };
|
static inline std::tuple<int, int> s_currShortcut = { -1, -1 };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ namespace hex {
|
||||||
virtual void drawContent() = 0;
|
virtual void drawContent() = 0;
|
||||||
virtual void drawAlwaysVisible() { }
|
virtual void drawAlwaysVisible() { }
|
||||||
virtual void drawMenu();
|
virtual void drawMenu();
|
||||||
virtual bool handleShortcut(int key, int mods);
|
virtual bool handleShortcut(bool keys[512], bool ctrl, bool shift, bool alt);
|
||||||
virtual bool isAvailable();
|
virtual bool isAvailable();
|
||||||
virtual bool shouldProcess() { return this->isAvailable() && this->getWindowOpenState(); }
|
virtual bool shouldProcess() { return this->isAvailable() && this->getWindowOpenState(); }
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace hex {
|
||||||
View::View(std::string unlocalizedName) : m_unlocalizedViewName(unlocalizedName) { }
|
View::View(std::string unlocalizedName) : m_unlocalizedViewName(unlocalizedName) { }
|
||||||
|
|
||||||
void View::drawMenu() { }
|
void View::drawMenu() { }
|
||||||
bool View::handleShortcut(int key, int mods) { return false; }
|
bool View::handleShortcut(bool keys[512], bool ctrl, bool shift, bool alt) { return false; }
|
||||||
|
|
||||||
bool View::isAvailable() {
|
bool View::isAvailable() {
|
||||||
return SharedData::currentProvider != nullptr && SharedData::currentProvider->isAvailable();
|
return SharedData::currentProvider != nullptr && SharedData::currentProvider->isAvailable();
|
||||||
|
|
|
@ -67,8 +67,8 @@ namespace hex {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ViewCommandPalette::handleShortcut(int key, int mods) {
|
bool ViewCommandPalette::handleShortcut(bool keys[512], bool ctrl, bool shift, bool alt) {
|
||||||
if (key == 'P' && mods == (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL)) {
|
if (ctrl && shift && keys['P']) {
|
||||||
View::doLater([this] {
|
View::doLater([this] {
|
||||||
ImGui::OpenPopup("hex.view.command_palette.name"_lang);
|
ImGui::OpenPopup("hex.view.command_palette.name"_lang);
|
||||||
this->m_commandPaletteOpen = true;
|
this->m_commandPaletteOpen = true;
|
||||||
|
|
|
@ -533,38 +533,49 @@ namespace hex {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ViewHexEditor::handleShortcut(int key, int mods) {
|
bool ViewHexEditor::handleShortcut(bool keys[512], bool ctrl, bool shift, bool alt) {
|
||||||
if (mods == GLFW_MOD_CONTROL && key == 'S') {
|
if (ctrl && keys['S']) {
|
||||||
save();
|
save();
|
||||||
return true;
|
return true;
|
||||||
} else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT) && key == 'S') {
|
} else if (ctrl && shift && keys['S']) {
|
||||||
saveAs();
|
saveAs();
|
||||||
return true;
|
return true;
|
||||||
} else if (mods == GLFW_MOD_CONTROL && key == 'Z') {
|
}
|
||||||
|
|
||||||
|
if (ImGui::Begin(View::toWindowName("hex.view.hexeditor.name").c_str())) {
|
||||||
|
ON_SCOPE_EXIT { ImGui::End(); };
|
||||||
|
|
||||||
|
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows)) {
|
||||||
|
if (ctrl && keys['Z']) {
|
||||||
if (SharedData::currentProvider != nullptr)
|
if (SharedData::currentProvider != nullptr)
|
||||||
SharedData::currentProvider->undo();
|
SharedData::currentProvider->undo();
|
||||||
} else if (mods == GLFW_MOD_CONTROL && key == 'Y') {
|
return true;
|
||||||
|
} else if (ctrl && keys['Y']) {
|
||||||
if (SharedData::currentProvider != nullptr)
|
if (SharedData::currentProvider != nullptr)
|
||||||
SharedData::currentProvider->redo();
|
SharedData::currentProvider->redo();
|
||||||
} else if (mods == GLFW_MOD_CONTROL && key == 'F') {
|
} else if (ctrl && keys['F']) {
|
||||||
View::doLater([]{ ImGui::OpenPopup("hex.view.hexeditor.menu.file.search"_lang); });
|
View::doLater([]{ ImGui::OpenPopup("hex.view.hexeditor.menu.file.search"_lang); });
|
||||||
return true;
|
return true;
|
||||||
} else if (mods == GLFW_MOD_CONTROL && key == 'G') {
|
} else if (ctrl && keys['G']) {
|
||||||
View::doLater([]{ ImGui::OpenPopup("hex.view.hexeditor.menu.file.goto"_lang); });
|
View::doLater([]{ ImGui::OpenPopup("hex.view.hexeditor.menu.file.goto"_lang); });
|
||||||
return true;
|
return true;
|
||||||
} else if (mods == GLFW_MOD_CONTROL && key == 'O') {
|
} else if (ctrl && keys['O']) {
|
||||||
View::doLater([]{ ImGui::OpenPopup("hex.view.hexeditor.open_file"_lang); });
|
View::doLater([]{ ImGui::OpenPopup("hex.view.hexeditor.open_file"_lang); });
|
||||||
return true;
|
return true;
|
||||||
} else if (mods == GLFW_MOD_CONTROL && key == 'C') {
|
} else if (ctrl && keys['C']) {
|
||||||
this->copyBytes();
|
this->copyBytes();
|
||||||
return true;
|
return true;
|
||||||
} else if (mods == (GLFW_MOD_CONTROL | GLFW_MOD_SHIFT) && key == 'C') {
|
} else if (ctrl && shift && keys['C']) {
|
||||||
this->copyString();
|
this->copyString();
|
||||||
return true;
|
return true;
|
||||||
} else if (mods == GLFW_MOD_CONTROL && key == 'V') {
|
} else if (ctrl && keys['V']) {
|
||||||
this->pasteBytes();
|
this->pasteBytes();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -181,10 +181,17 @@ namespace hex {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::loop() {
|
void Window::loop() {
|
||||||
|
bool pressedKeys[512] = { 0 };
|
||||||
|
|
||||||
this->m_lastFrameTime = glfwGetTime();
|
this->m_lastFrameTime = glfwGetTime();
|
||||||
while (!glfwWindowShouldClose(this->m_window)) {
|
while (!glfwWindowShouldClose(this->m_window)) {
|
||||||
|
std::copy_n(ImGui::GetIO().KeysDown, 512, this->m_prevKeysDown);
|
||||||
|
|
||||||
this->frameBegin();
|
this->frameBegin();
|
||||||
|
|
||||||
|
for (u16 i = 0; i < 512; i++)
|
||||||
|
pressedKeys[i] = ImGui::GetIO().KeysDown[i] && !this->m_prevKeysDown[i];
|
||||||
|
|
||||||
for (const auto &call : View::getDeferedCalls())
|
for (const auto &call : View::getDeferedCalls())
|
||||||
call();
|
call();
|
||||||
View::getDeferedCalls().clear();
|
View::getDeferedCalls().clear();
|
||||||
|
@ -201,6 +208,7 @@ namespace hex {
|
||||||
|
|
||||||
ImGui::SetNextWindowSizeConstraints(minSize, view->getMaxSize());
|
ImGui::SetNextWindowSizeConstraints(minSize, view->getMaxSize());
|
||||||
view->drawContent();
|
view->drawContent();
|
||||||
|
view->handleShortcut(pressedKeys, ImGui::GetIO().KeyCtrl, ImGui::GetIO().KeyShift, ImGui::GetIO().KeyAlt);
|
||||||
}
|
}
|
||||||
|
|
||||||
View::drawCommonInterfaces();
|
View::drawCommonInterfaces();
|
||||||
|
@ -352,17 +360,6 @@ namespace hex {
|
||||||
ImGui::EndMenuBar();
|
ImGui::EndMenuBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto &[key, mods] = Window::s_currShortcut; key != -1) {
|
|
||||||
for (auto &view : ContentRegistry::Views::getEntries()) {
|
|
||||||
if (view->shouldProcess()) {
|
|
||||||
if (view->handleShortcut(key, mods))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Window::s_currShortcut = { -1, -1 };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SharedData::currentProvider == nullptr) {
|
if (SharedData::currentProvider == nullptr) {
|
||||||
char title[256];
|
char title[256];
|
||||||
ImFormatString(title, IM_ARRAYSIZE(title), "%s/DockSpace_%08X", ImGui::GetCurrentWindow()->Name, ImGui::GetID("MainDock"));
|
ImFormatString(title, IM_ARRAYSIZE(title), "%s/DockSpace_%08X", ImGui::GetCurrentWindow()->Name, ImGui::GetID("MainDock"));
|
||||||
|
|
Loading…
Reference in New Issue