Adds VelocityComponent and CameraComponent

Adds instructions doc explaining the process of adding new components
master
Joey Pollack 3 years ago
parent 9e49450b0b
commit cd541e7d75

@ -0,0 +1,7 @@
STEPS FOR CREATING A NEW COMPONENT:
1) add the struct to components.h
2) add the gui code to component_guis.h
3) add the code to call the gui function to properties_view.cpp
4) add the code to add a new instance of the component to selected entity in properties_view's ADD_COMPONENT popup (using PRESENT_COMP_CHOICE)
5) add serialize/deserialize code to entity.cpp

@ -1,5 +1,6 @@
High Importance: High Importance:
✔ Add Component button on properties view no longer works @critical @done(22-09-07 13:44)
✔ AssetBrowser back button does not stop at the asset root directory @high @done(22-07-05 13:53) ✔ AssetBrowser back button does not stop at the asset root directory @high @done(22-07-05 13:53)
✔ Editor does not get absolute paths from the file browser - replace with NFD dialogs @critical @done(22-05-20 18:36) ✔ Editor does not get absolute paths from the file browser - replace with NFD dialogs @critical @done(22-05-20 18:36)
✔ The Map Editor does not get the tile maps when a project is opened @high @done (3/3/2022, 2:47:41 PM) ✔ The Map Editor does not get the tile maps when a project is opened @high @done (3/3/2022, 2:47:41 PM)

@ -28,6 +28,9 @@ namespace lunarium
bool operator==(const Vec2<T>& rhs) const { return (this->X == rhs.X && this->Y == rhs.Y); } bool operator==(const Vec2<T>& rhs) const { return (this->X == rhs.X && this->Y == rhs.Y); }
bool operator!=(const Vec2<T>& rhs) const { return !((*this) == rhs); } bool operator!=(const Vec2<T>& rhs) const { return !((*this) == rhs); }
Vec2() = default;
Vec2(const glm::vec3& rhs) : X(rhs.x), Y(rhs.y) {}
Vec2(T x, T y) : X(x), Y(y) {}
operator glm::vec3() { return glm::vec3(X, Y, 0.0f); } operator glm::vec3() { return glm::vec3(X, Y, 0.0f); }
}; };

@ -15,8 +15,9 @@ namespace lunarium
{ {
/// This function was taken from the Hazel engine written by Cherno! /// This function was taken from the Hazel engine written by Cherno!
/// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp /// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp
void ImGuiExt::Vec3Control(const std::string& label, glm::vec3& values, float resetValue, float columnWidth) bool ImGuiExt::Vec3Control(const std::string& label, glm::vec3& values, float resetValue, float columnWidth)
{ {
bool values_updated = false;
ImGui::PushID(label.c_str()); ImGui::PushID(label.c_str());
ImGui::Columns(2); ImGui::Columns(2);
@ -40,7 +41,7 @@ namespace lunarium
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
ImGui::SameLine(); ImGui::SameLine();
ImGui::DragFloat("##X", &values.x, 0.1f, 0.0f, 0.0f, "%.2f"); values_updated = ImGui::DragFloat("##X", &values.x, 0.1f, 0.0f, 0.0f, "%.2f") | values_updated;
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::SameLine(); ImGui::SameLine();
@ -54,7 +55,7 @@ namespace lunarium
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
ImGui::SameLine(); ImGui::SameLine();
ImGui::DragFloat("##Y", &values.y, 0.1f, 0.0f, 0.0f, "%.2f"); values_updated = ImGui::DragFloat("##Y", &values.y, 0.1f, 0.0f, 0.0f, "%.2f") | values_updated;
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::SameLine(); ImGui::SameLine();
@ -68,7 +69,7 @@ namespace lunarium
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
ImGui::SameLine(); ImGui::SameLine();
ImGui::DragFloat("##Z", &values.z, 0.1f, 0.0f, 0.0f, "%.2f"); values_updated = ImGui::DragFloat("##Z", &values.z, 0.1f, 0.0f, 0.0f, "%.2f") | values_updated;
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
@ -76,11 +77,14 @@ namespace lunarium
ImGui::Columns(1); ImGui::Columns(1);
ImGui::PopID(); ImGui::PopID();
return values_updated;
} }
void ImGuiExt::Vec2Control(const std::string& label, glm::vec3& values, float resetValue, float columnWidth) bool ImGuiExt::Vec2Control(const std::string& label, glm::vec3& values, float resetValue, float columnWidth)
{ {
bool values_updated = false;
ImGui::PushID(label.c_str()); ImGui::PushID(label.c_str());
ImGui::Columns(2); ImGui::Columns(2);
@ -104,7 +108,7 @@ namespace lunarium
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
ImGui::SameLine(); ImGui::SameLine();
ImGui::DragFloat("##X", &values.x, 0.1f, 0.0f, 0.0f, "%.2f"); values_updated = ImGui::DragFloat("##X", &values.x, 0.1f, 0.0f, 0.0f, "%.2f") | values_updated;
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::SameLine(); ImGui::SameLine();
@ -118,7 +122,7 @@ namespace lunarium
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
ImGui::SameLine(); ImGui::SameLine();
ImGui::DragFloat("##Y", &values.y, 0.1f, 0.0f, 0.0f, "%.2f"); values_updated = ImGui::DragFloat("##Y", &values.y, 0.1f, 0.0f, 0.0f, "%.2f") | values_updated;
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopStyleVar(); ImGui::PopStyleVar();
@ -126,11 +130,14 @@ namespace lunarium
ImGui::Columns(1); ImGui::Columns(1);
ImGui::PopID(); ImGui::PopID();
return values_updated;
} }
void ImGuiExt::FloatControl(const std::string& label, float& value, float resetValue, float columnWidth) bool ImGuiExt::FloatControl(const std::string& label, float& value, float resetValue, float columnWidth)
{ {
bool values_updated = false;
ImGui::PushID(label.c_str()); ImGui::PushID(label.c_str());
ImGui::Columns(2); ImGui::Columns(2);
@ -154,7 +161,7 @@ namespace lunarium
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
ImGui::SameLine(); ImGui::SameLine();
ImGui::DragFloat("##X", &value, 0.1f, 0.0f, 0.0f, "%.2f"); values_updated = ImGui::DragFloat("##X", &value, 0.1f, 0.0f, 0.0f, "%.2f");
ImGui::PopItemWidth(); ImGui::PopItemWidth();
@ -163,6 +170,7 @@ namespace lunarium
ImGui::Columns(1); ImGui::Columns(1);
ImGui::PopID(); ImGui::PopID();
return values_updated;
} }
/// Only works if this is the only item on the line /// Only works if this is the only item on the line

@ -21,9 +21,9 @@ namespace lunarium
/// This function was taken from the Hazel engine written by Cherno! /// This function was taken from the Hazel engine written by Cherno!
/// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp /// https://github.com/TheCherno/Hazel/blob/master/Hazelnut/src/Panels/SceneHierarchyPanel.cpp
static void Vec3Control(const std::string& label, glm::vec3& values, float resetValue = 0.0f, float columnWidth = 100.0f); static bool Vec3Control(const std::string& label, glm::vec3& values, float resetValue = 0.0f, float columnWidth = 100.0f);
static void Vec2Control(const std::string& label, glm::vec3& values, float resetValue = 0.0f, float columnWidth = 100.0f); static bool Vec2Control(const std::string& label, glm::vec3& values, float resetValue = 0.0f, float columnWidth = 100.0f);
static void FloatControl(const std::string& label, float& values, float resetValue = 0.0f, float columnWidth = 100.0f); static bool FloatControl(const std::string& label, float& values, float resetValue = 0.0f, float columnWidth = 100.0f);
static void TextCentered(const std::string text); static void TextCentered(const std::string text);
static bool ButtonCentered(const char* label, float alignment = 0.5f); static bool ButtonCentered(const char* label, float alignment = 0.5f);
}; };

@ -63,6 +63,21 @@ namespace lunarium
RecalculateView(); RecalculateView();
} }
Vec2f OrthographicCamera::GetPosition() const
{
return mPosition;
}
float OrthographicCamera::GetRotation() const
{
return mRotation;
}
Sizef OrthographicCamera::GetViewportSize() const
{
return mViewportSize;
}
void OrthographicCamera::RecalculateView() void OrthographicCamera::RecalculateView()
{ {
mProjection = glm::ortho(0.0f, mViewportSize.Width, mViewportSize.Height, 0.0f, -1.0f, 1.0f); mProjection = glm::ortho(0.0f, mViewportSize.Width, mViewportSize.Height, 0.0f, -1.0f, 1.0f);

@ -30,6 +30,10 @@ namespace lunarium
glm::mat4 GetViewProjection(); glm::mat4 GetViewProjection();
Sizef GetViewport() const; Sizef GetViewport() const;
Vec2f GetPosition() const;
float GetRotation() const;
Sizef GetViewportSize() const;
private: private:
void RecalculateView(); void RecalculateView();

@ -59,4 +59,36 @@ namespace lunarium { namespace editor
comp.Rotation = glm::radians(rotation); comp.Rotation = glm::radians(rotation);
ImGuiExt::Vec2Control("Scale", comp.Scale, 1.0f, 85.0f); ImGuiExt::Vec2Control("Scale", comp.Scale, 1.0f, 85.0f);
} }
void CompGui::RenderVelocityComp(VelocityComponent& comp)
{
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, ImGui::GetStyle().ItemSpacing.y + 10.0f));
DrawTitle("Velocity Component");
ImGui::PopStyleVar();
ImGuiExt::Vec2Control("Velocity", comp.Velocity, 0.0f, 85.0f);
}
void CompGui::RenderCameraComp(CameraComponent& comp)
{
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(ImGui::GetStyle().ItemSpacing.x, ImGui::GetStyle().ItemSpacing.y + 10.0f));
DrawTitle("Camera Component");
ImGui::PopStyleVar();
glm::vec3 pos = comp.Camera.GetPosition();
if (ImGuiExt::Vec2Control("Position", pos))
{
comp.Camera.SetPosition(pos);
}
float rot = comp.Camera.GetRotation();
ImGui::Text("Rotation Angle: ");
ImGui::SameLine();
if (ImGui::DragFloat("##rotation", &rot, 0.5f, 0.0f, 360.0f))
{
comp.Camera.SetRotation(rot);
}
}
}} }}

@ -18,6 +18,8 @@ namespace lunarium { namespace editor
public: public:
static void RenderTagComp(TagComponent& comp); static void RenderTagComp(TagComponent& comp);
static void RenderTransformComp(TransformComponent& comp); static void RenderTransformComp(TransformComponent& comp);
static void RenderVelocityComp(VelocityComponent& comp);
static void RenderCameraComp(CameraComponent& comp);
}; };
}} }}

@ -206,7 +206,7 @@ namespace editor
{ {
if (ImGui::Selectable((*iter)->GetFileLocation().stem().string().c_str())) if (ImGui::Selectable((*iter)->GetFileLocation().stem().string().c_str()))
{ {
// TODO: Show properties if this is new selection (meaning wasn't selected last frame) // Show properties if this is new selection (meaning wasn't selected last frame)
// Logger::Info(Editor::LogCat, "Asset selected. Properties shold show in the PropertiesView Panel"); // Logger::Info(Editor::LogCat, "Asset selected. Properties shold show in the PropertiesView Panel");
mpEditor->OnAssetSelected((*iter)); mpEditor->OnAssetSelected((*iter));
} }

@ -23,6 +23,10 @@ if (ImGui::Selectable(str_name)){\
return false;\ return false;\
}\ }\
pv->mpSelectedEntity->AddComponent<type_name>();\ pv->mpSelectedEntity->AddComponent<type_name>();\
if (!pv->mpSelectedEntity->HasComponent<type_name>()) {\
Logger::Error(Editor::LogCat, "Component not added to entity - unknown error");\
}\
Logger::Info(Editor::LogCat, "Component added to entity: %ll", pv->mpSelectedEntity->GetUUID());\
return false;\ return false;\
} }
@ -36,16 +40,22 @@ namespace lunarium { namespace editor
AddPopup(Popup::ADD_COMP, "Add Component", [](Panel* p) AddPopup(Popup::ADD_COMP, "Add Component", [](Panel* p)
{ {
PropertiesView* pv = (PropertiesView*)p; PropertiesView* pv = (PropertiesView*)p;
int is_hover = ImGui::IsWindowHovered();
// List components that can be added // List components that can be added
PRESENT_COMP_CHOICE("Tag Component", TagComponent, pv) PRESENT_COMP_CHOICE("Tag Component", TagComponent, pv)
PRESENT_COMP_CHOICE("Transform Component", TransformComponent, pv) PRESENT_COMP_CHOICE("Transform Component", TransformComponent, pv)
PRESENT_COMP_CHOICE("Velocity Component", VelocityComponent, pv)
PRESENT_COMP_CHOICE("Camera Component", CameraComponent, pv)
if ((ImGui::IsMouseClicked(ImGuiMouseButton_Left) && !ImGui::IsWindowHovered())) if ((ImGui::IsMouseClicked(ImGuiMouseButton_Left) && is_hover < 1))
{ {
return false; return false;
} }
// ImGui::Text("Is Window Hovered? %d", is_hover);
return true; return true;
}).LogIfFailed(Editor::LogCat); }).LogIfFailed(Editor::LogCat);
@ -144,6 +154,22 @@ namespace lunarium { namespace editor
ImGui::Separator(); ImGui::Separator();
} }
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 // After all components rendered
if (ImGuiExt::ButtonCentered("Add Component")) if (ImGuiExt::ButtonCentered("Add Component"))
{ {

@ -23,7 +23,7 @@ namespace lunarium { namespace editor
{ {
MapCanvas::MapCanvas(MapEditor* editor) MapCanvas::MapCanvas(MapEditor* editor)
: Panel("Map Canvas", PanelDockZone::DDZ_CENTER, true, ImGuiWindowFlags_NoScrollbar), : Panel("Map Canvas", PanelDockZone::DDZ_CENTER, true, ImGuiWindowFlags_NoScrollbar),
mpMapEditor(editor), mpCanvasImage(nullptr), mMap(nullptr), mSelectedTile({-1, -1}), mFrameBuffer(nullptr), mMapSizeChanged(false), mpMapEditor(editor), mpCanvasImage(nullptr), mMap(nullptr), mSelectedTile({-1, {0, 0}}), mFrameBuffer(nullptr), mMapSizeChanged(false),
mZoomFactor(1.0f), mScrollDragged(false), mCurrentRegion({0, 0}), mRegionString("") mZoomFactor(1.0f), mScrollDragged(false), mCurrentRegion({0, 0}), mRegionString("")
{ {
mRegionString = std::to_string(mCurrentRegion.X); mRegionString = std::to_string(mCurrentRegion.X);

@ -372,7 +372,7 @@ namespace lunarium
ImGui::SetNextItemWidth(65); ImGui::SetNextItemWidth(65);
if (ImGui::InputInt2(id.c_str(), v)) if (ImGui::InputInt2(id.c_str(), v))
{ {
mTestMap.SetTile(editor::TileRef {0, v[0], v[1] }, Vec2i { i, j }); mTestMap.SetTile(editor::TileRef {0, {v[0], v[1]} }, Vec2i { i, j });
} }
if (j + 1 < mTestMap.GetSizeInTiles().Height) if (j + 1 < mTestMap.GetSizeInTiles().Height)

@ -57,6 +57,14 @@ namespace lunarium
} }
}; };
struct VelocityComponent
{
glm::vec3 Velocity = { 0.0f, 0.0f, 0.0f};
VelocityComponent() = default;
VelocityComponent(const VelocityComponent&) = default;
};
struct CameraComponent struct CameraComponent
{ {
OrthographicCamera Camera; OrthographicCamera Camera;

@ -91,10 +91,45 @@ namespace lunarium
components.emplace_back(transform); components.emplace_back(transform);
} }
if (HasComponent<VelocityComponent>())
{
nlohmann::ordered_json velocity;
VelocityComponent& comp = GetComponent<VelocityComponent>();
velocity["type_name"] = "VelocityComponent";
auto& pos = velocity["velocity"];
pos["x"] = comp.Velocity.x;
pos["y"] = comp.Velocity.y;
pos["z"] = comp.Velocity.z;
components.emplace_back(velocity);
}
if (HasComponent<CameraComponent>())
{
nlohmann::ordered_json camera;
CameraComponent& comp = GetComponent<CameraComponent>();
camera["type_name"] = "CameraComponent";
auto& pos = camera["position"];
pos["x"] = comp.Camera.GetPosition().X;
pos["y"] = comp.Camera.GetPosition().Y;
pos["z"] = 0.0f;
auto& rot = camera["rotation"];
rot["degrees"] = comp.Camera.GetRotation();
auto& vpsize = camera["viewport_size"];
vpsize["width"] = comp.Camera.GetViewportSize().Width;
vpsize["height"] = comp.Camera.GetViewportSize().Height;
components.emplace_back(camera);
}
// TODO: ADD CODE TO SERIALIZE ANY NEW COMPONENTS // TODO: ADD CODE TO SERIALIZE ANY NEW COMPONENTS
// Children // Children
auto& children = node["children"]; auto& children = node["children"];
for (int i = 0; i < mChildren.size(); i++) for (int i = 0; i < mChildren.size(); i++)
@ -150,6 +185,38 @@ namespace lunarium
AddComponent<TransformComponent>(position, rotation, scale); AddComponent<TransformComponent>(position, rotation, scale);
} }
if ("VelocityComponent" == comp_type_name)
{
auto& vel = comp["velocity"];
float x = vel["x"].get<f32>();
float y = vel["y"].get<f32>();
float z = vel["z"].get<f32>();
glm::vec3 velocity(x, y, z);
AddComponent<VelocityComponent>(velocity);
}
if ("CameraComponent" == comp_type_name)
{
auto& pos = comp["position"];
float x = pos["x"].get<f32>();
float y = pos["y"].get<f32>();
float z = pos["z"].get<f32>();
glm::vec3 position(x, y, z);
auto& rot = comp["rotation"];
float deg = rot["degrees"];
auto& vps = comp["viewport_size"];
float width = vps["width"].get<f32>();
float height = vps["height"].get<f32>();
OrthographicCamera cam({x, y}, {width, height});
cam.SetRotation(deg);
AddComponent<CameraComponent>(cam);
}
} }

Loading…
Cancel
Save