Layers and Events
This commit is contained in:
3
include/utils/Events/Event.h
Normal file
3
include/utils/Events/Event.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "pch.hpp"
|
||||
|
||||
class Event {};
|
||||
15
include/utils/Layers/Layer.h
Normal file
15
include/utils/Layers/Layer.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "pch.hpp"
|
||||
#include "utils/Events/Event.h"
|
||||
|
||||
class Layer {
|
||||
|
||||
virtual ~Layer() {}
|
||||
|
||||
virtual void onRender() = 0;
|
||||
|
||||
virtual void onAttach() = 0;
|
||||
|
||||
virtual void onDetach() = 0;
|
||||
|
||||
virtual bool onEvent(const Event&) = 0;
|
||||
};
|
||||
31
include/utils/Layers/Layerstack.h
Normal file
31
include/utils/Layers/Layerstack.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "pch.hpp"
|
||||
#include "Layer.h"
|
||||
|
||||
class Layerstack {
|
||||
|
||||
public:
|
||||
Layerstack() {}
|
||||
|
||||
~Layerstack() {
|
||||
while(!lstack.empty()) {
|
||||
pop();
|
||||
}
|
||||
}
|
||||
|
||||
void push(Layer* l) { lstack.push_front(l); }
|
||||
|
||||
void pop() {
|
||||
Layer* l = lstack.front();
|
||||
lstack.pop_front();
|
||||
delete l;
|
||||
}
|
||||
|
||||
void renderAll() {
|
||||
for(Layer* l : lstack)
|
||||
l->onRender();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::list<Layer*> lstack;
|
||||
};
|
||||
100
modules/ImguiModule/src/ImguiModule.cpp
Normal file
100
modules/ImguiModule/src/ImguiModule.cpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "ImguiModule.h"
|
||||
|
||||
#include "imgui.h"
|
||||
#include "backends/imgui_impl_glfw.h"
|
||||
#include "backends/imgui_impl_opengl3.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
ImguiModule::ImguiModule(void* h, Archimedes::App* a) : Archimedes::GuiModule(h, a) {
|
||||
|
||||
name = "ImguiModule";
|
||||
}
|
||||
|
||||
ImguiModule::~ImguiModule() {
|
||||
|
||||
std::list<Archimedes::Renderer::renderCmd*>* cmdList =
|
||||
std::any_cast<std::list<Archimedes::Renderer::renderCmd*>*>(depsInstances["WindowModule"]->getData("renderCmdList"));
|
||||
cmdList->erase(rcmd);
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
|
||||
void ImguiModule::onLoad() {
|
||||
|
||||
Archimedes::Module* wm = depsInstances["WindowModule"];
|
||||
|
||||
if(!wm) {
|
||||
std::cout << "No WindowModule for ImguiModule!\n";
|
||||
std::abort();
|
||||
}
|
||||
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
||||
|
||||
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
|
||||
//io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
|
||||
|
||||
// Setup Dear ImGui style
|
||||
ImGui::StyleColorsDark();
|
||||
//ImGui::StyleColorsLight();
|
||||
|
||||
// Setup Platform/Renderer backends
|
||||
GLFWwindow* w = std::any_cast<GLFWwindow*>(wm->getData("window"));
|
||||
|
||||
if(!ImGui_ImplGlfw_InitForOpenGL(w, true))
|
||||
std::cout << "GLFWImpl failed\n";
|
||||
if(!ImGui_ImplOpenGL3_Init("#version 330")) {
|
||||
std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::list<Archimedes::Renderer::renderCmd*>* cmdList =
|
||||
std::any_cast<std::list<Archimedes::Renderer::renderCmd*>*>(wm->getData("renderCmdList"));
|
||||
|
||||
cmdList->push_back([](){
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
});
|
||||
|
||||
rcmd = --cmdList->end();
|
||||
cmdList->end()++;
|
||||
|
||||
//Compute first frame ahead of first WindowModule->run()
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
ImGui::Render();
|
||||
}
|
||||
|
||||
void ImguiModule::run() {
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
|
||||
{
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
static float f = 0.0f;
|
||||
static int counter = 0;
|
||||
|
||||
ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
|
||||
|
||||
ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
|
||||
|
||||
ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
||||
|
||||
if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
|
||||
counter++;
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("counter = %d", counter);
|
||||
|
||||
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||
ImGui::End();
|
||||
}
|
||||
ImGui::Render();
|
||||
}
|
||||
30
modules/ImguiModule/src/ImguiModule.h
Normal file
30
modules/ImguiModule/src/ImguiModule.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef GUIMODULE
|
||||
#define GUIMODULE
|
||||
#endif
|
||||
|
||||
#include "Archimedes.h"
|
||||
|
||||
#include "utils/Window/Window.h"
|
||||
|
||||
class ImguiModule : public Archimedes::GuiModule {
|
||||
|
||||
public:
|
||||
ImguiModule(void*, Archimedes::App*);
|
||||
|
||||
~ImguiModule();
|
||||
|
||||
void onLoad();
|
||||
|
||||
void run();
|
||||
|
||||
|
||||
private:
|
||||
std::list<Archimedes::Renderer::renderCmd*>::iterator rcmd;
|
||||
|
||||
Archimedes::Window* window;
|
||||
};
|
||||
|
||||
#ifdef TESTIMGUI_DYNAMIC
|
||||
#define MODULE_TYPE ImguiModule
|
||||
#include "endModule.h"
|
||||
#endif
|
||||
@@ -27,10 +27,12 @@ class WindowModule : public Archimedes::Module {
|
||||
void onLoad();
|
||||
|
||||
//interface for other modules
|
||||
std::string sayHello() { return "Call from TestImgui!"; }
|
||||
|
||||
private:
|
||||
|
||||
Archimedes::Window* window;
|
||||
Archimedes::Renderer* renderer;
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -23,7 +23,8 @@ TestImgui::~TestImgui() {
|
||||
|
||||
void TestImgui::onLoad() {
|
||||
|
||||
Archimedes::Module* wm = depsInstances["WindowModule"];
|
||||
WindowModule* wm = (WindowModule*) depsInstances["WindowModule"];
|
||||
std::cout << "Say hello: " << wm->sayHello() << std::endl;
|
||||
|
||||
if(!wm) {
|
||||
std::cout << "No WindowModule for TestImgui!\n";
|
||||
|
||||
Reference in New Issue
Block a user