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"]
path = Dependencies/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"
include "../Dependencies/GLFW/"
include "../Dependencies/GLAD/"
include "../Dependencies/imgui/"

View file

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

1
Dependencies/imgui vendored Submodule

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

View file

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

View file

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

View file

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

View file

@ -4,6 +4,8 @@
#include "RenderCommand.h"
#include "UserInterface/UserInterface.h"
struct GLFWwindow {};
namespace Light {
@ -19,6 +21,7 @@ namespace Light {
static GraphicsContext* s_Context;
std::unique_ptr<RenderCommand> m_RenderCommand;
std::unique_ptr<UserInterface> m_UserInterface;
protected:
GraphicsAPI m_GraphicsAPI;
@ -29,7 +32,8 @@ namespace Light {
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:
std::string m_Name;
public:
Layer(const std::string& name): m_Name(name) {}
virtual ~Layer() = default;
inline const std::string& GetName() const { return m_Name; }
// Updates
virtual void OnUpdate(float deltaTime) {}
virtual void OnUserInterfaceUpdate() {}
// Mouse events
virtual bool OnMouseMoved(const MouseMovedEvent& event) { return false; }
virtual bool OnButtonPressed(const ButtonPressedEvent& event) { return false; }

View file

@ -2,30 +2,32 @@
#include "LayerStack.h"
#include <functional>
namespace Light {
LayerStack* LayerStack::s_Context = nullptr;
LayerStack::LayerStack()
{
s_Context = this; // TODO: ASSERT
}
LayerStack::~LayerStack()
{
for (Layer* layer : m_Layers)
delete layer;
}
void LayerStack::PushLayer(Layer* layer)
void LayerStack::OnUpdate(float deltaTime)
{
m_Layers.push_back(layer);
m_Begin = m_Layers.begin();
m_End = m_Layers.end();
LT_ENGINE_TRACE("LayerStack::PushLayer: Attached [{}]", layer->GetName());
for (auto it = m_Begin; it != m_End; it++)
(*it)->OnUpdate(deltaTime);
}
void LayerStack::PopLayer(Layer* layer)
void LayerStack::OnUserInterfaceUpdate()
{
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());
for (auto it = m_Begin; it != m_End; it++)
(*it)->OnUserInterfaceUpdate();
}
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
{
private:
static LayerStack* s_Context;
std::vector<Layer*> m_Layers;
std::vector<Layer*>::iterator m_Begin;
std::vector<Layer*>::iterator m_End;
public:
LayerStack();
~LayerStack();
void PushLayer(Layer* layer);
void PopLayer(Layer* layer);
static inline void AttachLayer(Layer* layer) { s_Context->AttachLayerImpl(layer); }
static inline void DetatchLayer(Layer* layer) { s_Context->DetatchLayerImpl(layer); }
void OnUpdate(float deltaTime);
void OnUserInterfaceUpdate();
void OnEvent(const Event& event);
std::vector<Layer*>::iterator begin() { return m_Layers.begin(); }
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 "UserInterface.h"
#include "Graphics/GraphicsContext.h"
#include "OpenGL/glUserInterface.h"
namespace Light {
class UserInterface
UserInterface* UserInterface::Create(GLFWwindow* windowHandle)
{
private:
public:
};
switch (GraphicsContext::GetGraphicsAPI())
{
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/**.cpp",
"%{prj.location}/**.lua",
"%{prj.location}/premake5.lua",
}
-- Dependencies --
@ -30,12 +31,17 @@ project "Sandbox"
-- 3rd party
(dependenciesdir .. "spdlog/include/"),
(dependenciesdir .. "imgui/"),
(dependenciesdir .. "imgui/backends"),
(dependenciesdir .. "glm/"),
}
links
{
"Engine",
"GLFW",
"GLAD",
"ImGui",
}
--- Filters ---
@ -50,7 +56,7 @@ project "Sandbox"
defines "LT_DEBUG"
symbols "on"
-- release
-- release
filter "configurations:Release"
defines "LT_RELEASE"
optimize "on"

View file

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

View file

@ -1,9 +1,9 @@
#include <LightEngine.h>
class TestLayer : public Light::Layer
class SandboxLayer : public Light::Layer
{
public:
TestLayer(const std::string& name) : Light::Layer(name) {}
SandboxLayer(const std::string& name): Light::Layer(name) {}
// Mouse events
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; }
// Keyboard events
virtual bool OnKeyPressed(const Light::KeyPressedEvent& event) override
{
virtual bool OnKeyPressed(const Light::KeyPressedEvent& event) override
{
LT_ENGINE_TRACE(event.GetInfoLog());
return true;
return true;
}
virtual bool OnKeyReleased(const Light::KeyReleasedEvent& event) override { LT_ENGINE_TRACE(event.GetInfoLog()); return false; }