2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/views/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>
|
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/helpers/shared_data.hpp>
|
2021-01-04 00:19:56 +01:00
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
|
|
|
|
View::View(std::string viewName) : m_viewName(viewName) { }
|
|
|
|
|
|
|
|
void View::drawMenu() { }
|
|
|
|
bool View::handleShortcut(int key, int mods) { return false; }
|
|
|
|
|
|
|
|
std::vector<std::function<void()>>& View::getDeferedCalls() {
|
2021-01-12 23:28:41 +01:00
|
|
|
return SharedData::deferredCalls;
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
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);
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void View::drawCommonInterfaces() {
|
2021-01-14 17:01:44 +01:00
|
|
|
if (ImGui::BeginPopupModal("Error", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
|
2020-12-22 18:10:01 +01:00
|
|
|
ImGui::NewLine();
|
|
|
|
if (ImGui::BeginChild("##scrolling", ImVec2(300, 100))) {
|
2021-01-13 23:08:41 +01:00
|
|
|
ImGui::SetCursorPosX((300 - ImGui::CalcTextSize(SharedData::errorPopupMessage.c_str(), nullptr, false).x) / 2.0F);
|
|
|
|
ImGui::TextWrapped("%s", SharedData::errorPopupMessage.c_str());
|
2020-12-22 18:10:01 +01:00
|
|
|
ImGui::EndChild();
|
|
|
|
}
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::SetCursorPosX(75);
|
2021-01-14 17:01:44 +01:00
|
|
|
if (ImGui::Button("Okay", ImVec2(150, 20)) || ImGui::IsKeyDown(ImGuiKey_Escape))
|
2020-12-22 18:10:01 +01:00
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void View::showErrorPopup(std::string_view errorMessage) {
|
2021-01-13 23:08:41 +01:00
|
|
|
SharedData::errorPopupMessage = errorMessage;
|
2020-12-22 18:10:01 +01:00
|
|
|
|
|
|
|
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 {
|
2020-12-22 18:10:01 +01:00
|
|
|
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) {
|
2021-01-04 00:19:56 +01:00
|
|
|
EventManager::subscribe(eventType, this, callback);
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
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 { }; });
|
|
|
|
}
|
|
|
|
|
2020-12-22 18:10:01 +01:00
|
|
|
void View::unsubscribeEvent(Events eventType) {
|
2021-01-04 00:19:56 +01:00
|
|
|
EventManager::unsubscribe(eventType, this);
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void View::doLater(std::function<void()> &&function) {
|
2021-01-12 23:28:41 +01:00
|
|
|
SharedData::deferredCalls.push_back(function);
|
2020-12-22 18:10:01 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-12-22 18:10:01 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|