1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2024-11-26 20:28:49 -05:00

Added Layer system

This commit is contained in:
Gordon McCann 2021-12-19 15:47:45 -05:00
parent 58a0d46009
commit afe2053151
8 changed files with 141 additions and 5 deletions

View File

@ -1,6 +1,21 @@
#ifndef NAVIGATOR_H #ifndef NAVIGATOR_H
#define NAVIGATOR_H #define NAVIGATOR_H
#include <iostream>
#include <fstream>
#include <memory>
#include <utility>
#include <algorithm>
#include <functional>
#include <string>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <cstdint>
#include "Navigator/Logger.h" #include "Navigator/Logger.h"
#include "Navigator/Application.h" #include "Navigator/Application.h"

View File

@ -8,6 +8,8 @@ namespace Navigator {
m_runFlag(true) m_runFlag(true)
{ {
s_instance = this; s_instance = this;
PushLayer(new Layer());
} }
Application::~Application() {} Application::~Application() {}
@ -26,15 +28,25 @@ namespace Navigator {
return true; 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() void Application::Run()
{ {
while(m_runFlag) while(m_runFlag)
{ {
NAV_TRACE("Doing a run."); NAV_TRACE("Doing a run.");
for(auto layer : m_stack)
NAV_TRACE("Layer with name {0} found!", layer->GetName());
WindowCloseEvent event; WindowCloseEvent event;
OnEvent(event); OnEvent(event);
} }

View File

@ -4,6 +4,8 @@
#include "NavCore.h" #include "NavCore.h"
#include "Events/Event.h" #include "Events/Event.h"
#include "Events/AppEvent.h" #include "Events/AppEvent.h"
#include "LayerStack.h"
#include "Layer.h"
namespace Navigator { namespace Navigator {
@ -16,8 +18,8 @@ namespace Navigator {
void Run(); void Run();
void OnEvent(Event& event); void OnEvent(Event& event);
void PushLayer(); void PushLayer(Layer* layer);
void PushOverlay(); void PushOverlay(Layer* layer);
inline static Application& Get() { return *s_instance; } inline static Application& Get() { return *s_instance; }
@ -26,6 +28,7 @@ namespace Navigator {
private: private:
bool OnWindowCloseEvent(WindowCloseEvent& event); bool OnWindowCloseEvent(WindowCloseEvent& event);
LayerStack m_stack;
bool m_runFlag; bool m_runFlag;
static Application* s_instance; static Application* s_instance;

View File

@ -2,7 +2,6 @@
#define EVENT_H #define EVENT_H
#include "NavCore.h" #include "NavCore.h"
#include <sstream>
namespace Navigator { namespace Navigator {

View File

@ -0,0 +1,11 @@
#include "Layer.h"
namespace Navigator {
Layer::Layer(const std::string& name) :
m_name(name)
{
}
Layer::~Layer() {}
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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<Layer*>::iterator begin() { return m_stack.begin(); }
std::vector<Layer*>::iterator end() { return m_stack.end(); }
private:
std::vector<Layer*> m_stack; //These layers are owned by the LayerStack!
unsigned int m_insertIndex=0;
};
}
#endif