1
0
mirror of synced 2024-11-13 18:50:53 +01:00

Implemented Open File and Preferences button in Welcome screen

This commit is contained in:
WerWolv 2021-01-27 14:26:24 +01:00
parent bc6d33e4fb
commit 319068eef5
13 changed files with 66 additions and 33 deletions

View File

@ -36,7 +36,7 @@ namespace ImGui {
const ImU32 col = hovered ? GetColorU32(ImGuiCol_ButtonHovered) : GetColorU32(ImGuiCol_ButtonActive); const ImU32 col = hovered ? GetColorU32(ImGuiCol_ButtonHovered) : GetColorU32(ImGuiCol_ButtonActive);
PushStyleColor(ImGuiCol_Text, ImU32(col)); PushStyleColor(ImGuiCol_Text, ImU32(col));
TextEx(label, NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting TextEx(label, NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting
GetOverlayDrawList()->AddLine(ImVec2(pos.x, pos.y + size.y), pos + size, ImU32(col)); GetWindowDrawList()->AddLine(ImVec2(pos.x, pos.y + size.y), pos + size, ImU32(col));
PopStyleColor(); PopStyleColor();
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags); IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags);
@ -70,7 +70,7 @@ namespace ImGui {
PushStyleColor(ImGuiCol_Text, ImU32(col)); PushStyleColor(ImGuiCol_Text, ImU32(col));
RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize * 0.5f, g.FontSize * 0.5f), col); RenderBullet(window->DrawList, bb.Min + ImVec2(style.FramePadding.x + g.FontSize * 0.5f, g.FontSize * 0.5f), col);
RenderText(bb.Min + ImVec2(g.FontSize + style.FramePadding.x * 2, 0.0f), label, nullptr, false); RenderText(bb.Min + ImVec2(g.FontSize + style.FramePadding.x * 2, 0.0f), label, nullptr, false);
GetOverlayDrawList()->AddLine(bb.Min + ImVec2(style.FramePadding.x, size.y), pos + size + ImVec2(g.FontSize * 2, 0), ImU32(col)); GetWindowDrawList()->AddLine(bb.Min + ImVec2(style.FramePadding.x, size.y), pos + size + ImVec2(g.FontSize * 2, 0), ImU32(col));
ImGui::NewLine(); ImGui::NewLine();
PopStyleColor(); PopStyleColor();
@ -137,7 +137,7 @@ namespace ImGui {
PushStyleColor(ImGuiCol_Text, ImU32(color)); PushStyleColor(ImGuiCol_Text, ImU32(color));
TextEx(label, NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting TextEx(label, NULL, ImGuiTextFlags_NoWidthForLargeClippedText); // Skip formatting
GetOverlayDrawList()->AddLine(ImVec2(pos.x, pos.y + size.y), pos + size, ImU32(color)); GetWindowDrawList()->AddLine(ImVec2(pos.x, pos.y + size.y), pos + size, ImU32(color));
PopStyleColor(); PopStyleColor();
} }

View File

@ -21,6 +21,7 @@ namespace hex {
void drawContent() override; void drawContent() override;
void drawMenu() override; void drawMenu() override;
bool isAvailable() override { return true; }
bool handleShortcut(int key, int mods) override; bool handleShortcut(int key, int mods) override;

View File

@ -21,6 +21,7 @@ namespace hex {
void drawContent() override; void drawContent() override;
void drawMenu() override; void drawMenu() override;
bool isAvailable() override { return true; }
bool hasViewMenuItemEntry() override { return false; } bool hasViewMenuItemEntry() override { return false; }

View File

@ -14,10 +14,9 @@ namespace hex {
void drawContent() override; void drawContent() override;
void drawMenu() override; void drawMenu() override;
bool isAvailable() override { return true; }
bool hasViewMenuItemEntry() override { return false; } bool hasViewMenuItemEntry() override { return false; }
private:
bool m_settingsWindowOpen = false;
}; };
} }

View File

@ -39,7 +39,7 @@ namespace hex {
void deinitGLFW(); void deinitGLFW();
void deinitImGui(); void deinitImGui();
GLFWwindow* m_window; GLFWwindow* m_window = nullptr;
float m_globalScale = 1.0f, m_fontScale = 1.0f; float m_globalScale = 1.0f, m_fontScale = 1.0f;
bool m_fpsVisible = false; bool m_fpsVisible = false;

View File

@ -26,6 +26,7 @@ namespace hex {
SettingsChanged, SettingsChanged,
OpenWindow,
/* This is not a real event but a flag to show all events after this one are plugin ones */ /* This is not a real event but a flag to show all events after this one are plugin ones */
Events_BuiltinEnd Events_BuiltinEnd

View File

@ -9,7 +9,6 @@
#include <hex/api/content_registry.hpp> #include <hex/api/content_registry.hpp>
#include <hex/api/imhex_api.hpp> #include <hex/api/imhex_api.hpp>
#include <hex/api/event.hpp> #include <hex/api/event.hpp>
#include <hex/views/view.hpp>
#include <imgui.h> #include <imgui.h>
#include <ImGuiFileBrowser.h> #include <ImGuiFileBrowser.h>
@ -25,6 +24,7 @@ namespace hex::plugin::internal {
namespace hex { namespace hex {
namespace prv { class Provider; } namespace prv { class Provider; }
class View;
class SharedData { class SharedData {
SharedData() = default; SharedData() = default;

View File

@ -6,6 +6,8 @@
#include <ImGuiFileBrowser.h> #include <ImGuiFileBrowser.h>
#include <hex/api/event.hpp> #include <hex/api/event.hpp>
#include <hex/helpers/shared_data.hpp>
#include <hex/providers/provider.hpp>
#include <functional> #include <functional>
#include <string> #include <string>
@ -22,6 +24,7 @@ namespace hex {
virtual void drawContent() = 0; virtual void drawContent() = 0;
virtual void drawMenu(); virtual void drawMenu();
virtual bool handleShortcut(int key, int mods); virtual bool handleShortcut(int key, int mods);
virtual bool isAvailable() { return SharedData::currentProvider != nullptr && SharedData::currentProvider->isAvailable(); }
static void openFileBrowser(std::string title, imgui_addons::ImGuiFileBrowser::DialogMode mode, std::string validExtensions, const std::function<void(std::string)> &callback); static void openFileBrowser(std::string title, imgui_addons::ImGuiFileBrowser::DialogMode mode, std::string validExtensions, const std::function<void(std::string)> &callback);
static void doLater(std::function<void()> &&function); static void doLater(std::function<void()> &&function);

View File

@ -5,8 +5,6 @@
namespace hex { namespace hex {
ViewCommandPalette::ViewCommandPalette() : View("Command Palette") { ViewCommandPalette::ViewCommandPalette() : View("Command Palette") {
this->getWindowOpenState() = true;
this->m_commandBuffer.resize(1024, 0x00); this->m_commandBuffer.resize(1024, 0x00);
this->m_lastResults = this->getCommandResults(""); this->m_lastResults = this->getCommandResults("");
} }
@ -17,6 +15,8 @@ namespace hex {
void ViewCommandPalette::drawContent() { void ViewCommandPalette::drawContent() {
if (!this->getWindowOpenState()) return;
auto windowPos = SharedData::windowPos; auto windowPos = SharedData::windowPos;
auto windowSize = SharedData::windowSize; auto windowSize = SharedData::windowSize;
auto paletteSize = this->getMinSize(); auto paletteSize = this->getMinSize();
@ -49,7 +49,10 @@ namespace hex {
} }
ImGui::EndPopup(); ImGui::EndPopup();
} else {
this->getWindowOpenState() = false;
} }
} }
void ViewCommandPalette::drawMenu() { void ViewCommandPalette::drawMenu() {
@ -61,6 +64,7 @@ namespace hex {
View::doLater([this] { View::doLater([this] {
this->m_justOpened = true; this->m_justOpened = true;
ImGui::OpenPopup("Command Palette"); ImGui::OpenPopup("Command Palette");
this->getWindowOpenState() = true;
}); });
return true; return true;
} }

View File

@ -5,7 +5,6 @@
namespace hex { namespace hex {
ViewHelp::ViewHelp() : View("Help") { ViewHelp::ViewHelp() : View("Help") {
this->getWindowOpenState() = true;
} }
ViewHelp::~ViewHelp() { ViewHelp::~ViewHelp() {
@ -303,6 +302,9 @@ namespace hex {
} }
void ViewHelp::drawContent() { void ViewHelp::drawContent() {
if (!this->m_aboutWindowOpen && !this->m_mathHelpWindowOpen && !this->m_patternHelpWindowOpen)
this->getWindowOpenState() = false;
this->drawAboutPopup(); this->drawAboutPopup();
this->drawPatternHelpPopup(); this->drawPatternHelpPopup();
this->drawMathEvaluatorHelp(); this->drawMathEvaluatorHelp();
@ -313,13 +315,16 @@ namespace hex {
if (ImGui::MenuItem("About", "")) { if (ImGui::MenuItem("About", "")) {
View::doLater([] { ImGui::OpenPopup("About"); }); View::doLater([] { ImGui::OpenPopup("About"); });
this->m_aboutWindowOpen = true; this->m_aboutWindowOpen = true;
this->getWindowOpenState() = true;
} }
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("Pattern Language Cheat Sheet", "")) { if (ImGui::MenuItem("Pattern Language Cheat Sheet", "")) {
this->m_patternHelpWindowOpen = true; this->m_patternHelpWindowOpen = true;
this->getWindowOpenState() = true;
} }
if (ImGui::MenuItem("Calculator Cheat Sheet", "")) { if (ImGui::MenuItem("Calculator Cheat Sheet", "")) {
this->m_mathHelpWindowOpen = true; this->m_mathHelpWindowOpen = true;
this->getWindowOpenState() = true;
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }

View File

@ -129,10 +129,19 @@ namespace hex {
}); });
View::subscribeEvent(Events::PatternChanged, [this](auto) { View::subscribeEvent(Events::PatternChanged, [this](auto) {
this->m_highlightedBytes.clear(); this->m_highlightedBytes.clear();
for (const auto &pattern : this->m_patternData) for (const auto &pattern : this->m_patternData)
this->m_highlightedBytes.merge(pattern->getHighlightedAddresses()); this->m_highlightedBytes.merge(pattern->getHighlightedAddresses());
});
View::subscribeEvent(Events::OpenWindow, [this](auto name) {
if (std::any_cast<const char*>(name) == std::string("Open File")) {
View::openFileBrowser("Open File", imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, "*.*", [this](auto path) {
this->openFile(path);
this->getWindowOpenState() = true;
});
}
}); });
} }

View File

@ -5,18 +5,23 @@
namespace hex { namespace hex {
ViewSettings::ViewSettings() : View("Settings") { ViewSettings::ViewSettings() : View("Settings") {
this->getWindowOpenState() = true; View::subscribeEvent(Events::OpenWindow, [this](auto name) {
if (std::any_cast<const char*>(name) == std::string("Preferences")) {
View::doLater([]{ ImGui::OpenPopup("Preferences"); });
this->getWindowOpenState() = true;
}
});
} }
ViewSettings::~ViewSettings() { ViewSettings::~ViewSettings() {
View::unsubscribeEvent(Events::OpenWindow);
} }
void ViewSettings::drawContent() { void ViewSettings::drawContent() {
ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX)); ImGui::SetNextWindowSizeConstraints(ImVec2(0, 0), ImVec2(FLT_MAX, FLT_MAX));
if (ImGui::BeginPopupModal("Preferences", &this->m_settingsWindowOpen, ImGuiWindowFlags_AlwaysAutoResize)) { if (ImGui::BeginPopupModal("Preferences", &this->getWindowOpenState(), ImGuiWindowFlags_AlwaysAutoResize)) {
for (auto &[category, entries] : ContentRegistry::Settings::getEntries()) { for (auto &[category, entries] : ContentRegistry::Settings::getEntries()) {
ImGui::TextUnformatted(category.c_str()); ImGui::TextUnformatted(category.c_str());
ImGui::Separator(); ImGui::Separator();
@ -30,7 +35,8 @@ namespace hex {
ImGui::NewLine(); ImGui::NewLine();
} }
ImGui::EndPopup(); ImGui::EndPopup();
} } else
this->getWindowOpenState() = false;
} }
@ -38,7 +44,7 @@ namespace hex {
if (ImGui::BeginMenu("Help")) { if (ImGui::BeginMenu("Help")) {
if (ImGui::MenuItem("Preferences")) { if (ImGui::MenuItem("Preferences")) {
View::doLater([]{ ImGui::OpenPopup("Preferences"); }); View::doLater([]{ ImGui::OpenPopup("Preferences"); });
this->m_settingsWindowOpen = true; this->getWindowOpenState() = true;
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }

View File

@ -93,18 +93,16 @@ namespace hex {
call(); call();
View::getDeferedCalls().clear(); View::getDeferedCalls().clear();
if (SharedData::currentProvider != nullptr) { for (auto &view : ContentRegistry::Views::getEntries()) {
for (auto &view : ContentRegistry::Views::getEntries()) { if (!view->isAvailable() || !view->getWindowOpenState())
if (!view->getWindowOpenState()) continue;
continue;
auto minSize = view->getMinSize(); auto minSize = view->getMinSize();
minSize.x *= this->m_globalScale; minSize.x *= this->m_globalScale;
minSize.y *= this->m_globalScale; minSize.y *= this->m_globalScale;
ImGui::SetNextWindowSizeConstraints(minSize, view->getMaxSize()); ImGui::SetNextWindowSizeConstraints(minSize, view->getMaxSize());
view->drawContent(); view->drawContent();
}
} }
View::drawCommonInterfaces(); View::drawCommonInterfaces();
@ -178,7 +176,7 @@ namespace hex {
if (ImGui::BeginMenu("View")) { if (ImGui::BeginMenu("View")) {
for (auto &view : ContentRegistry::Views::getEntries()) { for (auto &view : ContentRegistry::Views::getEntries()) {
if (view->hasViewMenuItemEntry()) if (view->isAvailable() && view->hasViewMenuItemEntry())
ImGui::MenuItem((std::string(view->getName()) + " View").c_str(), "", &view->getWindowOpenState()); ImGui::MenuItem((std::string(view->getName()) + " View").c_str(), "", &view->getWindowOpenState());
} }
ImGui::EndMenu(); ImGui::EndMenu();
@ -219,12 +217,16 @@ namespace hex {
Window::s_currShortcut = { -1, -1 }; Window::s_currShortcut = { -1, -1 };
} }
if (SharedData::currentProvider == nullptr) { bool anyViewOpen = false;
for (auto &view : ContentRegistry::Views::getEntries())
anyViewOpen = anyViewOpen || (view->getWindowOpenState() && view->isAvailable());
if (!anyViewOpen) {
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"));
if (ImGui::Begin(title)) { if (ImGui::Begin(title)) {
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10 * this->m_globalScale, 10 * this->m_globalScale)); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10 * this->m_globalScale, 10 * this->m_globalScale));
if (ImGui::BeginChild("Welcome Screen", ImVec2(0, 0), ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_NoDecoration)) { if (ImGui::BeginChild("Welcome Screen", ImVec2(0, 0), false, ImGuiWindowFlags_AlwaysUseWindowPadding | ImGuiWindowFlags_NoDecoration)) {
this->drawWelcomeScreen(); this->drawWelcomeScreen();
} }
ImGui::EndChild(); ImGui::EndChild();
@ -268,7 +270,8 @@ namespace hex {
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("Start"); ImGui::Text("Start");
{ {
ImGui::BulletHyperlink("Open file"); if (ImGui::BulletHyperlink("Open File"))
EventManager::post(Events::OpenWindow, "Open File");
} }
ImGui::TableNextRow(ImGuiTableRowFlags_None, 100); ImGui::TableNextRow(ImGuiTableRowFlags_None, 100);
ImGui::TableNextColumn(); ImGui::TableNextColumn();
@ -292,7 +295,8 @@ namespace hex {
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("Customize"); ImGui::Text("Customize");
{ {
ImGui::DescriptionButton("Settings", "Change preferences of ImHex", ImVec2(ImGui::GetContentRegionAvail().x * 0.8, 0)); if (ImGui::DescriptionButton("Settings", "Change preferences of ImHex", ImVec2(ImGui::GetContentRegionAvail().x * 0.8f, 0)))
EventManager::post(Events::OpenWindow, "Preferences");
} }
ImGui::TableNextRow(ImGuiTableRowFlags_None, 100); ImGui::TableNextRow(ImGuiTableRowFlags_None, 100);
ImGui::TableNextColumn(); ImGui::TableNextColumn();