summaryrefslogtreecommitdiffhomepage
path: root/main/menu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main/menu.cpp')
-rw-r--r--main/menu.cpp90
1 files changed, 58 insertions, 32 deletions
diff --git a/main/menu.cpp b/main/menu.cpp
index f49f4105..3357aa6e 100644
--- a/main/menu.cpp
+++ b/main/menu.cpp
@@ -5,8 +5,6 @@
namespace floormat {
-constexpr inline auto noop = []{};
-
struct raii_wrapper final
{
using F = void(*)(void);
@@ -37,22 +35,22 @@ constexpr inline const auto* imgui_name = "floormat editor";
else
return {};
}
-[[nodiscard]] static raii_wrapper begin_menu(const char* name, bool enabled = true) {
- if (ImGui::BeginMenu(name, enabled))
+[[nodiscard]] static raii_wrapper begin_menu(Containers::StringView name, bool enabled = true) {
+ if (ImGui::BeginMenu(name.data(), enabled))
return {&ImGui::EndMenu};
else
return {};
}
-[[nodiscard]] static raii_wrapper begin_list_box(const char* name, ImVec2 size = {}) {
- if (ImGui::BeginListBox(name, size))
+[[nodiscard]] static raii_wrapper begin_list_box(Containers::StringView name, ImVec2 size = {}) {
+ if (ImGui::BeginListBox(name.data(), size))
return {&ImGui::EndListBox};
else
return {};
}
-[[nodiscard]] static raii_wrapper tree_node(const char* name) {
- if (ImGui::TreeNode(name))
+[[nodiscard]] static raii_wrapper tree_node(Containers::StringView name, ImGuiTreeNodeFlags_ flags = {}) {
+ if (ImGui::TreeNodeEx(name.data(), flags))
return {&ImGui::TreePop};
else
return {};
@@ -95,21 +93,7 @@ void app::display_menu()
void app::draw_menu()
{
_imgui.newFrame();
-
- if (ImGui::GetIO().WantTextInput && !isTextInputActive())
- startTextInput();
- else if (!ImGui::GetIO().WantTextInput && isTextInputActive())
- stopTextInput();
-
- auto& style = ImGui::GetStyle();
- ImGui::StyleColorsDark(&style);
- style.WindowPadding = {8, 8};
- style.WindowBorderSize = {};
- style.Colors[ImGuiCol_WindowBg] = {0, 0, 0, .5};
- style.Colors[ImGuiCol_FrameBg] = {0, 0, 0, 0};
-
- ImVec2 main_menu_pos;
-
+ float main_menu_height = 0;
if (auto b = begin_main_menu())
{
if (auto b = begin_menu("File"))
@@ -128,25 +112,61 @@ void app::draw_menu()
ImGui::MenuItem("Floors", "F2", _editor.mode() == editor_mode::floors);
ImGui::MenuItem("Walls", "F3", _editor.mode() == editor_mode::walls);
}
- main_menu_pos = ImGui::GetContentRegionMax();
+
+ main_menu_height = ImGui::GetContentRegionMax().y;
}
- if (main_menu_pos.y > 0)
+ draw_menu_(_editor.floors(), main_menu_height);
+}
+
+void app::draw_menu_(tile_type& type, float main_menu_height)
+{
+ if (ImGui::GetIO().WantTextInput && !isTextInputActive())
+ startTextInput();
+ else if (!ImGui::GetIO().WantTextInput && isTextInputActive())
+ stopTextInput();
+
+ auto& style = ImGui::GetStyle();
+ ImGui::StyleColorsDark(&style);
+ style.WindowPadding = {8, 8};
+ style.WindowBorderSize = {};
+ style.FramePadding = {4, 4};
+ style.Colors[ImGuiCol_WindowBg] = {0, 0, 0, .5};
+ style.Colors[ImGuiCol_FrameBg] = {0, 0, 0, 0};
+
+ if (main_menu_height > 0)
{
- ImGui::SetNextWindowPos({0, main_menu_pos.y+style.WindowPadding.y});
+ ImGui::SetNextWindowPos({0, main_menu_height+style.WindowPadding.y});
ImGui::SetNextFrameWantCaptureKeyboard(false);
- ImGui::SetNextWindowSize({450, windowSize()[1] - main_menu_pos.y - style.WindowPadding.y*2});
+ ImGui::SetNextWindowSize({450, windowSize()[1] - main_menu_height - style.WindowPadding.y});
if (auto b = begin_window(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings))
{
- ImGui::Text("Items:");
+ const float window_width = ImGui::GetWindowWidth() - 32;
+
+ char buf[64];
//ImGui::SetNextWindowBgAlpha(.2f);
if (auto b = begin_list_box("##tiles", {-FLT_MIN, -1}))
{
- for (const auto& [k, v] : _editor.floors())
+ for (const auto& [k, v] : type)
{
+ const auto& k_ = k;
+ const auto& v_ = v;
+ const auto click_event = [&] {
+ if (ImGui::IsItemClicked(ImGuiMouseButton_Right) && ImGui::GetIO().MouseClicked[1])
+ {
+ Debug{} << "shuffle" << k_.data();
+ }
+ };
+ const auto add_tile_count = [&] {
+ snprintf(buf, sizeof(buf), "%zu", (std::size_t)v_->num_tiles().product());
+ ImGui::SameLine(window_width - ImGui::CalcTextSize(buf).x - style.FramePadding.x - 4);
+ ImGui::Text("%s", buf);
+ };
const std::size_t N = v->num_tiles().product();
- if (auto b = tree_node(k.data()))
+ if (auto b = tree_node(k.data(), ImGuiTreeNodeFlags_(ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Framed)))
{
+ click_event();
+ add_tile_count();
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});
@@ -155,20 +175,26 @@ void app::draw_menu()
{
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);
+ Debug{} << "tile" << buf+2 << i;
fflush(stdout);
}
+ else
+ click_event();
ImGui::SameLine();
}
ImGui::NewLine();
}
+ else
+ {
+ click_event();
+ add_tile_count();
+ }
}
}
}