From 689e04da905c1ceb8142975d3306c7b334eebbc9 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 18 Oct 2022 05:32:30 +0200 Subject: a --- main/app.cpp | 32 ++++++--- main/app.hpp | 13 ++-- main/camera.cpp | 4 +- main/draw.cpp | 2 +- main/gui.cpp | 199 ------------------------------------------------------ main/imgui.cpp | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main/keyboard.cpp | 14 ---- 7 files changed, 231 insertions(+), 232 deletions(-) delete mode 100644 main/gui.cpp create mode 100644 main/imgui.cpp (limited to 'main') diff --git a/main/app.cpp b/main/app.cpp index f60b1287..84831978 100644 --- a/main/app.cpp +++ b/main/app.cpp @@ -81,10 +81,8 @@ void app::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event) void app::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event) { - if (_imgui.handleMouseMoveEvent(event) && false) - return _cursor_tile = std::nullopt, event.setAccepted(); - - _cursor_pos = event.position(); + _cursor_in_imgui = _imgui.handleMouseMoveEvent(event); + _cursor_pixel = event.position(); recalc_cursor_tile(); } @@ -100,15 +98,29 @@ void app::textInputEvent(Platform::Sdl2Application::TextInputEvent& event) return keys = {}, event.setAccepted(); } +void app::keyPressEvent(Platform::Sdl2Application::KeyEvent& event) +{ + if (_imgui.handleKeyPressEvent(event)) + return event.setAccepted(); + do_key(event.key(), event.modifiers(), true, event.isRepeated()); +} + +void app::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event) +{ + if (_imgui.handleKeyReleaseEvent(event)) + return keys = {}, event.setAccepted(); + do_key(event.key(), event.modifiers(), false, false); +} + void app::anyEvent(SDL_Event& event) { if (event.type == SDL_WINDOWEVENT) switch (event.window.event) { case SDL_WINDOWEVENT_FOCUS_LOST: - return event_leave(); + return event_focus_out(); case SDL_WINDOWEVENT_FOCUS_GAINED: - return event_enter(); + return event_focus_in(); case SDL_WINDOWEVENT_LEAVE: return event_mouse_leave(); case SDL_WINDOWEVENT_ENTER: @@ -118,19 +130,19 @@ void app::anyEvent(SDL_Event& event) } } -void app::event_leave() +void app::event_focus_out() { - _cursor_pos = std::nullopt; + _cursor_pixel = std::nullopt; _cursor_tile = std::nullopt; } -void app::event_enter() +void app::event_focus_in() { } void app::event_mouse_leave() { - _cursor_pos = std::nullopt; + _cursor_pixel = std::nullopt; _cursor_tile = std::nullopt; } diff --git a/main/app.hpp b/main/app.hpp index 89439dc0..7417962e 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -37,18 +37,18 @@ struct app final : Platform::Application void update_window_scale(Vector2i window_size); void recalc_cursor_tile(); - void keyPressEvent(KeyEvent& event) override; - void keyReleaseEvent(KeyEvent& event) override; + void viewportEvent(ViewportEvent& event) override; void mousePressEvent(MouseEvent& event) override; void mouseReleaseEvent(MouseEvent& event) override; void mouseMoveEvent(MouseMoveEvent& event) override; void mouseScrollEvent(MouseScrollEvent& event) override; void textInputEvent(TextInputEvent& event) override; - void viewportEvent(ViewportEvent& event) override; + void keyPressEvent(KeyEvent& event) override; + void keyReleaseEvent(KeyEvent& event) override; void anyEvent(SDL_Event& event) override; - void event_leave(); - void event_enter(); + void event_focus_out(); + void event_focus_in(); void event_mouse_enter(); void event_mouse_leave(); @@ -97,9 +97,10 @@ struct app final : Platform::Application enum_bitset keys; Magnum::Timeline timeline; editor _editor; - std::optional _cursor_pos; + std::optional _cursor_pixel; std::optional _cursor_tile; float _frame_time = 0; + bool _cursor_in_imgui = false; static constexpr std::int16_t BASE_X = 0, BASE_Y = 0; }; diff --git a/main/camera.cpp b/main/camera.cpp index 7086b562..c3a3b2f7 100644 --- a/main/camera.cpp +++ b/main/camera.cpp @@ -48,8 +48,8 @@ void app::update_window_scale(Vector2i sz) void app::recalc_cursor_tile() { - if (_cursor_pos) - _cursor_tile = pixel_to_tile(Vector2d(*_cursor_pos)); + if (_cursor_pixel) + _cursor_tile = pixel_to_tile(Vector2d(*_cursor_pixel)); else _cursor_tile = std::nullopt; } diff --git a/main/draw.cpp b/main/draw.cpp index 6d165c89..85cabb90 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -94,7 +94,7 @@ void app::draw_wireframe_box(local_coords pt) void app::draw_cursor_tile() { - if (_cursor_tile) + if (_cursor_tile && !_cursor_in_imgui) draw_wireframe_quad(*_cursor_tile); } diff --git a/main/gui.cpp b/main/gui.cpp deleted file mode 100644 index 8d2cb97e..00000000 --- a/main/gui.cpp +++ /dev/null @@ -1,199 +0,0 @@ -#include "app.hpp" -#include -#ifndef __CLION_IDE__ -#include "imgui-raii.hpp" -#include -#endif - -namespace floormat { - -using namespace floormat::imgui; - -void app::display_menu() -{ - GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add, GL::Renderer::BlendEquation::Add); - GL::Renderer::setBlendFunction(GL::Renderer::BlendFunction::SourceAlpha, GL::Renderer::BlendFunction::OneMinusSourceAlpha); - GL::Renderer::enable(GL::Renderer::Feature::Blending); - - GL::Renderer::enable(GL::Renderer::Feature::ScissorTest); - GL::Renderer::disable(GL::Renderer::Feature::FaceCulling); - GL::Renderer::disable(GL::Renderer::Feature::DepthTest); - - _imgui.drawFrame(); -} - -float app::draw_main_menu() -{ - float main_menu_height = 0; - if (auto b = begin_main_menu()) - { - if (auto b = begin_menu("File")) - { - ImGui::MenuItem("Open", "Ctrl+O"); - ImGui::MenuItem("Recent"); - ImGui::Separator(); - ImGui::MenuItem("Save", "Ctrl+S"); - ImGui::MenuItem("Save as...", "Ctrl+Shift+S"); - ImGui::Separator(); - ImGui::MenuItem("Close"); - } - if (auto b = begin_menu("Mode")) - { - ImGui::MenuItem("Select", "F1", _editor.mode() == editor_mode::select); - ImGui::MenuItem("Floor", "F2", _editor.mode() == editor_mode::floor); - ImGui::MenuItem("Walls", "F3", _editor.mode() == editor_mode::walls); - } - - main_menu_height = ImGui::GetContentRegionMax().y; - } - return main_menu_height; -} - -void app::draw_ui() -{ - _imgui.newFrame(); - ImGui::StyleColorsDark(&ImGui::GetStyle()); - - const float main_menu_height = draw_main_menu(); - draw_editor_pane(_editor.floor(), main_menu_height); - draw_fps(); - draw_cursor_coord(); -} - -void app::draw_editor_pane(tile_type& type, float main_menu_height) -{ - if (ImGui::GetIO().WantTextInput && !isTextInputActive()) - startTextInput(); - else if (!ImGui::GetIO().WantTextInput && isTextInputActive()) - stopTextInput(); - - const raii_wrapper vars[] = { - push_style_var(ImGuiStyleVar_WindowPadding, {8, 8}), - push_style_var(ImGuiStyleVar_WindowBorderSize, 0), - push_style_var(ImGuiStyleVar_FramePadding, {4, 4}), - push_style_color(ImGuiCol_WindowBg, {0, 0, 0, .5}), - push_style_color(ImGuiCol_FrameBg, {0, 0, 0, 0}), - }; - - const auto& style = ImGui::GetStyle(); - - if (main_menu_height > 0) - { - ImGui::SetNextWindowPos({0, main_menu_height+style.WindowPadding.y}); - ImGui::SetNextFrameWantCaptureKeyboard(false); - ImGui::SetNextWindowSize({450, windowSize()[1] - main_menu_height - style.WindowPadding.y}); - if (const auto flags = ImGuiWindowFlags_(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings); - auto b = begin_window({}, flags)) - { - 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] : type) - { - ///const auto& k_ = k; - const auto& v_ = v; - const auto click_event = [&] { - if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) - _editor.floor().select_tile_permutation(v_); - }; - 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 (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}), - }; - 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(); - snprintf(buf, sizeof(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()) - _editor.floor().select_tile(v, (std::uint8_t)i); - else - click_event(); - ImGui::SameLine(); - } - ImGui::NewLine(); - } - else - { - click_event(); - add_tile_count(); - } - } - } - } - } -} - -void app::draw_fps() -{ - const ImVec2 max_size = ImGui::CalcTextSize("999.1 FPS"); - auto c1 = push_style_var(ImGuiStyleVar_FramePadding, {0, 0}); - auto c2 = push_style_var(ImGuiStyleVar_WindowPadding, {0, 0}); - auto c3 = push_style_var(ImGuiStyleVar_WindowBorderSize, 0); - auto c4 = push_style_var(ImGuiStyleVar_WindowMinSize, {1, 1}); - auto c5 = push_style_var(ImGuiStyleVar_ScrollbarSize, 0); - auto c6 = push_style_color(ImGuiCol_Text, {0, 1, 0, 1}); - ImGui::SetNextWindowPos({windowSize()[0] - 5 - max_size.x, 3}); - ImGui::SetNextWindowSize(max_size); - if (auto flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoInputs | - ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground; - auto b = begin_window("framerate", ImGuiWindowFlags_(flags))) - { - const double dt = _frame_time > 1e-6 ? std::round(1/double(_frame_time)*10.)*.1 + 0.05 : 999; - char buf[16]; - snprintf(buf, sizeof(buf), "%.1f FPS", dt); - ImGui::SameLine(max_size.x - ImGui::CalcTextSize(buf).x); - ImGui::Text("%s", buf); - } -} - -void app::draw_cursor_coord() -{ - if (!_cursor_tile) - return; - - auto c1 = push_style_var(ImGuiStyleVar_FramePadding, {0, 0}); - auto c2 = push_style_var(ImGuiStyleVar_WindowPadding, {0, 0}); - auto c3 = push_style_var(ImGuiStyleVar_WindowBorderSize, 0); - auto c4 = push_style_var(ImGuiStyleVar_WindowMinSize, {1, 1}); - auto c5 = push_style_var(ImGuiStyleVar_ScrollbarSize, 0); - auto c6 = push_style_color(ImGuiCol_Text, {.9, .9, .9, 1}); - - char buf[64]; - const auto coord = *_cursor_tile; - const auto chunk = coord.chunk(); - const auto local = coord.local(); - snprintf(buf, sizeof(buf), "%hd:%hd - %hhu:%hhu", chunk.x, chunk.y, local.x, local.y); - const auto size = ImGui::CalcTextSize(buf); - - ImGui::SetNextWindowPos({windowSize()[0]/2 - size.x/2, 3}); - ImGui::SetNextWindowSize(size); - if (auto flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoInputs | - ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground; - auto b = begin_window("tile-coord", ImGuiWindowFlags_(flags))) - { - ImGui::Text("%s", buf); - } -} - -} // namespace floormat diff --git a/main/imgui.cpp b/main/imgui.cpp new file mode 100644 index 00000000..8d2cb97e --- /dev/null +++ b/main/imgui.cpp @@ -0,0 +1,199 @@ +#include "app.hpp" +#include +#ifndef __CLION_IDE__ +#include "imgui-raii.hpp" +#include +#endif + +namespace floormat { + +using namespace floormat::imgui; + +void app::display_menu() +{ + GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add, GL::Renderer::BlendEquation::Add); + GL::Renderer::setBlendFunction(GL::Renderer::BlendFunction::SourceAlpha, GL::Renderer::BlendFunction::OneMinusSourceAlpha); + GL::Renderer::enable(GL::Renderer::Feature::Blending); + + GL::Renderer::enable(GL::Renderer::Feature::ScissorTest); + GL::Renderer::disable(GL::Renderer::Feature::FaceCulling); + GL::Renderer::disable(GL::Renderer::Feature::DepthTest); + + _imgui.drawFrame(); +} + +float app::draw_main_menu() +{ + float main_menu_height = 0; + if (auto b = begin_main_menu()) + { + if (auto b = begin_menu("File")) + { + ImGui::MenuItem("Open", "Ctrl+O"); + ImGui::MenuItem("Recent"); + ImGui::Separator(); + ImGui::MenuItem("Save", "Ctrl+S"); + ImGui::MenuItem("Save as...", "Ctrl+Shift+S"); + ImGui::Separator(); + ImGui::MenuItem("Close"); + } + if (auto b = begin_menu("Mode")) + { + ImGui::MenuItem("Select", "F1", _editor.mode() == editor_mode::select); + ImGui::MenuItem("Floor", "F2", _editor.mode() == editor_mode::floor); + ImGui::MenuItem("Walls", "F3", _editor.mode() == editor_mode::walls); + } + + main_menu_height = ImGui::GetContentRegionMax().y; + } + return main_menu_height; +} + +void app::draw_ui() +{ + _imgui.newFrame(); + ImGui::StyleColorsDark(&ImGui::GetStyle()); + + const float main_menu_height = draw_main_menu(); + draw_editor_pane(_editor.floor(), main_menu_height); + draw_fps(); + draw_cursor_coord(); +} + +void app::draw_editor_pane(tile_type& type, float main_menu_height) +{ + if (ImGui::GetIO().WantTextInput && !isTextInputActive()) + startTextInput(); + else if (!ImGui::GetIO().WantTextInput && isTextInputActive()) + stopTextInput(); + + const raii_wrapper vars[] = { + push_style_var(ImGuiStyleVar_WindowPadding, {8, 8}), + push_style_var(ImGuiStyleVar_WindowBorderSize, 0), + push_style_var(ImGuiStyleVar_FramePadding, {4, 4}), + push_style_color(ImGuiCol_WindowBg, {0, 0, 0, .5}), + push_style_color(ImGuiCol_FrameBg, {0, 0, 0, 0}), + }; + + const auto& style = ImGui::GetStyle(); + + if (main_menu_height > 0) + { + ImGui::SetNextWindowPos({0, main_menu_height+style.WindowPadding.y}); + ImGui::SetNextFrameWantCaptureKeyboard(false); + ImGui::SetNextWindowSize({450, windowSize()[1] - main_menu_height - style.WindowPadding.y}); + if (const auto flags = ImGuiWindowFlags_(ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings); + auto b = begin_window({}, flags)) + { + 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] : type) + { + ///const auto& k_ = k; + const auto& v_ = v; + const auto click_event = [&] { + if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) + _editor.floor().select_tile_permutation(v_); + }; + 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 (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}), + }; + 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(); + snprintf(buf, sizeof(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()) + _editor.floor().select_tile(v, (std::uint8_t)i); + else + click_event(); + ImGui::SameLine(); + } + ImGui::NewLine(); + } + else + { + click_event(); + add_tile_count(); + } + } + } + } + } +} + +void app::draw_fps() +{ + const ImVec2 max_size = ImGui::CalcTextSize("999.1 FPS"); + auto c1 = push_style_var(ImGuiStyleVar_FramePadding, {0, 0}); + auto c2 = push_style_var(ImGuiStyleVar_WindowPadding, {0, 0}); + auto c3 = push_style_var(ImGuiStyleVar_WindowBorderSize, 0); + auto c4 = push_style_var(ImGuiStyleVar_WindowMinSize, {1, 1}); + auto c5 = push_style_var(ImGuiStyleVar_ScrollbarSize, 0); + auto c6 = push_style_color(ImGuiCol_Text, {0, 1, 0, 1}); + ImGui::SetNextWindowPos({windowSize()[0] - 5 - max_size.x, 3}); + ImGui::SetNextWindowSize(max_size); + if (auto flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoInputs | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground; + auto b = begin_window("framerate", ImGuiWindowFlags_(flags))) + { + const double dt = _frame_time > 1e-6 ? std::round(1/double(_frame_time)*10.)*.1 + 0.05 : 999; + char buf[16]; + snprintf(buf, sizeof(buf), "%.1f FPS", dt); + ImGui::SameLine(max_size.x - ImGui::CalcTextSize(buf).x); + ImGui::Text("%s", buf); + } +} + +void app::draw_cursor_coord() +{ + if (!_cursor_tile) + return; + + auto c1 = push_style_var(ImGuiStyleVar_FramePadding, {0, 0}); + auto c2 = push_style_var(ImGuiStyleVar_WindowPadding, {0, 0}); + auto c3 = push_style_var(ImGuiStyleVar_WindowBorderSize, 0); + auto c4 = push_style_var(ImGuiStyleVar_WindowMinSize, {1, 1}); + auto c5 = push_style_var(ImGuiStyleVar_ScrollbarSize, 0); + auto c6 = push_style_color(ImGuiCol_Text, {.9, .9, .9, 1}); + + char buf[64]; + const auto coord = *_cursor_tile; + const auto chunk = coord.chunk(); + const auto local = coord.local(); + snprintf(buf, sizeof(buf), "%hd:%hd - %hhu:%hhu", chunk.x, chunk.y, local.x, local.y); + const auto size = ImGui::CalcTextSize(buf); + + ImGui::SetNextWindowPos({windowSize()[0]/2 - size.x/2, 3}); + ImGui::SetNextWindowSize(size); + if (auto flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoInputs | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground; + auto b = begin_window("tile-coord", ImGuiWindowFlags_(flags))) + { + ImGui::Text("%s", buf); + } +} + +} // namespace floormat diff --git a/main/keyboard.cpp b/main/keyboard.cpp index 66dafa7e..b40daec4 100644 --- a/main/keyboard.cpp +++ b/main/keyboard.cpp @@ -32,18 +32,4 @@ app::~app() loader_::destroy(); } -void app::keyPressEvent(Platform::Sdl2Application::KeyEvent& event) -{ - if (_imgui.handleKeyPressEvent(event)) - return event.setAccepted(); - do_key(event.key(), event.modifiers(), true, event.isRepeated()); -} - -void app::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event) -{ - if (_imgui.handleKeyReleaseEvent(event)) - return keys = {}, event.setAccepted(); - do_key(event.key(), event.modifiers(), false, false); -} - } // namespace floormat -- cgit v1.2.3