From bcfd2f2ce953b0f9ed0a59a46d8db463aef4e714 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 25 May 2023 07:38:40 +0200 Subject: some work on inspecting other enttiy subtypes --- editor/inspect-draw.cpp | 12 ++---- editor/inspect-types.cpp | 110 +++++++++++++++++++++++++++++++++++++++-------- editor/inspect.hpp | 8 ++-- entity/accessor.hpp | 2 - src/light-falloff.hpp | 2 +- 5 files changed, 101 insertions(+), 33 deletions(-) diff --git a/editor/inspect-draw.cpp b/editor/inspect-draw.cpp index a1adbba3..1f1ccec3 100644 --- a/editor/inspect-draw.cpp +++ b/editor/inspect-draw.cpp @@ -53,17 +53,11 @@ void app::draw_inspector() snformat(buf, "{} ({}x{}:{} -> {}x{})"_cf, name, ch.x, ch.y, (int)z, (int)pos.x, (int)pos.y); bool is_open = true; - if (e.type() == entity_type::scenery) + if (auto b2 = begin_window(buf, &is_open)) { - auto& s2 = static_cast(e); - if (auto b2 = begin_window(buf, &is_open)) - { - bool ret = entities::inspect_type(s2); - (void)ret; - } + bool ret = entities::inspect_entity_subtype(e); + (void)ret; } - else - is_open = false; if (!is_open) inspectors.erase(inspectors.begin() + (ptrdiff_t)i); } diff --git a/editor/inspect-types.cpp b/editor/inspect-types.cpp index b93813b9..849dcfa3 100644 --- a/editor/inspect-types.cpp +++ b/editor/inspect-types.cpp @@ -6,6 +6,8 @@ #include "inspect.hpp" #include "loader/loader.hpp" #include "chunk.hpp" +#include "src/character.hpp" +#include "src/light.hpp" #include namespace floormat::entities { @@ -75,10 +77,20 @@ struct entity_accessors { }; template struct has_anim_atlas : std::false_type {}; + +template +requires requires (const T& x) { { x.atlas } -> std::convertible_to&>; } +struct has_anim_atlas : std::true_type { + static const anim_atlas& get_atlas(const entity& x) { return *x.atlas; } +}; + +#if 0 template<> struct has_anim_atlas : std::true_type { static const anim_atlas& get_atlas(const entity& x) { return *x.atlas; } }; template<> struct has_anim_atlas : has_anim_atlas {}; +template<> struct has_anim_atlas : has_anim_atlas {}; +#endif using enum_pair = std::pair; template struct enum_values; @@ -94,23 +106,26 @@ template enum_pair_array(std::array array, size_t) -> en template requires (!std::is_enum_v) -struct enum_values : std::true_type { - static constexpr std::array get() { return {}; } +struct enum_values +{ + static constexpr std::array get(const U&) { return {}; } }; -template struct enum_values : std::true_type { +template struct enum_values +{ static constexpr auto ret = std::to_array({ { "blocked"_s, (size_t)pass_mode::blocked, }, { "see-through"_s, (size_t)pass_mode::see_through, }, { "shoot-through"_s, (size_t)pass_mode::shoot_through, }, { "pass"_s, (size_t)pass_mode::pass }, }); - static constexpr const auto& get() { return ret; } + static constexpr const auto& get(const U&) { return ret; } }; template requires has_anim_atlas::value -struct enum_values : std::false_type { +struct enum_values +{ static auto get(const U& x) { const anim_atlas& atlas = has_anim_atlas::get_atlas(x); std::array array; @@ -132,27 +147,86 @@ struct enum_values : std::false_type { } }; -template bool inspect_type(T& x) +template +static bool inspect_type(T& x) { bool ret = false; visit_tuple([&](const auto& field) { using type = typename std::decay_t::FieldType; - using enum_type = enum_values; - if constexpr(enum_type::value) - { - constexpr auto list = enum_type::get(); - ret |= inspect_field(&x, field.erased(), list); - } - else - { - const auto& list = enum_type::get(x); - ret |= inspect_field(&x, field.erased(), list); - } + using enum_type = enum_values; + const auto& list = enum_type::get(x); + ret |= inspect_field(&x, field.erased(), list); }, entity_metadata::accessors); return ret; } -template bool inspect_type(entity&); +template<> +struct entity_accessors { + static constexpr auto accessors() + { + using E = Entity; + auto tuple0 = entity_accessors::accessors(); + auto tuple = std::tuple{ + E::type::field{"name"_s, + [](const character& x) { return x.name; }, + [](character& x, const String& value) { x.name = value; }}, + E::type::field{"playable"_s, + [](const character& x) { return x.playable; }, + [](character& x, bool value) { x.playable = value; }, + }, + }; + return std::tuple_cat(tuple0, tuple); + } +}; + +template struct enum_values +{ + static constexpr auto ret = std::to_array({ + { "constant"_s, (size_t)light_falloff::constant }, + { "linear"_s, (size_t)light_falloff::linear }, + { "quadratic"_s, (size_t)light_falloff::quadratic }, + }); + static constexpr const auto& get(const U&) { return ret; } +}; + +template<> +struct entity_accessors { + static constexpr auto accessors() + { + using E = Entity; + auto tuple0 = entity_accessors::accessors(); + auto tuple = std::tuple{ + E::type::field{"color"_s, + [](const light& x) { return Vector3ub(x.color); }, + [](light& x, Vector3ub value) { x.color = Color3ub{value}; }, + constantly(constraints::range{{0, 0, 0}, {255, 255, 255}}), + }, + E::type::field{"falloff"_s, + [](const light& x) { return x.falloff; }, + [](light& x, light_falloff value) { x.falloff = value; }, + }, + // half_dist + // symmetric + }; + return std::tuple_cat(tuple0, tuple); + } +}; + +//template bool inspect_type(entity&); template bool inspect_type(scenery&); +template bool inspect_type(character&); +template bool inspect_type(light&); + +bool inspect_entity_subtype(entity& x) +{ + switch (auto type = x.type()) + { + default: fm_warn_once("unknown entity subtype '%d'", (int)type); return false; + //case entity_type::none: return inspect_type(x); + case entity_type::scenery: return inspect_type(static_cast(x)); + case entity_type::character: return inspect_type(static_cast(x)); + case entity_type::light: return inspect_type(static_cast(x)); + } +} } // namespace floormat::entities diff --git a/editor/inspect.hpp b/editor/inspect.hpp index 98649e13..f7769fae 100644 --- a/editor/inspect.hpp +++ b/editor/inspect.hpp @@ -28,10 +28,12 @@ template using field_repr_cbx = field_repr_ bool inspect_field(void* datum, const entities::erased_accessor& accessor, const ArrayView>& list); -template bool inspect_type(T& x); +bool inspect_entity_subtype(entity& x); -template requires std::is_enum_v bool inspect_field(void* datum, const entities::erased_accessor& accessor, - const ArrayView>& list) +template +requires std::is_enum_v +bool inspect_field(void* datum, const entities::erased_accessor& accessor, + const ArrayView>& list) { return inspect_field>>(datum, accessor, list); } diff --git a/entity/accessor.hpp b/entity/accessor.hpp index 3a25cd22..468c51c3 100644 --- a/entity/accessor.hpp +++ b/entity/accessor.hpp @@ -170,6 +170,4 @@ field_status erased_accessor::is_enabled(const void* x) const noexcept { return erased_constraints::range erased_accessor::get_range(const void* x) const noexcept { return range_fun(x,range); } erased_constraints::max_length erased_accessor::get_max_length(const void* x) const noexcept { return length_fun(x,length); } -template void get_erased_accessors(std::vector& ret); - } // namespace floormat::entities diff --git a/src/light-falloff.hpp b/src/light-falloff.hpp index e90d11aa..b9b1c4bf 100644 --- a/src/light-falloff.hpp +++ b/src/light-falloff.hpp @@ -3,7 +3,7 @@ namespace floormat { enum class light_falloff : uint8_t { - linear, quadratic, constant, + constant = 1, linear = 0, quadratic = 2, }; } // namespace floormat -- cgit v1.2.3