impr: Update all of ImHex to the new popup system

This commit is contained in:
WerWolv
2023-04-08 00:58:53 +02:00
parent 9c9ac23818
commit 80edaea392
36 changed files with 578 additions and 478 deletions

View File

@@ -0,0 +1,80 @@
#include <hex/ui/popup.hpp>
#include <hex/api/localization.hpp>
#include <functional>
#include <string>
namespace hex::plugin::builtin {
class PopupFileChooser : public Popup<PopupFileChooser> {
public:
PopupFileChooser(const std::vector<std::fs::path> &files, const std::vector<nfdfilteritem_t> &validExtensions, bool multiple, const std::function<void(std::fs::path)> &callback)
: hex::Popup<PopupFileChooser>("hex.builtin.common.choose_file", false),
m_indices({ }), m_files(files),
m_openCallback(callback),
m_validExtensions(validExtensions), m_multiple(multiple) { }
void drawContent() override {
bool doubleClicked = false;
if (ImGui::BeginListBox("##files", scaled(ImVec2(500, 400)))) {
u32 index = 0;
for (auto &path : this->m_files) {
ImGui::PushID(index);
bool selected = this->m_indices.contains(index);
if (ImGui::Selectable(wolv::util::toUTF8String(path.filename()).c_str(), selected, ImGuiSelectableFlags_DontClosePopups)) {
if (!this->m_multiple) {
this->m_indices.clear();
this->m_indices.insert(index);
} else {
if (selected) {
this->m_indices.erase(index);
} else {
this->m_indices.insert(index);
}
}
}
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0))
doubleClicked = true;
ImGui::InfoTooltip(wolv::util::toUTF8String(path).c_str());
ImGui::PopID();
index++;
}
ImGui::EndListBox();
}
if (ImGui::Button("hex.builtin.common.open"_lang) || doubleClicked) {
for (const auto &index : this->m_indices)
this->m_openCallback(this->m_files[index]);
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("hex.builtin.common.browse"_lang)) {
fs::openFileBrowser(fs::DialogMode::Open, this->m_validExtensions, [this](const auto &path) {
this->m_openCallback(path);
ImGui::CloseCurrentPopup();
}, {}, this->m_multiple);
}
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_AlwaysAutoResize;
}
private:
std::set<u32> m_indices;
std::vector<std::fs::path> m_files;
std::function<void(std::fs::path)> m_openCallback;
std::vector<nfdfilteritem_t> m_validExtensions;
bool m_multiple = false;
};
}

View File

@@ -0,0 +1,74 @@
#include <hex/ui/popup.hpp>
#include <hex/api/localization.hpp>
#include <functional>
#include <string>
namespace hex::plugin::builtin {
namespace impl {
template<typename T>
class PopupNotification : public Popup<T> {
public:
PopupNotification(std::string unlocalizedName, std::string message, std::function<void()> function)
: hex::Popup<T>(std::move(unlocalizedName), false),
m_message(std::move(message)), m_function(std::move(function)) { }
void drawContent() override {
ImGui::TextFormattedWrapped("{}", this->m_message.c_str());
ImGui::NewLine();
ImGui::Separator();
if (ImGui::Button("hex.builtin.common.okay"_lang) || ImGui::IsKeyDown(ImGuiKey_Escape))
this->m_function();
ImGui::SetWindowPos((ImHexApi::System::getMainWindowSize() - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_AlwaysAutoResize;
}
[[nodiscard]] ImVec2 getMinSize() const override {
return scaled({ 400, 100 });
}
[[nodiscard]] ImVec2 getMaxSize() const override {
return scaled({ 600, 300 });
}
private:
std::string m_message;
std::function<void()> m_function;
};
}
class PopupInfo : public impl::PopupNotification<PopupInfo> {
public:
explicit PopupInfo(std::string message)
: PopupNotification("hex.builtin.common.info", std::move(message), []() {
Popup::close();
}) { }
};
class PopupError : public impl::PopupNotification<PopupError> {
public:
explicit PopupError(std::string message)
: PopupNotification("hex.builtin.common.error", std::move(message), []() {
Popup::close();
}) { }
};
class PopupFatal : public impl::PopupNotification<PopupFatal> {
public:
explicit PopupFatal(std::string message)
: PopupNotification("hex.builtin.common.fatal", std::move(message), []() {
ImHexApi::System::closeImHex();
Popup::close();
}) { }
};
}

View File

@@ -0,0 +1,51 @@
#include <hex/ui/popup.hpp>
#include <hex/api/localization.hpp>
#include <functional>
#include <string>
namespace hex::plugin::builtin {
class PopupQuestion : public Popup<PopupQuestion> {
public:
PopupQuestion(std::string message, std::function<void()> yesFunction, std::function<void()> noFunction)
: hex::Popup<PopupQuestion>("hex.builtin.common.question", false),
m_message(std::move(message)),
m_yesFunction(std::move(yesFunction)), m_noFunction(std::move(noFunction)) { }
void drawContent() override {
ImGui::TextFormattedWrapped("{}", this->m_message.c_str());
ImGui::NewLine();
ImGui::Separator();
auto width = ImGui::GetWindowWidth();
ImGui::SetCursorPosX(width / 9);
if (ImGui::Button("hex.builtin.common.yes"_lang, ImVec2(width / 3, 0)))
this->m_yesFunction();
ImGui::SameLine();
ImGui::SetCursorPosX(width / 9 * 5);
if (ImGui::Button("hex.builtin.common.no"_lang, ImVec2(width / 3, 0)))
this->m_noFunction();
ImGui::SetWindowPos((ImHexApi::System::getMainWindowSize() - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_AlwaysAutoResize;
}
[[nodiscard]] ImVec2 getMinSize() const override {
return scaled({ 400, 100 });
}
[[nodiscard]] ImVec2 getMaxSize() const override {
return scaled({ 600, 300 });
}
private:
std::string m_message;
std::function<void()> m_yesFunction, m_noFunction;
};
}

View File

@@ -0,0 +1,37 @@
#include <hex/ui/popup.hpp>
#include <hex/api/localization.hpp>
#include <functional>
#include <string>
namespace hex::plugin::builtin {
class PopupTasksWaiting : public Popup<PopupTasksWaiting> {
public:
PopupTasksWaiting()
: hex::Popup<PopupTasksWaiting>("hex.builtin.popup.waiting_for_tasks.title", false) { }
void drawContent() override {
ImGui::TextUnformatted("hex.builtin.popup.waiting_for_tasks.desc"_lang);
ImGui::Separator();
ImGui::SetCursorPosX((ImGui::GetWindowWidth() - ImGui::CalcTextSize("[-]").x) / 2);
ImGui::TextSpinner("");
ImGui::NewLine();
ImGui::SetCursorPosX((ImGui::GetWindowWidth() - 150_scaled) / 2);
if (ImGui::ButtonEx("hex.builtin.common.cancel"_lang, ImVec2(150, 0)) || ImGui::IsKeyDown(ImGuiKey_Escape))
ImGui::CloseCurrentPopup();
if (TaskManager::getRunningTaskCount() == 0 && TaskManager::getRunningBackgroundTaskCount() == 0) {
ImGui::CloseCurrentPopup();
ImHexApi::System::closeImHex();
}
}
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
return ImGuiWindowFlags_NoResize | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove;
}
};
}

View File

@@ -1,14 +1,18 @@
#pragma once
#include <hex/ui/view.hpp>
#include <hex/providers/provider.hpp>
#include <hex/ui/popup.hpp>
#include <pl/pattern_language.hpp>
#include <pl/core/errors/error.hpp>
#include <hex/providers/provider.hpp>
#include <content/helpers/provider_extra_data.hpp>
#include <content/providers/memory_file_provider.hpp>
#include <ui/hex_editor.hpp>
#include <ui/pattern_drawer.hpp>
#include <content/providers/memory_file_provider.hpp>
#include <cstring>
#include <filesystem>
@@ -38,6 +42,54 @@ namespace hex::plugin::builtin {
Deny
};
class PopupAcceptPattern : public Popup<PopupAcceptPattern> {
public:
PopupAcceptPattern(ViewPatternEditor *view) : Popup("hex.builtin.view.pattern_editor.accept_pattern"), m_view(view) {}
void drawContent() override {
ImGui::TextFormattedWrapped("{}", static_cast<const char *>("hex.builtin.view.pattern_editor.accept_pattern.desc"_lang));
std::vector<std::string> entries;
entries.resize(this->m_view->m_possiblePatternFiles.size());
for (u32 i = 0; i < entries.size(); i++) {
entries[i] = wolv::util::toUTF8String(this->m_view->m_possiblePatternFiles[i].filename());
}
if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) {
u32 index = 0;
for (auto &path : this->m_view->m_possiblePatternFiles) {
if (ImGui::Selectable(wolv::util::toUTF8String(path.filename()).c_str(), index == this->m_view->m_selectedPatternFile))
this->m_view->m_selectedPatternFile = index;
index++;
}
ImGui::EndListBox();
}
ImGui::NewLine();
ImGui::TextUnformatted("hex.builtin.view.pattern_editor.accept_pattern.question"_lang);
auto provider = ImHexApi::Provider::get();
confirmButtons(
"hex.builtin.common.yes"_lang, "hex.builtin.common.no"_lang, [this, provider] {
this->m_view->loadPatternFile(this->m_view->m_possiblePatternFiles[this->m_view->m_selectedPatternFile], provider);
Popup::close();
},
[] {
Popup::close();
}
);
if (ImGui::IsKeyDown(ImGui::GetKeyIndex(ImGuiKey_Escape)))
Popup::close();
}
private:
ViewPatternEditor *m_view;
};
private:
using PlData = ProviderExtraData::Data::PatternLanguage;