diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-15 23:12:05 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-15 23:12:05 +0200 |
commit | 0c2e1a070477b91180edda3094442c5329cec4f6 (patch) | |
tree | 10f441f38602f5d1f593a92529f47f276cab8751 | |
parent | d5955d35b9fd32b31b4cd7b5b6f286e1b9c43494 (diff) |
a
-rw-r--r-- | main/menu.cpp | 62 | ||||
-rw-r--r-- | src/tile-atlas.cpp | 9 |
2 files changed, 54 insertions, 17 deletions
diff --git a/main/menu.cpp b/main/menu.cpp index f1d9f8db..f49f4105 100644 --- a/main/menu.cpp +++ b/main/menu.cpp @@ -1,6 +1,7 @@ #include "app.hpp" #include <Magnum/GL/Renderer.h> #include <Magnum/ImGuiIntegration/Integration.h> +#include <Magnum/Math/Color.h> namespace floormat { @@ -50,6 +51,31 @@ constexpr inline const auto* imgui_name = "floormat editor"; return {}; } +[[nodiscard]] static raii_wrapper tree_node(const char* name) { + if (ImGui::TreeNode(name)) + return {&ImGui::TreePop}; + else + return {}; +} + +[[nodiscard]] static raii_wrapper push_style_var(ImGuiStyleVar_ var, Vector2 value) +{ + ImGui::PushStyleVar(var, {value[0], value[1]}); + return {[]{ ImGui::PopStyleVar(); }}; +} + +[[nodiscard]] static raii_wrapper push_style_var(ImGuiStyleVar_ var, float value) +{ + ImGui::PushStyleVar(var, value); + return {[]{ ImGui::PopStyleVar(); }}; +} + +[[nodiscard]] static raii_wrapper push_style_color(ImGuiCol_ var, const Color4& value) +{ + ImGui::PushStyleColor(var, {value[0], value[1], value[2], value[3]}); + return {[]{ ImGui::PopStyleColor(); }}; +} + void app::setup_menu() { GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add, GL::Renderer::BlendEquation::Add); @@ -106,30 +132,42 @@ void app::draw_menu() } if (main_menu_pos.y > 0) { - return; ImGui::SetNextWindowPos({0, main_menu_pos.y+style.WindowPadding.y}); ImGui::SetNextFrameWantCaptureKeyboard(false); + ImGui::SetNextWindowSize({450, windowSize()[1] - main_menu_pos.y - style.WindowPadding.y*2}); if (auto b = begin_window(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings)) { ImGui::Text("Items:"); //ImGui::SetNextWindowBgAlpha(.2f); - if (auto b = begin_list_box("##tiles", {200, 200})) + if (auto b = begin_list_box("##tiles", {-FLT_MIN, -1})) { for (const auto& [k, v] : _editor.floors()) { const std::size_t N = v->num_tiles().product(); - ImGui::CollapsingHeader(k.data()); - constexpr std::size_t per_row = 8; - for (std::size_t i = 0; i < N; i++) + if (auto b = tree_node(k.data())) { - if (i > 0 && i % per_row == 0) - ImGui::NewLine(); - const auto uv = v->texcoords_for_id(i); - ImGui::Image((void*)0, - {TILE_SIZE[0], TILE_SIZE[1]}, - { uv[3][0], uv[3][1] }, { uv[0][0], uv[0][1] }, - {1, 1, 1, 1}, {1, 1, 1, 1}); + auto c = push_style_var(ImGuiStyleVar_FramePadding, {1, 1}); + auto c2 = push_style_var(ImGuiStyleVar_FrameBorderSize, 3); + auto c3 = push_style_color(ImGuiCol_Button, {1, 1, 1, 1}); + constexpr std::size_t per_row = 8; + for (std::size_t i = 0; i < N; i++) + { + if (i > 0 && i % per_row == 0) + ImGui::NewLine(); + char buf[64]; + sprintf(buf, "##item_%zu", 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()) + { + printf("clicked %s %zu\n", buf+2, i); + fflush(stdout); + } + ImGui::SameLine(); + } + ImGui::NewLine(); } } } diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp index c3b2449c..50f28cd0 100644 --- a/src/tile-atlas.cpp +++ b/src/tile-atlas.cpp @@ -31,12 +31,11 @@ std::array<Vector2, 4> tile_atlas::texcoords_for_id(std::size_t id_) const const Vector2ui id = { (UnsignedInt)id_ % dims_[0], (UnsignedInt)id_ / dims_[0] }; const Vector2 p0(id * sz), p1(sz); const auto x0 = p0.x(), x1 = p1.x()-1, y0 = p0.y(), y1 = p1.y()-1; - Vector2 size{size_}; return {{ - { (x0+x1)/size[0], (y0+y1)/size[1] }, // bottom right - { (x0+x1)/size[0], y0/size[1] }, // top right - { x0/size[0], (y0+y1)/size[1] }, // bottom left - { x0/size[0], y0/size[1] } // top left + { (x0+x1)/size_[0], (y0+y1)/size_[1] }, // bottom right + { (x0+x1)/size_[0], y0/size_[1] }, // top right + { x0/size_[0], (y0+y1)/size_[1] }, // bottom left + { x0/size_[0], y0/size_[1] } // top left }}; } |