2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/ui/view.hpp>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <imgui.h>
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
std::string View::s_popupMessage;
|
|
|
|
|
|
|
|
u32 View::s_selectableFileIndex;
|
|
|
|
std::vector<fs::path> View::s_selectableFiles;
|
|
|
|
std::function<void(fs::path)> View::s_selectableFileOpenCallback;
|
|
|
|
std::vector<nfdfilteritem_t> View::s_selectableFilesValidExtensions;
|
|
|
|
|
|
|
|
ImFontAtlas *View::s_fontAtlas;
|
|
|
|
ImFontConfig View::s_fontConfig;
|
|
|
|
|
2021-03-03 22:26:17 +01:00
|
|
|
View::View(std::string unlocalizedName) : m_unlocalizedViewName(unlocalizedName) { }
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-01-10 21:05:37 +01:00
|
|
|
bool View::isAvailable() const {
|
2021-09-21 02:29:54 +02:00
|
|
|
return ImHexApi::Provider::isValid() && ImHexApi::Provider::get()->isAvailable();
|
2021-02-03 00:56:33 +01:00
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void View::drawCommonInterfaces() {
|
2022-02-01 18:09:40 +01:00
|
|
|
auto windowSize = ImHexApi::System::getMainWindowSize();
|
|
|
|
|
2022-01-11 23:48:18 +01:00
|
|
|
ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300)));
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::BeginPopupModal("hex.builtin.common.info"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
|
2022-02-01 18:09:40 +01:00
|
|
|
ImGui::TextFormattedWrapped("{}", s_popupMessage.c_str());
|
2020-12-22 18:10:01 +01:00
|
|
|
ImGui::NewLine();
|
2021-01-31 00:05:07 +01:00
|
|
|
ImGui::Separator();
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::Button("hex.builtin.common.okay"_lang) || ImGui::IsKeyDown(ImGuiKey_Escape))
|
2020-12-22 18:10:01 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
2021-01-31 00:05:07 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ImGui::SetWindowPos((windowSize - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
|
2020-12-22 18:10:01 +01:00
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2022-01-11 23:48:18 +01:00
|
|
|
ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300)));
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::BeginPopupModal("hex.builtin.common.error"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
|
2022-02-01 18:09:40 +01:00
|
|
|
ImGui::TextFormattedWrapped("{}", s_popupMessage.c_str());
|
2021-09-22 17:56:06 +02:00
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::Separator();
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::Button("hex.builtin.common.okay"_lang) || ImGui::IsKeyDown(ImGuiKey_Escape))
|
2021-09-22 17:56:06 +02:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ImGui::SetWindowPos((windowSize - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
|
2021-09-22 17:56:06 +02:00
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
2022-01-11 23:48:18 +01:00
|
|
|
ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300)));
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::BeginPopupModal("hex.builtin.common.fatal"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
|
2022-02-01 18:09:40 +01:00
|
|
|
ImGui::TextFormattedWrapped("{}", s_popupMessage.c_str());
|
2021-04-18 20:24:42 +02:00
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::Separator();
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::Button("hex.builtin.common.okay"_lang) || ImGui::IsKeyDown(ImGuiKey_Escape)) {
|
2021-08-21 13:55:21 +02:00
|
|
|
ImHexApi::Common::closeImHex();
|
2021-04-18 20:24:42 +02:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
ImGui::SetWindowPos((windowSize - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
|
2021-04-18 20:24:42 +02:00
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
2022-01-23 21:52:43 +01:00
|
|
|
|
|
|
|
bool opened = true;
|
|
|
|
ImGui::SetNextWindowPos(ImGui::GetMainViewport()->GetCenter(), ImGuiCond_Appearing, ImVec2(0.5F, 0.5F));
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::BeginPopupModal("hex.builtin.common.choose_file"_lang, &opened, ImGuiWindowFlags_AlwaysAutoResize)) {
|
2022-01-23 21:52:43 +01:00
|
|
|
|
|
|
|
if (ImGui::BeginListBox("##files", ImVec2(300_scaled, 0))) {
|
|
|
|
|
|
|
|
u32 index = 0;
|
2022-02-01 18:09:40 +01:00
|
|
|
for (auto &path : View::s_selectableFiles) {
|
|
|
|
if (ImGui::Selectable(path.filename().string().c_str(), index == View::s_selectableFileIndex))
|
|
|
|
View::s_selectableFileIndex = index;
|
2022-01-23 21:52:43 +01:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndListBox();
|
|
|
|
}
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::Button("hex.builtin.common.open"_lang)) {
|
2022-02-01 18:09:40 +01:00
|
|
|
View::s_selectableFileOpenCallback(View::s_selectableFiles[View::s_selectableFileIndex]);
|
2022-01-23 21:52:43 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
if (ImGui::Button("hex.builtin.common.browse"_lang)) {
|
|
|
|
hex::openFileBrowser("hex.builtin.common.open"_lang, DialogMode::Open, View::s_selectableFilesValidExtensions, [](const auto &path) {
|
2022-02-01 18:09:40 +01:00
|
|
|
View::s_selectableFileOpenCallback(path);
|
2022-01-23 21:52:43 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 17:56:06 +02:00
|
|
|
void View::showMessagePopup(const std::string &message) {
|
2022-02-01 18:09:40 +01:00
|
|
|
s_popupMessage = message;
|
2021-09-22 17:56:06 +02:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
ImHexApi::Tasks::doLater([] { ImGui::OpenPopup("hex.builtin.common.info"_lang); });
|
2021-09-22 17:56:06 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
void View::showErrorPopup(const std::string &errorMessage) {
|
2022-02-01 18:09:40 +01:00
|
|
|
s_popupMessage = errorMessage;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
ImHexApi::Tasks::doLater([] { ImGui::OpenPopup("hex.builtin.common.error"_lang); });
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
void View::showFatalPopup(const std::string &errorMessage) {
|
2022-02-01 18:09:40 +01:00
|
|
|
s_popupMessage = errorMessage;
|
2021-04-18 20:24:42 +02:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
ImHexApi::Tasks::doLater([] { ImGui::OpenPopup("hex.builtin.common.fatal"_lang); });
|
2021-04-18 20:24:42 +02:00
|
|
|
}
|
|
|
|
|
2022-01-23 21:52:43 +01:00
|
|
|
void View::showFileChooserPopup(const std::vector<fs::path> &paths, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(fs::path)> &callback) {
|
2022-02-01 22:09:44 +01:00
|
|
|
View::s_selectableFileIndex = 0;
|
|
|
|
View::s_selectableFiles = paths;
|
2022-02-01 18:09:40 +01:00
|
|
|
View::s_selectableFilesValidExtensions = validExtensions;
|
2022-02-01 22:09:44 +01:00
|
|
|
View::s_selectableFileOpenCallback = callback;
|
2022-01-23 21:52:43 +01:00
|
|
|
|
2022-02-02 00:36:09 +01:00
|
|
|
ImHexApi::Tasks::doLater([] { ImGui::OpenPopup("hex.builtin.common.choose_file"_lang); });
|
2022-01-23 21:52:43 +01:00
|
|
|
}
|
|
|
|
|
2022-01-10 21:05:37 +01:00
|
|
|
bool View::hasViewMenuItemEntry() const {
|
2020-12-22 18:10:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-10 21:05:37 +01:00
|
|
|
ImVec2 View::getMinSize() const {
|
2022-01-11 23:48:18 +01:00
|
|
|
return scaled(ImVec2(480, 720));
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2022-01-10 21:05:37 +01:00
|
|
|
ImVec2 View::getMaxSize() const {
|
2021-09-22 17:56:06 +02:00
|
|
|
return { FLT_MAX, FLT_MAX };
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
bool &View::getWindowOpenState() {
|
2020-12-22 18:10:01 +01:00
|
|
|
return this->m_windowOpen;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const bool &View::getWindowOpenState() const {
|
2022-01-10 21:05:37 +01:00
|
|
|
return this->m_windowOpen;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::string &View::getUnlocalizedName() const {
|
2021-03-03 22:26:17 +01:00
|
|
|
return this->m_unlocalizedViewName;
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 00:10:10 +01:00
|
|
|
std::string View::getName() const {
|
|
|
|
return View::toWindowName(this->m_unlocalizedViewName);
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:23:50 +01:00
|
|
|
void View::discardNavigationRequests() {
|
|
|
|
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows))
|
|
|
|
ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NavEnableKeyboard;
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:24 +02:00
|
|
|
void View::confirmButtons(const std::string &textLeft, const std::string &textRight, const std::function<void()> &leftButtonFn, const std::function<void()> &rightButtonFn) {
|
2020-12-22 18:10:01 +01:00
|
|
|
auto width = ImGui::GetWindowWidth();
|
|
|
|
ImGui::SetCursorPosX(width / 9);
|
2021-09-08 15:18:24 +02:00
|
|
|
if (ImGui::Button(textLeft.c_str(), ImVec2(width / 3, 0)))
|
2020-12-22 18:10:01 +01:00
|
|
|
leftButtonFn();
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::SetCursorPosX(width / 9 * 5);
|
2021-09-08 15:18:24 +02:00
|
|
|
if (ImGui::Button(textRight.c_str(), ImVec2(width / 3, 0)))
|
2020-12-22 18:10:01 +01:00
|
|
|
rightButtonFn();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|