From 0e5d33e616c1ad511ba3cd2e2eb61d41a9d53813 Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Mon, 22 Apr 2024 19:19:49 +0200 Subject: [PATCH] Add param CanResize --- src/immvision/image.h | 3 ++ src/immvision/internal/image.cpp | 48 ++++++++++++++++++++++++ src/immvision/internal/image_cache.h | 1 + src_all_in_one/immvision/immvision.cpp | 51 ++++++++++++++++++++++++++ src_all_in_one/immvision/immvision.h | 3 ++ 5 files changed, 106 insertions(+) diff --git a/src/immvision/image.h b/src/immvision/image.h index c587588..f8190f3 100644 --- a/src/immvision/image.h +++ b/src/immvision/image.h @@ -134,6 +134,9 @@ namespace ImmVision bool PanWithMouse = true; bool ZoomWithMouseWheel = true; + // Can the image widget be resized by the user + bool CanResize = true; + // Color Order: RGB or RGBA versus BGR or BGRA (Note: by default OpenCV uses BGR and BGRA) bool IsColorOrderBGR = true; diff --git a/src/immvision/internal/image.cpp b/src/immvision/internal/image.cpp index 808d4fd..92383af 100644 --- a/src/immvision/internal/image.cpp +++ b/src/immvision/internal/image.cpp @@ -375,6 +375,8 @@ namespace ImmVision // Mouse dragging auto fnHandleMouseDragging = [¶ms](CachedParams & cacheParams) { + if (cacheParams.IsResizing) + return; ZoomPanTransform::MatrixType& zoomMatrix = params->ZoomPanMatrix; int mouseDragButton = 0; @@ -478,6 +480,50 @@ namespace ImmVision return mouseInfo; }; + // + // Lambda / Show resize widget in the bottom right corner + // + auto fnShowResizeWidget = [¶ms](CachedParams & cacheParams) + { + if (!params->CanResize) + return; + ImVec2 imageBottomRight = ImGui::GetItemRectMax(); + float em = ImGui::GetFontSize(); + float size = em * 1.0f; + ImVec2 br(imageBottomRight.x, imageBottomRight.y); + ImVec2 bl(br.x - size, br.y); + ImVec2 tr(br.x, br.y - size); + ImVec2 tl(br.x - size, br.y - size); + + ImRect zone(tl, br); + ImGui::GetWindowDrawList()->AddTriangleFilled(br, bl, tr, ImGui::GetColorU32(ImGuiCol_ButtonHovered)); + + if (!cacheParams.IsResizing) + { + if (ImGui::IsMouseHoveringRect(zone.Min, zone.Max) && ImGui::IsMouseDown(0)) + { + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNWSE); + cacheParams.IsResizing = true; + } + } + if (cacheParams.IsResizing) + { + if (ImGui::IsMouseDown(0)) + { + if (ImGui::GetIO().MouseDelta.x != 0. || ImGui::GetIO().MouseDelta.y != 0.) + { + params->ImageDisplaySize.width += ImGui::GetIO().MouseDelta.x; + params->ImageDisplaySize.height += ImGui::GetIO().MouseDelta.y; + } + } + else + { + ImGui::SetMouseCursor(ImGuiMouseCursor_Arrow); + cacheParams.IsResizing = false; + } + } + }; + // // Lambda / Show pixel info @@ -506,6 +552,7 @@ namespace ImmVision ImGui::BeginGroup(); // Show image auto mouseInfo = fnShowImage(*cacheImages.GlTexture); + fnShowResizeWidget(cacheParams); // Add Watched Pixel on double click if ( params->AddWatchedPixelOnDoubleClick && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) @@ -630,6 +677,7 @@ namespace ImmVision imageParams.ShowOptionsPanel = false; imageParams.ZoomWithMouseWheel = false; imageParams.PanWithMouse = false; + imageParams.CanResize = false; imageParams.ShowPixelInfo = false; imageParams.ShowImageInfo = false; imageParams.ShowGrid = false; diff --git a/src/immvision/internal/image_cache.h b/src/immvision/internal/image_cache.h index 5e6d15b..81063da 100644 --- a/src/immvision/internal/image_cache.h +++ b/src/immvision/internal/image_cache.h @@ -22,6 +22,7 @@ namespace ImmVision std::vector FilenameEditBuffer = std::vector(1000, '\0'); bool IsMouseDragging = false; bool WasZoomJustUpdatedByLink = false; + bool IsResizing = false; cv::Size PreviousImageSize; struct ImageParams PreviousParams; }; diff --git a/src_all_in_one/immvision/immvision.cpp b/src_all_in_one/immvision/immvision.cpp index ff5e65f..97bc4c1 100644 --- a/src_all_in_one/immvision/immvision.cpp +++ b/src_all_in_one/immvision/immvision.cpp @@ -147,6 +147,9 @@ namespace ImmVision bool PanWithMouse = true; bool ZoomWithMouseWheel = true; + // Can the image widget be resized by the user + bool CanResize = true; + // Color Order: RGB or RGBA versus BGR or BGRA (Note: by default OpenCV uses BGR and BGRA) bool IsColorOrderBGR = true; @@ -9363,6 +9366,7 @@ namespace ImmVision std::vector FilenameEditBuffer = std::vector(1000, '\0'); bool IsMouseDragging = false; bool WasZoomJustUpdatedByLink = false; + bool IsResizing = false; cv::Size PreviousImageSize; struct ImageParams PreviousParams; }; @@ -9811,6 +9815,8 @@ namespace ImmVision // Mouse dragging auto fnHandleMouseDragging = [¶ms](CachedParams & cacheParams) { + if (cacheParams.IsResizing) + return; ZoomPanTransform::MatrixType& zoomMatrix = params->ZoomPanMatrix; int mouseDragButton = 0; @@ -9914,6 +9920,50 @@ namespace ImmVision return mouseInfo; }; + // + // Lambda / Show resize widget in the bottom right corner + // + auto fnShowResizeWidget = [¶ms](CachedParams & cacheParams) + { + if (!params->CanResize) + return; + ImVec2 imageBottomRight = ImGui::GetItemRectMax(); + float em = ImGui::GetFontSize(); + float size = em * 1.0f; + ImVec2 br(imageBottomRight.x, imageBottomRight.y); + ImVec2 bl(br.x - size, br.y); + ImVec2 tr(br.x, br.y - size); + ImVec2 tl(br.x - size, br.y - size); + + ImRect zone(tl, br); + ImGui::GetWindowDrawList()->AddTriangleFilled(br, bl, tr, ImGui::GetColorU32(ImGuiCol_ButtonHovered)); + + if (!cacheParams.IsResizing) + { + if (ImGui::IsMouseHoveringRect(zone.Min, zone.Max) && ImGui::IsMouseDown(0)) + { + ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNWSE); + cacheParams.IsResizing = true; + } + } + if (cacheParams.IsResizing) + { + if (ImGui::IsMouseDown(0)) + { + if (ImGui::GetIO().MouseDelta.x != 0. || ImGui::GetIO().MouseDelta.y != 0.) + { + params->ImageDisplaySize.width += ImGui::GetIO().MouseDelta.x; + params->ImageDisplaySize.height += ImGui::GetIO().MouseDelta.y; + } + } + else + { + ImGui::SetMouseCursor(ImGuiMouseCursor_Arrow); + cacheParams.IsResizing = false; + } + } + }; + // // Lambda / Show pixel info @@ -9942,6 +9992,7 @@ namespace ImmVision ImGui::BeginGroup(); // Show image auto mouseInfo = fnShowImage(*cacheImages.GlTexture); + fnShowResizeWidget(cacheParams); // Add Watched Pixel on double click if ( params->AddWatchedPixelOnDoubleClick && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left) diff --git a/src_all_in_one/immvision/immvision.h b/src_all_in_one/immvision/immvision.h index 3bfa378..c474084 100644 --- a/src_all_in_one/immvision/immvision.h +++ b/src_all_in_one/immvision/immvision.h @@ -142,6 +142,9 @@ namespace ImmVision bool PanWithMouse = true; bool ZoomWithMouseWheel = true; + // Can the image widget be resized by the user + bool CanResize = true; + // Color Order: RGB or RGBA versus BGR or BGRA (Note: by default OpenCV uses BGR and BGRA) bool IsColorOrderBGR = true;