2020-11-10 15:26:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
|
|
|
#include "imgui.h"
|
|
|
|
|
2020-11-28 21:55:52 +01:00
|
|
|
#include "helpers/event.hpp"
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2020-11-16 00:07:42 +01:00
|
|
|
#include <functional>
|
2020-11-23 23:57:19 +01:00
|
|
|
#include <string>
|
2020-11-16 00:07:42 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
2020-11-10 15:26:38 +01:00
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
class View {
|
|
|
|
public:
|
2020-11-23 23:57:19 +01:00
|
|
|
View(std::string viewName) : m_viewName(viewName) { }
|
2020-11-10 15:26:38 +01:00
|
|
|
virtual ~View() { }
|
|
|
|
|
|
|
|
virtual void createView() = 0;
|
|
|
|
virtual void createMenu() { }
|
2020-11-11 14:41:44 +01:00
|
|
|
virtual bool handleShortcut(int key, int mods) { return false; }
|
2020-11-12 09:38:52 +01:00
|
|
|
|
2020-11-16 00:07:42 +01:00
|
|
|
static std::vector<std::function<void()>>& getDeferedCalls() {
|
|
|
|
return View::s_deferedCalls;
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:58:50 +01:00
|
|
|
static void postEvent(Events eventType, const void *userData = nullptr) {
|
|
|
|
View::s_eventManager.post(eventType, userData);
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:45:27 +01:00
|
|
|
static void drawCommonInterfaces() {
|
|
|
|
if (ImGui::BeginPopupModal("Error", nullptr, ImGuiWindowFlags_NoResize)) {
|
|
|
|
ImGui::NewLine();
|
|
|
|
if (ImGui::BeginChild("##scrolling", ImVec2(300, 100))) {
|
|
|
|
ImGui::SetCursorPosX((300 - ImGui::CalcTextSize(View::s_errorMessage.c_str(), nullptr, false).x) / 2.0F);
|
|
|
|
ImGui::TextWrapped("%s", View::s_errorMessage.c_str());
|
|
|
|
ImGui::EndChild();
|
|
|
|
}
|
|
|
|
ImGui::NewLine();
|
|
|
|
ImGui::SetCursorPosX(75);
|
|
|
|
if (ImGui::Button("Okay", ImVec2(150, 20)))
|
|
|
|
ImGui::CloseCurrentPopup();
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void showErrorPopup(std::string_view errorMessage) {
|
|
|
|
View::s_errorMessage = errorMessage;
|
|
|
|
|
|
|
|
ImGui::OpenPopup("Error");
|
|
|
|
}
|
|
|
|
|
2020-11-28 22:01:50 +01:00
|
|
|
virtual bool hasViewMenuItemEntry() { return true; }
|
|
|
|
|
2020-11-23 23:57:19 +01:00
|
|
|
bool& getWindowOpenState() {
|
|
|
|
return this->m_windowOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string getName() const {
|
|
|
|
return this->m_viewName;
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
protected:
|
2020-11-17 13:58:50 +01:00
|
|
|
void subscribeEvent(Events eventType, std::function<void(const void*)> callback) {
|
2020-11-12 09:38:52 +01:00
|
|
|
View::s_eventManager.subscribe(eventType, this, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unsubscribeEvent(Events eventType) {
|
|
|
|
View::s_eventManager.unsubscribe(eventType, this);
|
|
|
|
}
|
|
|
|
|
2020-11-16 00:07:42 +01:00
|
|
|
void doLater(std::function<void()> &&function) {
|
|
|
|
View::s_deferedCalls.push_back(function);
|
|
|
|
}
|
|
|
|
|
2020-11-23 23:57:19 +01:00
|
|
|
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
private:
|
2020-11-23 23:57:19 +01:00
|
|
|
std::string m_viewName;
|
|
|
|
bool m_windowOpen = false;
|
|
|
|
|
2020-11-12 09:38:52 +01:00
|
|
|
static inline EventManager s_eventManager;
|
2020-11-16 00:07:42 +01:00
|
|
|
static inline std::vector<std::function<void()>> s_deferedCalls;
|
2020-11-27 13:45:27 +01:00
|
|
|
|
|
|
|
static inline std::string s_errorMessage;
|
2020-11-10 15:26:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|