Skip to content

Commit

Permalink
feat(imgui): move BeginTable inside DataInspector
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Feb 1, 2024
1 parent 9b43cca commit 988334b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 46 deletions.
19 changes: 6 additions & 13 deletions engine/include/cubos/engine/imgui/data_inspector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,34 @@ namespace cubos::engine
{
public:
/// @brief Displays a reflectable value on the UI.
/// @note This should always be called inside a `ImGui::BeginTable(..., 2)` and `ImGui::EndTable()`.
/// @param name Value name.
/// @param type Value type.
/// @param value Pointer to value.
void show(const std::string& name, const core::reflection::Type& type, const void* value);
void show(const core::reflection::Type& type, const void* value);

/// @brief Displays a reflectable value on the UI and allows modifying it.
/// @note This should always be called inside a `ImGui::BeginTable(..., 2)` and `ImGui::EndTable()`.
/// @param name Value name.
/// @param type Value type.
/// @param value Pointer to value.
/// @return Whether the object was modified.
bool edit(const std::string& name, const core::reflection::Type& type, void* value);
bool edit(const core::reflection::Type& type, void* value);

/// @copybrief show
/// @note This should always be called inside a `ImGui::BeginTable(..., 2)` and `ImGui::EndTable()`.
/// @tparam T Value type.
/// @param name Value name.
/// @param value Pointer to value.
template <typename T>
void show(const std::string& name, const T& value)
void show(const T& value)
{
this->show(name, core::reflection::reflect<T>(), &value);
this->show(core::reflection::reflect<T>(), &value);
}

/// @copybrief edit
/// @note This should always be called inside a `ImGui::BeginTable(..., 2)` and `ImGui::EndTable()`.
/// @tparam T Value type.
/// @param name Value name.
/// @param value Pointer to value.
/// @return Whether the object was modified.
template <typename T>
bool edit(const std::string& name, T& value)
bool edit(T& value)
{
return this->edit(name, core::reflection::reflect<T>(), &value);
return this->edit(core::reflection::reflect<T>(), &value);
}

private:
Expand Down
24 changes: 15 additions & 9 deletions engine/samples/imgui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,26 @@ CUBOS_REFLECT_IMPL(Person)

struct DummyResource
{
CUBOS_REFLECT;

int integer;
Person person;
std::vector<Person> persons;
std::vector<int32_t> vec;
std::map<int32_t, int32_t> map;
};

CUBOS_REFLECT_IMPL(DummyResource)
{
return Type::create("DummyResource")
.with(FieldsTrait()
.withField("integer", &DummyResource::integer)
.withField("person", &DummyResource::person)
.withField("persons", &DummyResource::persons)
.withField("vec", &DummyResource::vec)
.withField("v", &DummyResource::map))
.with(ConstructibleTrait::typed<Person>().withDefaultConstructor().build());
}
/// [Creating a dummy resource]

int main()
Expand Down Expand Up @@ -105,15 +119,7 @@ int main()
.tagged("cubos.imgui")
.call([](DataInspector& inspector, DummyResource& data) {
ImGui::Begin("Data Inspector");
ImGui::BeginTable("id1", 2);

inspector.show("data.integer", data.integer);
inspector.edit("data.person", data.person);
inspector.edit("data.persons", data.persons);
inspector.edit("data.vec", data.vec);
inspector.edit("data.map", data.map);

ImGui::EndTable();
inspector.edit(data);
ImGui::End();
});
/// [DataInspector window example]
Expand Down
67 changes: 43 additions & 24 deletions engine/src/cubos/engine/imgui/data_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ using namespace cubos::engine;
/// @brief Displays a menu where the user can "set" the data to null (if it has NullableTrait)
static bool nullifyMenu(const Type& type, void* value);

void DataInspector::show(const std::string& name, const core::reflection::Type& type, const void* value)
void DataInspector::show(const core::reflection::Type& type, const void* value)
{
this->inspect(name, type, const_cast<void*>(value), true);
ImGui::BeginTable("inspector", 2);
this->inspect("", type, const_cast<void*>(value), true);
ImGui::EndTable();
}

bool DataInspector::edit(const std::string& name, const core::reflection::Type& type, void* value)
bool DataInspector::edit(const core::reflection::Type& type, void* value)
{
return this->inspect(name, type, value, false);
ImGui::BeginTable("inspector", 2);
auto changed = this->inspect("", type, value, false);
ImGui::EndTable();
return changed;
}

bool DataInspector::inspect(const std::string& name, const Type& type, void* value, bool readOnly)
Expand Down Expand Up @@ -336,13 +341,17 @@ std::string DataInspector::stringKnown(const void* value, const Type& type)

void DataInspector::showKnown(const std::string& name, const Type& type, const void* value)
{
ImGui::TableSetColumnIndex(0);
ImGui::AlignTextToFramePadding();
ImGui::TreeNodeEx(name.c_str(),
ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))

if (!name.empty())
{
ImGui::SetTooltip("%s", type.name().c_str());
ImGui::TableSetColumnIndex(0);
ImGui::AlignTextToFramePadding();
ImGui::TreeNodeEx(name.c_str(),
ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
{
ImGui::SetTooltip("%s", type.name().c_str());
}
}

std::string str = stringKnown(value, type);
Expand All @@ -352,18 +361,25 @@ void DataInspector::showKnown(const std::string& name, const Type& type, const v

void DataInspector::withStructured(const std::string& name, const Type& type, auto fn)
{
ImGui::TableSetColumnIndex(0);
ImGui::AlignTextToFramePadding();
bool nodeOpen = ImGui::TreeNode(name.c_str());
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
if (!name.empty())
{
ImGui::SetTooltip("%s", type.name().c_str());
}
ImGui::TableSetColumnIndex(0);
ImGui::AlignTextToFramePadding();
bool nodeOpen = ImGui::TreeNode(name.c_str());
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
{
ImGui::SetTooltip("%s", type.name().c_str());
}

if (nodeOpen)
if (nodeOpen)
{
fn();
ImGui::TreePop();
}
}
else
{
fn();
ImGui::TreePop();
}
}

Expand All @@ -381,13 +397,16 @@ void DataInspector::withStructured(const std::string& name, const Type& type, au

bool DataInspector::editKnown(const std::string& name, const Type& type, void* value)
{
ImGui::TableSetColumnIndex(0);
ImGui::AlignTextToFramePadding();
ImGui::TreeNodeEx(name.c_str(),
ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
if (!name.empty())
{
ImGui::SetTooltip("%s", type.name().c_str());
ImGui::TableSetColumnIndex(0);
ImGui::AlignTextToFramePadding();
ImGui::TreeNodeEx(name.c_str(),
ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_Bullet);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
{
ImGui::SetTooltip("%s", type.name().c_str());
}
}

ImGui::PushID("editKnown");
Expand Down

0 comments on commit 988334b

Please sign in to comment.