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..8b27a2f 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) 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;