From cd4f23b732ba3f9efa11b71b0399d0a1e712b207 Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Tue, 2 Nov 2021 14:31:59 -0400 Subject: [PATCH] Main editor window working --- CMakeLists.txt | 5 +- docs/editor.todo | 4 +- docs/game.todo | 1 + src/core/core.cpp | 52 ++++++++++------- src/run_modes/editor/CMakeLists.txt | 11 ++++ src/run_modes/editor/editor.cpp | 64 +++++++++++++++++++++ src/run_modes/editor/editor.h | 40 +++++++++++++ src/run_modes/editor/panels/iPanel.cpp | 59 +++++++++++++++++++ src/run_modes/editor/panels/iPanel.h | 46 +++++++++++++++ src/run_modes/editor/panels/mainPanel.cpp | 69 +++++++++++++++++++++++ src/run_modes/editor/panels/mainPanel.h | 43 ++++++++++++++ src/window/window.cpp | 17 ++++++ src/window/window.h | 3 + test_data/engine_state.xml | 2 +- 14 files changed, 391 insertions(+), 25 deletions(-) create mode 100644 src/run_modes/editor/CMakeLists.txt create mode 100644 src/run_modes/editor/panels/iPanel.cpp create mode 100644 src/run_modes/editor/panels/iPanel.h create mode 100644 src/run_modes/editor/panels/mainPanel.cpp create mode 100644 src/run_modes/editor/panels/mainPanel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ef5c30..2003e5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,6 +119,9 @@ add_subdirectory(external/box2d) # add run mode tester add_subdirectory(src/run_modes/tester) +# add run mode editor +add_subdirectory(src/run_modes/editor) + target_include_directories(${PROJECT_NAME} PUBLIC "${PROJECT_BINARY_DIR}" PUBLIC src @@ -146,7 +149,7 @@ target_link_directories(${PROJECT_NAME} PRIVATE external/box2d/src ) -target_link_libraries(${PROJECT_NAME} glfw glad glm dearimgui utils assets lua_static pugixml freetype box2d tester) +target_link_libraries(${PROJECT_NAME} glfw glad glm dearimgui utils assets lua_static pugixml freetype box2d tester editor) if(WIN32) target_link_libraries(${PROJECT_NAME} opengl32.lib) diff --git a/docs/editor.todo b/docs/editor.todo index 67d9975..5f3453f 100644 --- a/docs/editor.todo +++ b/docs/editor.todo @@ -1,7 +1,7 @@ -Editor: @high +Editor: ✔ Come up with project directory structure @done (9/17/2021, 6:46:44 PM) - ☐ Make the editor a separate module @high + ✔ Make the editor a separate module @high @done (11/1/2021, 2:24:35 PM) ☐ Implement Run Mode interface class @high ☐ Reference raw asset files in a "content" folder@high ☐ Platform independant file browsing diff --git a/docs/game.todo b/docs/game.todo index 787dda3..5e372cc 100644 --- a/docs/game.todo +++ b/docs/game.todo @@ -27,6 +27,7 @@ Game: ☐ Collider (maybe via Box2D?) ☐ Script ☐ RigidBody (via Box2D) + ☐ Audio Listener Animations: ☐ Animated Sprite clas \ No newline at end of file diff --git a/src/core/core.cpp b/src/core/core.cpp index 33c9fd1..422a3a1 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -14,6 +14,7 @@ // Run modes #include +#include // Sub Systems #include @@ -62,6 +63,11 @@ namespace lunarium &mpInstance->mState.Display.WindowStartPosition.Y); mpInstance->mState.SaveToFile("lunarium_state.xml"); + + // Run Mode shuts down first + mpInstance->mpRunMode->Shutdown(); + delete mpInstance->mpRunMode; + mpInstance->mpRunMode = nullptr; // Shutdown subsystems CoreAPI::FreeInstance(); @@ -86,10 +92,6 @@ namespace lunarium delete mpInstance->mpArgs; mpInstance->mpArgs = nullptr; - - mpInstance->mpRunMode->Shutdown(); - delete mpInstance->mpRunMode; - mpInstance->mpRunMode = nullptr; mpInstance->mbIsInit = false; delete mpInstance; @@ -102,6 +104,11 @@ namespace lunarium //////////////////////////////////////////////////////////// void Core::Initialize(int argc, char** argv) { + if (mbIsInit) + { + return; + } + // Setup the log system mMasterLogFile.open("Lunarium_Master.log", std::ios_base::app); mMasterLogFile << "\n\n"; @@ -139,23 +146,6 @@ namespace lunarium Logger::Log(LogCategory::CORE, LogLevel::INFO, "Loaded state file: engine_state.xml"); } - // RUN MODE - const char* types[] = { "game", "editor", "test" }; - Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running in mode: %s", types[mState.Mode]); - if (RunMode::MODE_TEST == mState.Mode) - { - mpRunMode = new Tester; - } - - // Initialize the Run Mode - result = mpRunMode->Initialize(); - if (Failed(result)) - { - Logger::Log(LogCategory::CORE, LogLevel::FATAL_ERROR, - "Could not initialize the Run Mode: %s", result.Description.c_str()); - return; - } - // Parse command line args -- None right now std::vector sd; mpArgs = new Args(argc, argv, '-', sd); @@ -232,6 +222,26 @@ namespace lunarium "Could not initialized the LUA Core API: %s", result.Description); } +// RUN MODE + const char* types[] = { "game", "editor", "test" }; + Logger::Log(LogCategory::CORE, LogLevel::INFO, "Running in mode: %s", types[mState.Mode]); + if (RunMode::MODE_TEST == mState.Mode) + { + mpRunMode = new Tester; + } + else if (RunMode::MODE_EDITOR == mState.Mode) + { + mpRunMode = new Editor; + } + + // Initialize the Run Mode + result = mpRunMode->Initialize(); + if (Failed(result)) + { + Logger::Log(LogCategory::CORE, LogLevel::FATAL_ERROR, + "Could not initialize the Run Mode: %s", result.Description.c_str()); + return; + } mbIsInit = true; } diff --git a/src/run_modes/editor/CMakeLists.txt b/src/run_modes/editor/CMakeLists.txt new file mode 100644 index 0000000..123a2ab --- /dev/null +++ b/src/run_modes/editor/CMakeLists.txt @@ -0,0 +1,11 @@ +add_library(editor editor.cpp panels/iPanel.cpp panels/mainPanel.cpp) + +target_include_directories(editor + PUBLIC "${PROJECT_BINARY_DIR}" + PUBLIC ../../ + PUBLIC ../../internal_libs + PUBLIC ../../../external/glm + PUBLIC ../../../external/glad/include + PUBLIC ../../../external/glfw/include + PUBLIC ../../../external/box2d/include +) \ No newline at end of file diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index e69de29..aade936 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -0,0 +1,64 @@ +/****************************************************************************** +* File - editor.cpp +* Author - Joey Pollack +* Date - 2021/11/01 (y/m/d) +* Mod Date - 2021/11/01 (y/m/d) +* Description - Entry point for the editor run mode. +******************************************************************************/ + +#include "editor.h" + +#include "panels/mainPanel.h" +#include +#include + +namespace lunarium +{ + Editor::Editor() + : mLogCat(-1), mpMainPanel(nullptr) + { + + } + + OpRes Editor::Initialize() + { + //Core::MainWindow().Hide(); + + mLogCat = Logger::RegisterCategory("EDITOR"); + mpMainPanel = &MainPanel::GetInstance(); + mpMainPanel->SetEditor(this); + + return OpRes::OK(); + } + + void Editor::Shutdown() + { + MainPanel::FreeInstance(); + } + + void Editor::OnTick(double delta) + { + int x, y, w, h; + mpMainPanel->GetPosition(x, y); + mpMainPanel->GetSize(w, h); + + if (w > 0 && h > 0) + { + // NOTE TODO: Hardcoding fullscreen to false! + Core::MainWindow().ChangeDisplayMode(false, x, y, w, h); + } + } + + void Editor::OnRender(IGraphics* g) + { + if (!mpMainPanel->DoFrame()) + { + Core::GetInstance().SignalShutdown(); + } + } + + uint32_t Editor::GetLogCat() const + { + return mLogCat; + } +} \ No newline at end of file diff --git a/src/run_modes/editor/editor.h b/src/run_modes/editor/editor.h index e69de29..6f0e9db 100644 --- a/src/run_modes/editor/editor.h +++ b/src/run_modes/editor/editor.h @@ -0,0 +1,40 @@ +/****************************************************************************** +* File - editor.h +* Author - Joey Pollack +* Date - 2021/11/01 (y/m/d) +* Mod Date - 2021/11/01 (y/m/d) +* Description - Entry point for the editor run mode. +******************************************************************************/ + +#ifndef EDITOR_H_ +#define EDITOR_H_ + +#include +#include + +namespace lunarium +{ + class MainPanel; + class Editor : public iRunMode + { + public: + Editor(); + OpRes Initialize(); + void Shutdown(); + void OnTick(double delta); + void OnRender(IGraphics* g); + + + uint32_t GetLogCat() const; + + private: + Editor(const Editor&) = delete; + const Editor& operator=(const Editor&) = delete; + + private: // Data + uint32_t mLogCat; + MainPanel* mpMainPanel; + }; +} + +#endif // EDITOR_H_ \ No newline at end of file diff --git a/src/run_modes/editor/panels/iPanel.cpp b/src/run_modes/editor/panels/iPanel.cpp new file mode 100644 index 0000000..6f74a24 --- /dev/null +++ b/src/run_modes/editor/panels/iPanel.cpp @@ -0,0 +1,59 @@ +/****************************************************************************** +* File - iPanel.cpp +* Author - Joey Pollack +* Date - 2021/11/01 (y/m/d) +* Mod Date - 2021/11/01 (y/m/d) +* Description - Base class for all editor panels +******************************************************************************/ + +#include "iPanel.h" + +#include + +namespace lunarium +{ + Panel::Panel(PanelType type, bool isOpen) + : mType(type), mIsOpen(isOpen) + { + + } + + PanelType Panel::GetType() const + { + return mType; + } + + void Panel::SetOpen(bool isOpen) + { + mIsOpen = isOpen; + } + + bool Panel::IsOpen() + { + return mIsOpen; + } + + + void Panel::GetPosition(int& x, int& y) const + { + x = mX; + y = mY; + } + + void Panel::GetSize(int& w, int& h) const + { + w = mWidth; + h = mHeight; + } + + void Panel::UpdateMetaInfo() + { + ImVec2 p = ImGui::GetWindowPos(); + ImVec2 s = ImGui::GetWindowSize(); + + mX = p.x; + mY = p.y; + mWidth = s.x; + mHeight = s.y; + } +} \ No newline at end of file diff --git a/src/run_modes/editor/panels/iPanel.h b/src/run_modes/editor/panels/iPanel.h new file mode 100644 index 0000000..daa1c19 --- /dev/null +++ b/src/run_modes/editor/panels/iPanel.h @@ -0,0 +1,46 @@ +/****************************************************************************** +* File - iPanel.h +* Author - Joey Pollack +* Date - 2021/11/01 (y/m/d) +* Mod Date - 2021/11/01 (y/m/d) +* Description - Base class for all editor panels +******************************************************************************/ + +#ifndef PANEL_H_ +#define PANEL_H_ + +namespace lunarium +{ + enum PanelType + { + PT_MAIN, + PT_UNKNOWN, + }; + + class Panel + { + public: + Panel(PanelType type, bool isOpen = false); + PanelType GetType() const; + + virtual bool DoFrame() = 0; + void SetOpen(bool isOpen); + bool IsOpen(); + void GetPosition(int& x, int& y) const; + void GetSize(int& w, int& h) const; + + protected: + PanelType mType; + bool mIsOpen; + + int mX; + int mY; + int mWidth; + int mHeight; + + protected: + void UpdateMetaInfo(); + }; +} + +#endif // PANEL_H_ \ No newline at end of file diff --git a/src/run_modes/editor/panels/mainPanel.cpp b/src/run_modes/editor/panels/mainPanel.cpp new file mode 100644 index 0000000..cff7aa8 --- /dev/null +++ b/src/run_modes/editor/panels/mainPanel.cpp @@ -0,0 +1,69 @@ +/****************************************************************************** +* File - mainPanel.cpp +* Author - Joey Pollack +* Date - 2021/11/01 (y/m/d) +* Mod Date - 2021/11/01 (y/m/d) +* Description - The main window for the editor. +******************************************************************************/ + +#include "mainPanel.h" + +#include "../editor.h" +#include +#include +#include + +namespace lunarium +{ + MainPanel* MainPanel::mpInstance = nullptr; + MainPanel::MainPanel() + : Panel(PT_MAIN, true), mpEditor(nullptr) + { + + Core::MainWindow().GetFramebufferSize(&mStartWidth, &mStartHeight); + Core::MainWindow().GetPosition(&mStartX, &mStartY); + } + + + MainPanel& MainPanel::GetInstance() + { + if (!mpInstance) + { + mpInstance = new MainPanel; + } + + return *mpInstance; + } + + + void MainPanel::FreeInstance() + { + delete mpInstance; + mpInstance = nullptr; + } + + void MainPanel::SetEditor(Editor* e) + { + mpEditor = e; + } + + bool MainPanel::DoFrame() + { + if (!mIsOpen) + return false; + ImGui::SetNextWindowSize(ImVec2(mStartWidth, mStartHeight), ImGuiCond_Appearing); + ImGui::SetNextWindowPos(ImVec2(mStartX, mStartY), ImGuiCond_Appearing); + if (!ImGui::Begin("Lunarium Editor", &mIsOpen, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_DockNodeHost)) + { + ImGui::End(); + return false; + } + + // Logger::Log(mpEditor->GetLogCat(), LogLevel::INFO, "EDITOR WINDOW SHOWING"); + + // NOTE: Must always update these values! + Panel::UpdateMetaInfo(); + ImGui::End(); + return true; + } +} \ No newline at end of file diff --git a/src/run_modes/editor/panels/mainPanel.h b/src/run_modes/editor/panels/mainPanel.h new file mode 100644 index 0000000..3c9cd40 --- /dev/null +++ b/src/run_modes/editor/panels/mainPanel.h @@ -0,0 +1,43 @@ +/****************************************************************************** +* File - mainPanel.h +* Author - Joey Pollack +* Date - 2021/11/01 (y/m/d) +* Mod Date - 2021/11/01 (y/m/d) +* Description - The main window for the editor. +******************************************************************************/ + +#ifndef PANEL_MAIN_H_ +#define PANEL_MAIN_H_ + +#include "iPanel.h" + +namespace lunarium +{ + class Editor; + class MainPanel : public Panel + { + public: + MainPanel(); + static MainPanel& GetInstance(); + static void FreeInstance(); + + void SetEditor(Editor* e); + + // Returns false if the window is closed + bool DoFrame(); + + private: + static MainPanel* mpInstance; + MainPanel(const MainPanel&) = delete; + MainPanel& operator=(const MainPanel&) = delete; + + private: + Editor* mpEditor; + int mStartWidth; + int mStartHeight; + int mStartX; + int mStartY; + }; +} + +#endif // PANEL_MAIN_H_ \ No newline at end of file diff --git a/src/window/window.cpp b/src/window/window.cpp index 3803bb8..41d4129 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -56,6 +56,11 @@ namespace lunarium Logger::Log(LogCategory::GRAPHICS, LogLevel::INFO, "Creating OpenGL Context version %d, %d and glsl %s", OPENGL_MAJOR_VERSION, OPENGL_MINOR_VERSION, System::GetGLSLVersionString().c_str()); + + if (state.Mode == RunMode::MODE_EDITOR) + { + glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); + } mpWindow = glfwCreateWindow( state.Display.WindowedSize.Width, state.Display.WindowedSize.Height, "Lunarium", NULL, NULL ); if (!mpWindow) @@ -68,6 +73,7 @@ namespace lunarium return OpRes::Fail(oss.str().c_str()); } + glfwSetWindowPos(mpWindow, state.Display.WindowStartPosition.X, state.Display.WindowStartPosition.Y); glfwMakeContextCurrent(mpWindow); @@ -109,6 +115,17 @@ namespace lunarium return { (float)x, (float)y }; } + + void Window::Hide() + { + glfwHideWindow(mpWindow); + } + + void Window::Show() + { + glfwShowWindow(mpWindow); + } + void Window::SetCursorPos(glm::vec2 pos) { glfwSetCursorPos(mpWindow, pos.x, pos.y); diff --git a/src/window/window.h b/src/window/window.h index afa3bf3..25d9da6 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -40,6 +40,9 @@ namespace lunarium void GetFramebufferSize(int* width, int* height) const; void GetPosition(int* x, int* y) const; + void Hide(); + void Show(); + void SetShouldCloseFlag(bool should_close); bool ShouldWindowClose() const; static void PollEvents(); diff --git a/test_data/engine_state.xml b/test_data/engine_state.xml index 5cf7851..277eb02 100644 --- a/test_data/engine_state.xml +++ b/test_data/engine_state.xml @@ -1,6 +1,6 @@ data/ - +