From e56cc36b7b9d7b919b66921ae3b0b21151dbd9f7 Mon Sep 17 00:00:00 2001 From: Illia Vysochyn Date: Fri, 2 Feb 2024 18:58:58 +0100 Subject: [PATCH] [#52832] src: widget: widget_video.cpp: Resize window to native sizes when image context changes Signed-off-by: Illia Vysochyn --- include/gui_node/widget/widget_video.hpp | 3 +++ src/widget/widget_video.cpp | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/gui_node/widget/widget_video.hpp b/include/gui_node/widget/widget_video.hpp index 0c630b8..2f18be2 100644 --- a/include/gui_node/widget/widget_video.hpp +++ b/include/gui_node/widget/widget_video.hpp @@ -82,9 +82,12 @@ class BaseVideoWidget : public Widget bool texture_initialized = false; ///< Whether the texture has been initialized bool keep_aspect_ratio = true; ///< Whether to keep the aspect ratio of the image on resizing + bool reset_image_size = false; ///< Whether to keep the aspect ratio of the image on resizing float base_width = 1920; ///< The base width for the ImGui window float scale_factor = 1.0; ///< The scale factor for the ImGui window std::vector last_image_data; ///< The last image data received + int last_image_width = 0; ///< The last image width received + int last_image_height = 0; ///< The last image height received ConverterFunc frame_converter; ///< Converts received messages to sensor_msgs::msg::Image format std::function imgui_callback; ///< Function to call when drawing the ImGui window diff --git a/src/widget/widget_video.cpp b/src/widget/widget_video.cpp index 785312c..706ab78 100644 --- a/src/widget/widget_video.cpp +++ b/src/widget/widget_video.cpp @@ -89,6 +89,8 @@ bool BaseVideoWidget::updateTexture(const std::vector &buffer, in { gui_engine->addTexture(ros_data_name, buffer, width, height, channels); last_image_data = buffer; + last_image_width = width; + last_image_height = height; texture_initialized = true; } std::shared_ptr texture_loader = gui_engine->getTexture(ros_data_name); @@ -99,6 +101,12 @@ bool BaseVideoWidget::updateTexture(const std::vector &buffer, in return false; } last_image_data = buffer; + if (width != last_image_width || height != last_image_height) + { + reset_image_size = true; + last_image_width = width; + last_image_height = height; + } } drawImGuiFrame(texture_loader); return true; @@ -144,7 +152,6 @@ void BaseVideoWidget::drawImGuiFrame(std::shared_ptr texture_load NULL, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_MenuBar); { - bool reset_image_size = false; ImVec2 view = ImGui::GetWindowSize(); scale_factor = view.x / base_width; ImGui::SetWindowSize(window_configs.window_size, ImGuiCond_Once); @@ -165,6 +172,7 @@ void BaseVideoWidget::drawImGuiFrame(std::shared_ptr texture_load if (reset_image_size) { ImGui::SetWindowSize(window_configs.window_size); + reset_image_size = false; } ImGui::Image( (ImTextureID)texture_loader->getDescriptorSet(),