1.1 Making a new Component - quesswho/Dodo GitHub Wiki
Making a new Component
To add a new component to the editor you must follow all the steps except the optional addition to the Scene::Draw() function.
1. Make a new Componentflag in src/Core/Graphics/Scene/Component/Components.h.
- It must follow according to the name
ComponentFlag_XXX
- It must be added under the newest component
2. Create the new component class in src/Core/Graphics/Scene/Component
- Must be in the Dodo namespace
- It must contain the following:
#include "Components.h"
#include "Core/Math/MathFunc.h"
namespace Dodo {
class XXX
{
XXX(xxx);
~XXX();
static inline bool IsDrawable() { return /*true*/ /*false*/; }
static inline const std::string& GetName() { return std::string("XXX"); }
static constexpr ComponentFlag GetFlagType() { return ComponentFlag::ComponentFlag_XXX; }
static constexpr int GetIndex() { return Math::floorlog2(GetFlagType()); }
}
}
3. In Dodo/src/Core/Graphics/Scene/Entity.h add a new type template
using ComponentType = std::variant<ModelComponent*, Rectangle2DComponent*, ..., XXX*, std::monostate>;
4. Dodo/src/Core/Graphics/Scene/Scene.cpp
- In Scene::Draw() inside the switch statement
case n: // n is the same as GetIndex() in the component
std::get<n>(drawable)->Draw(m_Camera);
break;
5. Dodo/src/Core/Data/AsciiSceneFile.cpp
- In AsciiSceneFile::Write() under the components section
case n: // n is same as GetIndex() in the component
auto comp = std::get<n>(varcomp);
m_File.CreateSection("XXX");
// Additional data required from the component
m_File.UnIndent();
break;
- In AsciiSceneFile::Read() under the lastest component
else if (section == "XXX")
{
result->AddComponent(id, new XXX(X); // Each component implementation can be different therefore check the implementations of other components.
}
6. Dodoeditor/src/Interface.cpp
- In InitInterface()
m_HierarchyComponents.push_back(Component("XXX"));
- In DrawHierarchy() under
if (ent.second.m_ComponentFlags != ComponentFlag_None)
if (ent.second.m_ComponentFlags & ComponentFlag_XXX)
{
ImGui::BulletText("XXX");
}
- In DrawInspector()
if (ent.m_ComponentFlags & ComponentFlag_XXX)
{
if (ImGui::TreeNode("XXX"))
{
auto& compvar = ent.FindComponent(XXX::GetIndex());
if (compvar.index() == XXX::GetIndex())
{
XXX* comp = std::get<XXX::GetIndex()>(compvar);
// Display all the properties of the component such as position, rotation, etc
}
else
{
// The component has not been created. This is useful because you can, for example, use a browse option before creating the component
if (ImGui::Button("Browse")) {
std::string str = Application::s_Application->m_Window->OpenFileSelector(X); // Replace X with what is desired. Two examples are "Model\0*.fbx;*.obj\0" & "Texture\0*.png;*.jpg;*.tga\0"
if (str != "")
{
if (str._Starts_with(Application::s_Application->m_Window->GetMainWorkDirectory()))
str.erase(0, Application::s_Application->m_Window->GetMainWorkDirectory().length() + 1);
m_Scene->AddComponent(e.first, new XXX(str.c_str()));
}
}
ImGui::SameLine();
ImGui::Text("...");
}
ImGui::TreePop();
}
}