1
0
mirror of synced 2024-12-02 11:17:19 +01:00
ImHex/lib/libimhex/source/views/view.cpp

130 lines
4.5 KiB
C++
Raw Normal View History

#include <hex/views/view.hpp>
#include <imgui.h>
#include <functional>
#include <string>
#include <vector>
#include <hex/helpers/shared_data.hpp>
namespace hex {
View::View(std::string unlocalizedName) : m_unlocalizedViewName(unlocalizedName) { }
2022-01-10 21:05:37 +01:00
bool View::isAvailable() const {
return ImHexApi::Provider::isValid() && ImHexApi::Provider::get()->isAvailable();
2021-02-03 00:56:33 +01:00
}
std::vector<std::function<void()>>& View::getDeferedCalls() {
return SharedData::deferredCalls;
}
void View::drawCommonInterfaces() {
ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300)));
if (ImGui::BeginPopupModal("hex.common.info"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
2022-01-15 14:14:53 +01:00
ImGui::TextFormattedWrapped("{}", SharedData::popupMessage.c_str());
ImGui::NewLine();
ImGui::Separator();
if (ImGui::Button("hex.common.okay"_lang) || ImGui::IsKeyDown(ImGuiKey_Escape))
ImGui::CloseCurrentPopup();
2021-10-03 12:32:58 +02:00
ImGui::SetWindowPos((SharedData::windowSize - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
ImGui::EndPopup();
}
ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300)));
if (ImGui::BeginPopupModal("hex.common.error"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
2022-01-15 14:14:53 +01:00
ImGui::TextFormattedWrapped("{}", SharedData::popupMessage.c_str());
ImGui::NewLine();
ImGui::Separator();
if (ImGui::Button("hex.common.okay"_lang) || ImGui::IsKeyDown(ImGuiKey_Escape))
ImGui::CloseCurrentPopup();
2021-10-03 12:32:58 +02:00
ImGui::SetWindowPos((SharedData::windowSize - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
ImGui::EndPopup();
}
ImGui::SetNextWindowSizeConstraints(scaled(ImVec2(400, 100)), scaled(ImVec2(600, 300)));
if (ImGui::BeginPopupModal("hex.common.fatal"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
2022-01-15 14:14:53 +01:00
ImGui::TextFormattedWrapped("{}", SharedData::popupMessage.c_str());
ImGui::NewLine();
ImGui::Separator();
if (ImGui::Button("hex.common.okay"_lang) || ImGui::IsKeyDown(ImGuiKey_Escape)) {
ImHexApi::Common::closeImHex();
ImGui::CloseCurrentPopup();
}
2021-10-03 12:32:58 +02:00
ImGui::SetWindowPos((SharedData::windowSize - ImGui::GetWindowSize()) / 2, ImGuiCond_Appearing);
ImGui::EndPopup();
}
}
void View::showMessagePopup(const std::string &message) {
SharedData::popupMessage = message;
View::doLater([] { ImGui::OpenPopup("hex.common.info"_lang); });
}
void View::showErrorPopup(const std::string &errorMessage) {
SharedData::popupMessage = errorMessage;
View::doLater([] { ImGui::OpenPopup("hex.common.error"_lang); });
}
void View::showFatalPopup(const std::string &errorMessage) {
SharedData::popupMessage = errorMessage;
View::doLater([] { ImGui::OpenPopup("hex.common.fatal"_lang); });
}
2022-01-10 21:05:37 +01:00
bool View::hasViewMenuItemEntry() const {
return true;
}
2022-01-10 21:05:37 +01:00
ImVec2 View::getMinSize() const {
return scaled(ImVec2(480, 720));
}
2022-01-10 21:05:37 +01:00
ImVec2 View::getMaxSize() const {
return { FLT_MAX, FLT_MAX };
}
bool& View::getWindowOpenState() {
return this->m_windowOpen;
}
2022-01-10 21:05:37 +01:00
const bool& View::getWindowOpenState() const {
return this->m_windowOpen;
}
const std::string& View::getUnlocalizedName() const {
return this->m_unlocalizedViewName;
}
std::string View::getName() const {
return View::toWindowName(this->m_unlocalizedViewName);
}
void View::discardNavigationRequests() {
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows))
ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NavEnableKeyboard;
}
void View::doLater(std::function<void()> &&function) {
SharedData::deferredCalls.push_back(function);
}
void View::confirmButtons(const std::string &textLeft, const std::string &textRight, const std::function<void()> &leftButtonFn, const std::function<void()> &rightButtonFn) {
auto width = ImGui::GetWindowWidth();
ImGui::SetCursorPosX(width / 9);
if (ImGui::Button(textLeft.c_str(), ImVec2(width / 3, 0)))
leftButtonFn();
ImGui::SameLine();
ImGui::SetCursorPosX(width / 9 * 5);
if (ImGui::Button(textRight.c_str(), ImVec2(width / 3, 0)))
rightButtonFn();
}
}