From 37215facfcecb3a4133a740d7fda57c0ef564395 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 28 May 2024 03:44:47 +0200 Subject: w --- editor/imgui-raii.cpp | 14 +++++++ editor/imgui-raii.hpp | 7 ++++ editor/inspect-types.cpp | 34 ++++++++++++++++ editor/inspect.cpp | 14 +------ editor/tests-private.hpp | 14 ++++--- editor/tests/hole-test.cpp | 98 ++++++++++++++++++++++++++++++++++++++++++++++ editor/vobj-editor.cpp | 30 ++++++++++++++ 7 files changed, 192 insertions(+), 19 deletions(-) create mode 100644 editor/tests/hole-test.cpp (limited to 'editor') diff --git a/editor/imgui-raii.cpp b/editor/imgui-raii.cpp index 8cae0818..b5997c7b 100644 --- a/editor/imgui-raii.cpp +++ b/editor/imgui-raii.cpp @@ -1,5 +1,6 @@ #include "imgui-raii.hpp" #include "compat/assert.hpp" +#include #include #include #include @@ -159,4 +160,17 @@ raii_wrapper begin_child(StringView name, const ImVec2& size, int flags, int win return {&ImGui::EndChild}; } +const char* label_left_(StringView label, char* buf, size_t buf_len, float width); + +const char* label_left_(StringView label, char* buf, size_t buf_len, float width) +{ + std::snprintf(buf, buf_len, "##%s", label.data()); + float x = ImGui::GetCursorPosX(); + ImGui::TextEx(label.data(), label.data() + label.size()); + ImGui::SameLine(); + ImGui::SetCursorPosX(x + width + ImGui::GetStyle().ItemInnerSpacing.x); + ImGui::SetNextItemWidth(-1); + return buf; +} + } // namespace floormat::imgui diff --git a/editor/imgui-raii.hpp b/editor/imgui-raii.hpp index 63c749bb..59134deb 100644 --- a/editor/imgui-raii.hpp +++ b/editor/imgui-raii.hpp @@ -67,4 +67,11 @@ private: float font_size; }; +template +const char* label_left(StringView label, char(&buf)[N], float width) +{ + const char* label_left_(StringView, char*, size_t, float); + return label_left_(label, static_cast(buf), N, width); +} + } // namespace floormat::imgui diff --git a/editor/inspect-types.cpp b/editor/inspect-types.cpp index cb85f574..0968eeec 100644 --- a/editor/inspect-types.cpp +++ b/editor/inspect-types.cpp @@ -9,6 +9,7 @@ #include "src/chunk.hpp" #include "src/critter.hpp" #include "src/light.hpp" +#include "src/hole.hpp" #include #include @@ -140,6 +141,38 @@ template<> struct entity_accessors } }; +template<> struct entity_accessors +{ + static constexpr auto accessors() + { + using E = Entity; + auto tuple0 = entity_accessors::accessors(); + auto tuple = std::tuple{ + E::type::field{"height"_s, + &hole::height, + &hole::set_height, + [](const hole& x) { return x.flags.is_wall ? st::enabled : st::readonly; }, + }, + E::type::field{"z-offset"_s, + &hole::z_offset, + &hole::set_z_offset, + [](const hole& x) { return x.flags.is_wall ? st::enabled : st::readonly; }, + constantly(constraints::range{0, tile_size_z}), + }, + E::type::field{"on-render"_s, + [](const hole& x) { return x.flags.on_render; }, + [](hole& x, bool value) { x.set_enabled(value, x.flags.on_physics); }, + }, + E::type::field{ + "on-physics"_s, + [](const hole& x) { return x.flags.on_physics; }, + [](hole& x, bool value) { x.set_enabled(x.flags.on_render, value); }, + }, + }; + return std::tuple_cat(tuple0, tuple); + } +}; + template struct has_anim_atlas : std::false_type {}; template @@ -318,6 +351,7 @@ bool inspect_object_subtype(object& x) } case object_type::critter: return inspect_type(static_cast(x), inspect_intent_t{}); case object_type::light: return inspect_type(static_cast(x), inspect_intent_t{}); + case object_type::hole: return inspect_type(static_cast(x), inspect_intent_t{}); } fm_warn_once("unknown object subtype '%d'", (int)type); return false; diff --git a/editor/inspect.cpp b/editor/inspect.cpp index e7d8878a..349b5e3d 100644 --- a/editor/inspect.cpp +++ b/editor/inspect.cpp @@ -17,18 +17,6 @@ namespace floormat::entities { namespace { -template -const char* label_left(StringView label, char(&buf)[N], size_t width) -{ - std::snprintf(buf, N, "##%s", label.data()); - float x = ImGui::GetCursorPosX(); - ImGui::TextEx(label.data(), label.data() + label.size()); - ImGui::SameLine(); - ImGui::SetCursorPosX(x + (float)width + ImGui::GetStyle().ItemInnerSpacing.x); - ImGui::SetNextItemWidth(-1); - return buf; -} - template struct IGDT_; template<> struct IGDT_ : std::integral_constant {}; template<> struct IGDT_ : std::integral_constant {}; @@ -89,7 +77,7 @@ bool do_inspect_field(void* datum, const erased_accessor& accessor, field_repr r should_disable = should_disable || !accessor.can_write(); [[maybe_unused]] auto disabler = begin_disabled(should_disable); bool ret = false; - const char* const label = label_left(accessor.field_name, buf, label_width); + const char* const label = label_left(accessor.field_name, buf, (float)label_width); T value{}; accessor.read_fun(datum, accessor.reader, &value); auto orig = value; diff --git a/editor/tests-private.hpp b/editor/tests-private.hpp index dc939878..d6a04cf5 100644 --- a/editor/tests-private.hpp +++ b/editor/tests-private.hpp @@ -32,7 +32,7 @@ protected: enum class Test : uint32_t { //todo add a speedometer overlay test - none, path, raycast, region, walk, COUNT, + none, path, raycast, region, walk, hole, COUNT, }; struct tests_data final : tests_data_ @@ -46,6 +46,7 @@ struct tests_data final : tests_data_ static Pointer make_test_raycast(); static Pointer make_test_region(); static Pointer make_test_walk(); + static Pointer make_test_hole(); Pointer current_test; Test current_index = Test::none; @@ -58,11 +59,12 @@ struct tests_data final : tests_data_ }; static constexpr test_tuple fields[] = { - { "None"_s, Test::none, make_test_none, }, - { "Path search"_s, Test::path, make_test_path, }, - { "Raycasting"_s, Test::raycast, make_test_raycast }, - { "Region"_s, Test::region, make_test_region }, - { "Walking"_s, Test::walk, make_test_walk }, + { "None"_s, Test::none, make_test_none, }, + { "Path search"_s, Test::path, make_test_path, }, + { "Raycasting"_s, Test::raycast, make_test_raycast }, + { "Region"_s, Test::region, make_test_region }, + { "Walking"_s, Test::walk, make_test_walk }, + { "Hole"_s, Test::hole, make_test_hole }, }; }; diff --git a/editor/tests/hole-test.cpp b/editor/tests/hole-test.cpp new file mode 100644 index 00000000..6cb60449 --- /dev/null +++ b/editor/tests/hole-test.cpp @@ -0,0 +1,98 @@ +#include "../tests-private.hpp" +#include "compat/shared-ptr-wrapper.hpp" +#include "src/tile-constants.hpp" +#include "src/chunk-region.hpp" +#include "src/object.hpp" +#include "src/world.hpp" +#include "../app.hpp" +#include "../imgui-raii.hpp" +#include "floormat/main.hpp" +#include "src/critter.hpp" + +namespace floormat::tests { +namespace { + +using namespace floormat::imgui; + +struct hole_test final : base_test +{ + ~hole_test() noexcept override = default; + + bool handle_key(app& a, const key_event& e, bool is_down) override; + bool handle_mouse_click(app& a, const mouse_button_event& e, bool is_down) override; + bool handle_mouse_move(app& a, const mouse_move_event& e) override; + void draw_overlay(app& a) override; + void draw_ui(app& a, float menu_bar_height) override; + void update_pre(app& a, const Ns& dt) override; + void update_post(app&, const Ns&) override {} +}; + +bool hole_test::handle_key(app& a, const key_event& e, bool is_down) +{ + return false; +} + +bool hole_test::handle_mouse_click(app& a, const mouse_button_event& e, bool is_down) +{ + return false; +} + +bool hole_test::handle_mouse_move(app& a, const mouse_move_event& e) +{ + return false; +} + +void hole_test::draw_overlay(app& a) +{ +} + +void hole_test::draw_ui(app& a, float menu_bar_height) +{ + const auto& m = a.main(); + const auto size_x = ImGui::GetWindowSize().x; + const auto window_size = ImVec2{size_x, size_x}; + //const auto dpi = m.dpi_scale(); + constexpr auto igcf = ImGuiChildFlags_None; + constexpr auto igwf = ImGuiWindowFlags_NoDecoration; + + ImGui::NewLine(); + + char buf[32]; + + ImGui::LabelText("##test-area", "Test area"); + + ImGui::NewLine(); + if (auto b1 = imgui::begin_child("Test area"_s, window_size, igcf, igwf)) + { + const auto& win = *ImGui::GetCurrentWindow(); + ImDrawList& draw = *win.DrawList; + } + ImGui::NewLine(); + + const auto label_width = ImGui::CalcTextSize("MMMM").x; + + label_left("width", buf, label_width); + ImGui::NewLine(); + + label_left("height", buf, label_width); + ImGui::NewLine(); + + label_left("x", buf, label_width); + ImGui::NewLine(); + + label_left("y", buf, label_width); + ImGui::NewLine(); + + label_left("z", buf, label_width); + ImGui::NewLine(); +} + +void hole_test::update_pre(app& a, const Ns& dt) +{ +} + +} // namespace + +Pointer tests_data::make_test_hole() { return Pointer{InPlaceInit}; } + +} // namespace floormat::tests diff --git a/editor/vobj-editor.cpp b/editor/vobj-editor.cpp index e82c6d27..9eb36e39 100644 --- a/editor/vobj-editor.cpp +++ b/editor/vobj-editor.cpp @@ -1,6 +1,7 @@ #include "vobj-editor.hpp" #include "src/world.hpp" #include "src/light.hpp" +#include "src/hole.hpp" #include "loader/loader.hpp" #include "loader/vobj-cell.hpp" #include "app.hpp" @@ -74,6 +75,8 @@ start: while (auto id = a.get_object_colliding_with_cursor()) #pragma clang diagnostic ignored "-Wweak-vtables" #endif +namespace { + struct light_factory final : vobj_factory { object_type type() const override; @@ -98,6 +101,32 @@ std::shared_ptr light_factory::make(world& w, object_id id, global_coord return ret; } +struct hole_factory final : vobj_factory +{ + object_type type() const override; + const vobj_cell& info() const override; + std::shared_ptr make(world& w, object_id id, global_coords pos) const override; +}; + +object_type hole_factory::type() const { return object_type::hole; } + +const vobj_cell& hole_factory::info() const +{ + constexpr auto NAME = "hole"_s; + static const vobj_cell& ret = loader.vobj(NAME); + fm_debug_assert(ret.name == NAME); + fm_debug_assert(ret.atlas != nullptr); + return ret; +} + +std::shared_ptr hole_factory::make(world& w, object_id id, global_coords pos) const +{ + auto ret = w.make_object(id, pos, hole_proto{}); + return ret; +} + +} // namespace + auto vobj_editor::make_vobj_type_map() -> std::map { constexpr auto add = [](auto& m, std::unique_ptr&& x) { @@ -106,6 +135,7 @@ auto vobj_editor::make_vobj_type_map() -> std::map }; std::map map; add(map, std::make_unique()); + add(map, std::make_unique()); return map; } -- cgit v1.2.3