diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-05-17 17:06:16 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-05-17 17:08:52 +0200 |
commit | 8138cc9a269844b6c0a84c193a7a43aec7010592 (patch) | |
tree | 1d7c1f1b37c7f714fd0976bfa839e0b18ca2385d /editor | |
parent | 08b89c6575947e8fdcba9b6c329c25aa8472e52b (diff) |
wip vobj
Diffstat (limited to 'editor')
-rw-r--r-- | editor/vobj-editor.cpp | 69 | ||||
-rw-r--r-- | editor/vobj-editor.hpp | 65 |
2 files changed, 134 insertions, 0 deletions
diff --git a/editor/vobj-editor.cpp b/editor/vobj-editor.cpp new file mode 100644 index 00000000..533f797a --- /dev/null +++ b/editor/vobj-editor.cpp @@ -0,0 +1,69 @@ +#include "vobj-editor.hpp" +#include "loader/vobj-info.hpp" +#include "loader/loader.hpp" +#include "src/world.hpp" +#include "src/light.hpp" +#include <array> +#include <Corrade/Containers/ArrayView.h> +#include <Corrade/Containers/StringView.h> + +#if defined __clang__ || defined __CLION_IDE__ +#pragma clang diagnostic ignored "-Wweak-vtables" +#endif + +namespace floormat { + +[[nodiscard]] +std::shared_ptr<entity> make_vobj(world& w, entity_type type, object_id id, global_coords pos); + +StringView vobj_factory::name() const { return info().name; } +StringView vobj_factory::descr() const { return info().descr; } +std::shared_ptr<anim_atlas> vobj_factory::atlas() const { return info().atlas; } + +struct light_factory final : vobj_factory +{ + entity_type type() const override { return entity_type::light; } + + const vobj_info& info() const override + { + constexpr auto NAME = "light"_s; + static const vobj_info& ret = loader.vobj(NAME); + fm_debug_assert(ret.name == NAME); + return ret; + } + + std::shared_ptr<entity> make(world& w, object_id id, global_coords pos) const override + { + auto ret = w.make_entity<light>(id, pos, light_proto{}); + return ret; + } +}; + +template<typename T> struct factory_ { static constexpr const T value = {}; }; + +static consteval auto make_factory_array() +{ + const auto size = (1uz << entity_type_BITS)-1; + std::array<const vobj_factory*, size> array = {}; + array[(unsigned)entity_type::light] = &factory_<light_factory>::value; + return array; +} + +static constexpr auto factory_array = make_factory_array(); +const ArrayView<const vobj_factory* const> vobj_editor::_types = { factory_array.data(), factory_array.size() }; + +const vobj_factory* vobj_editor::get_factory(entity_type type) +{ + const auto idx = size_t(type); + fm_debug_assert(idx < std::size(factory_array)); + const auto* ptr = factory_array[idx]; + if (!ptr) + { + fm_warn_once("invalid vobj type '%zu'", idx); + return {}; + } + else + return ptr; +} + +} // namespace floormat diff --git a/editor/vobj-editor.hpp b/editor/vobj-editor.hpp new file mode 100644 index 00000000..3ebf7bd8 --- /dev/null +++ b/editor/vobj-editor.hpp @@ -0,0 +1,65 @@ +#pragma once +#include "src/entity-type.hpp" +#include <memory> +//#include <Corrade/Containers/ArrayView.h> + +namespace Corrade::Containers { +template<typename T> class BasicStringView; +using StringView = BasicStringView<const char>; +template<typename T> class ArrayView; +} // namespace Corrade::Containers + +namespace floormat { + +struct world; +struct global_coords; +struct entity; +struct anim_atlas; +struct vobj_info; + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wweak-vtables" +#endif + +struct vobj_factory +{ + constexpr vobj_factory() = default; + virtual constexpr ~vobj_factory() noexcept = default; + virtual const vobj_info& info() const = 0; + virtual entity_type type() const = 0; + virtual std::shared_ptr<entity> make(world& w, object_id id, global_coords pos) const = 0; + + StringView name() const; + StringView descr() const; + std::shared_ptr<anim_atlas> atlas() const; +}; + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +struct vobj_editor final +{ + vobj_editor(); + + void select_type(entity_type type); + void clear_selection(); + + [[nodiscard]] static const vobj_factory* get_factory(entity_type type); + bool is_type_selected(entity_type type) const; + bool is_anything_selected() const; + + static void place_tile(world& w, global_coords pos, const vobj_factory& factory); + + static auto cbegin() noexcept { return _types.cbegin(); } + static auto cend() noexcept { return _types.cend(); } + static auto begin() noexcept { return _types.cbegin(); } + static auto end() noexcept { return _types.cend(); } + +private: + static const ArrayView<const vobj_factory* const> _types; + const vobj_factory* _selected = nullptr; +}; + +} // namespace floormat |