Added default widget drawer

This commit is contained in:
antopilo
2025-04-03 21:26:54 -04:00
parent 2df8cdf150
commit 709633dce6

View File

@@ -14,7 +14,11 @@ namespace Nuake
class WidgetDrawer
{
public:
WidgetDrawer() = default;
WidgetDrawer()
{
RegisterDefaulWidgets();
}
~WidgetDrawer() = default;
static WidgetDrawer& Get()
@@ -23,6 +27,17 @@ namespace Nuake
return instance;
}
void RegisterDefaulWidgets()
{
auto& drawer = Get();
drawer.RegisterTypeDrawer<float, &WidgetDrawer::DrawFloat>(&drawer);
drawer.RegisterTypeDrawer<bool, &WidgetDrawer::DrawBool>(&drawer);
drawer.RegisterTypeDrawer<std::string, &WidgetDrawer::DrawString>(&drawer);
drawer.RegisterTypeDrawer<Vector2, &WidgetDrawer::DrawVector2>(&drawer);
drawer.RegisterTypeDrawer<Vector3, &WidgetDrawer::DrawVector3>(&drawer);
drawer.RegisterTypeDrawer<DynamicItemList, &WidgetDrawer::DrawDynamicItemList>(&drawer);
}
void DrawFloat(entt::meta_data& type, entt::meta_any& instance)
{
float stepSize = 1.f;
@@ -62,6 +77,172 @@ namespace Nuake
}
}
void DrawBool(entt::meta_data& type, entt::meta_any& instance)
{
auto prop = type.prop(HashedName::DisplayName);
auto propVal = prop.value();
const char* displayName = *propVal.try_cast<const char*>();
if (displayName != nullptr)
{
ImGui::Text(displayName);
ImGui::TableNextColumn();
auto fieldVal = type.get(instance);
bool* boolPtr = fieldVal.try_cast<bool>();
if (boolPtr != nullptr)
{
bool boolProxy = *boolPtr;
std::string controlId = std::string("##") + displayName;
if (ImGui::Checkbox(controlId.c_str(), &boolProxy))
{
type.set(instance, boolProxy);
}
}
else
{
ImGui::Text("ERR");
}
}
}
void DrawString(entt::meta_data& type, entt::meta_any& instance)
{
auto prop = type.prop(HashedName::DisplayName);
auto propVal = prop.value();
const char* displayName = *propVal.try_cast<const char*>();
if (displayName != nullptr)
{
ImGui::Text(displayName);
ImGui::TableNextColumn();
auto fieldVal = type.get(instance);
std::string* fieldValPtr = fieldVal.try_cast<std::string>();
if (fieldValPtr != nullptr)
{
std::string fieldValProxy = *fieldValPtr;
std::string controlId = std::string("##") + displayName;
ImGui::InputText(controlId.c_str(), &fieldValProxy);
if (fieldValProxy != *fieldValPtr)
{
type.set(instance, fieldValProxy);
}
}
else
{
ImGui::Text("ERR");
}
}
}
void DrawVector2(entt::meta_data& field, entt::meta_any& component)
{
auto prop = field.prop(HashedName::DisplayName);
auto propVal = prop.value();
const char* displayName = *propVal.try_cast<const char*>();
if (displayName != nullptr)
{
ImGui::Text(displayName);
ImGui::TableNextColumn();
auto fieldVal = field.get(component);
Vector2* vec2Ptr = fieldVal.try_cast<Vector2>();
std::string controlId = std::string("##") + displayName;
ImGui::PushID(controlId.c_str());
if (ImGuiHelper::DrawVec2(controlId, vec2Ptr, 0.5f, 100.0, 0.01f))
{
field.set(component, *vec2Ptr);
Engine::GetProject()->IsDirty = true;
}
ImGui::PopID();
}
}
void DrawVector3(entt::meta_data& field, entt::meta_any& component)
{
auto prop = field.prop(HashedName::DisplayName);
auto propVal = prop.value();
const char* displayName = *propVal.try_cast<const char*>();
if (displayName != nullptr)
{
ImGui::Text(displayName);
ImGui::TableNextColumn();
auto fieldVal = field.get(component);
Vector3* vec3Ptr = fieldVal.try_cast<Vector3>();
std::string controlId = std::string("##") + displayName;
ImGui::PushID(controlId.c_str());
if (ImGuiHelper::DrawVec3(controlId, vec3Ptr, 0.5f, 100.0, 0.01f))
{
field.set(component, *vec3Ptr);
Engine::GetProject()->IsDirty = true;
}
ImGui::PopID();
}
}
void DrawDynamicItemList(entt::meta_data& field, entt::meta_any& component)
{
auto propDisplayName = field.prop(HashedName::DisplayName);
const char* displayName = *propDisplayName.value().try_cast<const char*>();
if (displayName != nullptr)
{
ImGui::Text(displayName);
ImGui::TableNextColumn();
auto fieldVal = field.get(component);
auto fieldValPtr = fieldVal.try_cast<DynamicItemList>();
if (fieldValPtr == nullptr)
{
ImGui::Text("ERR");
}
const auto& items = fieldValPtr->items;
const int index = fieldValPtr->index;
// Check first to see if we are within the bounds
std::string selectedStr = "";
if (index >= 0 || index < items.size())
{
selectedStr = items[index];
}
std::string controlName = std::string("##") + displayName;
if (ImGui::BeginCombo(controlName.c_str(), selectedStr.c_str()))
{
for (int i = 0; i < items.size(); i++)
{
bool isSelected = (index == i);
std::string name = items[i];
if (name.empty())
{
name = "Empty";
}
if (ImGui::Selectable(name.c_str(), isSelected))
{
field.set(component, i);
}
if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
}
}
void DrawWidget(entt::meta_data& dataType, entt::meta_any& instance)
{
entt::id_type dataId = dataType.type().id();