diff options
-rw-r--r-- | main/CMakeLists.txt | 1 | ||||
-rw-r--r-- | main/editor.cpp | 44 | ||||
-rw-r--r-- | main/editor.hpp | 7 | ||||
-rw-r--r-- | main/imgui-raii.hpp | 2 | ||||
-rw-r--r-- | main/imgui.cpp | 38 | ||||
-rw-r--r-- | src/tile-atlas.cpp | 6 |
6 files changed, 66 insertions, 32 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 9780ad4c..389da970 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,6 +1,7 @@ set(self "${PROJECT_NAME}-main") file(GLOB sources "*.cpp" CONFIGURE_ARGS) +add_definitions(-DIMGUI_DISABLE_OBSOLETE_FUNCTIONS) link_libraries(${PROJECT_NAME} ${PROJECT_NAME}-draw) link_libraries(Magnum::Sdl2Application Magnum::Trade) link_libraries(MagnumIntegration::ImGui) diff --git a/main/editor.cpp b/main/editor.cpp index 7a19d841..5658a785 100644 --- a/main/editor.cpp +++ b/main/editor.cpp @@ -77,13 +77,13 @@ void tile_type::select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas _permutation = { atlas, {} }; } -bool tile_type::is_tile_selected(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant) +bool tile_type::is_tile_selected(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant) const { fm_assert(atlas); return _selection_mode == sel_tile && _selected_tile == std::make_tuple(atlas, variant); } -bool tile_type::is_permutation_selected(const std::shared_ptr<tile_atlas>& atlas) +bool tile_type::is_permutation_selected(const std::shared_ptr<tile_atlas>& atlas) const { fm_assert(atlas); return _selection_mode == sel_perm && std::get<0>(_permutation) == atlas; @@ -141,6 +141,9 @@ void tile_type::place_tile(world& world, global_coords pos, const auto& [atlas, variant] = img; switch (_mode) { + default: + fm_warn_once("invalid editor mode '%u'", (unsigned)_mode); + break; case editor_mode::select: break; case editor_mode::floor: { @@ -165,6 +168,27 @@ void editor::set_mode(editor_mode mode) on_release(); } +const tile_type* editor::current() const +{ + switch (_mode) + { + case editor_mode::select: + return nullptr; + case editor_mode::floor: + return &_floor; + case editor_mode::walls: + return nullptr; // todo + default: + fm_warn_once("invalid editor mode '%u'", (unsigned)_mode); + return nullptr; + } +} + +tile_type* editor::current() +{ + return const_cast<tile_type*>(static_cast<const editor&>(*this).current()); +} + void editor::on_release() { _last_pos = std::nullopt; @@ -181,26 +205,16 @@ void editor::on_mouse_move(world& world, const global_coords pos) void editor::on_click(world& world, global_coords pos) { - switch (_mode) + if (auto* mode = current(); mode) { - case editor_mode::select: - break; - case editor_mode::floor: { - auto opt = _floor.get_selected(); + auto opt = mode->get_selected(); if (opt) { _last_pos = pos; - _floor.place_tile(world, pos, *opt); + mode->place_tile(world, pos, *opt); } else on_release(); - break; - } - case editor_mode::walls: - break; // TODO - default: - fm_warn_once("invalid editor mode '%u'", (unsigned)_mode); - break; } } diff --git a/main/editor.hpp b/main/editor.hpp index c96c0b2f..34a6671c 100644 --- a/main/editor.hpp +++ b/main/editor.hpp @@ -35,8 +35,8 @@ struct tile_type final void clear_selection(); void select_tile(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant); void select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas); - bool is_tile_selected(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant); - bool is_permutation_selected(const std::shared_ptr<tile_atlas>& atlas); + bool is_tile_selected(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant) const; + bool is_permutation_selected(const std::shared_ptr<tile_atlas>& atlas) const; std::optional<std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>> get_selected(); void place_tile(world& world, global_coords pos, const std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>& img); @@ -70,6 +70,9 @@ struct editor final tile_type& floor() { return _floor; } const tile_type& floor() const { return _floor; } + tile_type* current(); + const tile_type* current() const; + void on_click(world& world, global_coords pos); void on_mouse_move(world& world, const global_coords pos); void on_release(); diff --git a/main/imgui-raii.hpp b/main/imgui-raii.hpp index 9f446bf2..afae29d6 100644 --- a/main/imgui-raii.hpp +++ b/main/imgui-raii.hpp @@ -2,7 +2,7 @@ #include <Corrade/Containers/StringView.h> #include <Magnum/Math/Color.h> -#ifndef __CLION_IDE__ +#ifndef __CLION_IDE__zz #include <imgui.h> #endif diff --git a/main/imgui.cpp b/main/imgui.cpp index 878eabcd..2911d849 100644 --- a/main/imgui.cpp +++ b/main/imgui.cpp @@ -1,7 +1,7 @@ #include "app.hpp" #include <Magnum/GL/Renderer.h> #include "imgui-raii.hpp" -#ifndef __CLION_IDE__ +#ifndef __CLION_IDE__zz #include <Magnum/ImGuiIntegration/Integration.h> #endif @@ -64,12 +64,17 @@ void app::draw_ui() void app::draw_editor_pane(tile_type& type, float main_menu_height) { + constexpr + Color4 color_perm_selected{1, 1, 1, .7f}, + color_selected{1, 0.843f, 0, .8f}, + color_hover{0, .8f, 1, .7f}; + if (ImGui::GetIO().WantTextInput && !isTextInputActive()) startTextInput(); else if (!ImGui::GetIO().WantTextInput && isTextInputActive()) stopTextInput(); - const raii_wrapper vars[] = { + [[maybe_unused]] const raii_wrapper vars[] = { push_style_var(ImGuiStyleVar_WindowPadding, {8, 8}), push_style_var(ImGuiStyleVar_WindowBorderSize, 0), push_style_var(ImGuiStyleVar_FramePadding, {4, 4}), @@ -88,11 +93,12 @@ void app::draw_editor_pane(tile_type& type, float main_menu_height) auto b = begin_window({}, flags)) { const float window_width = ImGui::GetWindowWidth() - 32; + const auto* ed = _editor.current(); char buf[64]; //ImGui::SetNextWindowBgAlpha(.2f); - if (auto b = begin_list_box("##tiles", {-FLT_MIN, -1})) + if (auto b = begin_list_box("##atlases", {-FLT_MIN, -1})) { for (const auto& [k, v] : type) { @@ -107,28 +113,38 @@ void app::draw_editor_pane(tile_type& type, float main_menu_height) ImGui::SameLine(window_width - ImGui::CalcTextSize(buf).x - style.FramePadding.x - 4); ImGui::Text("%s", buf); }; - const std::size_t N = v->num_tiles(); + const auto N = (std::uint8_t)v->num_tiles(); if (const auto flags = ImGuiTreeNodeFlags_(ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Framed); auto b = tree_node(k.data(), flags)) { click_event(); add_tile_count(); - const raii_wrapper vars[] = { - push_style_var(ImGuiStyleVar_FramePadding, {1, 1}), - push_style_var(ImGuiStyleVar_FrameBorderSize, 3), - push_style_color(ImGuiCol_Button, {1, 1, 1, 1}), + [[maybe_unused]] const raii_wrapper vars[] = { + push_style_var(ImGuiStyleVar_FramePadding, {2, 2}), + push_style_color(ImGuiCol_ButtonHovered, color_hover), }; + const bool perm_selected = ed ? ed->is_permutation_selected(v) : false; constexpr std::size_t per_row = 5; - for (std::size_t i = 0; i < N; i++) + for (std::uint8_t i = 0; i < N; i++) { + const bool selected = ed ? ed->is_tile_selected(v, i) : false; + if (i > 0 && i % per_row == 0) ImGui::NewLine(); - snprintf(buf, sizeof(buf), "##item_%zu", i); + + [[maybe_unused]] const raii_wrapper vars[] = { + selected ? push_style_color(ImGuiCol_Button, color_selected) : raii_wrapper{}, + selected ? push_style_color(ImGuiCol_ButtonHovered, color_selected) : raii_wrapper{}, + perm_selected ? push_style_color(ImGuiCol_Button, color_perm_selected) : raii_wrapper{}, + perm_selected ? push_style_color(ImGuiCol_ButtonHovered, color_perm_selected) : raii_wrapper{}, + }; + + snprintf(buf, sizeof(buf), "##item_%hhu", i); const auto uv = v->texcoords_for_id(i); ImGui::ImageButton(buf, (void*)&v->texture(), {TILE_SIZE[0]/2, TILE_SIZE[1]/2}, { uv[3][0], uv[3][1] }, { uv[0][0], uv[0][1] }); if (ImGui::IsItemClicked()) - _editor.floor().select_tile(v, (std::uint8_t)i); + _editor.floor().select_tile(v, i); else click_event(); ImGui::SameLine(); diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp index d08b4798..324b052c 100644 --- a/src/tile-atlas.cpp +++ b/src/tile-atlas.cpp @@ -11,9 +11,9 @@ tile_atlas::tile_atlas(Containers::StringView name, const ImageView2D& image, Ve texcoords_{make_texcoords_array(Vector2ui(image.size()), dims)}, name_{name}, size_{image.size()}, dims_{dims} { - CORRADE_INTERNAL_ASSERT(dims_[0] > 0 && dims_[1] > 0); - CORRADE_INTERNAL_ASSERT(size_ % dims_ == Vector2ui()); - CORRADE_INTERNAL_ASSERT(dims_.product() < 256); + fm_assert(dims_[0] > 0 && dims_[1] > 0); + fm_assert(size_ % dims_ == Vector2ui()); + fm_assert(dims_.product() < 256); tex_.setWrapping(GL::SamplerWrapping::ClampToEdge) .setMagnificationFilter(GL::SamplerFilter::Linear) .setMinificationFilter(GL::SamplerFilter::Linear) |