From 8ef886df83c7b8d796ead1723394900643ff6e11 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Tue, 16 May 2023 21:59:10 +0200 Subject: [PATCH] Clean up layer stack code, input now goes through each layer --- Editor/src/EditorLayer.h | 148 ++++++++++--------------------- Editor/src/app.cpp | 35 +------- YoggieEngine/src/Application.cpp | 47 +++++++++- YoggieEngine/src/Application.h | 1 + YoggieEngine/src/Layer.h | 11 ++- 5 files changed, 102 insertions(+), 140 deletions(-) diff --git a/Editor/src/EditorLayer.h b/Editor/src/EditorLayer.h index bf7aa77..1d7bf8f 100644 --- a/Editor/src/EditorLayer.h +++ b/Editor/src/EditorLayer.h @@ -22,8 +22,14 @@ 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 { + public: EditorLayer() : Layer(), @@ -64,11 +70,6 @@ public: void OnUpdate() override { scene.Update(); - /* - * if (Sceneview.isFocused) { - UpdateSceneCamera(sceneview); - } - */ auto components = scene.getReg().view(); 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; @@ -375,7 +411,7 @@ public: ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{ 0,0 }); ImGui::Begin("SceneView",nullptr,viewportWindowFlags); // spdlog::info("{0}x{1}", ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); - + SceneisFocused = ImGui::IsWindowFocused() || ImGui::IsWindowHovered(); ImGui::Image((ImTextureID)(intptr_t)renderer.getCurrentFrameBuffer().GetColourAttachment(), ImVec2{(float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight()}); @@ -474,6 +510,9 @@ private: Renderer renderer; EditorCamera* camera = new EditorCamera(); Mesh cube ; + bool SceneisFocused = false; + + void LoadLastOrEmptyProject() { // 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; - */ - - - } - }; \ No newline at end of file diff --git a/Editor/src/app.cpp b/Editor/src/app.cpp index 3ecb8c7..7227d54 100644 --- a/Editor/src/app.cpp +++ b/Editor/src/app.cpp @@ -11,43 +11,12 @@ public: void Run() override { + PushLayer(new EditorLayer()); - - // 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(); - + Application::Run(); } - - -private: - - - std::vector layers = std::vector(); - - }; YoggieEngine::Application* CreateApplication() { diff --git a/YoggieEngine/src/Application.cpp b/YoggieEngine/src/Application.cpp index f20d675..f88f7a7 100644 --- a/YoggieEngine/src/Application.cpp +++ b/YoggieEngine/src/Application.cpp @@ -50,12 +50,57 @@ namespace YoggieEngine { //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() { + + 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(); + } } diff --git a/YoggieEngine/src/Application.h b/YoggieEngine/src/Application.h index efbf677..d32f2a8 100644 --- a/YoggieEngine/src/Application.h +++ b/YoggieEngine/src/Application.h @@ -20,6 +20,7 @@ namespace YoggieEngine { void PushLayer(Layer* layer); + static void HandleKey(GLFWwindow* window, int key, int scancode, int action, int mods); protected: diff --git a/YoggieEngine/src/Layer.h b/YoggieEngine/src/Layer.h index adbdc65..e761056 100644 --- a/YoggieEngine/src/Layer.h +++ b/YoggieEngine/src/Layer.h @@ -1,5 +1,5 @@ #pragma once - +#include class Layer { public: @@ -12,12 +12,17 @@ public: virtual void OnUpdate(){} virtual void OnUI(){} + + virtual bool OnKey(int key , int status ) { + spdlog::info( "Key {0} , {1}", key, status); + return false; + } + + virtual void OnStartup(){} virtual void OnAttach() {} virtual void OnDetach() {} - - virtual void OnCreate() {} virtual void OnDestroy(){}