Skip to content

Commit

Permalink
imgui: edit returns bool
Browse files Browse the repository at this point in the history
  • Loading branch information
phisko committed Mar 2, 2023
1 parent 40b6a3a commit a4248e0
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 29 deletions.
9 changes: 5 additions & 4 deletions kengine/core/log/imgui/systems/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 153,11 @@ namespace kengine::core::log::imgui {
}
}

putils::reflection::imgui_edit("Categories", filters.category_severities);
control->category_severities.clear();
for (const auto & [category, severity] : filters.category_severities)
control->category_severities.emplace(category, severity);
if (putils::reflection::imgui_edit("Categories", filters.category_severities)) {
control->category_severities.clear();
for (const auto & [category, severity] : filters.category_severities)
control->category_severities.emplace(category, severity);
}

if (ImGui::InputText("Category", filters.category_search, putils::lengthof(filters.category_search))) {
kengine_logf(r, verbose, log_category, "Category filter changed to '%s'", filters.category_search);
Expand Down
3 changes: 2 additions & 1 deletion kengine/meta/imgui/entity_editor/systems/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 74,8 @@ namespace kengine::meta::imgui::entity_editor {
kengine_logf(r, very_verbose, log_category, "Displaying [%u] (%s)", selected, window_title.c_str());

if (ImGui::Begin(window_title.c_str(), &open, ImGuiWindowFlags_NoSavedSettings))
edit_entity_and_model({ r, selected });
if (edit_entity_and_model({ r, selected }))
kengine_logf(r, log, log_category, "[%u] was modified", selected);
ImGui::End();

if (!open) {
Expand Down
2 changes: 1 addition & 1 deletion kengine/meta/imgui/functions/edit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,7 @@
#include "kengine/base_function.hpp"

namespace kengine::meta::imgui {
using edit_signature = void(entt::handle);
using edit_signature = bool(entt::handle);
//! putils reflect all
//! parents: [refltype::base]
struct edit : base_function<edit_signature> {};
Expand Down
6 changes: 5 additions & 1 deletion kengine/meta/imgui/functions/edit.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 5,17 @@
## Prototype

```cpp
void (entt::handle e);
bool (entt::handle e);
```

### Parameters

* `e`: entity for which the parent component should be edited

### Return value

Whether the component was modified.

## Usage

It is up to the user to implement this `meta component` for the component types they wish to be able to edit.
Expand Down
32 changes: 21 additions & 11 deletions kengine/meta/imgui/helpers/edit_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 29,25 @@
namespace kengine::meta::imgui {
static constexpr auto log_category = "meta_imgui";

void edit_entity(entt::handle e) noexcept {
bool edit_entity(entt::handle e) noexcept {
KENGINE_PROFILING_SCOPE;
kengine_logf(*e.registry(), very_verbose, log_category, "Editing [%u]", e.entity());

const auto & r = *e.registry();

if (!kengine::imgui::set_context(r))
return;
return false;

bool ret = false;
if (ImGui::BeginPopupContextWindow()) {
const auto types = core::sort::get_name_sorted_entities<const has, const emplace_or_replace>(r);

for (const auto & [_, name, has, emplace_or_replace] : types)
if (!has->call(e))
if (ImGui::MenuItem(name->name.c_str()))
if (ImGui::MenuItem(name->name.c_str())) {
ret = true;
emplace_or_replace->call(e, nullptr);
}

ImGui::EndPopup();
}
Expand All @@ -58,49 61,56 @@ namespace kengine::meta::imgui {

if (const auto remove = r.try_get<meta::remove>(type_entity)) {
if (ImGui::BeginPopupContextItem()) {
if (ImGui::MenuItem("Remove"))
if (ImGui::MenuItem("Remove")) {
ret = true;
remove->call(e);
}
ImGui::EndPopup();
}
}
if (tree_node_open) {
edit->call(e);
ret |= edit->call(e);
ImGui::TreePop();
}
}

return ret;
}

void edit_entity_and_model(entt::handle e) noexcept {
bool edit_entity_and_model(entt::handle e) noexcept {
KENGINE_PROFILING_SCOPE;
kengine_logf(*e.registry(), very_verbose, log_category, "Editing [%u] and its model", e.entity());

if (!kengine::imgui::set_context(*e.registry()))
return;
return false;

const auto instance = e.try_get<instance::instance>();
if (!instance || instance->model == entt::null) {
kengine_log(*e.registry(), very_verbose, log_category, "No model found");
edit_entity(e);
return;
return edit_entity(e);
}

kengine_logf(*e.registry(), very_verbose, log_category, "Found model [%u]", instance->model);

bool ret = false;
if (ImGui::BeginTabBar("##tabs")) {
ImGui::PushItemWidth(ImGui::GetWindowWidth() / 2.f);

if (ImGui::BeginTabItem("object")) {
edit_entity(e);
ret |= edit_entity(e);
ImGui::EndTabItem();
}

if (ImGui::BeginTabItem("Model")) {
edit_entity({ *e.registry(), instance->model });
ret |= edit_entity({ *e.registry(), instance->model });
ImGui::EndTabItem();
}

ImGui::PopItemWidth();

ImGui::EndTabBar();
}

return ret;
}
}
4 changes: 2 additions & 2 deletions kengine/meta/imgui/helpers/edit_entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 4,6 @@
#include <entt/entity/fwd.hpp>

namespace kengine::meta::imgui {
KENGINE_META_IMGUI_EXPORT void edit_entity(entt::handle e) noexcept;
KENGINE_META_IMGUI_EXPORT void edit_entity_and_model(entt::handle e) noexcept;
KENGINE_META_IMGUI_EXPORT bool edit_entity(entt::handle e) noexcept;
KENGINE_META_IMGUI_EXPORT bool edit_entity_and_model(entt::handle e) noexcept;
}
4 changes: 2 additions & 2 deletions kengine/meta/imgui/helpers/edit_entity.md
Original file line number Diff line number Diff line change
@@ -1,9 1,9 @@
# [edit_entity](edit_entity.hpp)

```cpp
void edit_entity(entt::handle e) noexcept;
bool edit_entity(entt::handle e) noexcept;
```
Displays an entity as an ImGui tree with write-enabled attributes.
Displays an entity as an ImGui tree with write-enabled attributes. Returns whether a component was modified.
For components to appear in the ImGui tree, the [edit](../functions/edit.md) `meta component` must first have been registered for them, along with the basic [has](../../functions/has.md), [emplace_or_replace](../../functions/emplace_or_replace.md) and [remove](../../functions/remove.md).
2 changes: 1 addition & 1 deletion kengine/meta/imgui/helpers/impl/edit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,7 @@
namespace kengine::meta {
template<typename T>
struct meta_component_implementation<imgui::edit, T> : std::true_type {
static void function(entt::handle e) noexcept;
static bool function(entt::handle e) noexcept;
};
}

Expand Down
17 changes: 12 additions & 5 deletions kengine/meta/imgui/helpers/impl/edit.inl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 17,7 @@

namespace kengine::meta {
template<typename T>
void meta_component_implementation<imgui::edit, T>::function(entt::handle e) noexcept {
bool meta_component_implementation<imgui::edit, T>::function(entt::handle e) noexcept {
KENGINE_PROFILING_SCOPE;
kengine_logf(*e.registry(), very_verbose, "meta::imgui::edit", "Editing [%u]'s %s", e.entity(), putils::reflection::get_class_name<T>());

Expand All @@ -33,12 33,19 @@ namespace kengine::meta {

#ifdef KENGINE_IMGUI
if (!kengine::imgui::set_context(*e.registry()))
return;
return false;
#endif

if (comp)
putils::reflection::imgui_edit(*comp);
else
if (!comp) {
kengine_log(*e.registry(), very_verbose, "meta::imgui::edit", "Component not found");
return false;
}

if (putils::reflection::imgui_edit(*comp)) {
kengine_logf(*e.registry(), verbose, "meta::imgui::edit", "Modified [%u]'s %s", e.entity(), putils::reflection::get_class_name<T>());
return true;
}

return false;
}
}

0 comments on commit a4248e0

Please sign in to comment.