Adds BlockOutComponent for graphically representing an entity with a simple quad.

WorldView preview drawing works!
Middle Mouse drag to move works for WorldView
master
Joey Pollack 3 years ago
parent 73f2e06541
commit c330319ad2

@ -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

@ -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

@ -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;
}
}
}}

@ -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);
};
}}

@ -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)

@ -17,6 +17,9 @@
#include <editor/contents/editor_asset.h>
#include <editor/component_guis.h>
/////////////////////////////////////////////////////////////////////
// MACROS MACROS
/////////////////////////////////////////////////////////////////////
#define PRESENT_COMP_CHOICE(str_name, type_name, pv) \
if (ImGui::Selectable(str_name)){\
if (pv->mpSelectedEntity->HasComponent<type_name>()) {\
@ -31,6 +34,19 @@ if (ImGui::Selectable(str_name)){\
return false;\
}
#define DRAW_COMP_GUI(type_name, func_name) \
if (mpSelectedEntity->HasComponent<type_name>()) \
{ \
ImGui::PopStyleVar(); \
CompGui::func_name(mpSelectedEntity->GetComponent<type_name>()); \
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<TransformComponent>())
{
ImGui::PopStyleVar();
CompGui::RenderTransformComp(mpSelectedEntity->GetComponent<TransformComponent>());
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<VelocityComponent>())
{
ImGui::PopStyleVar();
CompGui::RenderVelocityComp(mpSelectedEntity->GetComponent<VelocityComponent>());
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y));
ImGui::Separator();
}
if (mpSelectedEntity->HasComponent<CameraComponent>())
{
ImGui::PopStyleVar();
CompGui::RenderCameraComp(mpSelectedEntity->GetComponent<CameraComponent>());
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, spacing_y));
ImGui::Separator();
}
// After all components rendered
if (ImGuiExt::ButtonCentered("Add Component"))

@ -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()
{
}
}}

@ -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

@ -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_

@ -126,6 +126,25 @@ namespace lunarium
components.emplace_back(camera);
}
if (HasComponent<BlockOutComponent>())
{
nlohmann::ordered_json blockout;
BlockOutComponent& comp = GetComponent<BlockOutComponent>();
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<CameraComponent>(cam);
}
if ("BlockOutComponent" == comp_type_name)
{
auto& color = comp["color"];
glm::vec4 Color(color["r"].get<f32>(), color["g"].get<f32>(), color["b"].get<f32>(), color["a"].get<f32>());
auto& size = comp["size"];
glm::vec2 Size(size["width"].get<f32>(), size["height"].get<f32>());
AddComponent<BlockOutComponent>(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"];

@ -16,6 +16,7 @@
#include <renderer/renderer2D.h>
#include <renderer/orthographic_camera.h>
#include "entity.h"
#include "components.h"
namespace lunarium
{
@ -41,22 +42,40 @@ 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;
// switch (mMode)
// {
// case RunMode::EDITOR: RenderEditor(pGraphics); break;
// }
if (!pCam)
// 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<TransformComponent, BlockOutComponent>().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
/////////////////////////////////////////////////////////////////////

@ -51,6 +51,15 @@ namespace lunarium
std::vector<lunarium::Image*> 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<Entity*> mEntities;
RunMode mMode;
OrthographicCamera* mpActiveCamera;
@ -95,7 +107,8 @@ namespace lunarium
// TEST STUFF
std::map<LUUID, Entity*> mEntitiesByUUID;
private: // HELPERS
void RenderEditor(lunarium::Renderer2D* pGraphics) const;
};
}

Loading…
Cancel
Save