ImGui & UserInterface class

This commit is contained in:
Light3039 2021-05-27 10:41:32 +04:30
parent 8841216238
commit 4621f86cb2
19 changed files with 192 additions and 28 deletions

3
.gitmodules vendored
View file

@ -7,3 +7,6 @@
[submodule "Dependencies/glm"] [submodule "Dependencies/glm"]
path = Dependencies/glm path = Dependencies/glm
url = https://github.com/g-truc/glm url = https://github.com/g-truc/glm
[submodule "Dependencies/imgui"]
path = Dependencies/imgui
url = https://github.com/Light3039/imgui

View file

@ -23,3 +23,4 @@ include "../Engine/"
group "Dependencies" group "Dependencies"
include "../Dependencies/GLFW/" include "../Dependencies/GLFW/"
include "../Dependencies/GLAD/" include "../Dependencies/GLAD/"
include "../Dependencies/imgui/"

View file

@ -15,6 +15,8 @@ project "GLAD"
{ {
"**.c", "**.c",
"**.h", "**.h",
"premake5.lua"
} }
-- Dependencies -- -- Dependencies --

1
Dependencies/imgui vendored Submodule

@ -0,0 +1 @@
Subproject commit 04fd5072fbc635cc7a8814b8543d40836248d3a7

View file

@ -35,6 +35,8 @@ project "Engine"
(dependenciesdir .. "spdlog/include/"), (dependenciesdir .. "spdlog/include/"),
(dependenciesdir .. "glfw/include/"), (dependenciesdir .. "glfw/include/"),
(dependenciesdir .. "glad/include"), (dependenciesdir .. "glad/include"),
(dependenciesdir .. "imgui/"),
(dependenciesdir .. "imgui/backends"),
(dependenciesdir .. "glm/"), (dependenciesdir .. "glm/"),
} }
@ -42,6 +44,7 @@ project "Engine"
{ {
"GLFW", "GLFW",
"GLAD", "GLAD",
"ImGui",
} }
--- Filters --- --- Filters ---

View file

@ -32,6 +32,10 @@ namespace Light {
m_Window->GetGfxContext()->GetRenderCommand()->SwapBuffers(); m_Window->GetGfxContext()->GetRenderCommand()->SwapBuffers();
m_Window->GetGfxContext()->GetRenderCommand()->ClearBackBuffer(); m_Window->GetGfxContext()->GetRenderCommand()->ClearBackBuffer();
m_Window->GetGfxContext()->GetUserInterface()->Begin();
m_Window->GetGfxContext()->GetUserInterface()->End();
} }
} }

View file

@ -5,7 +5,7 @@
namespace Light { namespace Light {
GraphicsContext* GraphicsContext::s_Context; GraphicsContext* GraphicsContext::s_Context = nullptr;
GraphicsContext* GraphicsContext::Create(GraphicsAPI api, GLFWwindow* windowHandle) GraphicsContext* GraphicsContext::Create(GraphicsAPI api, GLFWwindow* windowHandle)
{ {
@ -15,6 +15,7 @@ namespace Light {
case Light::GraphicsAPI::OpenGL: case Light::GraphicsAPI::OpenGL:
s_Context = new glGraphicsContext(windowHandle); s_Context = new glGraphicsContext(windowHandle);
s_Context->m_RenderCommand = std::unique_ptr<RenderCommand>(RenderCommand::Create(windowHandle)); s_Context->m_RenderCommand = std::unique_ptr<RenderCommand>(RenderCommand::Create(windowHandle));
s_Context->m_UserInterface = std::unique_ptr<UserInterface>(UserInterface::Create(windowHandle));
// ...Renderer // ...Renderer
// ...UserInterface... // ...UserInterface...

View file

@ -4,6 +4,8 @@
#include "RenderCommand.h" #include "RenderCommand.h"
#include "UserInterface/UserInterface.h"
struct GLFWwindow {}; struct GLFWwindow {};
namespace Light { namespace Light {
@ -19,6 +21,7 @@ namespace Light {
static GraphicsContext* s_Context; static GraphicsContext* s_Context;
std::unique_ptr<RenderCommand> m_RenderCommand; std::unique_ptr<RenderCommand> m_RenderCommand;
std::unique_ptr<UserInterface> m_UserInterface;
protected: protected:
GraphicsAPI m_GraphicsAPI; GraphicsAPI m_GraphicsAPI;
@ -29,7 +32,8 @@ namespace Light {
static inline GraphicsAPI GetGraphicsAPI() { return s_Context->m_GraphicsAPI; } static inline GraphicsAPI GetGraphicsAPI() { return s_Context->m_GraphicsAPI; }
virtual RenderCommand* GetRenderCommand() { return m_RenderCommand.get(); } inline RenderCommand* GetRenderCommand() { return m_RenderCommand.get(); }
inline UserInterface* GetUserInterface() { return m_UserInterface.get(); }
}; };
} }

View file

@ -14,12 +14,17 @@ namespace Light {
{ {
private: private:
std::string m_Name; std::string m_Name;
public: public:
Layer(const std::string& name): m_Name(name) {} Layer(const std::string& name): m_Name(name) {}
virtual ~Layer() = default; virtual ~Layer() = default;
inline const std::string& GetName() const { return m_Name; } inline const std::string& GetName() const { return m_Name; }
// Updates
virtual void OnUpdate(float deltaTime) {}
virtual void OnUserInterfaceUpdate() {}
// Mouse events // Mouse events
virtual bool OnMouseMoved(const MouseMovedEvent& event) { return false; } virtual bool OnMouseMoved(const MouseMovedEvent& event) { return false; }
virtual bool OnButtonPressed(const ButtonPressedEvent& event) { return false; } virtual bool OnButtonPressed(const ButtonPressedEvent& event) { return false; }

View file

@ -2,30 +2,32 @@
#include "LayerStack.h" #include "LayerStack.h"
#include <functional> #include <functional>
namespace Light { namespace Light {
LayerStack* LayerStack::s_Context = nullptr;
LayerStack::LayerStack()
{
s_Context = this; // TODO: ASSERT
}
LayerStack::~LayerStack() LayerStack::~LayerStack()
{ {
for (Layer* layer : m_Layers) for (Layer* layer : m_Layers)
delete layer; delete layer;
} }
void LayerStack::PushLayer(Layer* layer) void LayerStack::OnUpdate(float deltaTime)
{ {
m_Layers.push_back(layer); for (auto it = m_Begin; it != m_End; it++)
m_Begin = m_Layers.begin(); (*it)->OnUpdate(deltaTime);
m_End = m_Layers.end();
LT_ENGINE_TRACE("LayerStack::PushLayer: Attached [{}]", layer->GetName());
} }
void LayerStack::PopLayer(Layer* layer) void LayerStack::OnUserInterfaceUpdate()
{ {
m_Layers.erase(std::find(m_Layers.begin(), m_Layers.end(), layer)); for (auto it = m_Begin; it != m_End; it++)
m_Begin = m_Layers.begin(); (*it)->OnUserInterfaceUpdate();
m_End = m_Layers.end();
LT_ENGINE_TRACE("LayerStack::PushLayer: Detatched[{}]", layer->GetName());
} }
void LayerStack::OnEvent(const Event& event) void LayerStack::OnEvent(const Event& event)
@ -84,5 +86,22 @@ namespace Light {
} }
} }
void LayerStack::AttachLayerImpl(Layer* layer)
{
m_Layers.push_back(layer);
m_Begin = m_Layers.begin();
m_End = m_Layers.end();
LT_ENGINE_TRACE("LayerStack::PushLayer: Attached [{}]", layer->GetName());
}
void LayerStack::DetatchLayerImpl(Layer* layer)
{
m_Layers.erase(std::find(m_Layers.begin(), m_Layers.end(), layer));
m_Begin = m_Layers.begin();
m_End = m_Layers.end();
LT_ENGINE_TRACE("LayerStack::PushLayer: Detatched[{}]", layer->GetName());
}
} }

View file

@ -13,22 +13,32 @@ namespace Light {
class LayerStack class LayerStack
{ {
private: private:
static LayerStack* s_Context;
std::vector<Layer*> m_Layers; std::vector<Layer*> m_Layers;
std::vector<Layer*>::iterator m_Begin; std::vector<Layer*>::iterator m_Begin;
std::vector<Layer*>::iterator m_End; std::vector<Layer*>::iterator m_End;
public: public:
LayerStack();
~LayerStack(); ~LayerStack();
void PushLayer(Layer* layer); static inline void AttachLayer(Layer* layer) { s_Context->AttachLayerImpl(layer); }
void PopLayer(Layer* layer); static inline void DetatchLayer(Layer* layer) { s_Context->DetatchLayerImpl(layer); }
void OnUpdate(float deltaTime);
void OnUserInterfaceUpdate();
void OnEvent(const Event& event); void OnEvent(const Event& event);
std::vector<Layer*>::iterator begin() { return m_Layers.begin(); } std::vector<Layer*>::iterator begin() { return m_Layers.begin(); }
std::vector<Layer*>::iterator end() { return m_Layers.end(); } std::vector<Layer*>::iterator end() { return m_Layers.end(); }
private:
void AttachLayerImpl(Layer* layer);
void DetatchLayerImpl(Layer* layer);
}; };
} }

View file

@ -1,11 +1,19 @@
#include "ltpch.h" #include "ltpch.h"
#include "UserInterface.h"
#include "Graphics/GraphicsContext.h"
#include "OpenGL/glUserInterface.h"
namespace Light { namespace Light {
class UserInterface UserInterface* UserInterface::Create(GLFWwindow* windowHandle)
{ {
private: switch (GraphicsContext::GetGraphicsAPI())
public: {
}; case GraphicsAPI::OpenGL:
return new glUserInterface(windowHandle);
}
}
} }

View file

@ -0,0 +1,19 @@
#pragma once
#include "Base.h"
struct GLFWwindow;
namespace Light {
class UserInterface
{
private:
public:
static UserInterface* Create(GLFWwindow* windowHandle);
virtual void Begin() = 0;
virtual void End() = 0;
};
}

View file

@ -0,0 +1,45 @@
#include "ltpch.h"
#include "glUserInterface.h"
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
namespace Light {
glUserInterface::glUserInterface(GLFWwindow* windowHandle)
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(windowHandle, true);
ImGui_ImplOpenGL3_Init();
}
glUserInterface::~glUserInterface()
{
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void glUserInterface::Begin()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow();
}
void glUserInterface::End()
{
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
}

View file

@ -0,0 +1,19 @@
#pragma once
#include "Base.h"
#include "UserInterface/UserInterface.h"
namespace Light {
class glUserInterface : public UserInterface
{
private:
public:
glUserInterface(GLFWwindow* windowHandle);
~glUserInterface();
void Begin() override;
void End() override;
};
}

10
Sandbox/imgui.ini Normal file
View file

@ -0,0 +1,10 @@
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0
[Window][Dear ImGui Demo]
Pos=219,40
Size=550,536
Collapsed=0

View file

@ -16,7 +16,8 @@ project "Sandbox"
{ {
"%{prj.location}/src/**.h", "%{prj.location}/src/**.h",
"%{prj.location}/src/**.cpp", "%{prj.location}/src/**.cpp",
"%{prj.location}/**.lua",
"%{prj.location}/premake5.lua",
} }
-- Dependencies -- -- Dependencies --
@ -30,12 +31,17 @@ project "Sandbox"
-- 3rd party -- 3rd party
(dependenciesdir .. "spdlog/include/"), (dependenciesdir .. "spdlog/include/"),
(dependenciesdir .. "imgui/"),
(dependenciesdir .. "imgui/backends"),
(dependenciesdir .. "glm/"), (dependenciesdir .. "glm/"),
} }
links links
{ {
"Engine", "Engine",
"GLFW",
"GLAD",
"ImGui",
} }
--- Filters --- --- Filters ---
@ -50,7 +56,7 @@ project "Sandbox"
defines "LT_DEBUG" defines "LT_DEBUG"
symbols "on" symbols "on"
-- release -- release
filter "configurations:Release" filter "configurations:Release"
defines "LT_RELEASE" defines "LT_RELEASE"
optimize "on" optimize "on"

View file

@ -1,11 +1,15 @@
#include <LightEngine.h> #include <LightEngine.h>
#include "SandboxLayer.h"
class Sandbox : public Light::Application class Sandbox : public Light::Application
{ {
public: public:
Sandbox() Sandbox()
{ {
LT_CLIENT_TRACE("Sandbox::Sandbox"); LT_CLIENT_TRACE("Sandbox::Sandbox");
Light::LayerStack::AttachLayer(new SandboxLayer("SandboxLayer"));
} }
~Sandbox() ~Sandbox()

View file

@ -1,9 +1,9 @@
#include <LightEngine.h> #include <LightEngine.h>
class TestLayer : public Light::Layer class SandboxLayer : public Light::Layer
{ {
public: public:
TestLayer(const std::string& name) : Light::Layer(name) {} SandboxLayer(const std::string& name): Light::Layer(name) {}
// Mouse events // Mouse events
virtual bool OnMouseMoved(const Light::MouseMovedEvent& event) override { LT_ENGINE_TRACE("{}", event.GetInfoLog()); return false; } virtual bool OnMouseMoved(const Light::MouseMovedEvent& event) override { LT_ENGINE_TRACE("{}", event.GetInfoLog()); return false; }
@ -12,10 +12,10 @@ public:
virtual bool OnWheelScrolled(const Light::WheelScrolledEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } virtual bool OnWheelScrolled(const Light::WheelScrolledEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }
// Keyboard events // Keyboard events
virtual bool OnKeyPressed(const Light::KeyPressedEvent& event) override virtual bool OnKeyPressed(const Light::KeyPressedEvent& event) override
{ {
LT_ENGINE_TRACE(event.GetInfoLog()); LT_ENGINE_TRACE(event.GetInfoLog());
return true; return true;
} }
virtual bool OnKeyReleased(const Light::KeyReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; } virtual bool OnKeyReleased(const Light::KeyReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }