Skip to content

Commit

Permalink
[#52832] src: widget: widget_video.cpp: Resize window to native sizes…
Browse files Browse the repository at this point in the history
… when image context changes

Signed-off-by: Illia Vysochyn <[email protected]>
  • Loading branch information
ivysochyn committed Feb 6, 2024
1 parent cdc2ae9 commit e56cc36
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/gui_node/widget/widget_video.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char> 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<void(void)> imgui_callback; ///< Function to call when drawing the ImGui window
Expand Down
10 changes: 9 additions & 1 deletion src/widget/widget_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ bool BaseVideoWidget::updateTexture(const std::vector<unsigned char> &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<TextureLoader> texture_loader = gui_engine->getTexture(ros_data_name);
Expand All @@ -99,6 +101,12 @@ bool BaseVideoWidget::updateTexture(const std::vector<unsigned char> &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;
Expand Down Expand Up @@ -144,7 +152,6 @@ void BaseVideoWidget::drawImGuiFrame(std::shared_ptr<TextureLoader> 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);
Expand All @@ -165,6 +172,7 @@ void BaseVideoWidget::drawImGuiFrame(std::shared_ptr<TextureLoader> texture_load
if (reset_image_size)
{
ImGui::SetWindowSize(window_configs.window_size);
reset_image_size = false;
}
ImGui::Image(
(ImTextureID)texture_loader->getDescriptorSet(),
Expand Down

0 comments on commit e56cc36

Please sign in to comment.