get layer and filter with name, reworked layer system with template
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include "SDL3/SDL_timer.h"
|
#include "SDL3/SDL_timer.h"
|
||||||
#include "imgui.h"
|
#include "imgui.h"
|
||||||
#include "imgui_impl_sdl3.h"
|
#include "imgui_impl_sdl3.h"
|
||||||
|
#include "imguilayer.h"
|
||||||
|
|
||||||
#define SDL_MAIN_HANDLED 1
|
#define SDL_MAIN_HANDLED 1
|
||||||
|
|
||||||
@@ -125,7 +126,8 @@ void Application::Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Rendering
|
// Rendering
|
||||||
m_ImGuiLayer->Begin();
|
// m_ImGuiLayer->Begin();
|
||||||
|
this->GetLayer<SakuraVNE::ImGuiLayer>()->Begin();
|
||||||
|
|
||||||
SDL_SetRenderScale(m_Renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
|
SDL_SetRenderScale(m_Renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
|
||||||
SDL_SetRenderDrawColor(m_Renderer, (Uint8)111, (Uint8)232, (Uint8)168, (Uint8)0);
|
SDL_SetRenderDrawColor(m_Renderer, (Uint8)111, (Uint8)232, (Uint8)168, (Uint8)0);
|
||||||
@@ -135,7 +137,8 @@ void Application::Run() {
|
|||||||
layer->OnImGuiRender();
|
layer->OnImGuiRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_ImGuiLayer->End();
|
// m_ImGuiLayer->End();
|
||||||
|
this->GetLayer<SakuraVNE::ImGuiLayer>()->End();
|
||||||
|
|
||||||
SDL_RenderPresent(m_Renderer);
|
SDL_RenderPresent(m_Renderer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include "LayerStack.h"
|
#include "LayerStack.h"
|
||||||
#include "SDL3/SDL_render.h"
|
#include "SDL3/SDL_render.h"
|
||||||
#include "SDL3/SDL_video.h"
|
#include "SDL3/SDL_video.h"
|
||||||
#include "imguilayer.h"
|
// #include "imguilayer.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
@@ -52,6 +52,17 @@ public:
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename TLayer>
|
||||||
|
requires(std::is_base_of_v<SakuraVNE::Layer, TLayer>)
|
||||||
|
TLayer *GetLayer(const std::string &layerName) {
|
||||||
|
for (const auto &layer : m_LayerStack) {
|
||||||
|
if (auto casted = dynamic_cast<TLayer *>(layer.get()) && layer->GetName() == layerName) {
|
||||||
|
return casted;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename TLayer>
|
template <typename TLayer>
|
||||||
requires(std::is_base_of_v<SakuraVNE::Layer, TLayer>)
|
requires(std::is_base_of_v<SakuraVNE::Layer, TLayer>)
|
||||||
void PushOverlay() {
|
void PushOverlay() {
|
||||||
@@ -85,7 +96,7 @@ private:
|
|||||||
AppData m_AppData;
|
AppData m_AppData;
|
||||||
|
|
||||||
SakuraVNE::LayerStack m_LayerStack;
|
SakuraVNE::LayerStack m_LayerStack;
|
||||||
SakuraVNE::ImGuiLayer *m_ImGuiLayer;
|
// SakuraVNE::ImGuiLayer *m_ImGuiLayer;
|
||||||
|
|
||||||
static Application *s_Instance;
|
static Application *s_Instance;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,5 +2,5 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace SakuraVNE {
|
namespace SakuraVNE {
|
||||||
Layer::Layer(const std::string &name) : m_LayerName(name), isActive(true) {}
|
Layer::Layer(const std::string &name, bool isActive) : m_LayerName(name), m_isActive(isActive) {}
|
||||||
} // namespace SakuraVNE
|
} // namespace SakuraVNE
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
namespace SakuraVNE {
|
namespace SakuraVNE {
|
||||||
class Layer {
|
class Layer {
|
||||||
public:
|
public:
|
||||||
Layer(const std::string &name = "Layer");
|
Layer(const std::string &name = "Layer", bool isActive = true);
|
||||||
virtual ~Layer() = default;
|
virtual ~Layer() = default;
|
||||||
|
|
||||||
virtual void OnFrame(Uint64 timestamp) {}
|
virtual void OnFrame(Uint64 timestamp) {}
|
||||||
@@ -22,6 +22,6 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string m_LayerName;
|
std::string m_LayerName;
|
||||||
bool isActive;
|
bool m_isActive = true;
|
||||||
};
|
};
|
||||||
} // namespace SakuraVNE
|
} // namespace SakuraVNE
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "LayerStack.h"
|
#include "LayerStack.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace SakuraVNE {
|
namespace SakuraVNE {
|
||||||
@@ -10,9 +11,8 @@ LayerStack::~LayerStack() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// should these be fixed with std::move and take layer param as std::unique_ptr?
|
void LayerStack::PushLayer(std::unique_ptr<Layer> layer) {
|
||||||
void LayerStack::PushLayer(Layer *layer) {
|
m_LayerStack.emplace(m_LayerStack.begin() + m_LayerIndex, std::move(layer));
|
||||||
m_LayerStack.emplace(m_LayerStack.begin() + m_LayerIndex, layer);
|
|
||||||
m_LayerIndex++;
|
m_LayerIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ void LayerStack::PopLayer(Layer *layer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LayerStack::PushOverLay(Layer *layer) { m_LayerStack.emplace_back(layer); }
|
void LayerStack::PushOverLay(std::unique_ptr<Layer> layer) { m_LayerStack.emplace_back(std::move(layer)); }
|
||||||
|
|
||||||
void LayerStack::PopOverlay(Layer *layer) {
|
void LayerStack::PopOverlay(Layer *layer) {
|
||||||
auto match = std::find_if(m_LayerStack.begin() + m_LayerIndex, m_LayerStack.end(), [layer](const std::unique_ptr<Layer> &ptr) { return ptr.get() == layer; });
|
auto match = std::find_if(m_LayerStack.begin() + m_LayerIndex, m_LayerStack.end(), [layer](const std::unique_ptr<Layer> &ptr) { return ptr.get() == layer; });
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ public:
|
|||||||
LayerStack() = default;
|
LayerStack() = default;
|
||||||
~LayerStack();
|
~LayerStack();
|
||||||
|
|
||||||
void PushLayer(Layer *layer);
|
void PushLayer(std::unique_ptr<Layer> layer);
|
||||||
void PopLayer(Layer *layer);
|
void PopLayer(Layer *layer);
|
||||||
void PushOverLay(Layer *layer);
|
void PushOverLay(std::unique_ptr<Layer> layer);
|
||||||
void PopOverlay(Layer *layer);
|
void PopOverlay(Layer *layer);
|
||||||
|
|
||||||
std::vector<std::unique_ptr<Layer>>::iterator begin() { return m_LayerStack.begin(); }
|
std::vector<std::unique_ptr<Layer>>::iterator begin() { return m_LayerStack.begin(); }
|
||||||
|
|||||||
@@ -3,19 +3,21 @@
|
|||||||
#include "Layer.h"
|
#include "Layer.h"
|
||||||
#include "LayerStack.h"
|
#include "LayerStack.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
TEST_CASE("Layer operations", "[Layer]") {
|
TEST_CASE("Layer operations", "[Layer]") {
|
||||||
SakuraVNE::LayerStack lstack;
|
SakuraVNE::LayerStack lstack;
|
||||||
const auto &layers = lstack.GetLayers();
|
const auto &layers = lstack.GetLayers();
|
||||||
|
|
||||||
SakuraVNE::Layer *layer1 = new SakuraVNE::Layer("layer1");
|
// Create the unique_ptr, keep a raw pointer for testing Pop operations later, then move it.
|
||||||
lstack.PushLayer(layer1);
|
auto u_layer1 = std::make_unique<SakuraVNE::Layer>("layer1");
|
||||||
|
SakuraVNE::Layer *layer1 = u_layer1.get();
|
||||||
|
lstack.PushLayer(std::move(u_layer1));
|
||||||
|
|
||||||
SECTION("PushLayer should make the size 2") {
|
SECTION("PushLayer should make the size 2") {
|
||||||
SakuraVNE::Layer *layer2 = new SakuraVNE::Layer("layer2");
|
auto u_layer2 = std::make_unique<SakuraVNE::Layer>("layer2");
|
||||||
lstack.PushLayer(layer2);
|
lstack.PushLayer(std::move(u_layer2));
|
||||||
REQUIRE(layers.size() == 2);
|
REQUIRE(layers.size() == 2);
|
||||||
REQUIRE(layers[1]->GetName() == "layer2");
|
REQUIRE(layers[1]->GetName() == "layer2");
|
||||||
}
|
}
|
||||||
@@ -24,19 +26,20 @@ TEST_CASE("Layer operations", "[Layer]") {
|
|||||||
REQUIRE(layers.size() == 0);
|
REQUIRE(layers.size() == 0);
|
||||||
}
|
}
|
||||||
SECTION("PushOverlay should always put at the end of the list and should always be after the last fence index") {
|
SECTION("PushOverlay should always put at the end of the list and should always be after the last fence index") {
|
||||||
SakuraVNE::Layer *layer2 = new SakuraVNE::Layer("layer2");
|
auto u_layer2 = std::make_unique<SakuraVNE::Layer>("layer2");
|
||||||
lstack.PushOverLay(layer2);
|
lstack.PushOverLay(std::move(u_layer2));
|
||||||
REQUIRE(layers[1]->GetName() == "layer2");
|
REQUIRE(layers[1]->GetName() == "layer2");
|
||||||
|
|
||||||
SakuraVNE::Layer *layer3 = new SakuraVNE::Layer("layer3");
|
auto u_layer3 = std::make_unique<SakuraVNE::Layer>("layer3");
|
||||||
lstack.PushLayer(layer3);
|
lstack.PushLayer(std::move(u_layer3));
|
||||||
|
|
||||||
REQUIRE(layers[1]->GetName() == "layer3");
|
REQUIRE(layers[1]->GetName() == "layer3");
|
||||||
REQUIRE(layers[2]->GetName() == "layer2");
|
REQUIRE(layers[2]->GetName() == "layer2");
|
||||||
}
|
}
|
||||||
SECTION("Pop overlay") {
|
SECTION("Pop overlay") {
|
||||||
SakuraVNE::Layer *layer6 = new SakuraVNE::Layer("layer6");
|
auto u_layer6 = std::make_unique<SakuraVNE::Layer>("layer6");
|
||||||
lstack.PushOverLay(layer6);
|
SakuraVNE::Layer *layer6 = u_layer6.get();
|
||||||
|
lstack.PushOverLay(std::move(u_layer6));
|
||||||
REQUIRE(layers[1]->GetName() == "layer6");
|
REQUIRE(layers[1]->GetName() == "layer6");
|
||||||
|
|
||||||
lstack.PopOverlay(layer6);
|
lstack.PopOverlay(layer6);
|
||||||
|
|||||||
Reference in New Issue
Block a user