From 927200de5715970406c85dd0de908cd13d739d4a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 15 Mar 2025 02:41:41 +0000 Subject: Update ImGui to latest --- thirdparty/imgui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thirdparty/imgui b/thirdparty/imgui index 2db79d0..b4c9635 160000 --- a/thirdparty/imgui +++ b/thirdparty/imgui @@ -1 +1 @@ -Subproject commit 2db79d0868f7b02d26f7557a72504a0b6f844937 +Subproject commit b4c96355c9b51b54c4deb52e7d7cdfc7bf79bc2f -- cgit v1.2.3 From 92a67da8fa1734732a01a0a7d12fc3f6d697b2b3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 15 Mar 2025 02:42:10 +0000 Subject: Wrap imgui_intenal if requested --- ui/imgui_wrap.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ui/imgui_wrap.h b/ui/imgui_wrap.h index 1d619a4..520d8b8 100644 --- a/ui/imgui_wrap.h +++ b/ui/imgui_wrap.h @@ -3,4 +3,11 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wold-style-cast" #include "imgui.h" // IWYU pragma: export +#ifdef IMGUI_INTERNAL +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wconversion" +# pragma GCC diagnostic ignored "-Wsign-conversion" +# include "imgui_internal.h" // IWYU pragma: export +# pragma GCC diagnostic pop +#endif #pragma GCC diagnostic pop -- cgit v1.2.3 From b3378e7ed11f7f4ec16e1eb268768791d6861331 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 15 Mar 2025 02:50:08 +0000 Subject: Add ImGui wrappers for creating a toolbar docked to a viewport edge Based on, but simplified, code for BeginMainMenuBar. --- ui/imgui_extras.cpp | 33 +++++++++++++++++++++++++++++++++ ui/imgui_extras.h | 10 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 ui/imgui_extras.cpp create mode 100644 ui/imgui_extras.h diff --git a/ui/imgui_extras.cpp b/ui/imgui_extras.cpp new file mode 100644 index 0000000..1643f4f --- /dev/null +++ b/ui/imgui_extras.cpp @@ -0,0 +1,33 @@ +#define IMGUI_INTERNAL +#include "imgui_extras.h" + +namespace IltGui { + bool + BeginToolbar(const char * name, ImGuiDir dir, float axisSize, ImGuiWindowFlags windowFlags) + { + return BeginToolbar(name, ImGui::GetMainViewport(), dir, axisSize, windowFlags); + } + + bool + BeginToolbar( + const char * name, ImGuiViewport * viewport, ImGuiDir dir, float axisSize, ImGuiWindowFlags windowFlags) + { + bool isOpen = ImGui::BeginViewportSideBar(name, viewport, dir, axisSize, + windowFlags | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings); + if (isOpen) { + if (dir == ImGuiDir_Up || dir == ImGuiDir_Down) { + ImGui::GetCurrentWindow()->DC.LayoutType = ImGuiLayoutType_Horizontal; + } + } + else { + ImGui::End(); + } + return isOpen; + } + + void + EndToolbar() + { + ImGui::End(); + } +} diff --git a/ui/imgui_extras.h b/ui/imgui_extras.h new file mode 100644 index 0000000..0babaa3 --- /dev/null +++ b/ui/imgui_extras.h @@ -0,0 +1,10 @@ +#include "imgui_wrap.h" + +namespace IltGui { + // NOLINTBEGIN(readability-identifier-naming) + bool BeginToolbar(const char * name, ImGuiViewport * viewport, ImGuiDir dir, float axisSize, + ImGuiWindowFlags windowFlags = 0); + bool BeginToolbar(const char * name, ImGuiDir dir, float axisSize, ImGuiWindowFlags windowFlags = 0); + void EndToolbar(); + // NOLINTEND(readability-identifier-naming) +} -- cgit v1.2.3 From 57c64359bd759ca23ec56affe561ea7fd97909a5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 15 Mar 2025 13:42:49 +0000 Subject: Tidy glArrays, fixes warnings --- lib/glArrays.h | 26 ++++++++++++++++++++------ test/test-lib.cpp | 4 ++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/glArrays.h b/lib/glArrays.h index 787ea17..842a593 100644 --- a/lib/glArrays.h +++ b/lib/glArrays.h @@ -6,6 +6,7 @@ #include #include +// NOLINTNEXTLINE(readability-identifier-naming) template class glArraysBase { static_assert(N > 0); @@ -15,20 +16,30 @@ public: CUSTOM_MOVE(glArraysBase); // NOLINTNEXTLINE(hicpp-explicit-conversions) - inline operator GLuint() const + requires(N == 1) { - static_assert(N == 1, "Implicit cast only if N == 1"); return ids.front(); } - inline auto + GLuint + operator*() const + requires(N == 1) + { + return ids.front(); + } + + const auto & operator[](size_t n) const { return ids[n]; } - constexpr static auto size {N}; + constexpr static auto + size() + { + return N; + } protected: glArraysBase() noexcept = default; @@ -49,6 +60,7 @@ glArraysBase::operator=(glArraysBase && src) noexcept return *this; } +// NOLINTNEXTLINE(readability-identifier-naming) template class glArrays : public glArraysBase { public: using glArraysBase::glArraysBase; @@ -56,12 +68,12 @@ public: DEFAULT_MOVE_COPY(glArrays); - inline glArrays() noexcept + glArrays() noexcept { (*Gen)(N, this->ids.data()); } - inline ~glArrays() noexcept + ~glArrays() noexcept { if (this->ids.front()) { (*Del)(N, this->ids.data()); @@ -69,6 +81,7 @@ public: } }; +// NOLINTBEGIN(readability-identifier-naming) template using glVertexArrays = glArrays; using glVertexArray = glVertexArrays<1>; template using glBuffers = glArrays; @@ -79,3 +92,4 @@ template using glFrameBuffers = glArrays; template using glRenderBuffers = glArrays; using glRenderBuffer = glRenderBuffers<1>; +// NOLINTEND(readability-identifier-naming) diff --git a/test/test-lib.cpp b/test/test-lib.cpp index ec91f6e..a1a8c80 100644 --- a/test/test-lib.cpp +++ b/test/test-lib.cpp @@ -46,9 +46,9 @@ BOOST_AUTO_TEST_CASE(generate_move_and_delete) { { TestArray a; - BOOST_CHECK_EQUAL(TestArray::size, active.size()); + BOOST_CHECK_EQUAL(TestArray::size(), active.size()); const TestArray b {std::move(a)}; - BOOST_CHECK_EQUAL(TestArray::size, active.size()); + BOOST_CHECK_EQUAL(TestArray::size(), active.size()); } BOOST_CHECK(active.empty()); } -- cgit v1.2.3 From 9d200bc8f749f8d64e4f1e630ddf3a797b9b8fe1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 15 Mar 2025 13:50:51 +0000 Subject: Add operator to get ImTextureID from an Icon --- ui/icon.cpp | 8 ++++++++ ui/icon.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/ui/icon.cpp b/ui/icon.cpp index c3b5078..847b117 100644 --- a/ui/icon.cpp +++ b/ui/icon.cpp @@ -28,3 +28,11 @@ Icon::Bind() const glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_texture); } + +ImTextureID +Icon::operator*() const +{ + static_assert(sizeof(m_texture) <= sizeof(ImTextureID)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr) This is how ImGui works + return reinterpret_cast(*m_texture); +} diff --git a/ui/icon.h b/ui/icon.h index 76cd3ae..5f3a812 100644 --- a/ui/icon.h +++ b/ui/icon.h @@ -1,5 +1,6 @@ #pragma once +#include "imgui_wrap.h" #include #include #include @@ -13,6 +14,7 @@ public: void Bind() const; const glm::vec2 size; + ImTextureID operator*() const; private: glTexture m_texture; -- cgit v1.2.3 From ced16a1bb9ef735a2fe44b64ac594bf9b4a7f9bf Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 19 Mar 2025 01:44:18 +0000 Subject: Replace basic rail builder UI with a ImGui version --- ui/editNetwork.cpp | 35 ++++++++++++++++++----------------- ui/editNetwork.h | 5 +---- ui/gameMainSelector.cpp | 2 +- ui/gameMainSelector.h | 2 +- ui/gameMainWindow.cpp | 32 ++++++++++++++++++++++++-------- 5 files changed, 45 insertions(+), 31 deletions(-) diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp index 2887491..a11fe3c 100644 --- a/ui/editNetwork.cpp +++ b/ui/editNetwork.cpp @@ -2,6 +2,7 @@ #include "builders/freeExtend.h" #include "builders/join.h" #include "builders/straight.h" +#include "imgui_wrap.h" #include "text.h" #include #include @@ -11,16 +12,7 @@ const std::filesystem::path fontpath {"/usr/share/fonts/hack/Hack-Regular.ttf"}; constexpr const glm::u8vec4 TRANSPARENT_BLUE {30, 50, 255, 200}; -EditNetwork::EditNetwork(Network * n) : - network {n}, - builderToolbar { - {"ui/icon/network.png", mode.toggle()}, - {"ui/icon/network.png", mode.toggle()}, - {"ui/icon/network.png", mode.toggle()}, - }, - blue {1, 1, &TRANSPARENT_BLUE}, font {fontpath, 15} -{ -} +EditNetwork::EditNetwork(Network * n) : network {n}, blue {1, 1, &TRANSPARENT_BLUE}, font {fontpath, 15} { } bool EditNetwork::click(const SDL_MouseButtonEvent & e, const Ray & ray) @@ -42,9 +34,9 @@ EditNetwork::move(const SDL_MouseMotionEvent & e, const Ray & } bool -EditNetwork::handleInput(const SDL_Event & e, const UIComponent::Position & parentPos) +EditNetwork::handleInput(const SDL_Event &, const UIComponent::Position &) { - return builderToolbar.handleInput(e, parentPos); + return false; } void @@ -75,10 +67,19 @@ EditNetwork::Builder::setHeightsFor(Network * network, const Link::CCollection & } void -EditNetwork::render(const UIShader & shader, const UIComponent::Position & parentPos) const +EditNetwork::render(const UIShader &, const UIComponent::Position &) { - if (builder) { - Text {builder->hint(), font, {{50, 10}, {0, 15}}, {1, 1, 0}}.render(shader, parentPos); - } - builderToolbar.render(shader, parentPos); + ImGui::Begin("Edit Network"); + + auto builderChoice = [this](const char * name) { + if (ImGui::RadioButton(name, dynamic_cast(builder.get()))) { + builder = std::make_unique(); + } + }; + builderChoice.operator()("Straight"); + builderChoice.operator()("Join"); + builderChoice.operator()("Free Extend"); + ImGui::TextUnformatted(builder ? builder->hint().c_str() : "Select a build mode"); + + ImGui::End(); } diff --git a/ui/editNetwork.h b/ui/editNetwork.h index ae887bd..805e7ec 100644 --- a/ui/editNetwork.h +++ b/ui/editNetwork.h @@ -2,7 +2,6 @@ #include "game/geoData.h" #include "gameMainSelector.h" -#include "modeHelper.h" #include "toolbar.h" #include "worldOverlay.h" #include @@ -19,7 +18,7 @@ public: bool move(const SDL_MouseMotionEvent & e, const Ray &) override; bool handleInput(const SDL_Event & e, const UIComponent::Position &) override; void render(const SceneShader &, const Frustum &) const override; - void render(const UIShader & shader, const UIComponent::Position & pos) const override; + void render(const UIShader & shader, const UIComponent::Position & pos) override; using NetworkClickPos = std::variant; @@ -42,8 +41,6 @@ public: private: Network * network; Builder::Ptr builder; - Mode mode {builder}; - Toolbar builderToolbar; Texture blue; const Font font; }; diff --git a/ui/gameMainSelector.cpp b/ui/gameMainSelector.cpp index 23ae8c0..d5cbf62 100644 --- a/ui/gameMainSelector.cpp +++ b/ui/gameMainSelector.cpp @@ -110,7 +110,7 @@ GameMainSelector::Component::handleInput(const SDL_Event &, const Position &) } void -GameMainSelector::Component::render(const UIShader &, const UIComponent::Position &) const +GameMainSelector::Component::render(const UIShader &, const UIComponent::Position &) { } diff --git a/ui/gameMainSelector.h b/ui/gameMainSelector.h index e715823..16f70eb 100644 --- a/ui/gameMainSelector.h +++ b/ui/gameMainSelector.h @@ -23,7 +23,7 @@ public: virtual bool click(const SDL_MouseButtonEvent &, const Ray &); virtual bool move(const SDL_MouseMotionEvent &, const Ray &); virtual bool handleInput(const SDL_Event &, const Position & pos); - virtual void render(const UIShader & shader, const Position & pos) const; + virtual void render(const UIShader & shader, const Position & pos); virtual void render(const SceneShader &, const Frustum &) const; }; diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index d88bab5..c1e135b 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -1,10 +1,8 @@ #include "gameMainWindow.h" #include "editNetwork.h" #include "gameMainSelector.h" +#include "imgui_extras.h" #include "manualCameraController.h" -#include "modeHelper.h" -#include "toolbar.h" -#include "window.h" #include #include #include @@ -17,15 +15,33 @@ #include #include -class GameMainToolbar : Mode, public Toolbar { +class GameMainToolbar : public UIComponent { public: - explicit GameMainToolbar(GameMainSelector * gms_) : - Mode {gms_->target}, - Toolbar { - {"ui/icon/network.png", toggle>()}, + static constexpr auto TOOLBAR_HEIGHT = 54.F; + static constexpr ImVec2 TOOLBAR_ICON_SIZE {32, 32}; + + explicit GameMainToolbar(GameMainSelector * gms) : UIComponent {{{}, {}}}, gms {gms} { } + + void + render(const UIShader &, const Position &) const override + { + if (IltGui::BeginToolbar("bottomBar", ImGuiDir_Down, TOOLBAR_HEIGHT)) { + if (ImGui::ImageButton("Build rails", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { + gms->target = std::make_unique>(); + } + IltGui::EndToolbar(); } + } + + bool + handleInput(const SDL_Event &, const Position &) override { + return false; } + +private: + Icon buildRailsIcon {"ui/icon/network.png"}; + GameMainSelector * gms; }; GameMainWindow::GameMainWindow(size_t w, size_t h) : WindowContent {w, h}, SceneRenderer {{w, h}, 0} -- cgit v1.2.3 From 1f14089d0d6adbd1072b022dfaeb17a1975e8b38 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 19 Mar 2025 01:44:18 +0000 Subject: Replace basic query tool with a ImGui version --- ui/gameMainSelector.cpp | 22 +--------------------- ui/gameMainSelector.h | 2 -- ui/gameMainWindow.cpp | 4 ++++ ui/queryTool.cpp | 39 +++++++++++++++++++++++++++++++++++++++ ui/queryTool.h | 14 ++++++++++++++ 5 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 ui/queryTool.cpp create mode 100644 ui/queryTool.h diff --git a/ui/gameMainSelector.cpp b/ui/gameMainSelector.cpp index d5cbf62..55977ed 100644 --- a/ui/gameMainSelector.cpp +++ b/ui/gameMainSelector.cpp @@ -1,5 +1,4 @@ #include "gameMainSelector.h" -#include "collection.h" #include "text.h" #include "ui/uiComponent.h" #include @@ -8,9 +7,7 @@ #include #include // IWYU pragma: keep #include -#include #include -#include const std::filesystem::path fontpath {"/usr/share/fonts/hack/Hack-Regular.ttf"}; @@ -27,9 +24,6 @@ GameMainSelector::render(const UIShader & shader, const Position & parentPos) co if (target) { target->render(shader, parentPos + position + TargetPos); } - if (!clicked.empty()) { - Text {clicked, font, {{50, 10}, {0, 15}}, {1, 1, 0}}.render(shader, parentPos); - } } void @@ -73,22 +67,8 @@ GameMainSelector::handleInput(const SDL_Event & e, const Position & parentPos) } void -GameMainSelector::defaultClick(const Ray & ray) +GameMainSelector::defaultClick(const Ray &) { - BaryPosition baryPos {}; - RelativeDistance distance {}; - - if (const auto selected = gameState->world.applyOne(&Selectable::intersectRay, ray, baryPos, distance); - selected != gameState->world.end()) { - const auto & ref = *selected.base()->get(); - clicked = typeid(ref).name(); - } - else if (const auto pos = gameState->terrain->intersectRay(ray)) { - clicked = streamed_string(*pos); - } - else { - clicked.clear(); - } } bool diff --git a/ui/gameMainSelector.h b/ui/gameMainSelector.h index 16f70eb..df29842 100644 --- a/ui/gameMainSelector.h +++ b/ui/gameMainSelector.h @@ -7,7 +7,6 @@ #include "worldOverlay.h" #include #include -#include class SceneShader; template class Ray; @@ -41,5 +40,4 @@ public: private: const Camera * camera; const Font font; - std::string clicked; }; diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index c1e135b..356d522 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -3,6 +3,7 @@ #include "gameMainSelector.h" #include "imgui_extras.h" #include "manualCameraController.h" +#include "queryTool.h" #include #include #include @@ -29,6 +30,9 @@ public: if (ImGui::ImageButton("Build rails", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { gms->target = std::make_unique>(); } + if (ImGui::ImageButton("Query", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { + gms->target = std::make_unique(); + } IltGui::EndToolbar(); } } diff --git a/ui/queryTool.cpp b/ui/queryTool.cpp new file mode 100644 index 0000000..e48bae1 --- /dev/null +++ b/ui/queryTool.cpp @@ -0,0 +1,39 @@ +#include "queryTool.h" +#include "imgui_wrap.h" +#include +#include +#include +#include +#include +#include + +bool +QueryTool::click(const SDL_MouseButtonEvent & event, const Ray & ray) +{ + if (event.button != SDL_BUTTON_LEFT) { + return false; + } + BaryPosition baryPos {}; + RelativeDistance distance {}; + + if (const auto selected = gameState->world.applyOne(&Selectable::intersectRay, ray, baryPos, distance); + selected != gameState->world.end()) { + const auto & ref = *selected.base()->get(); + clicked = typeid(ref).name(); + } + else if (const auto pos = gameState->terrain->intersectRay(ray)) { + clicked = streamed_string(*pos); + } + else { + clicked.clear(); + } + return true; +} + +void +QueryTool::render(const UIShader &, const UIComponent::Position &) +{ + ImGui::Begin("Query Tool"); + ImGui::TextUnformatted(clicked.c_str()); + ImGui::End(); +} diff --git a/ui/queryTool.h b/ui/queryTool.h new file mode 100644 index 0000000..8462214 --- /dev/null +++ b/ui/queryTool.h @@ -0,0 +1,14 @@ +#pragma once + +#include "gameMainSelector.h" + +class QueryTool : public GameMainSelector::Component { +protected: + using GameMainSelector::Component::render; + + bool click(const SDL_MouseButtonEvent &, const Ray &) override; + void render(const UIShader & shader, const UIComponent::Position & pos) override; + +private: + std::string clicked; +}; -- cgit v1.2.3 From 0d432961a29d509cd0d1fa80361f04009dcf9c17 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 19 Mar 2025 03:19:38 +0000 Subject: Remove lots of stuff not required or superseded with ImGui use --- test/test-text.cpp | 14 --------- ui/editNetwork.cpp | 8 ++--- ui/editNetwork.h | 6 ++-- ui/gameMainSelector.cpp | 26 +++++++-------- ui/gameMainSelector.h | 12 +++---- ui/gameMainWindow.cpp | 11 ++++--- ui/icon.cpp | 7 ----- ui/icon.h | 1 - ui/iconButton.cpp | 57 --------------------------------- ui/iconButton.h | 26 --------------- ui/manualCameraController.cpp | 4 +-- ui/manualCameraController.h | 6 ++-- ui/modeHelper.h | 40 ------------------------ ui/queryTool.cpp | 2 +- ui/queryTool.h | 2 +- ui/text.cpp | 73 ------------------------------------------- ui/text.h | 34 -------------------- ui/toolbar.cpp | 35 --------------------- ui/toolbar.h | 23 -------------- ui/uiComponent.cpp | 33 ------------------- ui/uiComponent.h | 19 ++--------- ui/uiComponentPlacer.cpp | 27 ---------------- ui/uiComponentPlacer.h | 19 ----------- ui/windowContent.cpp | 2 +- 24 files changed, 38 insertions(+), 449 deletions(-) delete mode 100644 ui/iconButton.cpp delete mode 100644 ui/iconButton.h delete mode 100644 ui/modeHelper.h delete mode 100644 ui/text.cpp delete mode 100644 ui/text.h delete mode 100644 ui/toolbar.cpp delete mode 100644 ui/toolbar.h delete mode 100644 ui/uiComponent.cpp delete mode 100644 ui/uiComponentPlacer.cpp delete mode 100644 ui/uiComponentPlacer.h diff --git a/test/test-text.cpp b/test/test-text.cpp index b0a9503..0729ce8 100644 --- a/test/test-text.cpp +++ b/test/test-text.cpp @@ -7,7 +7,6 @@ #include "testMainWindow.h" #include "testRenderOutput.h" -#include "ui/text.h" #include #include #include @@ -112,19 +111,6 @@ BOOST_AUTO_TEST_CASE(render_font) } } -BOOST_AUTO_TEST_CASE(render_text) -{ - TestRenderOutput output; - glBindFramebuffer(GL_FRAMEBUFFER, output.output); - glViewport(0, 0, 640, 480); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - Text t {"I Like Trains", *this, {{10, 10}, {200, 40}}, {1, 1, 1}}; - UIShader s {640, 480}; - t.render(s, {}); - Texture::save(output.outImage, "/tmp/text.tga"); -} - BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_CASE(stream_vec) diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp index a11fe3c..ac9aef9 100644 --- a/ui/editNetwork.cpp +++ b/ui/editNetwork.cpp @@ -3,16 +3,14 @@ #include "builders/join.h" #include "builders/straight.h" #include "imgui_wrap.h" -#include "text.h" #include #include #include #include -const std::filesystem::path fontpath {"/usr/share/fonts/hack/Hack-Regular.ttf"}; constexpr const glm::u8vec4 TRANSPARENT_BLUE {30, 50, 255, 200}; -EditNetwork::EditNetwork(Network * n) : network {n}, blue {1, 1, &TRANSPARENT_BLUE}, font {fontpath, 15} { } +EditNetwork::EditNetwork(Network * n) : network {n}, blue {1, 1, &TRANSPARENT_BLUE} { } bool EditNetwork::click(const SDL_MouseButtonEvent & e, const Ray & ray) @@ -34,7 +32,7 @@ EditNetwork::move(const SDL_MouseMotionEvent & e, const Ray & } bool -EditNetwork::handleInput(const SDL_Event &, const UIComponent::Position &) +EditNetwork::handleInput(const SDL_Event &) { return false; } @@ -67,7 +65,7 @@ EditNetwork::Builder::setHeightsFor(Network * network, const Link::CCollection & } void -EditNetwork::render(const UIShader &, const UIComponent::Position &) +EditNetwork::render(const UIShader &) { ImGui::Begin("Edit Network"); diff --git a/ui/editNetwork.h b/ui/editNetwork.h index 805e7ec..c25bef2 100644 --- a/ui/editNetwork.h +++ b/ui/editNetwork.h @@ -2,7 +2,6 @@ #include "game/geoData.h" #include "gameMainSelector.h" -#include "toolbar.h" #include "worldOverlay.h" #include #include @@ -16,9 +15,9 @@ public: bool click(const SDL_MouseButtonEvent & e, const Ray &) override; bool move(const SDL_MouseMotionEvent & e, const Ray &) override; - bool handleInput(const SDL_Event & e, const UIComponent::Position &) override; + bool handleInput(const SDL_Event & e) override; void render(const SceneShader &, const Frustum &) const override; - void render(const UIShader & shader, const UIComponent::Position & pos) override; + void render(const UIShader & shader) override; using NetworkClickPos = std::variant; @@ -42,7 +41,6 @@ private: Network * network; Builder::Ptr builder; Texture blue; - const Font font; }; template class EditNetworkOf : public EditNetwork { diff --git a/ui/gameMainSelector.cpp b/ui/gameMainSelector.cpp index 55977ed..7878eb4 100644 --- a/ui/gameMainSelector.cpp +++ b/ui/gameMainSelector.cpp @@ -1,5 +1,4 @@ #include "gameMainSelector.h" -#include "text.h" #include "ui/uiComponent.h" #include #include @@ -9,20 +8,15 @@ #include #include -const std::filesystem::path fontpath {"/usr/share/fonts/hack/Hack-Regular.ttf"}; - -GameMainSelector::GameMainSelector(const Camera * c, ScreenAbsCoord size) : - UIComponent {{{}, size}}, camera {c}, font {fontpath, 15} -{ -} +GameMainSelector::GameMainSelector(const Camera * c) : camera {c} { } constexpr ScreenAbsCoord TargetPos {5, 45}; void -GameMainSelector::render(const UIShader & shader, const Position & parentPos) const +GameMainSelector::render(const UIShader & shader) const { if (target) { - target->render(shader, parentPos + position + TargetPos); + target->render(shader); } } @@ -35,10 +29,12 @@ GameMainSelector::render(const SceneShader & shader, const Frustum & frustum) co } bool -GameMainSelector::handleInput(const SDL_Event & e, const Position & parentPos) +GameMainSelector::handleInput(const SDL_Event & e) { - const auto getRay = [this](const auto & e) { - const auto mouse = ScreenRelCoord {e.x, e.y} / position.size; + const auto getRay = [this, &window = e.window](const auto & e) { + glm::ivec2 size {}; + SDL_GetWindowSizeInPixels(SDL_GetWindowFromID(window.windowID), &size.x, &size.y); + const auto mouse = ScreenRelCoord {e.x, e.y} / ScreenRelCoord {size}; return camera->unProject(mouse); }; if (target) { @@ -54,7 +50,7 @@ GameMainSelector::handleInput(const SDL_Event & e, const Position & parentPos) } break; } - return target->handleInput(e, parentPos + position + TargetPos); + return target->handleInput(e); } else { switch (e.type) { @@ -84,13 +80,13 @@ GameMainSelector::Component::move(const SDL_MouseMotionEvent &, const Ray @@ -21,17 +20,17 @@ public: virtual bool click(const SDL_MouseButtonEvent &, const Ray &); virtual bool move(const SDL_MouseMotionEvent &, const Ray &); - virtual bool handleInput(const SDL_Event &, const Position & pos); - virtual void render(const UIShader & shader, const Position & pos); + virtual bool handleInput(const SDL_Event &); + virtual void render(const UIShader & shader); virtual void render(const SceneShader &, const Frustum &) const; }; - GameMainSelector(const Camera * c, ScreenAbsCoord size); + GameMainSelector(const Camera * c); - void render(const UIShader & shader, const Position & pos) const override; + void render(const UIShader & shader) const override; void render(const SceneShader & shader, const Frustum &) const override; - bool handleInput(const SDL_Event & e, const Position &) override; + bool handleInput(const SDL_Event & e) override; void defaultClick(const Ray & ray); @@ -39,5 +38,4 @@ public: private: const Camera * camera; - const Font font; }; diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index 356d522..280d1a8 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -1,6 +1,7 @@ #include "gameMainWindow.h" #include "editNetwork.h" #include "gameMainSelector.h" +#include "icon.h" #include "imgui_extras.h" #include "manualCameraController.h" #include "queryTool.h" @@ -21,10 +22,10 @@ public: static constexpr auto TOOLBAR_HEIGHT = 54.F; static constexpr ImVec2 TOOLBAR_ICON_SIZE {32, 32}; - explicit GameMainToolbar(GameMainSelector * gms) : UIComponent {{{}, {}}}, gms {gms} { } + explicit GameMainToolbar(GameMainSelector * gms) : gms {gms} { } void - render(const UIShader &, const Position &) const override + render(const UIShader &) const override { if (IltGui::BeginToolbar("bottomBar", ImGuiDir_Down, TOOLBAR_HEIGHT)) { if (ImGui::ImageButton("Build rails", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { @@ -38,7 +39,7 @@ public: } bool - handleInput(const SDL_Event &, const Position &) override + handleInput(const SDL_Event &) override { return false; } @@ -51,7 +52,7 @@ private: GameMainWindow::GameMainWindow(size_t w, size_t h) : WindowContent {w, h}, SceneRenderer {{w, h}, 0} { uiComponents.create(glm::vec2 {310'727'624, 494'018'810}); - auto gms = uiComponents.create(&camera, ScreenAbsCoord {w, h}); + auto gms = uiComponents.create(&camera); uiComponents.create(gms.get()); } @@ -89,7 +90,7 @@ GameMainWindow::render() const glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); - uiComponents.apply(&UIComponent::render, uiShader, UIComponent::Position {}); + uiComponents.apply(&UIComponent::render, uiShader); } void diff --git a/ui/icon.cpp b/ui/icon.cpp index 847b117..0bdc91a 100644 --- a/ui/icon.cpp +++ b/ui/icon.cpp @@ -22,13 +22,6 @@ Icon::Icon(const Image & tex) : size {tex.width, tex.height} GL_RGBA, GL_UNSIGNED_BYTE, tex.data.data()); } -void -Icon::Bind() const -{ - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_texture); -} - ImTextureID Icon::operator*() const { diff --git a/ui/icon.h b/ui/icon.h index 5f3a812..3d0788a 100644 --- a/ui/icon.h +++ b/ui/icon.h @@ -12,7 +12,6 @@ public: explicit Icon(const std::filesystem::path & fileName); explicit Icon(const Image & image); - void Bind() const; const glm::vec2 size; ImTextureID operator*() const; diff --git a/ui/iconButton.cpp b/ui/iconButton.cpp deleted file mode 100644 index fe8c817..0000000 --- a/ui/iconButton.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "iconButton.h" -#include "glArrays.h" -#include "ui/icon.h" -#include "ui/uiComponent.h" -#include -#include -#include -#include -#include -#include -#include - -IconButton::IconButton(const std::string & icon_, glm::vec2 position_, UIEvent click_) : - UIComponent {{position_, ICON_SIZE}}, icon {icon_}, click {std::move(click_)} -{ - glBindVertexArray(m_vertexArrayObject); - - glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffer); - glBufferData(GL_ARRAY_BUFFER, static_cast(sizeof(glm::vec4) * 4), nullptr, GL_DYNAMIC_DRAW); - - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), nullptr); - - glBindVertexArray(0); -} - -void -IconButton::render(const UIShader &, const Position & parentPos) const -{ - icon.Bind(); - glBindVertexArray(m_vertexArrayObject); - glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffer); - const auto abs = parentPos.origin + position.origin; - const auto limit = abs + ICON_SIZE; - std::array vertices {{ - {abs.x, abs.y, 0, 0}, - {limit.x, abs.y, 1, 0}, - {limit.x, limit.y, 1, 1}, - {abs.x, limit.y, 0, 1}, - }}; - glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), glm::value_ptr(vertices.front())); - glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); -} - -bool -IconButton::handleInput(const SDL_Event & e, const Position & parentPos) -{ - const auto absPos = position + parentPos; - if (absPos & e.button) { - if (e.button.type == SDL_MOUSEBUTTONUP && e.button.button == SDL_BUTTON_LEFT) { - click(e); - } - } - return false; -} diff --git a/ui/iconButton.h b/ui/iconButton.h deleted file mode 100644 index 0afe92d..0000000 --- a/ui/iconButton.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "icon.h" -#include "uiComponent.h" -#include -#include -#include - -class UIShader; -union SDL_Event; - -static const constexpr glm::vec2 ICON_SIZE {32.F, 32.F}; - -class IconButton : public UIComponent { -public: - IconButton(const std::string & icon, glm::vec2 position, UIEvent click); - - void render(const UIShader &, const Position & parentPos) const override; - - bool handleInput(const SDL_Event & e, const Position & parentPos) override; - - Icon icon; - UIEvent click; - glVertexArray m_vertexArrayObject; - glBuffer m_vertexArrayBuffer; -}; diff --git a/ui/manualCameraController.cpp b/ui/manualCameraController.cpp index fbd0ca3..128701d 100644 --- a/ui/manualCameraController.cpp +++ b/ui/manualCameraController.cpp @@ -5,7 +5,7 @@ #include bool -ManualCameraController::handleInput(const SDL_Event & e, const Position &) +ManualCameraController::handleInput(const SDL_Event & e) { switch (e.type) { case SDL_KEYDOWN: @@ -72,7 +72,7 @@ ManualCameraController::handleInput(const SDL_Event & e, const Position &) } void -ManualCameraController::render(const UIShader &, const Position &) const +ManualCameraController::render(const UIShader &) const { } diff --git a/ui/manualCameraController.h b/ui/manualCameraController.h index 2f955e7..0cfa38c 100644 --- a/ui/manualCameraController.h +++ b/ui/manualCameraController.h @@ -11,10 +11,10 @@ class Camera; class ManualCameraController : public CameraController, public UIComponent { public: - explicit ManualCameraController(GlobalPosition2D f) : UIComponent {{}}, focus {f} { } + explicit ManualCameraController(GlobalPosition2D f) : focus {f} { } - bool handleInput(const SDL_Event & e, const Position &) override; - void render(const UIShader &, const Position & parentPos) const override; + bool handleInput(const SDL_Event & e) override; + void render(const UIShader &) const override; void updateCamera(Camera * camera) const override; diff --git a/ui/modeHelper.h b/ui/modeHelper.h deleted file mode 100644 index d20f2db..0000000 --- a/ui/modeHelper.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -union SDL_Event; - -enum ModeSecondClick { Unset, Reset, NoAction }; - -template class Mode { -public: - explicit Mode(Target & t) : target {t} { } - - Target & target; - - template - auto - toggle(Params &&... params) - { - return [params..., this](const SDL_Event &) { - toggleSetMode(std::forward(params)...); - }; - } - -private: - template - void - toggleSetMode(Params &&... params) - { - if (dynamic_cast(target.get())) { - if constexpr (msc == ModeSecondClick::Unset) { - target.reset(); - } - if constexpr (msc == ModeSecondClick::Reset) { - target = std::make_unique(std::forward(params)...); - } - } - else { - target = std::make_unique(std::forward(params)...); - } - } -}; diff --git a/ui/queryTool.cpp b/ui/queryTool.cpp index e48bae1..e046084 100644 --- a/ui/queryTool.cpp +++ b/ui/queryTool.cpp @@ -31,7 +31,7 @@ QueryTool::click(const SDL_MouseButtonEvent & event, const Ray } void -QueryTool::render(const UIShader &, const UIComponent::Position &) +QueryTool::render(const UIShader &) { ImGui::Begin("Query Tool"); ImGui::TextUnformatted(clicked.c_str()); diff --git a/ui/queryTool.h b/ui/queryTool.h index 8462214..cef4b75 100644 --- a/ui/queryTool.h +++ b/ui/queryTool.h @@ -7,7 +7,7 @@ protected: using GameMainSelector::Component::render; bool click(const SDL_MouseButtonEvent &, const Ray &) override; - void render(const UIShader & shader, const UIComponent::Position & pos) override; + void render(const UIShader & shader) override; private: std::string clicked; diff --git a/ui/text.cpp b/ui/text.cpp deleted file mode 100644 index bdaaba5..0000000 --- a/ui/text.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "text.h" -#include "font.h" -#include "gfx/gl/uiShader.h" -#include "gfx/gl/vertexArrayObject.h" -#include "uiComponent.h" -#include -#include -#include -#include -#include -#include -#include - -Text::Text(std::string_view s, const Font & font, Position pos, glm::vec3 c) : - UIComponent {pos}, colour {c}, font {font} -{ - VertexArrayObject {vao}.addAttribs(quads.bufferName(), 0); - operator=(s); -} - -Text & -Text::operator=(const std::string_view s) -{ - auto tquads = font.render(s); - models.resize(tquads.size()); - const auto glyphCount = std::accumulate(tquads.begin(), tquads.end(), size_t {}, [](auto && init, const auto & q) { - return init += q.second.size(); - }); - quads.resize(glyphCount); - GLushort current = 0; - auto model = models.begin(); - auto quad = quads.begin(); - for (const auto & [texture, fquads] : tquads) { - model->textureId = texture; - model->range.resize(fquads.size() * 6); - for (auto out = model->range.begin(); const auto & q [[maybe_unused]] : fquads) { - static constexpr std::array quadIndices {0, 1, 2, 2, 3, 0}; - std::transform(quadIndices.begin(), quadIndices.end(), out, [current](auto x) { - return current + x; - }); - current += 4; - out += 6; - } - model++; - quad = std::transform(fquads.begin(), fquads.end(), quad, [this](const Font::Quad & q) { - return q * [this](const glm::vec4 & corner) { - return corner + glm::vec4 {this->position.origin, 0, 0}; - }; - }); - } - quads.unmap(); - return *this; -} - -void -Text::render(const UIShader & shader, const Position &) const -{ - shader.text.use(colour); - glActiveTexture(GL_TEXTURE0); - glBindVertexArray(vao); - for (const auto & m : models) { - glBindTexture(GL_TEXTURE_2D, m.textureId); - glDrawElements(GL_TRIANGLES, static_cast(m.range.size()), GL_UNSIGNED_SHORT, m.range.data()); - } - glBindVertexArray(0); - glBindTexture(GL_TEXTURE_2D, 0); -} - -bool -Text::handleInput(const SDL_Event &, const Position &) -{ - return false; -} diff --git a/ui/text.h b/ui/text.h deleted file mode 100644 index a367456..0000000 --- a/ui/text.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include "font.h" -#include "glContainer.h" -#include "uiComponent.h" -#include -#include -#include -#include - -class UIShader; -union SDL_Event; - -class Text : public UIComponent { -public: - Text(std::string_view s, const Font &, Position, glm::vec3 colour); - - void render(const UIShader &, const Position & parentPos) const override; - bool handleInput(const SDL_Event &, const Position & parentPos) override; - - Text & operator=(const std::string_view s); - -private: - struct TextData { - GLuint textureId; - std::vector range; - }; - - std::vector models; - glContainer quads; - glVertexArray vao; - glm::vec3 colour; - const Font & font; -}; diff --git a/ui/toolbar.cpp b/ui/toolbar.cpp deleted file mode 100644 index 31d87dc..0000000 --- a/ui/toolbar.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "toolbar.h" -#include "gfx/gl/uiShader.h" -#include "ui/iconButton.h" -#include "ui/uiComponent.h" -#include "uiComponentPlacer.h" -#include -#include - -Toolbar::Toolbar(const std::initializer_list & initInfo) : UIComponent {{{}, {}}} -{ - UIComponentPlacer placer {{10, 10}, 5, 1}; - for (const auto & ii : initInfo) { - icons.create(ii.first, placer.next(ICON_SIZE), ii.second); - } - this->position.size = placer.getLimit(); -} - -void -Toolbar::render(const UIShader & uiShader, const Position & parentPos) const -{ - uiShader.icon.use(); - const auto absPos = this->position + parentPos; - icons.apply(&UIComponent::render, uiShader, absPos); -} - -bool -Toolbar::handleInput(const SDL_Event & e, const Position & parentPos) -{ - const auto absPos = this->position + parentPos; - if (absPos & e.button) { - icons.applyOne(&UIComponent::handleInput, e, absPos); - return true; - } - return false; -} diff --git a/ui/toolbar.h b/ui/toolbar.h deleted file mode 100644 index ea560f5..0000000 --- a/ui/toolbar.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "collection.h" -#include "iconButton.h" -#include "uiComponent.h" -#include -#include -#include - -class UIShader; -union SDL_Event; - -class Toolbar : public UIComponent { -public: - using InitInfo = std::pair; - explicit Toolbar(const std::initializer_list & initInfo); - - void render(const UIShader & uiShader, const Position & parentPos) const override; - - bool handleInput(const SDL_Event & e, const Position & parentPos) override; - - Collection icons; -}; diff --git a/ui/uiComponent.cpp b/ui/uiComponent.cpp deleted file mode 100644 index aa4838d..0000000 --- a/ui/uiComponent.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "uiComponent.h" -#include - -UIComponent::UIComponent(Position position) : position {position} { } - -UIComponent::Position -UIComponent::Position::operator+(const Position & parentPos) const -{ - return *this + parentPos.origin; -} - -UIComponent::Position -UIComponent::Position::operator+(const glm::vec2 & parentPos) const -{ - return {origin + parentPos, size}; -} - -bool -UIComponent::Position::operator&(const glm::vec2 & pos) const -{ - return (pos.x >= origin.x && pos.y >= origin.y && pos.x < origin.x + size.x && pos.y < origin.y + size.y); -} - -bool -UIComponent::Position::operator&(const SDL_MouseButtonEvent & pos) const -{ - switch (pos.type) { - case SDL_MOUSEBUTTONUP: - case SDL_MOUSEBUTTONDOWN: - return *this & glm::vec2 {pos.x, pos.y}; - } - return false; -} diff --git a/ui/uiComponent.h b/ui/uiComponent.h index 71d2659..9178c6b 100644 --- a/ui/uiComponent.h +++ b/ui/uiComponent.h @@ -1,32 +1,19 @@ #pragma once -#include #include #include class UIShader; union SDL_Event; -struct SDL_MouseButtonEvent; -using UIEvent = std::function; class UIComponent { public: - struct Position { - glm::vec2 origin, size; - Position operator+(const Position &) const; - Position operator+(const glm::vec2 &) const; - bool operator&(const SDL_MouseButtonEvent &) const; - bool operator&(const glm::vec2 &) const; - }; - - explicit UIComponent(Position); + UIComponent() = default; virtual ~UIComponent() = default; NO_MOVE(UIComponent); NO_COPY(UIComponent); - virtual void render(const UIShader &, const Position & parentPos) const = 0; - virtual bool handleInput(const SDL_Event &, const Position & parentPos) = 0; - - Position position; + virtual void render(const UIShader &) const = 0; + virtual bool handleInput(const SDL_Event &) = 0; }; diff --git a/ui/uiComponentPlacer.cpp b/ui/uiComponentPlacer.cpp deleted file mode 100644 index 5e645d8..0000000 --- a/ui/uiComponentPlacer.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "uiComponentPlacer.h" -#include - -UIComponentPlacer::UIComponentPlacer(glm::vec2 padding, float spacing, glm::length_t axis) : - padding {padding}, spacing {spacing}, axis {axis}, current {padding[axis]} -{ -} - -glm::vec2 -UIComponentPlacer::next(glm::vec2 size) -{ - glm::vec2 n {}; - n[axis] = current; - n[1 - axis] = padding[1 - axis]; - current += spacing + size[axis]; - max = std::max(max, size[1 - axis]); - return n; -} - -glm::vec2 -UIComponentPlacer::getLimit() const -{ - glm::vec2 n {}; - n[axis] = current + padding[axis]; - n[1 - axis] = max + padding[1 - axis]; - return n; -} diff --git a/ui/uiComponentPlacer.h b/ui/uiComponentPlacer.h deleted file mode 100644 index 1e64f78..0000000 --- a/ui/uiComponentPlacer.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -class UIComponentPlacer { -public: - UIComponentPlacer(glm::vec2 padding, float spacing, glm::length_t axis = 0); - - glm::vec2 next(glm::vec2 size); - glm::vec2 getLimit() const; - -private: - const glm::vec2 padding; - const float spacing; - const glm::length_t axis; - - float current {}; - float max {}; -}; diff --git a/ui/windowContent.cpp b/ui/windowContent.cpp index 91732a7..f55a8d5 100644 --- a/ui/windowContent.cpp +++ b/ui/windowContent.cpp @@ -27,6 +27,6 @@ WindowContent::handleInput(const SDL_Event & e) eAdjusted.motion.y = size.y - e.motion.y; break; } - uiComponents.rapplyOne(&UIComponent::handleInput, eAdjusted, UIComponent::Position {{}, size}); + uiComponents.rapplyOne(&UIComponent::handleInput, eAdjusted); return true; } -- cgit v1.2.3 From 271d5c55260cc8a0198938f3259f793e3957e3e3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 19 Mar 2025 03:42:35 +0000 Subject: Remove unrequired UIShader --- gfx/gl/shaders/uiShader.fs | 11 ---------- gfx/gl/shaders/uiShader.vs | 13 ------------ gfx/gl/shaders/uiShaderFont.fs | 12 ----------- gfx/gl/uiShader.cpp | 27 ------------------------ gfx/gl/uiShader.h | 47 ------------------------------------------ ui/editNetwork.cpp | 2 +- ui/editNetwork.h | 2 +- ui/gameMainSelector.cpp | 6 +++--- ui/gameMainSelector.h | 5 ++--- ui/gameMainWindow.cpp | 6 +++--- ui/manualCameraController.cpp | 2 +- ui/manualCameraController.h | 3 +-- ui/queryTool.cpp | 2 +- ui/queryTool.h | 2 +- ui/uiComponent.h | 3 +-- ui/windowContent.cpp | 2 -- ui/windowContent.h | 6 +----- 17 files changed, 16 insertions(+), 135 deletions(-) delete mode 100644 gfx/gl/shaders/uiShader.fs delete mode 100644 gfx/gl/shaders/uiShader.vs delete mode 100644 gfx/gl/shaders/uiShaderFont.fs delete mode 100644 gfx/gl/uiShader.cpp delete mode 100644 gfx/gl/uiShader.h diff --git a/gfx/gl/shaders/uiShader.fs b/gfx/gl/shaders/uiShader.fs deleted file mode 100644 index c5f4e92..0000000 --- a/gfx/gl/shaders/uiShader.fs +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 core - -in vec2 texCoord0; - -uniform sampler2D sampler; - -void -main() -{ - gl_FragColor = texture(sampler, texCoord0); -} diff --git a/gfx/gl/shaders/uiShader.vs b/gfx/gl/shaders/uiShader.vs deleted file mode 100644 index e9e4373..0000000 --- a/gfx/gl/shaders/uiShader.vs +++ /dev/null @@ -1,13 +0,0 @@ -#version 330 core - -in vec4 position; - -out vec2 texCoord0; -uniform mat4 uiProjection; - -void -main() -{ - gl_Position = uiProjection * vec4(position.xy, 0.0, 1.0); - texCoord0 = position.zw; -} diff --git a/gfx/gl/shaders/uiShaderFont.fs b/gfx/gl/shaders/uiShaderFont.fs deleted file mode 100644 index a1ef6ef..0000000 --- a/gfx/gl/shaders/uiShaderFont.fs +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 core - -in vec2 texCoord0; - -uniform sampler2D sampler; -uniform vec3 colour; - -void -main() -{ - gl_FragColor = vec4(colour, texture(sampler, texCoord0).r); -} diff --git a/gfx/gl/uiShader.cpp b/gfx/gl/uiShader.cpp deleted file mode 100644 index 23da9dc..0000000 --- a/gfx/gl/uiShader.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "uiShader.h" -#include "gl_traits.h" -#include -#include -#include -#include -#include -#include -#include - -UIShader::IconProgram::IconProgram(const glm::mat4 & vp) : UIProgram {vp, uiShader_vs, uiShader_fs} { } - -UIShader::TextProgram::TextProgram(const glm::mat4 & vp) : UIProgram {vp, uiShader_vs, uiShaderFont_fs} { } - -UIShader::UIShader(size_t width, size_t height) : - UIShader {glm::ortho(0, static_cast(width), 0, static_cast(height))} -{ -} - -UIShader::UIShader(const glm::mat4 & viewProjection) : icon {viewProjection}, text {viewProjection} { } - -void -UIShader::TextProgram::use(const RGB & colour) const -{ - Program::use(); - glUniform(colorLoc, colour); -} diff --git a/gfx/gl/uiShader.h b/gfx/gl/uiShader.h deleted file mode 100644 index 6d00166..0000000 --- a/gfx/gl/uiShader.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include "config/types.h" -#include "gl_traits.h" -#include "program.h" -#include -#include -#include -#include - -class UIShader { -public: - UIShader(std::size_t width, std::size_t height); - -private: - explicit UIShader(const glm::mat4 & viewProjection); - - class UIProgram : public Program { - public: - template - explicit UIProgram(const glm::mat4 & vp, S &&... srcs) : Program {std::forward(srcs)...} - { - const RequiredUniformLocation uiProjectionLoc {*this, "uiProjection"}; - glUseProgram(*this); - glUniform(uiProjectionLoc, vp); - } - }; - - class IconProgram : public UIProgram { - public: - explicit IconProgram(const glm::mat4 & vp); - using Program::use; - }; - - class TextProgram : public UIProgram { - public: - explicit TextProgram(const glm::mat4 & vp); - void use(const RGB & colour) const; - - private: - RequiredUniformLocation colorLoc {*this, "colour"}; - }; - -public: - IconProgram icon; - TextProgram text; -}; diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp index ac9aef9..3a28af7 100644 --- a/ui/editNetwork.cpp +++ b/ui/editNetwork.cpp @@ -65,7 +65,7 @@ EditNetwork::Builder::setHeightsFor(Network * network, const Link::CCollection & } void -EditNetwork::render(const UIShader &) +EditNetwork::render() { ImGui::Begin("Edit Network"); diff --git a/ui/editNetwork.h b/ui/editNetwork.h index c25bef2..aa5676a 100644 --- a/ui/editNetwork.h +++ b/ui/editNetwork.h @@ -17,7 +17,7 @@ public: bool move(const SDL_MouseMotionEvent & e, const Ray &) override; bool handleInput(const SDL_Event & e) override; void render(const SceneShader &, const Frustum &) const override; - void render(const UIShader & shader) override; + void render() override; using NetworkClickPos = std::variant; diff --git a/ui/gameMainSelector.cpp b/ui/gameMainSelector.cpp index 7878eb4..55d0ae0 100644 --- a/ui/gameMainSelector.cpp +++ b/ui/gameMainSelector.cpp @@ -13,10 +13,10 @@ GameMainSelector::GameMainSelector(const Camera * c) : camera {c} { } constexpr ScreenAbsCoord TargetPos {5, 45}; void -GameMainSelector::render(const UIShader & shader) const +GameMainSelector::render() const { if (target) { - target->render(shader); + target->render(); } } @@ -86,7 +86,7 @@ GameMainSelector::Component::handleInput(const SDL_Event &) } void -GameMainSelector::Component::render(const UIShader &) +GameMainSelector::Component::render() { } diff --git a/ui/gameMainSelector.h b/ui/gameMainSelector.h index 9560fc6..e6a86d2 100644 --- a/ui/gameMainSelector.h +++ b/ui/gameMainSelector.h @@ -9,7 +9,6 @@ class SceneShader; template class Ray; -class UIShader; class Camera; class GameMainSelector : public UIComponent, public WorldOverlay { @@ -21,13 +20,13 @@ public: virtual bool click(const SDL_MouseButtonEvent &, const Ray &); virtual bool move(const SDL_MouseMotionEvent &, const Ray &); virtual bool handleInput(const SDL_Event &); - virtual void render(const UIShader & shader); + virtual void render(); virtual void render(const SceneShader &, const Frustum &) const; }; GameMainSelector(const Camera * c); - void render(const UIShader & shader) const override; + void render() const override; void render(const SceneShader & shader, const Frustum &) const override; bool handleInput(const SDL_Event & e) override; diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index 280d1a8..cfc64e3 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -25,7 +25,7 @@ public: explicit GameMainToolbar(GameMainSelector * gms) : gms {gms} { } void - render(const UIShader &) const override + render() const override { if (IltGui::BeginToolbar("bottomBar", ImGuiDir_Down, TOOLBAR_HEIGHT)) { if (ImGui::ImageButton("Build rails", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { @@ -49,7 +49,7 @@ private: GameMainSelector * gms; }; -GameMainWindow::GameMainWindow(size_t w, size_t h) : WindowContent {w, h}, SceneRenderer {{w, h}, 0} +GameMainWindow::GameMainWindow(size_t w, size_t h) : SceneRenderer {{w, h}, 0} { uiComponents.create(glm::vec2 {310'727'624, 494'018'810}); auto gms = uiComponents.create(&camera); @@ -90,7 +90,7 @@ GameMainWindow::render() const glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); - uiComponents.apply(&UIComponent::render, uiShader); + uiComponents.apply(&UIComponent::render); } void diff --git a/ui/manualCameraController.cpp b/ui/manualCameraController.cpp index 128701d..557945e 100644 --- a/ui/manualCameraController.cpp +++ b/ui/manualCameraController.cpp @@ -72,7 +72,7 @@ ManualCameraController::handleInput(const SDL_Event & e) } void -ManualCameraController::render(const UIShader &) const +ManualCameraController::render() const { } diff --git a/ui/manualCameraController.h b/ui/manualCameraController.h index 0cfa38c..d3bf0c4 100644 --- a/ui/manualCameraController.h +++ b/ui/manualCameraController.h @@ -6,7 +6,6 @@ #include #include -class UIShader; class Camera; class ManualCameraController : public CameraController, public UIComponent { @@ -14,7 +13,7 @@ public: explicit ManualCameraController(GlobalPosition2D f) : focus {f} { } bool handleInput(const SDL_Event & e) override; - void render(const UIShader &) const override; + void render() const override; void updateCamera(Camera * camera) const override; diff --git a/ui/queryTool.cpp b/ui/queryTool.cpp index e046084..d016afa 100644 --- a/ui/queryTool.cpp +++ b/ui/queryTool.cpp @@ -31,7 +31,7 @@ QueryTool::click(const SDL_MouseButtonEvent & event, const Ray } void -QueryTool::render(const UIShader &) +QueryTool::render() { ImGui::Begin("Query Tool"); ImGui::TextUnformatted(clicked.c_str()); diff --git a/ui/queryTool.h b/ui/queryTool.h index cef4b75..4d16960 100644 --- a/ui/queryTool.h +++ b/ui/queryTool.h @@ -7,7 +7,7 @@ protected: using GameMainSelector::Component::render; bool click(const SDL_MouseButtonEvent &, const Ray &) override; - void render(const UIShader & shader) override; + void render() override; private: std::string clicked; diff --git a/ui/uiComponent.h b/ui/uiComponent.h index 9178c6b..6ee347e 100644 --- a/ui/uiComponent.h +++ b/ui/uiComponent.h @@ -3,7 +3,6 @@ #include #include -class UIShader; union SDL_Event; class UIComponent { @@ -14,6 +13,6 @@ public: NO_MOVE(UIComponent); NO_COPY(UIComponent); - virtual void render(const UIShader &) const = 0; + virtual void render() const = 0; virtual bool handleInput(const SDL_Event &) = 0; }; diff --git a/ui/windowContent.cpp b/ui/windowContent.cpp index f55a8d5..0f6dc04 100644 --- a/ui/windowContent.cpp +++ b/ui/windowContent.cpp @@ -1,8 +1,6 @@ #include "windowContent.h" #include "SDL_events.h" -WindowContent::WindowContent(size_t width, size_t height) : uiShader {width, height} { } - void WindowContent::tick(TickDuration) { diff --git a/ui/windowContent.h b/ui/windowContent.h index 474445a..d7fcad2 100644 --- a/ui/windowContent.h +++ b/ui/windowContent.h @@ -2,16 +2,13 @@ #include "chronology.h" #include "collection.h" -#include "gfx/gl/uiShader.h" #include "special_members.h" #include "stdTypeDefs.h" #include "uiComponent.h" // IWYU pragma: keep -#include class WindowContent : public StdTypeDefs { public: - using Factory = std::function; - WindowContent(size_t width, size_t height); + WindowContent() = default; virtual ~WindowContent() = default; NO_MOVE(WindowContent); NO_COPY(WindowContent); @@ -22,5 +19,4 @@ public: protected: ::Collection uiComponents; - UIShader uiShader; }; -- cgit v1.2.3 From 1efe681967cc764db3185134e0750349061a6e41 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 20 Mar 2025 23:22:51 +0000 Subject: Support capturing and reacting to ImGui window closure --- ui/editNetwork.cpp | 4 ++-- ui/editNetwork.h | 2 +- ui/gameMainSelector.cpp | 10 +++++++--- ui/gameMainSelector.h | 4 ++-- ui/gameMainWindow.cpp | 2 +- ui/manualCameraController.cpp | 2 +- ui/manualCameraController.h | 2 +- ui/queryTool.cpp | 4 ++-- ui/queryTool.h | 2 +- ui/uiComponent.h | 2 +- 10 files changed, 19 insertions(+), 15 deletions(-) diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp index 3a28af7..a9b041c 100644 --- a/ui/editNetwork.cpp +++ b/ui/editNetwork.cpp @@ -65,9 +65,9 @@ EditNetwork::Builder::setHeightsFor(Network * network, const Link::CCollection & } void -EditNetwork::render() +EditNetwork::render(bool & open) { - ImGui::Begin("Edit Network"); + ImGui::Begin("Edit Network", &open); auto builderChoice = [this](const char * name) { if (ImGui::RadioButton(name, dynamic_cast(builder.get()))) { diff --git a/ui/editNetwork.h b/ui/editNetwork.h index aa5676a..a817ddf 100644 --- a/ui/editNetwork.h +++ b/ui/editNetwork.h @@ -17,7 +17,7 @@ public: bool move(const SDL_MouseMotionEvent & e, const Ray &) override; bool handleInput(const SDL_Event & e) override; void render(const SceneShader &, const Frustum &) const override; - void render() override; + void render(bool & open) override; using NetworkClickPos = std::variant; diff --git a/ui/gameMainSelector.cpp b/ui/gameMainSelector.cpp index 55d0ae0..0c40abc 100644 --- a/ui/gameMainSelector.cpp +++ b/ui/gameMainSelector.cpp @@ -13,10 +13,14 @@ GameMainSelector::GameMainSelector(const Camera * c) : camera {c} { } constexpr ScreenAbsCoord TargetPos {5, 45}; void -GameMainSelector::render() const +GameMainSelector::render() { if (target) { - target->render(); + bool open = true; + target->render(open); + if (!open) { + target.reset(); + } } } @@ -86,7 +90,7 @@ GameMainSelector::Component::handleInput(const SDL_Event &) } void -GameMainSelector::Component::render() +GameMainSelector::Component::render(bool &) { } diff --git a/ui/gameMainSelector.h b/ui/gameMainSelector.h index e6a86d2..8c2be4b 100644 --- a/ui/gameMainSelector.h +++ b/ui/gameMainSelector.h @@ -20,13 +20,13 @@ public: virtual bool click(const SDL_MouseButtonEvent &, const Ray &); virtual bool move(const SDL_MouseMotionEvent &, const Ray &); virtual bool handleInput(const SDL_Event &); - virtual void render(); + virtual void render(bool & open); virtual void render(const SceneShader &, const Frustum &) const; }; GameMainSelector(const Camera * c); - void render() const override; + void render() override; void render(const SceneShader & shader, const Frustum &) const override; bool handleInput(const SDL_Event & e) override; diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index cfc64e3..322c6e5 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -25,7 +25,7 @@ public: explicit GameMainToolbar(GameMainSelector * gms) : gms {gms} { } void - render() const override + render() override { if (IltGui::BeginToolbar("bottomBar", ImGuiDir_Down, TOOLBAR_HEIGHT)) { if (ImGui::ImageButton("Build rails", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { diff --git a/ui/manualCameraController.cpp b/ui/manualCameraController.cpp index 557945e..553afc1 100644 --- a/ui/manualCameraController.cpp +++ b/ui/manualCameraController.cpp @@ -72,7 +72,7 @@ ManualCameraController::handleInput(const SDL_Event & e) } void -ManualCameraController::render() const +ManualCameraController::render() { } diff --git a/ui/manualCameraController.h b/ui/manualCameraController.h index d3bf0c4..6501762 100644 --- a/ui/manualCameraController.h +++ b/ui/manualCameraController.h @@ -13,7 +13,7 @@ public: explicit ManualCameraController(GlobalPosition2D f) : focus {f} { } bool handleInput(const SDL_Event & e) override; - void render() const override; + void render() override; void updateCamera(Camera * camera) const override; diff --git a/ui/queryTool.cpp b/ui/queryTool.cpp index d016afa..0157246 100644 --- a/ui/queryTool.cpp +++ b/ui/queryTool.cpp @@ -31,9 +31,9 @@ QueryTool::click(const SDL_MouseButtonEvent & event, const Ray } void -QueryTool::render() +QueryTool::render(bool & open) { - ImGui::Begin("Query Tool"); + ImGui::Begin("Query Tool", &open); ImGui::TextUnformatted(clicked.c_str()); ImGui::End(); } diff --git a/ui/queryTool.h b/ui/queryTool.h index 4d16960..8633118 100644 --- a/ui/queryTool.h +++ b/ui/queryTool.h @@ -7,7 +7,7 @@ protected: using GameMainSelector::Component::render; bool click(const SDL_MouseButtonEvent &, const Ray &) override; - void render() override; + void render(bool & open) override; private: std::string clicked; diff --git a/ui/uiComponent.h b/ui/uiComponent.h index 6ee347e..b2c1a8f 100644 --- a/ui/uiComponent.h +++ b/ui/uiComponent.h @@ -13,6 +13,6 @@ public: NO_MOVE(UIComponent); NO_COPY(UIComponent); - virtual void render() const = 0; + virtual void render() = 0; virtual bool handleInput(const SDL_Event &) = 0; }; -- cgit v1.2.3 From 9a8e2b6388c453a03a06d81daa2c82e35fc7ce8a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 29 Mar 2025 12:45:18 +0000 Subject: Add basic instructions to query tool Sets default text. --- ui/queryTool.cpp | 2 ++ ui/queryTool.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/ui/queryTool.cpp b/ui/queryTool.cpp index 0157246..2267253 100644 --- a/ui/queryTool.cpp +++ b/ui/queryTool.cpp @@ -7,6 +7,8 @@ #include #include +QueryTool::QueryTool() : clicked {"Click something for details"} { } + bool QueryTool::click(const SDL_MouseButtonEvent & event, const Ray & ray) { diff --git a/ui/queryTool.h b/ui/queryTool.h index 8633118..74c5380 100644 --- a/ui/queryTool.h +++ b/ui/queryTool.h @@ -3,6 +3,9 @@ #include "gameMainSelector.h" class QueryTool : public GameMainSelector::Component { +public: + QueryTool(); + protected: using GameMainSelector::Component::render; -- cgit v1.2.3 From 0f183e620fbe3e94af6c29c399b61d99809af01c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 29 Mar 2025 12:46:54 +0000 Subject: Auto resize windows to content --- ui/editNetwork.cpp | 1 + ui/queryTool.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/ui/editNetwork.cpp b/ui/editNetwork.cpp index a9b041c..c900191 100644 --- a/ui/editNetwork.cpp +++ b/ui/editNetwork.cpp @@ -67,6 +67,7 @@ EditNetwork::Builder::setHeightsFor(Network * network, const Link::CCollection & void EditNetwork::render(bool & open) { + ImGui::SetNextWindowSize({-1, -1}); ImGui::Begin("Edit Network", &open); auto builderChoice = [this](const char * name) { diff --git a/ui/queryTool.cpp b/ui/queryTool.cpp index 2267253..549bd9e 100644 --- a/ui/queryTool.cpp +++ b/ui/queryTool.cpp @@ -35,6 +35,7 @@ QueryTool::click(const SDL_MouseButtonEvent & event, const Ray void QueryTool::render(bool & open) { + ImGui::SetNextWindowSize({-1, -1}); ImGui::Begin("Query Tool", &open); ImGui::TextUnformatted(clicked.c_str()); ImGui::End(); -- cgit v1.2.3 From eeb2b635bfbb0d89e186490e5ecaf5cfc22c9a5c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 31 Mar 2025 01:05:59 +0100 Subject: Remove explicit library referring to imguisdl2 It already exists via the library of all of thirdparty/release. --- Jamroot.jam | 1 - 1 file changed, 1 deletion(-) diff --git a/Jamroot.jam b/Jamroot.jam index 8587aaa..9b589cf 100644 --- a/Jamroot.jam +++ b/Jamroot.jam @@ -92,7 +92,6 @@ lib ilt : thirdparty/release thirdparty sdl2 - thirdparty//imguisdl2 freetype2 glib-2.0 mxml -- cgit v1.2.3 From 9ee03ef4813eaaf6e5098efac41daf8169fcee0e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 31 Mar 2025 01:09:51 +0100 Subject: Add lunasvg (and plutovg) This is the SVG library used by ImGui, so makes sense a choice even if we're not using it there yet. --- .gitmodules | 3 +++ thirdparty/Jamfile.jam | 12 ++++++++++++ thirdparty/lunasvg | 1 + 3 files changed, 16 insertions(+) create mode 160000 thirdparty/lunasvg diff --git a/.gitmodules b/.gitmodules index 63a1a38..b5aa2c7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "thirdparty/imgui"] path = thirdparty/imgui url = https://github.com/ocornut/imgui +[submodule "thirdparty/lunasvg"] + path = thirdparty/lunasvg + url = https://github.com/sammycage/lunasvg diff --git a/thirdparty/Jamfile.jam b/thirdparty/Jamfile.jam index b6ed163..26497c9 100644 --- a/thirdparty/Jamfile.jam +++ b/thirdparty/Jamfile.jam @@ -28,3 +28,15 @@ lib imguisdl2 : : : imgui ; + +lib lunasvg : + [ glob lunasvg/source/*.cpp lunasvg/plutovg/source/*.c ] + : + static + lunasvg/include + lunasvg/plutovg/include + off + -fPIC + : : + lunasvg/include + ; diff --git a/thirdparty/lunasvg b/thirdparty/lunasvg new file mode 160000 index 0000000..f8aabfb --- /dev/null +++ b/thirdparty/lunasvg @@ -0,0 +1 @@ +Subproject commit f8aabfb444bb37f69df7290790f57e4a27730a93 -- cgit v1.2.3 From c30eda289b5815b6b62799ef986eaf3c462fd72d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 31 Mar 2025 01:18:03 +0100 Subject: Add SvgIcon class Based on Icon class, but constructor replaced with calls to lunasvg. --- config/types.h | 1 + res/ui/icon/rails.svg | 36 ++++++++++++++++++++++++++++++++++++ test/Jamfile.jam | 1 + test/test-ui.cpp | 20 ++++++++++++++++++++ ui/svgIcon.cpp | 34 ++++++++++++++++++++++++++++++++++ ui/svgIcon.h | 18 ++++++++++++++++++ 6 files changed, 110 insertions(+) create mode 100644 res/ui/icon/rails.svg create mode 100644 test/test-ui.cpp create mode 100644 ui/svgIcon.cpp create mode 100644 ui/svgIcon.h diff --git a/config/types.h b/config/types.h index c501f41..06825b5 100644 --- a/config/types.h +++ b/config/types.h @@ -42,6 +42,7 @@ using Normal3D = Normal<3>; using Rotation2D = Rotation<2>; using Rotation3D = Rotation<3>; using TextureRelCoord = glm::vec<2, float>; +using ImageDimensions = glm::vec<2, GLsizei>; using TextureDimensions = glm::vec<3, GLsizei>; using TextureRelRegion = glm::vec<4, float>; using TextureAbsCoord = glm::vec<2, GLsizei>; diff --git a/res/ui/icon/rails.svg b/res/ui/icon/rails.svg new file mode 100644 index 0000000..81e9b94 --- /dev/null +++ b/res/ui/icon/rails.svg @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/Jamfile.jam b/test/Jamfile.jam index bedc2ad..c922187 100644 --- a/test/Jamfile.jam +++ b/test/Jamfile.jam @@ -65,6 +65,7 @@ run perf-instancing.cpp : \< : test-instancing : benchmark tes run test-glContainer.cpp : : : test ; run test-pack.cpp : : : test ; run test-environment.cpp : : : test ; +run test-ui.cpp : : : test ; compile test-static-enumDetails.cpp ; compile test-static-stream_support.cpp ; explicit perf-assetFactory ; diff --git a/test/test-ui.cpp b/test/test-ui.cpp new file mode 100644 index 0000000..2810cda --- /dev/null +++ b/test/test-ui.cpp @@ -0,0 +1,20 @@ +#define BOOST_TEST_MODULE UI +#include +#include + +#include "testMainWindow.h" +#include +#include +#include + +constexpr GLsizei RENDER_SIZE = 64; + +BOOST_GLOBAL_FIXTURE(TestMainWindowAppBase); + +BOOST_AUTO_TEST_CASE(LoadFromFile) +{ + SvgIcon svg(ImageDimensions {RENDER_SIZE}, Resource::mapPath("ui/icon/rails.svg")); + const auto size = Texture::getSize(svg.texture); + BOOST_CHECK_EQUAL(size, TextureDimensions(RENDER_SIZE, RENDER_SIZE, 1)); + Texture::save(svg.texture, "/tmp/rails.tga"); +} diff --git a/ui/svgIcon.cpp b/ui/svgIcon.cpp new file mode 100644 index 0000000..499d9cc --- /dev/null +++ b/ui/svgIcon.cpp @@ -0,0 +1,34 @@ +#include "svgIcon.h" +#include "gl_traits.h" +#include + +SvgIcon::SvgIcon(ImageDimensions dim, const std::filesystem::path & path) +{ + const auto svgDoc = lunasvg::Document::loadFromFile(Resource::mapPath(path).native()); + if (!svgDoc) { + throw std::runtime_error("Failed to load SVG from " + path.string()); + } + + auto bitmap = svgDoc->renderToBitmap(dim.x, dim.y); + if (bitmap.isNull()) { + throw std::runtime_error("Failed to render SVG " + path.string()); + } + bitmap.convertToRGBA(); + + glBindTexture(GL_TEXTURE_2D, texture); + + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); + + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dim.x, dim.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap.data()); +} + +ImTextureID +SvgIcon::operator*() const +{ + static_assert(sizeof(glTexture) <= sizeof(ImTextureID)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr) This is how ImGui works + return reinterpret_cast(*texture); +} diff --git a/ui/svgIcon.h b/ui/svgIcon.h new file mode 100644 index 0000000..106f97c --- /dev/null +++ b/ui/svgIcon.h @@ -0,0 +1,18 @@ +#pragma once + +#include "glArrays.h" +#include "imgui_wrap.h" +#include +#include +#include + +class SvgIcon { +public: + SvgIcon(ImageDimensions, const std::filesystem::path &); + + ImTextureID operator*() const; + +private: + friend class LoadFromFile; // Test case verifying size/content + glTexture texture; +}; -- cgit v1.2.3 From 401dc359171ff1179abece095ea9354f0064ea19 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 31 Mar 2025 18:10:26 +0100 Subject: Use new SVG icons on main toolbar --- res/ui/icon/magnifier.svg | 50 +++++++++++++++++++++++++++++++++++++++++++++++ res/ui/icon/road.svg | 27 +++++++++++++++++++++++++ ui/gameMainWindow.cpp | 13 ++++++------ 3 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 res/ui/icon/magnifier.svg create mode 100644 res/ui/icon/road.svg diff --git a/res/ui/icon/magnifier.svg b/res/ui/icon/magnifier.svg new file mode 100644 index 0000000..97abe9f --- /dev/null +++ b/res/ui/icon/magnifier.svg @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/ui/icon/road.svg b/res/ui/icon/road.svg new file mode 100644 index 0000000..3b9ae77 --- /dev/null +++ b/res/ui/icon/road.svg @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + diff --git a/ui/gameMainWindow.cpp b/ui/gameMainWindow.cpp index 322c6e5..bcda649 100644 --- a/ui/gameMainWindow.cpp +++ b/ui/gameMainWindow.cpp @@ -1,10 +1,10 @@ #include "gameMainWindow.h" #include "editNetwork.h" #include "gameMainSelector.h" -#include "icon.h" #include "imgui_extras.h" #include "manualCameraController.h" #include "queryTool.h" +#include "svgIcon.h" #include #include #include @@ -20,7 +20,7 @@ class GameMainToolbar : public UIComponent { public: static constexpr auto TOOLBAR_HEIGHT = 54.F; - static constexpr ImVec2 TOOLBAR_ICON_SIZE {32, 32}; + template static constexpr T TOOLBAR_ICON_SIZE {32, 32}; explicit GameMainToolbar(GameMainSelector * gms) : gms {gms} { } @@ -28,10 +28,10 @@ public: render() override { if (IltGui::BeginToolbar("bottomBar", ImGuiDir_Down, TOOLBAR_HEIGHT)) { - if (ImGui::ImageButton("Build rails", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { + if (ImGui::ImageButton("Build rails", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { gms->target = std::make_unique>(); - } - if (ImGui::ImageButton("Query", *buildRailsIcon, TOOLBAR_ICON_SIZE)) { + } + if (ImGui::ImageButton("Query", *queryToolIcon, TOOLBAR_ICON_SIZE)) { gms->target = std::make_unique(); } IltGui::EndToolbar(); @@ -45,7 +45,8 @@ public: } private: - Icon buildRailsIcon {"ui/icon/network.png"}; + SvgIcon buildRailsIcon {TOOLBAR_ICON_SIZE, "ui/icon/rails.svg"}; + SvgIcon queryToolIcon {TOOLBAR_ICON_SIZE, "ui/icon/magnifier.svg"}; GameMainSelector * gms; }; -- cgit v1.2.3 From 4e424a1386d6646657325d218c508a3779e1ceae Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 2 Apr 2025 18:02:44 +0100 Subject: Remove the old unused network.png icon --- res/ui/icon/network.png | Bin 16793 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 res/ui/icon/network.png diff --git a/res/ui/icon/network.png b/res/ui/icon/network.png deleted file mode 100644 index 7a091f3..0000000 Binary files a/res/ui/icon/network.png and /dev/null differ -- cgit v1.2.3