diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-08 12:00:45 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-08 12:00:45 +0100 |
commit | 9acf4738b15738cb2b9646481b75ba0c05a01e78 (patch) | |
tree | d8a8ce5007ea77f86d33136cdfa014e45d31d72b /editor | |
parent | a083f5c2124a6a907727b35a5c43d8175d396d73 (diff) |
draw, editor: visualize bounding boxes
Diffstat (limited to 'editor')
-rw-r--r-- | editor/app.hpp | 6 | ||||
-rw-r--r-- | editor/draw.cpp | 45 | ||||
-rw-r--r-- | editor/events.cpp | 29 | ||||
-rw-r--r-- | editor/imgui.cpp | 7 | ||||
-rw-r--r-- | editor/keys.hpp | 1 | ||||
-rw-r--r-- | editor/update.cpp | 9 |
6 files changed, 80 insertions, 17 deletions
diff --git a/editor/app.hpp b/editor/app.hpp index c34b7ebd..ba1133d7 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -6,12 +6,11 @@ #include "draw/quad-floor.hpp" #include "draw/quad-wall-n.hpp" #include "draw/quad-wall-w.hpp" +#include "draw/quad.hpp" #include "draw/box.hpp" #include "floormat/app.hpp" #include "keys.hpp" - #include <memory> - #include <Corrade/Containers/Pointer.h> #include <Corrade/Containers/Optional.h> #include <Magnum/ImGuiIntegration/Context.h> @@ -93,6 +92,7 @@ private: void do_quickload(); void do_new_file(); + void draw_collision_boxes(); void draw_editor_pane(float main_menu_height); void draw_editor_tile_pane_atlas(tile_editor& ed, StringView name, const std::shared_ptr<tile_atlas>& atlas); void draw_editor_scenery_pane(scenery_editor& ed); @@ -122,10 +122,12 @@ private: wireframe_mesh<wireframe::quad_wall_n> _wireframe_wall_n {_wireframe_texture}; wireframe_mesh<wireframe::quad_wall_w> _wireframe_wall_w {_wireframe_texture}; wireframe_mesh<wireframe::box> _wireframe_box {_wireframe_texture}; + wireframe_mesh<wireframe::quad> _wireframe_rect {_wireframe_texture}; editor _editor; key_set keys; std::array<int, key_set::COUNT> key_modifiers = {}; cursor_state cursor; + bool _draw_collision_boxes : 1 = false; }; } // namespace floormat diff --git a/editor/draw.cpp b/editor/draw.cpp index 275d99fc..32319c1b 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -6,6 +6,8 @@ #include "src/anim-atlas.hpp" #include "draw/anim.hpp" #include "src/camera-offset.hpp" +#include "src/world.hpp" +#include "src/collision.hpp" #include <Magnum/Math/Color.h> #include <Magnum/Math/Vector3.h> @@ -54,10 +56,53 @@ void app::draw_cursor() } } } + shader.set_tint({1, 1, 1, 1}); +} + +void app::draw_collision_boxes() +{ + const auto [minx, maxx, miny, maxy] = M->get_draw_bounds(); + const auto sz = M->window_size(); + auto& world = M->world(); + auto& shader = M->shader(); + + shader.set_tint({0, .5f, 1, 1}); + + for (std::int16_t y = miny; y <= maxy; y++) + for (std::int16_t x = minx; x <= maxx; x++) + { + const chunk_coords pos{x, y}; + auto& c = world[pos]; + if (c.empty()) + continue; + c.ensure_passability(); + auto* lqt = c.lqt_from_collision_type(collision::move); + if (!lqt) + continue; + const with_shifted_camera_offset o{shader, pos, {minx, miny}, {maxx, maxy}}; + if (floormat_main::check_chunk_visible(shader.camera_offset(), sz)) + { + auto bb = lqt->GetLooseBoundingBox(); + bb.left -= bb.width; bb.top -= bb.height; + bb.width *= 2; bb.height *= 2; + auto q = lqt->QueryInsideRegion(bb); + using extractor = std::decay_t<decltype(*lqt)>::BoundingBoxExtractor; + while (!q.EndOfQuery()) + { + loose_quadtree::BoundingBox<std::int16_t> bb{0, 0, 0, 0}; + extractor::ExtractBoundingBox(q.GetCurrent(), &bb); + _wireframe_rect.draw(shader, { Vector3(bb.left+bb.width/2.f, bb.top+bb.height/2.f, 0), Vector2(bb.width, bb.height), 3 }); + q.Next(); + } + } + } + shader.set_tint({1, 1, 1, 1}); } void app::draw() { + if (_draw_collision_boxes) + draw_collision_boxes(); if (_editor.current_tile_editor() || _editor.current_scenery_editor()) draw_cursor(); draw_ui(); diff --git a/editor/events.cpp b/editor/events.cpp index 6bf4b3e7..96f5c82c 100644 --- a/editor/events.cpp +++ b/editor/events.cpp @@ -113,20 +113,21 @@ auto app::resolve_keybinding(int k_, int mods_) const -> std::tuple<key, int> switch (int mods = k2 & kmod_mask; k2) { default: continue; - case SDLK_w: return { key_camera_up, mods }; - case SDLK_a: return { key_camera_left, mods }; - case SDLK_s: return { key_camera_down, mods }; - case SDLK_d: return { key_camera_right, mods }; - case SDLK_HOME: return { key_camera_reset, mods }; - case SDLK_r: return { key_rotate_tile, mods }; - case SDLK_1: return { key_mode_none, mods }; - case SDLK_2: return { key_mode_floor, mods }; - case SDLK_3: return { key_mode_walls, mods }; - case SDLK_4: return { key_mode_scenery, mods }; - case SDLK_F5: return { key_quicksave, mods }; - case SDLK_F9: return { key_quickload, mods }; - case SDLK_q | CTRL: return { key_quit, mods }; - case SDLK_n | CTRL: return { key_new_file, mods }; + case SDLK_w: return { key_camera_up, mods }; + case SDLK_a: return { key_camera_left, mods }; + case SDLK_s: return { key_camera_down, mods }; + case SDLK_d: return { key_camera_right, mods }; + case SDLK_HOME: return { key_camera_reset, mods }; + case SDLK_r: return { key_rotate_tile, mods }; + case SDLK_1: return { key_mode_none, mods }; + case SDLK_2: return { key_mode_floor, mods }; + case SDLK_3: return { key_mode_walls, mods }; + case SDLK_4: return { key_mode_scenery, mods }; + case SDLK_F5: return { key_quicksave, mods }; + case SDLK_F9: return { key_quickload, mods }; + case SDLK_q | CTRL: return { key_quit, mods }; + case SDLK_n | CTRL: return { key_new_file, mods }; + case SDLK_b | ALT: return { key_collision_boxes, mods }; } } } diff --git a/editor/imgui.cpp b/editor/imgui.cpp index 1b219561..69037861 100644 --- a/editor/imgui.cpp +++ b/editor/imgui.cpp @@ -77,6 +77,13 @@ float app::draw_main_menu() if (b_rotate) do_key(key_rotate_tile); } + if (auto b = begin_menu("View")) + { + bool show_collisions = _draw_collision_boxes; + ImGui::MenuItem("Collision boxes", "Alt+B", &show_collisions); + if (show_collisions) + do_key(key_collision_boxes); + } main_menu_height = ImGui::GetContentRegionMax().y; } diff --git a/editor/keys.hpp b/editor/keys.hpp index 1f56cb69..3311a69c 100644 --- a/editor/keys.hpp +++ b/editor/keys.hpp @@ -17,6 +17,7 @@ enum key : unsigned { key_NO_REPEAT, key_rotate_tile, key_mode_none, key_mode_floor, key_mode_walls, key_mode_scenery, + key_collision_boxes, key_GLOBAL, key_new_file, key_quit, diff --git a/editor/update.cpp b/editor/update.cpp index f6d64411..4dde4ffc 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -104,6 +104,8 @@ void app::do_key(key k, int mods) return _editor.set_mode(editor_mode::walls); case key_mode_scenery: return _editor.set_mode(editor_mode::scenery); + case key_collision_boxes: + return void(_draw_collision_boxes = !_draw_collision_boxes); case key_quicksave: return do_quicksave(); case key_quickload: @@ -130,9 +132,14 @@ void app::update_world(float dt) minx--; miny--; maxx++; maxy++; for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) - for (chunk_coords c{x, y}; auto [x, k, pt] : world[c]) + for (auto& c = world[chunk_coords{x, y}]; auto [x, k, pt] : c) if (auto [atlas, scenery] = x.scenery(); atlas != nullptr) + { + auto pass0 = scenery.passability; scenery.update(dt, *atlas); + if (pass0 != scenery.passability) + c.mark_scenery_modified(); + } } void app::update(float dt) |