From 2a4a20beefa533730d85a75bbc2dc6676fa247b5 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 5 Jun 2023 13:48:29 +0200 Subject: Introduce move based focusing --- src/components/Scrollbar.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/components/Scrollbar.cpp') diff --git a/src/components/Scrollbar.cpp b/src/components/Scrollbar.cpp index 76c5c1a..ac713e9 100644 --- a/src/components/Scrollbar.cpp +++ b/src/components/Scrollbar.cpp @@ -109,4 +109,40 @@ void Scrollbar::Refresh() { elements.push_back(bar); } +void Scrollbar::Focus(double XorY){ + double MTCanvasHeight = status->CanvasHeight - status->ScrollbarWidth; + double MTCanvasWidth = status->CanvasWidth - status->ScrollbarWidth; + // Check horizontal or vertical and if should scroll + if(IsHorizontal && status->MoveTableMaxX > MTCanvasWidth){ + // First normalize: + XorY-=MTCanvasWidth; // Do not scroll on first page + XorY=std::max(0.0,XorY); // Ensure focus is greater than 0 + XorY=std::min(XorY,(status->MoveTableMaxX-MTCanvasWidth)); // Ensure focus is than scroll max + // Then compute percent and apply scroll + double percent = XorY / (status->MoveTableMaxX-MTCanvasWidth); + status->ScrollX = + -(status->MoveTableMaxX - MTCanvasWidth) * percent; + // Do not forget to update scrollbar: + double maxScroll = bg.width - bar.width; + bar.x = maxScroll * percent; + bar.x = std::max(bg.x, bar.x); + bar.x = std::min(bg.x + maxScroll, bar.x); + } + else if(status->MoveTableMaxY > MTCanvasHeight){ + // First normalize: + XorY-=MTCanvasHeight; // Do not scroll on first page + XorY=std::max(0.0,XorY); // Ensure focus is greater than 0 + XorY=std::min(XorY,(status->MoveTableMaxY-MTCanvasHeight)); // Ensure focus is than scroll max + // Then compute percent and apply scroll + double percent = XorY / (status->MoveTableMaxY-MTCanvasHeight); + status->ScrollY = + -(status->MoveTableMaxY - MTCanvasHeight) * percent; + // Do not forget to update scrollbar: + double maxScroll = bg.height - bar.height; + bar.y = maxScroll * percent; + bar.y = std::max(bg.y, bar.y); + bar.y = std::min(bg.y + maxScroll, bar.y); + } +} + } // namespace cgeditor \ No newline at end of file -- cgit v1.2.3