1
0
mirror of synced 2024-12-14 16:52:53 +01:00
ImHex/plugins/libimhex/source/views/view.cpp

118 lines
3.8 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 viewName) : m_viewName(viewName) { }
void View::drawMenu() { }
bool View::handleShortcut(int key, int mods) { return false; }
2021-02-03 00:56:33 +01:00
bool View::isAvailable() {
return SharedData::currentProvider != nullptr && SharedData::currentProvider->isAvailable();
}
std::vector<std::function<void()>>& View::getDeferedCalls() {
return SharedData::deferredCalls;
}
2021-01-21 10:53:12 +01:00
std::vector<std::any> View::postEvent(Events eventType, const std::any &userData) {
return EventManager::post(eventType, userData);
}
2021-01-27 00:44:10 +01:00
void View::openFileBrowser(std::string title, imgui_addons::ImGuiFileBrowser::DialogMode mode, std::string validExtensions, const std::function<void(std::string)> &callback) {
SharedData::fileBrowserTitle = title;
SharedData::fileBrowserDialogMode = mode;
SharedData::fileBrowserValidExtensions = std::move(validExtensions);
SharedData::fileBrowserCallback = callback;
View::doLater([title]{
ImGui::OpenPopup(title.c_str());
});
}
void View::drawCommonInterfaces() {
if (ImGui::BeginPopupModal("Error", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::Text("%s", SharedData::errorPopupMessage.c_str());
ImGui::NewLine();
ImGui::Separator();
if (ImGui::Button("Okay") || ImGui::IsKeyDown(ImGuiKey_Escape))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
2021-01-27 00:44:10 +01:00
if (SharedData::fileBrowser.showFileDialog(SharedData::fileBrowserTitle, SharedData::fileBrowserDialogMode, ImVec2(0, 0), SharedData::fileBrowserValidExtensions)) {
SharedData::fileBrowserCallback(SharedData::fileBrowser.selected_path);
SharedData::fileBrowserTitle = "";
}
}
void View::showErrorPopup(std::string_view errorMessage) {
SharedData::errorPopupMessage = errorMessage;
2021-02-17 14:47:25 +01:00
View::doLater([] { ImGui::OpenPopup("Error"); });
}
bool View::hasViewMenuItemEntry() {
return true;
}
ImVec2 View::getMinSize() {
return ImVec2(480, 720);
}
ImVec2 View::getMaxSize() {
return ImVec2(FLT_MAX, FLT_MAX);
}
bool& View::getWindowOpenState() {
return this->m_windowOpen;
}
2021-01-21 10:53:12 +01:00
std::string_view View::getName() const {
return this->m_viewName;
}
2021-01-21 10:53:12 +01:00
void View::subscribeEvent(Events eventType, const std::function<std::any(const std::any&)> &callback) {
EventManager::subscribe(eventType, this, callback);
}
2021-01-21 10:53:12 +01:00
void View::subscribeEvent(Events eventType, const std::function<void(const std::any&)> &callback) {
EventManager::subscribe(eventType, this, [callback](auto userData) -> std::any { callback(userData); return { }; });
}
void View::unsubscribeEvent(Events eventType) {
EventManager::unsubscribe(eventType, this);
}
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);
}
2021-01-21 10:53:12 +01:00
void View::confirmButtons(const char *textLeft, const char *textRight, const std::function<void()> &leftButtonFn, const std::function<void()> &rightButtonFn) {
auto width = ImGui::GetWindowWidth();
ImGui::SetCursorPosX(width / 9);
if (ImGui::Button(textLeft, ImVec2(width / 3, 0)))
leftButtonFn();
ImGui::SameLine();
ImGui::SetCursorPosX(width / 9 * 5);
if (ImGui::Button(textRight, ImVec2(width / 3, 0)))
rightButtonFn();
}
}