diff --git a/docs/tasks/core.todo b/docs/tasks/core.todo index 665879a..237bb41 100644 --- a/docs/tasks/core.todo +++ b/docs/tasks/core.todo @@ -112,6 +112,7 @@ ECS: ✔ Transform @done(22-09-07 14:49) ✔ Velocity @done(22-09-07 14:49) ✔ Camera @done(22-09-07 14:49) + ✔ BlockOut @done(22-09-08 15:41) ☐ SpriteRenderer ☐ Animation Controller ☐ Script diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index 3b94bb3..940caa5 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -1,5 +1,6 @@ Editor: + ☐ Give Entities a name property separate from the tag component ☐ Add button to flip image assets vertically ☐ Design a custom editor style @low ☐ Save and unload world data when a new world is selected @@ -44,6 +45,9 @@ Editor: GUI Panels: World View: + ✔ Middle Mouse view dragging @done(22-09-08 15:45) + ✔ Render the current world and display on view panel @done(22-09-08 15:45) + World Hierarchy (Tree View): ☐ Handle showing Enities with children @high diff --git a/src/run_modes/editor/component_guis.cpp b/src/run_modes/editor/component_guis.cpp index 78eb144..a4df6ae 100644 --- a/src/run_modes/editor/component_guis.cpp +++ b/src/run_modes/editor/component_guis.cpp @@ -89,4 +89,23 @@ namespace lunarium { namespace editor comp.Camera.SetRotation(rot); } } + + + void CompGui::RenderBlockOutComp(BlockOutComponent& comp) + { + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, ImGui::GetStyle().ItemSpacing.y + 10.0f)); + DrawTitle("Block Out Component"); + ImGui::PopStyleVar(); + + ImGui::Text("Block Color: "); + ImGui::SameLine(); + ImGui::ColorEdit4("##BlockOut Color", &comp.Color.x, ImGuiColorEditFlags_DisplayRGB); + + glm::vec3 _size = {comp.Size.x, comp.Size.y, 0.0f }; + if (ImGuiExt::Vec2Control("Size", _size, 1.0f, 85.0f)) + { + comp.Size.x = _size.x; + comp.Size.y = _size.y; + } + } }} \ No newline at end of file diff --git a/src/run_modes/editor/component_guis.h b/src/run_modes/editor/component_guis.h index 71d96ac..01f69f9 100644 --- a/src/run_modes/editor/component_guis.h +++ b/src/run_modes/editor/component_guis.h @@ -20,6 +20,7 @@ namespace lunarium { namespace editor static void RenderTransformComp(TransformComponent& comp); static void RenderVelocityComp(VelocityComponent& comp); static void RenderCameraComp(CameraComponent& comp); + static void RenderBlockOutComp(BlockOutComponent& comp); }; }} diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index 5e9e48b..8be040e 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -468,6 +468,7 @@ namespace editor { // TODO: Unload current world, Load new world ((WorldTree*)mPanelManager.GetPanel(mPanels.WorldTree))->SetWorld(pWorld); + ((WorldView*)mPanelManager.GetPanel(mPanels.WorldView))->SetWorld(pWorld); } void Editor::OnAssetSelected(EditorAsset* pAsset) diff --git a/src/run_modes/editor/panels/properties_view.cpp b/src/run_modes/editor/panels/properties_view.cpp index ab8be25..a47c78c 100644 --- a/src/run_modes/editor/panels/properties_view.cpp +++ b/src/run_modes/editor/panels/properties_view.cpp @@ -17,6 +17,9 @@ #include #include +///////////////////////////////////////////////////////////////////// +// MACROS MACROS +///////////////////////////////////////////////////////////////////// #define PRESENT_COMP_CHOICE(str_name, type_name, pv) \ if (ImGui::Selectable(str_name)){\ if (pv->mpSelectedEntity->HasComponent()) {\ @@ -31,6 +34,19 @@ if (ImGui::Selectable(str_name)){\ return false;\ } +#define DRAW_COMP_GUI(type_name, func_name) \ +if (mpSelectedEntity->HasComponent()) \ + { \ + ImGui::PopStyleVar(); \ + CompGui::func_name(mpSelectedEntity->GetComponent()); \ + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y)); \ + ImGui::Separator(); \ + } + + +///////////////////////////////////////////////////////////////////// +// CLASS DEFINTION BEGIN +///////////////////////////////////////////////////////////////////// namespace lunarium { namespace editor { PropertiesView::PropertiesView(Editor* pEditor) @@ -48,6 +64,7 @@ namespace lunarium { namespace editor PRESENT_COMP_CHOICE("Transform Component", TransformComponent, pv) PRESENT_COMP_CHOICE("Velocity Component", VelocityComponent, pv) PRESENT_COMP_CHOICE("Camera Component", CameraComponent, pv) + PRESENT_COMP_CHOICE("Block Out Component", BlockOutComponent, pv) if ((ImGui::IsMouseClicked(ImGuiMouseButton_Left) && is_hover < 1)) @@ -147,29 +164,11 @@ namespace lunarium { namespace editor ImGui::Separator(); } - if (mpSelectedEntity->HasComponent()) - { - ImGui::PopStyleVar(); - CompGui::RenderTransformComp(mpSelectedEntity->GetComponent()); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y)); - ImGui::Separator(); - } + DRAW_COMP_GUI(TransformComponent, RenderTransformComp) + DRAW_COMP_GUI(VelocityComponent, RenderVelocityComp) + DRAW_COMP_GUI(CameraComponent, RenderCameraComp) + DRAW_COMP_GUI(BlockOutComponent, RenderBlockOutComp) - if (mpSelectedEntity->HasComponent()) - { - ImGui::PopStyleVar(); - CompGui::RenderVelocityComp(mpSelectedEntity->GetComponent()); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y)); - ImGui::Separator(); - } - - if (mpSelectedEntity->HasComponent()) - { - ImGui::PopStyleVar(); - CompGui::RenderCameraComp(mpSelectedEntity->GetComponent()); - ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y)); - ImGui::Separator(); - } // After all components rendered if (ImGuiExt::ButtonCentered("Add Component")) diff --git a/src/run_modes/editor/panels/world_view.cpp b/src/run_modes/editor/panels/world_view.cpp index 23c9e6d..078d24f 100644 --- a/src/run_modes/editor/panels/world_view.cpp +++ b/src/run_modes/editor/panels/world_view.cpp @@ -22,39 +22,52 @@ namespace lunarium { namespace editor { WorldView::WorldView() : Panel("World View", PanelDockZone::DDZ_CENTER, true, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar), mpWorld(nullptr), - mFrameBuffer(nullptr), mPrevWidth(0), mPrevHeight(0), mpCanvasImage(nullptr), mCamera(nullptr) + mFrameBuffer(nullptr), mWidth(0), mHeight(0), mNewViewSize(false), mIsWindowHovered(false), mpCanvasImage(nullptr), mEditorCamera(nullptr) { } + void WorldView::SetWorld(lunarium::World* pWorld) + { + mpWorld = pWorld; + } + + lunarium::World* WorldView::GetWorld() + { + return mpWorld; + } + void WorldView::Update(float delta) { // Need to remake the frame buffer if the window size has changed - int width, height; - GetSize(width, height); - - if ((width > 0 && height > 0) && (mPrevWidth != width || mPrevHeight != height)) + if (mNewViewSize) { - mPrevWidth = width; - mPrevHeight = height; + mNewViewSize = false; if (mFrameBuffer) { FrameBuffer::Destroy(&mFrameBuffer); - delete mCamera; } - mFrameBuffer = FrameBuffer::Create(width, height); - mCamera = new OrthographicCamera({ 0.0f, 0.0f }, { (float)width, (float)height }); + mFrameBuffer = FrameBuffer::Create(mWidth, mHeight); + Vec2f pos = (mEditorCamera ? mEditorCamera->GetPosition() : Vec2f {0.0f, 0.0f}); + delete mEditorCamera; + mEditorCamera = new OrthographicCamera(pos, { (float)mWidth, (float)mHeight }); } - // TODO: Handle view navigation input + // Handle view navigation input + if (ImGui::IsMouseDown(ImGuiMouseButton_Middle) && mIsWindowHovered) + { + + mEditorCamera->MoveLeft(-ImGui::GetIO().MouseDelta.x); + mEditorCamera->MoveUp(-ImGui::GetIO().MouseDelta.y); + } // Render the current state of the world - if (mpWorld) + if (mpWorld && mFrameBuffer) { mFrameBuffer->Bind(); - Core::Graphics().BeginDraw(mCamera); + Core::Graphics().BeginDraw(mEditorCamera); mpWorld->Render(&Core::Graphics()); Core::Graphics().EndDraw(); mFrameBuffer->Unbind(); @@ -65,6 +78,22 @@ namespace lunarium { namespace editor void WorldView::DoFrame() { // TODO: Draw toolbar + ImVec2 window_size = ImGui::GetWindowSize(); + + float child_height = ImGui::GetFrameHeight() * 2; + ImGui::BeginChild("World View Toolbar", ImVec2(ImGui::GetWindowSize().x, child_height), true); + DoToolBar(); + ImGui::EndChild(); + ImGui::BeginChild("World View", ImVec2(ImGui::GetWindowSize().x, ImGui::GetWindowSize().y - child_height), true); + + if (mWidth != ImGui::GetWindowSize().x || mHeight != ImGui::GetWindowSize().y) + { + mWidth = ImGui::GetWindowSize().x; + mHeight = ImGui::GetWindowSize().y; + mNewViewSize = true; + } + + mIsWindowHovered = ImGui::IsWindowHovered(); // Draw world render if (mpCanvasImage) @@ -72,5 +101,13 @@ namespace lunarium { namespace editor ImGui::Image((ImTextureID)mpCanvasImage->GetGLID64(), ImVec2(mpCanvasImage->GetWidth(), mpCanvasImage->GetHeight()), ImVec2(0, 1), ImVec2(1, 0)); } + + ImGui::EndChild(); + } + + + void WorldView::DoToolBar() + { + } }} \ No newline at end of file diff --git a/src/run_modes/editor/panels/world_view.h b/src/run_modes/editor/panels/world_view.h index 4997dde..dc4cd1c 100644 --- a/src/run_modes/editor/panels/world_view.h +++ b/src/run_modes/editor/panels/world_view.h @@ -29,17 +29,22 @@ namespace editor void Update(float delta) override; - void SetWorld(World* pWorld); - World* GetWorld(); + void SetWorld(lunarium::World* pWorld); + lunarium::World* GetWorld(); private: lunarium::World* mpWorld; Editor* mpEditor; - int mPrevWidth; - int mPrevHeight; + int mWidth; + int mHeight; + bool mNewViewSize; + bool mIsWindowHovered; lunarium::FrameBuffer* mFrameBuffer; - lunarium::OrthographicCamera* mCamera; + lunarium::OrthographicCamera* mEditorCamera; lunarium::Texture* mpCanvasImage; + + private: // HELPERS + void DoToolBar(); }; }} // lunarium::editor diff --git a/src/world/components.h b/src/world/components.h index b1df777..2069f3e 100644 --- a/src/world/components.h +++ b/src/world/components.h @@ -72,6 +72,15 @@ namespace lunarium CameraComponent() = default; CameraComponent(const CameraComponent&) = default; }; + + struct BlockOutComponent + { + glm::vec4 Color; + glm::vec2 Size; + + BlockOutComponent() = default; + BlockOutComponent(const BlockOutComponent&) = default; + }; } #endif // LUNARIUM_COMPONENTS_H_ \ No newline at end of file diff --git a/src/world/entity.cpp b/src/world/entity.cpp index 172cdeb..1e6e413 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -126,6 +126,25 @@ namespace lunarium components.emplace_back(camera); } + if (HasComponent()) + { + nlohmann::ordered_json blockout; + BlockOutComponent& comp = GetComponent(); + blockout["type_name"] = "BlockOutComponent"; + + auto& color = blockout["color"]; + color["r"] = comp.Color.x; + color["g"] = comp.Color.y; + color["b"] = comp.Color.a; + color["a"] = comp.Color.w; + + auto& size = blockout["size"]; + size["width"] = comp.Size.x; + size["height"] = comp.Size.y; + + components.emplace_back(blockout); + } + // TODO: ADD CODE TO SERIALIZE ANY NEW COMPONENTS @@ -217,10 +236,23 @@ namespace lunarium AddComponent(cam); } + + + if ("BlockOutComponent" == comp_type_name) + { + auto& color = comp["color"]; + glm::vec4 Color(color["r"].get(), color["g"].get(), color["b"].get(), color["a"].get()); + + auto& size = comp["size"]; + glm::vec2 Size(size["width"].get(), size["height"].get()); + + AddComponent(Color, Size); + } + + // TODO: ADD CODE TO DESERIALIZE ANY NEW COMPONENTS } - // TODO: ADD CODE TO DESERIALIZE ANY NEW COMPONENTS // TODO: Load children auto& children = node["children"]; diff --git a/src/world/world.cpp b/src/world/world.cpp index 20489aa..2b74c19 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -16,6 +16,7 @@ #include #include #include "entity.h" +#include "components.h" namespace lunarium { @@ -40,23 +41,41 @@ namespace lunarium { // TODO: Call OnUnLoad in the world script and on each region script } + + void World::SetRunMode(RunMode mode) + { + mMode = mode; + } + + World::RunMode World::GetRunMode() + { + return mMode; + } void World::Update(float dt) { // TODO: Update all entities + } void World::Render(lunarium::Renderer2D* pGraphics) const { - OrthographicCamera* pCam = mpActiveCamera; - - if (!pCam) + // switch (mMode) + // { + // case RunMode::EDITOR: RenderEditor(pGraphics); break; + // } + + + // Draw the Renderables that also have a transform + // Code from: + // https://github.com/skypjack/entt/wiki/Crash-Course:-entity-component-system#views-and-groups + mECSRegistry.view().each([&](auto entity, auto &transform, auto &blockout) { - // Get a camera component from an entity - } + Rectangle rect(transform.Position.x, transform.Position.y, blockout.Size.x, blockout.Size.y); + Color color(blockout.Color.x, blockout.Color.y, blockout.Color.z, blockout.Color.w); + pGraphics->DrawQuad(rect, color); + }); - // Get ViewProject matrix - // Render each entity that has a renderable component and a transform } @@ -110,6 +129,15 @@ namespace lunarium return iter == mEntities.end(); } + ///////////////////////////////////////////////////////////////////// + // RUN MODE HELPERS + ///////////////////////////////////////////////////////////////////// + + void World::RenderEditor(lunarium::Renderer2D* pGraphics) const + { + // NOTE: MAY BE REMOVED + } + ///////////////////////////////////////////////////////////////////// // SERIALIZING ///////////////////////////////////////////////////////////////////// diff --git a/src/world/world.h b/src/world/world.h index 1b09011..c78f4bf 100644 --- a/src/world/world.h +++ b/src/world/world.h @@ -51,6 +51,15 @@ namespace lunarium std::vector mLayers; }; + // NOTE: This RunMode may not be necessary. + enum class RunMode + { + EDITOR, + EDITOR_PLAY, + PLAY, + PAUSE, + }; + public: // INTERFACE //World(Camera* pCam, Sizei region_size, Sizei world_size); @@ -62,6 +71,8 @@ namespace lunarium void Render(lunarium::Renderer2D* pGraphics) const; void SetActiveCamera(OrthographicCamera* pCam); + void SetRunMode(RunMode mode); + RunMode GetRunMode(); entt::registry* GetEntityRegistry(); LUUID CreateEntity(); @@ -82,6 +93,7 @@ namespace lunarium std::string mName; entt::registry mECSRegistry; std::vector mEntities; + RunMode mMode; OrthographicCamera* mpActiveCamera; @@ -95,7 +107,8 @@ namespace lunarium // TEST STUFF std::map mEntitiesByUUID; - + private: // HELPERS + void RenderEditor(lunarium::Renderer2D* pGraphics) const; }; }