Clean up layer stack code, input now goes through each layer

This commit is contained in:
Nigel Barink 2023-05-16 21:59:10 +02:00
parent 817d0bdca9
commit 8ef886df83
5 changed files with 102 additions and 140 deletions

View File

@ -22,8 +22,14 @@
using namespace YoggieEngine; using namespace YoggieEngine;
const float movement_speed = 0.1f;
static float lastX = 400, lastY = 300;
const float sensitivity = 0.1;
static bool firstMouse = true;
class EditorLayer : public Layer { class EditorLayer : public Layer {
public: public:
EditorLayer() : EditorLayer() :
Layer(), Layer(),
@ -64,11 +70,6 @@ public:
void OnUpdate() override { void OnUpdate() override {
scene.Update(); scene.Update();
/*
* if (Sceneview.isFocused) {
UpdateSceneCamera(sceneview);
}
*/
auto components = scene.getReg().view<Render3DComponent>(); auto components = scene.getReg().view<Render3DComponent>();
for(auto component : components){ for(auto component : components){
@ -119,6 +120,41 @@ public:
} }
bool OnKey(int key, int mode) override {
if (SceneisFocused) {
spdlog::info("update camera!");
if (key == YOGGIE_KEY_UP)
camera->Rotation.x += movement_speed;
if (key == YOGGIE_KEY_DOWN)
camera->Rotation.x -= movement_speed;
if (key == YOGGIE_KEY_LEFT)
camera->Rotation.y += movement_speed;
if (key == YOGGIE_KEY_RIGHT)
camera->Rotation.y -= movement_speed;
if (key == YOGGIE_KEY_A)
camera->Position += glm::vec3(1.0f, 0.0f, 0.0f) * movement_speed;
if (key == YOGGIE_KEY_S)
camera->Position += glm::vec3(0.0f, 0.0f, -1.0f) * movement_speed;
if (key == YOGGIE_KEY_D)
camera->Position -= glm::vec3(1.0f, 0.0f, 0.0f) * movement_speed;
if (key == GLFW_KEY_W)
camera->Position -= glm::vec3(0.0f, 0.0f, -1.0f) * movement_speed;
}
return true;
}
ImGuizmo::OPERATION activeOperation = ImGuizmo::OPERATION::TRANSLATE; ImGuizmo::OPERATION activeOperation = ImGuizmo::OPERATION::TRANSLATE;
@ -375,7 +411,7 @@ public:
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0,0 }); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0,0 });
ImGui::Begin("SceneView",nullptr,viewportWindowFlags); ImGui::Begin("SceneView",nullptr,viewportWindowFlags);
// spdlog::info("{0}x{1}", ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); // spdlog::info("{0}x{1}", ImGui::GetWindowWidth(), ImGui::GetWindowHeight());
SceneisFocused = ImGui::IsWindowFocused() || ImGui::IsWindowHovered();
ImGui::Image((ImTextureID)(intptr_t)renderer.getCurrentFrameBuffer().GetColourAttachment(), ImGui::Image((ImTextureID)(intptr_t)renderer.getCurrentFrameBuffer().GetColourAttachment(),
ImVec2{(float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight()}); ImVec2{(float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight()});
@ -474,6 +510,9 @@ private:
Renderer renderer; Renderer renderer;
EditorCamera* camera = new EditorCamera(); EditorCamera* camera = new EditorCamera();
Mesh cube ; Mesh cube ;
bool SceneisFocused = false;
void LoadLastOrEmptyProject() { void LoadLastOrEmptyProject() {
// Check if there is a last known loaded project and // Check if there is a last known loaded project and
@ -507,101 +546,4 @@ private:
} }
} }
void UpdateSceneCamera() {
const float movement_speed = 0.01f;
static float lastX = 400, lastY = 300;
const float sensitivity = 0.1;
static bool firstMouse = true;
/*
if (MouseButtonPressed(YOGGIE_MOUSE_BUTTON_RIGHT)) {
glfwSetInputMode((GLFWwindow*)appWindow->GetHandle(), GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
auto newX = getCursorPosX(appWindow);
auto newY = getCursorPosY(appWindow);
if (firstMouse)
{
lastX = newX;
lastY = newY;
firstMouse = false;
}
float xoffset = newX - lastX;
float yoffset = newY - lastY;
lastX = newX;
lastY = newY;
xoffset *= sensitivity;
yoffset *= sensitivity;
sceneview.cam.Rotation.x += (xoffset / 2);
sceneview.cam.Rotation.y += (xoffset /2);
sceneview.cam.Rotation.z += yoffset;
if (sceneview.cam.pitch > 89.0f)
sceneview.cam.pitch = 89.0f;
if (sceneview.cam.pitch < -89.0f)
sceneview.cam.pitch = -89.0f;
}
else if (firstMouse == false)
{
glfwSetInputMode((GLFWwindow*)appWindow->GetHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);
firstMouse = true;
}
*/
/*
EditorCamera& cam = sceneview.GetCamera();
if (keyIsPressed(YOGGIE_KEY_UP))
cam.Rotation.x += movement_speed;
if (keyIsPressed(YOGGIE_KEY_DOWN))
cam.Rotation.x -= movement_speed;
if (keyIsPressed(YOGGIE_KEY_LEFT))
cam.Rotation.y += movement_speed;
if (keyIsPressed(YOGGIE_KEY_RIGHT))
cam.Rotation.y -= movement_speed;
cam.Update();
*/
/*
// Check for Camara movement input here!
if (keyIsPressed(YOGGIE_KEY_W))
sceneview.cam.Position -= sceneview.cam.Front * movement_speed;
if (keyIsPressed(YOGGIE_KEY_A))
sceneview.cam.Position += sceneview.cam.Right * movement_speed;
if (keyIsPressed(YOGGIE_KEY_S))
sceneview.cam.Position += sceneview.cam.Front * movement_speed;
if (keyIsPressed(YOGGIE_KEY_D))
sceneview.cam.Position -= sceneview.cam.Right * movement_speed;
*/
}
}; };

View File

@ -11,43 +11,12 @@ public:
void Run() override void Run() override
{ {
PushLayer(new EditorLayer());
Application::Run();
// Create EditorLayer
EditorLayer* firstLayer = new EditorLayer();
firstLayer->OnStartup();
double previous = glfwGetTime();
double lag = 0.0;
while (!appWindow->WindowShouldClose()) {
PollEvents();
double now = glfwGetTime();
double elapsed = now - previous;
previous = now;
lag += elapsed;
GuiBegin();
firstLayer->OnUpdate();
firstLayer->OnUI();
GuiEnd();
SwapBuffers();
}
firstLayer->OnDestroy();
} }
private:
std::vector<Layer*> layers = std::vector<Layer*>();
}; };
YoggieEngine::Application* CreateApplication() { YoggieEngine::Application* CreateApplication() {

View File

@ -50,12 +50,57 @@ namespace YoggieEngine {
//ImGuizmo::SetOrthographic(true); //ImGuizmo::SetOrthographic(true);
init_inputSystem(appWindow); //init_inputSystem(appWindow);
glfwSetWindowUserPointer((GLFWwindow*)this->appWindow->GetHandle(), this);
glfwSetKeyCallback((GLFWwindow*)this->appWindow->GetHandle(), HandleKey);
} }
void Application::HandleKey(GLFWwindow* window, int key, int scancode, int action, int mods) {
auto app = (Application*)glfwGetWindowUserPointer(window);
for (auto i = app->AppLayerstack.begin(); i < app->AppLayerstack.end(); i++) {
if ((*i)->OnKey(key, action)) {
break;
}
}
}
void Application::Run() { void Application::Run() {
for (auto i = AppLayerstack.begin(); i < AppLayerstack.end(); i++) {
(*i)->OnStartup();
}
double previous = glfwGetTime();
double lag = 0.0;
while (!appWindow->WindowShouldClose()) {
PollEvents();
double now = glfwGetTime();
double elapsed = now - previous;
previous = now;
lag += elapsed;
for (auto i = AppLayerstack.begin(); i < AppLayerstack.end(); i++) {
(*i)->OnUpdate();
}
GuiBegin();
for (auto i = AppLayerstack.begin(); i < AppLayerstack.end(); i++) {
(*i)->OnUI();
}
GuiEnd();
SwapBuffers();
}
for (auto i = AppLayerstack.begin(); i < AppLayerstack.end(); i++) {
(*i)->OnDestroy();
}
} }

View File

@ -20,6 +20,7 @@ namespace YoggieEngine {
void PushLayer(Layer* layer); void PushLayer(Layer* layer);
static void HandleKey(GLFWwindow* window, int key, int scancode, int action, int mods);
protected: protected:

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include <spdlog/spdlog.h>
class Layer { class Layer {
public: public:
@ -12,12 +12,17 @@ public:
virtual void OnUpdate(){} virtual void OnUpdate(){}
virtual void OnUI(){} virtual void OnUI(){}
virtual bool OnKey(int key , int status ) {
spdlog::info( "Key {0} , {1}", key, status);
return false;
}
virtual void OnStartup(){} virtual void OnStartup(){}
virtual void OnAttach() {} virtual void OnAttach() {}
virtual void OnDetach() {} virtual void OnDetach() {}
virtual void OnCreate() {} virtual void OnCreate() {}
virtual void OnDestroy(){} virtual void OnDestroy(){}