From afe20531517631748248813d5581dacf2aaa9820 Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sun, 19 Dec 2021 15:47:45 -0500 Subject: [PATCH] Added Layer system --- Navigator/src/Navigator.h | 15 ++++++++++ Navigator/src/Navigator/Application.cpp | 16 ++++++++-- Navigator/src/Navigator/Application.h | 7 +++-- Navigator/src/Navigator/Events/Event.h | 1 - Navigator/src/Navigator/Layer.cpp | 11 +++++++ Navigator/src/Navigator/Layer.h | 27 +++++++++++++++++ Navigator/src/Navigator/LayerStack.cpp | 40 +++++++++++++++++++++++++ Navigator/src/Navigator/LayerStack.h | 29 ++++++++++++++++++ 8 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 Navigator/src/Navigator/Layer.cpp create mode 100644 Navigator/src/Navigator/Layer.h create mode 100644 Navigator/src/Navigator/LayerStack.cpp create mode 100644 Navigator/src/Navigator/LayerStack.h diff --git a/Navigator/src/Navigator.h b/Navigator/src/Navigator.h index 683554b..f1f9b70 100644 --- a/Navigator/src/Navigator.h +++ b/Navigator/src/Navigator.h @@ -1,6 +1,21 @@ #ifndef NAVIGATOR_H #define NAVIGATOR_H +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + #include "Navigator/Logger.h" #include "Navigator/Application.h" diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index 07adf43..9f38a08 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -8,6 +8,8 @@ namespace Navigator { m_runFlag(true) { s_instance = this; + + PushLayer(new Layer()); } Application::~Application() {} @@ -26,15 +28,25 @@ namespace Navigator { return true; } - void Application::PushLayer() {} + void Application::PushLayer(Layer* layer) + { + m_stack.PushLayer(layer); + layer->OnAttach(); + } - void Application::PushOverlay() {} + void Application::PushOverlay(Layer* layer) + { + m_stack.PushOverlay(layer); + layer->OnAttach(); + } void Application::Run() { while(m_runFlag) { NAV_TRACE("Doing a run."); + for(auto layer : m_stack) + NAV_TRACE("Layer with name {0} found!", layer->GetName()); WindowCloseEvent event; OnEvent(event); } diff --git a/Navigator/src/Navigator/Application.h b/Navigator/src/Navigator/Application.h index ab56843..931061e 100644 --- a/Navigator/src/Navigator/Application.h +++ b/Navigator/src/Navigator/Application.h @@ -4,6 +4,8 @@ #include "NavCore.h" #include "Events/Event.h" #include "Events/AppEvent.h" +#include "LayerStack.h" +#include "Layer.h" namespace Navigator { @@ -16,8 +18,8 @@ namespace Navigator { void Run(); void OnEvent(Event& event); - void PushLayer(); - void PushOverlay(); + void PushLayer(Layer* layer); + void PushOverlay(Layer* layer); inline static Application& Get() { return *s_instance; } @@ -26,6 +28,7 @@ namespace Navigator { private: bool OnWindowCloseEvent(WindowCloseEvent& event); + LayerStack m_stack; bool m_runFlag; static Application* s_instance; diff --git a/Navigator/src/Navigator/Events/Event.h b/Navigator/src/Navigator/Events/Event.h index 3b0796b..c05f69d 100644 --- a/Navigator/src/Navigator/Events/Event.h +++ b/Navigator/src/Navigator/Events/Event.h @@ -2,7 +2,6 @@ #define EVENT_H #include "NavCore.h" -#include namespace Navigator { diff --git a/Navigator/src/Navigator/Layer.cpp b/Navigator/src/Navigator/Layer.cpp new file mode 100644 index 0000000..1cae494 --- /dev/null +++ b/Navigator/src/Navigator/Layer.cpp @@ -0,0 +1,11 @@ +#include "Layer.h" + +namespace Navigator { + + Layer::Layer(const std::string& name) : + m_name(name) + { + } + + Layer::~Layer() {} +} \ No newline at end of file diff --git a/Navigator/src/Navigator/Layer.h b/Navigator/src/Navigator/Layer.h new file mode 100644 index 0000000..82d3b4d --- /dev/null +++ b/Navigator/src/Navigator/Layer.h @@ -0,0 +1,27 @@ +#ifndef LAYER_H +#define LAYER_H + +#include "Events/Event.h" + +namespace Navigator { + + class Layer + { + public: + Layer(const std::string& name="Layer"); + virtual ~Layer(); + + virtual void OnAttach() {} + virtual void OnDetach() {} + virtual void OnImGuiRender() {} + virtual void OnEvent(Event& event) {} + + inline const std::string& GetName() { return m_name; } + + private: + std::string m_name; + }; + +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/LayerStack.cpp b/Navigator/src/Navigator/LayerStack.cpp new file mode 100644 index 0000000..3c3965b --- /dev/null +++ b/Navigator/src/Navigator/LayerStack.cpp @@ -0,0 +1,40 @@ +#include "LayerStack.h" + +namespace Navigator { + + LayerStack::LayerStack() {} + + LayerStack::~LayerStack() + { + for(Layer* layer : m_stack) + delete layer; + } + + void LayerStack::PushLayer(Layer* layer) + { + m_stack.emplace(m_stack.begin() + m_insertIndex, layer); + m_insertIndex++; + } + + void LayerStack::PushOverlay(Layer* layer) + { + m_stack.emplace_back(layer); + } + + void LayerStack::PopLayer(Layer* layer) + { + auto iter = std::find(m_stack.begin(), m_stack.end(), layer); + if(iter != m_stack.end()) + { + m_stack.erase(iter); + m_insertIndex--; + } + } + + void LayerStack::PopOverlay(Layer* layer) + { + auto iter = std::find(m_stack.begin(), m_stack.end(), layer); + if(iter != m_stack.end()) + m_stack.erase(iter); + } +} \ No newline at end of file diff --git a/Navigator/src/Navigator/LayerStack.h b/Navigator/src/Navigator/LayerStack.h new file mode 100644 index 0000000..84928e6 --- /dev/null +++ b/Navigator/src/Navigator/LayerStack.h @@ -0,0 +1,29 @@ +#ifndef LAYER_STACK_H +#define LAYER_STACK_H + +#include "NavCore.h" +#include "Layer.h" + +namespace Navigator { + + class LayerStack + { + public: + LayerStack(); + ~LayerStack(); + + void PushLayer(Layer* layer); + void PopLayer(Layer* layer); + void PushOverlay(Layer* layer); + void PopOverlay(Layer* layer); + + std::vector::iterator begin() { return m_stack.begin(); } + std::vector::iterator end() { return m_stack.end(); } + private: + std::vector m_stack; //These layers are owned by the LayerStack! + unsigned int m_insertIndex=0; + }; + +} + +#endif \ No newline at end of file