From 16b88ddc109a81f75ecb4667eaedfa81643f4da5 Mon Sep 17 00:00:00 2001 From: Martin Gerhardy Date: Sat, 15 Mar 2025 09:24:01 +0100 Subject: [PATCH] UI: use imgui for rendering the ui --- src/modules/ui/UI.cpp | 18 +++++++++++++++--- src/modules/ui/nodes/IUINodeMap.cpp | 5 +++-- src/modules/ui/nodes/UINode.cpp | 12 ++++++------ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/modules/ui/UI.cpp b/src/modules/ui/UI.cpp index d38e9b935..e8939900c 100644 --- a/src/modules/ui/UI.cpp +++ b/src/modules/ui/UI.cpp @@ -329,9 +329,21 @@ void UI::renderImage (Texture* texture, int x, int y, int w, int h, int16_t angl { if (!texture || !texture->isValid()) return; - _frontend->setColor(color); - _frontend->renderImage(texture, x, y, w, h, angle, color[3]); - _frontend->resetColor(); + + if (w == -1) + w = texture->getWidth(); + if (h == -1) + h = texture->getHeight(); + + _frontend->getTrimmed(texture, x, y, w, h); + + const TextureRect& r = texture->getSourceRect(); + const SDL_Rect srcRect { r.x, r.y, r.w, r.h }; + const ImColor imcolor(color[0], color[1], color[2], color[3]); + const ImVec2 uvmin(srcRect.x / (float)texture->getFullWidth(), srcRect.y / (float)texture->getFullHeight()); + const ImVec2 uvmax((srcRect.x + srcRect.w) / (float)texture->getFullWidth(), (srcRect.y + srcRect.h) / (float)texture->getFullHeight()); + const ImTextureID texId = (ImTextureID)_frontend->getTextureData(texture); + ImGui::GetWindowDrawList()->AddImage(texId, ImVec2(x, y), ImVec2(x + w, y + h), uvmin, uvmax, imcolor); } void UI::update (uint32_t deltaTime) diff --git a/src/modules/ui/nodes/IUINodeMap.cpp b/src/modules/ui/nodes/IUINodeMap.cpp index be3c1effe..1a940db5d 100644 --- a/src/modules/ui/nodes/IUINodeMap.cpp +++ b/src/modules/ui/nodes/IUINodeMap.cpp @@ -166,9 +166,10 @@ void IUINodeMap::onWindowResize () void IUINodeMap::render (int x, int y) const { - renderFilledRect(getRenderX(), getRenderY(), getRenderWidth(), getRenderHeight(), colorBlack); - if (_map.isActive()) + // renderFilledRect(getRenderX(), getRenderY(), getRenderWidth(), getRenderHeight(), colorBlack); + if (_map.isActive()) { _map.render(); + } UINode::render(0, 0); if (_map.isStarted()) diff --git a/src/modules/ui/nodes/UINode.cpp b/src/modules/ui/nodes/UINode.cpp index 93d137aea..80ef604f6 100644 --- a/src/modules/ui/nodes/UINode.cpp +++ b/src/modules/ui/nodes/UINode.cpp @@ -394,22 +394,22 @@ bool UINode::renderDebug (int x, int y, int textY, bool& focusHandled) const void UINode::renderRect (int x, int y, int w, int h, const Color& color) const { const float alpha = fequals(_alpha, 1.0f) ? color[3] : _alpha; - const Color alphaColor = { color[0], color[1], color[2], alpha }; - _frontend->renderRect(x, y, w, h, alphaColor); + const ImVec4 alphaColor = { color[0], color[1], color[2], alpha }; + ImGui::GetWindowDrawList()->AddRect(ImVec2(x, y), ImVec2(x + w, y + h), ImColor(alphaColor)); } void UINode::renderFilledRect (int x, int y, int w, int h, const Color& color) const { const float alpha = fequals(_alpha, 1.0f) ? color[3] : _alpha; - const Color alphaColor = { color[0], color[1], color[2], alpha }; - _frontend->renderFilledRect(x, y, w, h, alphaColor); + const ImVec4 alphaColor = { color[0], color[1], color[2], alpha }; + ImGui::GetWindowDrawList()->AddRectFilled(ImVec2(x, y), ImVec2(x + w, y + h), ImColor(alphaColor)); } void UINode::renderLine (int x1, int y1, int x2, int y2, const Color& color) const { const float alpha = fequals(_alpha, 1.0f) ? color[3] : _alpha; - const Color alphaColor = { color[0], color[1], color[2], alpha }; - _frontend->renderLine(x1, y1, x2, y2, alphaColor); + const ImVec4 alphaColor = { color[0], color[1], color[2], alpha }; + ImGui::GetWindowDrawList()->AddLine(ImVec2(x1, y1), ImVec2(x2, y2), ImColor(alphaColor)); } void UINode::renderImage (const TexturePtr& texture, int x, int y, int w, int h, float alpha) const