From 7867213a01fcabb1f05b1836c2ca59dc3bb2132f Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 11 Jan 2024 13:54:53 +0100 Subject: rename tile_atlas -> ground_atlas --- editor/app.hpp | 2 +- editor/editor.cpp | 6 +- editor/editor.hpp | 10 +-- editor/ground-editor.cpp | 166 +++++++++++++++++++++++++++++++++++++++++++++++ editor/ground-editor.hpp | 56 ++++++++++++++++ editor/imgui-editors.cpp | 4 +- editor/tile-editor.cpp | 166 ----------------------------------------------- editor/tile-editor.hpp | 56 ---------------- editor/update.cpp | 10 +-- 9 files changed, 238 insertions(+), 238 deletions(-) create mode 100644 editor/ground-editor.cpp create mode 100644 editor/ground-editor.hpp delete mode 100644 editor/tile-editor.cpp delete mode 100644 editor/tile-editor.hpp (limited to 'editor') diff --git a/editor/app.hpp b/editor/app.hpp index 45735400..09198c36 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -21,7 +21,7 @@ namespace floormat { struct chunk; struct floormat_main; -class tile_atlas; +class ground_atlas; struct fm_settings; class anim_atlas; struct critter; diff --git a/editor/editor.cpp b/editor/editor.cpp index de2e690e..86a180b5 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -191,7 +191,7 @@ void editor::set_mode(editor_mode mode) on_release(); } -const tile_editor* editor::current_ground_editor() const noexcept +const ground_editor* editor::current_ground_editor() const noexcept { switch (_mode) { @@ -229,9 +229,9 @@ const vobj_editor* editor::current_vobj_editor() const noexcept return nullptr; } -tile_editor* editor::current_ground_editor() noexcept +ground_editor* editor::current_ground_editor() noexcept { - return const_cast(static_cast(*this).current_ground_editor()); + return const_cast(static_cast(*this).current_ground_editor()); } wall_editor* editor::current_wall_editor() noexcept diff --git a/editor/editor.hpp b/editor/editor.hpp index dc897bc3..fae85c2f 100644 --- a/editor/editor.hpp +++ b/editor/editor.hpp @@ -4,7 +4,7 @@ #include "src/tile-image.hpp" #include "src/scenery.hpp" #include "editor-enums.hpp" -#include "tile-editor.hpp" +#include "ground-editor.hpp" #include "wall-editor.hpp" #include "scenery-editor.hpp" #include "vobj-editor.hpp" @@ -16,7 +16,7 @@ namespace floormat { struct world; class anim_atlas; -class tile_atlas; +class ground_atlas; struct app; struct editor final @@ -26,8 +26,8 @@ struct editor final [[nodiscard]] editor_mode mode() const noexcept { return _mode; } void set_mode(editor_mode mode); - tile_editor* current_ground_editor() noexcept; - const tile_editor* current_ground_editor() const noexcept; + ground_editor* current_ground_editor() noexcept; + const ground_editor* current_ground_editor() const noexcept; wall_editor* current_wall_editor() noexcept; const wall_editor* current_wall_editor() const noexcept; scenery_editor* current_scenery_editor() noexcept; @@ -57,7 +57,7 @@ private: app* _app; - tile_editor _floor; + ground_editor _floor; wall_editor _wall; scenery_editor _scenery; vobj_editor _vobj; diff --git a/editor/ground-editor.cpp b/editor/ground-editor.cpp new file mode 100644 index 00000000..0f67853e --- /dev/null +++ b/editor/ground-editor.cpp @@ -0,0 +1,166 @@ +#include "ground-editor.hpp" +#include "src/ground-atlas.hpp" +#include "src/world.hpp" +#include "src/random.hpp" +#include "keys.hpp" +#include "loader/loader.hpp" +#include "compat/exception.hpp" +#include +#include + +namespace floormat { + +ground_editor::ground_editor() +{ + load_atlases(); +} + +void ground_editor::load_atlases() +{ + for (const auto& atlas : loader.ground_atlases("floor.json"_s)) + { + auto& [_, vec] = _permutation; + vec.reserve(atlas->num_tiles()); + _atlases[atlas->name()] = atlas; + } +} + +std::shared_ptr ground_editor::maybe_atlas(StringView str) +{ + if (auto it = _atlases.find(str); it != _atlases.end()) + return it->second; + else + return nullptr; +} + +std::shared_ptr ground_editor::atlas(StringView str) +{ + if (auto ptr = maybe_atlas(str)) + return ptr; + else + fm_throw("no such atlas: {}"_cf, str); +} + +StringView ground_editor::name() const noexcept { return "floor"_s; } + +void ground_editor::clear_selection() +{ + _selected_tile = {}; + _permutation = {}; + _selection_mode = sel_none; +} + +void ground_editor::select_tile(const std::shared_ptr& atlas, size_t variant) +{ + fm_assert(atlas); + clear_selection(); + _selection_mode = sel_tile; + _selected_tile = { atlas, variant_t(variant % atlas->num_tiles()) }; +} + +void ground_editor::select_tile_permutation(const std::shared_ptr& atlas) +{ + fm_assert(atlas); + clear_selection(); + _selection_mode = sel_perm; + _permutation = { atlas, {} }; +} + +bool ground_editor::is_tile_selected(const std::shared_ptr& atlas, size_t variant) const +{ + return atlas && _selection_mode == sel_tile && _selected_tile && + atlas == _selected_tile.atlas && variant == _selected_tile.variant; +} + +bool ground_editor::is_permutation_selected(const std::shared_ptr& atlas) const +{ + const auto& [perm, _] = _permutation; + return atlas && _selection_mode == sel_perm && perm == atlas; +} + +bool ground_editor::is_atlas_selected(const std::shared_ptr& atlas) const +{ + switch (_selection_mode) + { + default: + case sel_none: + return false; + case sel_perm: + return is_permutation_selected(atlas); + case sel_tile: + return atlas && _selected_tile && atlas == _selected_tile.atlas; + } +} + +bool ground_editor::is_anything_selected() const +{ + return _selection_mode != sel_none; +} + +template +void fisher_yates(T begin, T end) +{ + const auto N = std::distance(begin, end); + for (auto i = N-1; i >= 1; i--) + { + const auto j = random(i+1); + using std::swap; + swap(begin[i], begin[j]); + } +} + +tile_image_proto ground_editor::get_selected_perm() +{ + auto& [atlas, vec] = _permutation; + const auto N = (variant_t)atlas->num_tiles(); + if (N == 0) + return {}; + if (vec.empty()) + { + for (variant_t i = 0; i < N; i++) + vec.push_back(i); + fisher_yates(vec.begin(), vec.end()); + } + const auto idx = vec.back(); + vec.pop_back(); + return {atlas, idx}; +} + +tile_image_proto ground_editor::get_selected() +{ + switch (_selection_mode) + { + default: + fm_warn_once("invalid editor mode '%u'", (unsigned)_selection_mode); + [[fallthrough]]; + case sel_none: + return {}; + case sel_tile: + return _selected_tile; + case sel_perm: + return get_selected_perm(); + } +} + +void ground_editor::place_tile(world& world, global_coords pos, const tile_image_proto& img) +{ + auto [c, t] = world[pos]; + c.mark_ground_modified(); + t.ground() = img; +} + +auto ground_editor::check_snap(int mods) const -> editor_snap_mode +{ + const bool ctrl = mods & kmod_ctrl, shift = mods & kmod_shift; + + if (!(ctrl | shift)) + return editor_snap_mode::none; + + if (shift) + return editor_snap_mode::horizontal; + if (ctrl) + return editor_snap_mode::vertical; + return editor_snap_mode::none; +} + +} // namespace floormat diff --git a/editor/ground-editor.hpp b/editor/ground-editor.hpp new file mode 100644 index 00000000..46c9dc3a --- /dev/null +++ b/editor/ground-editor.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include "editor-enums.hpp" +#include "src/tile-image.hpp" +#include "src/global-coords.hpp" +#include +#include +#include +#include + +namespace floormat { + +struct world; + +class ground_editor final +{ + enum selection_mode : unsigned char { + sel_none, sel_tile, sel_perm, + }; + + struct tuple final { + std::shared_ptr atlas; + std::vector variant; + }; + + std::map> _atlases; + tile_image_proto _selected_tile; + tuple _permutation; + selection_mode _selection_mode = sel_none; + + void load_atlases(); + tile_image_proto get_selected_perm(); + +public: + ground_editor(); + std::shared_ptr maybe_atlas(StringView str); + std::shared_ptr atlas(StringView str); + auto cbegin() const noexcept { return _atlases.cbegin(); } + auto cend() const noexcept { return _atlases.cend(); } + auto begin() const noexcept { return _atlases.cbegin(); } + auto end() const noexcept { return _atlases.cend(); } + StringView name() const noexcept; + + void clear_selection(); + void select_tile(const std::shared_ptr& atlas, size_t variant); + void select_tile_permutation(const std::shared_ptr& atlas); + bool is_tile_selected(const std::shared_ptr& atlas, size_t variant) const; + bool is_permutation_selected(const std::shared_ptr& atlas) const; + bool is_atlas_selected(const std::shared_ptr& atlas) const; + bool is_anything_selected() const; + tile_image_proto get_selected(); + void place_tile(world& world, global_coords pos, const tile_image_proto& img); + editor_snap_mode check_snap(int mods) const; +}; + +} // namespace floormat diff --git a/editor/imgui-editors.cpp b/editor/imgui-editors.cpp index 4ffb74ba..2e585d6b 100644 --- a/editor/imgui-editors.cpp +++ b/editor/imgui-editors.cpp @@ -2,7 +2,7 @@ #include "compat/format.hpp" #include "imgui-raii.hpp" #include "src/anim-atlas.hpp" -#include "src/tile-atlas.hpp" +#include "src/ground-atlas.hpp" #include "src/wall-atlas.hpp" #include "loader/loader.hpp" #include "floormat/main.hpp" @@ -51,7 +51,7 @@ void select_tile(wall_editor& wa, const wall_info* sc) { wa.select_atlas(sc->atl auto get_texcoords(const auto&, anim_atlas& atlas) { return atlas.texcoords_for_frame(atlas.first_rotation(), 0, !atlas.group(atlas.first_rotation()).mirror_from.isEmpty()); } auto get_texcoords(const wall_info* w, wall_atlas& atlas) { auto sz = get_size(w, atlas); return Quads::texcoords_at({}, sz, atlas.image_size()); }; -void draw_editor_tile_pane_atlas(tile_editor& ed, StringView name, const std::shared_ptr& atlas, Vector2 dpi) +void draw_editor_tile_pane_atlas(ground_editor& ed, StringView name, const std::shared_ptr& atlas, Vector2 dpi) { const auto b = push_id("tile-pane"); diff --git a/editor/tile-editor.cpp b/editor/tile-editor.cpp deleted file mode 100644 index 1b766208..00000000 --- a/editor/tile-editor.cpp +++ /dev/null @@ -1,166 +0,0 @@ -#include "tile-editor.hpp" -#include "src/tile-atlas.hpp" -#include "src/world.hpp" -#include "src/random.hpp" -#include "keys.hpp" -#include "loader/loader.hpp" -#include "compat/exception.hpp" -#include -#include - -namespace floormat { - -tile_editor::tile_editor() -{ - load_atlases(); -} - -void tile_editor::load_atlases() -{ - for (const auto& atlas : loader.tile_atlases("floor.json"_s)) - { - auto& [_, vec] = _permutation; - vec.reserve(atlas->num_tiles()); - _atlases[atlas->name()] = atlas; - } -} - -std::shared_ptr tile_editor::maybe_atlas(StringView str) -{ - if (auto it = _atlases.find(str); it != _atlases.end()) - return it->second; - else - return nullptr; -} - -std::shared_ptr tile_editor::atlas(StringView str) -{ - if (auto ptr = maybe_atlas(str)) - return ptr; - else - fm_throw("no such atlas: {}"_cf, str); -} - -StringView tile_editor::name() const noexcept { return "floor"_s; } - -void tile_editor::clear_selection() -{ - _selected_tile = {}; - _permutation = {}; - _selection_mode = sel_none; -} - -void tile_editor::select_tile(const std::shared_ptr& atlas, size_t variant) -{ - fm_assert(atlas); - clear_selection(); - _selection_mode = sel_tile; - _selected_tile = { atlas, variant_t(variant % atlas->num_tiles()) }; -} - -void tile_editor::select_tile_permutation(const std::shared_ptr& atlas) -{ - fm_assert(atlas); - clear_selection(); - _selection_mode = sel_perm; - _permutation = { atlas, {} }; -} - -bool tile_editor::is_tile_selected(const std::shared_ptr& atlas, size_t variant) const -{ - return atlas && _selection_mode == sel_tile && _selected_tile && - atlas == _selected_tile.atlas && variant == _selected_tile.variant; -} - -bool tile_editor::is_permutation_selected(const std::shared_ptr& atlas) const -{ - const auto& [perm, _] = _permutation; - return atlas && _selection_mode == sel_perm && perm == atlas; -} - -bool tile_editor::is_atlas_selected(const std::shared_ptr& atlas) const -{ - switch (_selection_mode) - { - default: - case sel_none: - return false; - case sel_perm: - return is_permutation_selected(atlas); - case sel_tile: - return atlas && _selected_tile && atlas == _selected_tile.atlas; - } -} - -bool tile_editor::is_anything_selected() const -{ - return _selection_mode != sel_none; -} - -template -void fisher_yates(T begin, T end) -{ - const auto N = std::distance(begin, end); - for (auto i = N-1; i >= 1; i--) - { - const auto j = random(i+1); - using std::swap; - swap(begin[i], begin[j]); - } -} - -tile_image_proto tile_editor::get_selected_perm() -{ - auto& [atlas, vec] = _permutation; - const auto N = (variant_t)atlas->num_tiles(); - if (N == 0) - return {}; - if (vec.empty()) - { - for (variant_t i = 0; i < N; i++) - vec.push_back(i); - fisher_yates(vec.begin(), vec.end()); - } - const auto idx = vec.back(); - vec.pop_back(); - return {atlas, idx}; -} - -tile_image_proto tile_editor::get_selected() -{ - switch (_selection_mode) - { - default: - fm_warn_once("invalid editor mode '%u'", (unsigned)_selection_mode); - [[fallthrough]]; - case sel_none: - return {}; - case sel_tile: - return _selected_tile; - case sel_perm: - return get_selected_perm(); - } -} - -void tile_editor::place_tile(world& world, global_coords pos, const tile_image_proto& img) -{ - auto [c, t] = world[pos]; - c.mark_ground_modified(); - t.ground() = img; -} - -auto tile_editor::check_snap(int mods) const -> editor_snap_mode -{ - const bool ctrl = mods & kmod_ctrl, shift = mods & kmod_shift; - - if (!(ctrl | shift)) - return editor_snap_mode::none; - - if (shift) - return editor_snap_mode::horizontal; - if (ctrl) - return editor_snap_mode::vertical; - return editor_snap_mode::none; -} - -} // namespace floormat diff --git a/editor/tile-editor.hpp b/editor/tile-editor.hpp deleted file mode 100644 index a33ed848..00000000 --- a/editor/tile-editor.hpp +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once - -#include "editor-enums.hpp" -#include "src/tile-image.hpp" -#include "src/global-coords.hpp" -#include -#include -#include -#include - -namespace floormat { - -struct world; - -class tile_editor final -{ - enum selection_mode : unsigned char { - sel_none, sel_tile, sel_perm, - }; - - struct tuple final { - std::shared_ptr atlas; - std::vector variant; - }; - - std::map> _atlases; - tile_image_proto _selected_tile; - tuple _permutation; - selection_mode _selection_mode = sel_none; - - void load_atlases(); - tile_image_proto get_selected_perm(); - -public: - tile_editor(); - std::shared_ptr maybe_atlas(StringView str); - std::shared_ptr atlas(StringView str); - auto cbegin() const noexcept { return _atlases.cbegin(); } - auto cend() const noexcept { return _atlases.cend(); } - auto begin() const noexcept { return _atlases.cbegin(); } - auto end() const noexcept { return _atlases.cend(); } - StringView name() const noexcept; - - void clear_selection(); - void select_tile(const std::shared_ptr& atlas, size_t variant); - void select_tile_permutation(const std::shared_ptr& atlas); - bool is_tile_selected(const std::shared_ptr& atlas, size_t variant) const; - bool is_permutation_selected(const std::shared_ptr& atlas) const; - bool is_atlas_selected(const std::shared_ptr& atlas) const; - bool is_anything_selected() const; - tile_image_proto get_selected(); - void place_tile(world& world, global_coords pos, const tile_image_proto& img); - editor_snap_mode check_snap(int mods) const; -}; - -} // namespace floormat diff --git a/editor/update.cpp b/editor/update.cpp index e80a5318..1140ad47 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -1,6 +1,6 @@ #include "app.hpp" #include "src/world.hpp" -#include "src/tile-atlas.hpp" +#include "src/ground-atlas.hpp" #include "src/anim-atlas.hpp" #include "main/clickable.hpp" #include "floormat/events.hpp" @@ -17,10 +17,10 @@ namespace floormat { void app::maybe_initialize_chunk_(const chunk_coords_& pos, chunk& c) { - auto floor1 = loader.tile_atlas("floor-tiles", {44, 4}, pass_mode::pass); - auto floor2 = loader.tile_atlas("metal1", {2, 2}, pass_mode::pass); - auto wall1 = loader.tile_atlas("wood2", {2, 1}, pass_mode::blocked); - auto wall2 = loader.tile_atlas("wood1", {2, 1}, pass_mode::blocked); + auto floor1 = loader.ground_atlas("floor-tiles", { 44, 4 }, pass_mode::pass); + auto floor2 = loader.ground_atlas("metal1", { 2, 2 }, pass_mode::pass); + auto wall1 = loader.ground_atlas("wood2", { 2, 1 }, pass_mode::blocked); + auto wall2 = loader.ground_atlas("wood1", { 2, 1 }, pass_mode::blocked); auto door = loader.anim_atlas("door-close", loader.SCENERY_PATH); auto table = loader.anim_atlas("table", loader.SCENERY_PATH); auto control_panel = loader.anim_atlas("control-panel", loader.SCENERY_PATH); -- cgit v1.2.3