diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index a9d28bdc1..699331652 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -55,6 +55,7 @@ set( gradientwidgets/displayedgradientswidget.cpp gradientwidgets/gradientslistwidget.cpp gradientwidgets/gradientwidget.cpp + misc/infinitemousemovement.cpp misc/noshortcutaction.cpp misc/keyfocustarget.cpp optimalscrollarena/minimalscrollwidget.cpp @@ -126,6 +127,7 @@ set( gradientwidgets/displayedgradientswidget.h gradientwidgets/gradientslistwidget.h gradientwidgets/gradientwidget.h + misc/infinitemousemovement.h misc/noshortcutaction.h misc/keyfocustarget.h optimalscrollarena/minimalscrollwidget.h diff --git a/src/ui/misc/infinitemousemovement.cpp b/src/ui/misc/infinitemousemovement.cpp new file mode 100644 index 000000000..652aeb1d0 --- /dev/null +++ b/src/ui/misc/infinitemousemovement.cpp @@ -0,0 +1,39 @@ +#include "infinitemousemovement.h" + +#include +#include +#include +#include + +bool isCursorInLeft(int x, QSize *screenSize) { return x == 0; } +bool isCursorInRight(int x, QSize *screenSize) { return x == screenSize->width(); } +bool isCursorInTop(int y, QSize *screenSize) { return y == 0; } +bool isCursorInBottom(int y, QSize *screenSize) { return y == screenSize->height(); } + +void setCursorMovementInfinite(QScreen *screen) { + auto size = screen.availableSize(); + auto point = QCursor::pos(); + auto x = point.x(); + auto y = point.y(); + + // Depending on which border the cursor is at (left, right, top, down) + // Move the cursor to the inverse direction + 1 px (not to get in an infinite loop) + if (isCursorInLeft(point, size)) { + // We set the cursor to the edge of the screen minus 1px + QCursor::setPos(screen, size.width() - 1, y); + }; + if (isCursorInRight(point, size)) { + // We set the cursor to the edge of the screen plus 1px + QCursor::setPos(screen, 1, y); + } + if(isCursorInTop(point, size)) { + // We set the cursor to the edge of the screen minus 1px + QCursor::setPos(screen, x, size.height() - 1); + } + if(isCursorInBottom(point, size)) { + // We set the cursor to the edge of the screen plus 1px + QCursor::setPos(screen, x, 1); + } +} + +void setCursorMovementDefault() {} diff --git a/src/ui/misc/infinitemousemovement.h b/src/ui/misc/infinitemousemovement.h new file mode 100644 index 000000000..f702e542f --- /dev/null +++ b/src/ui/misc/infinitemousemovement.h @@ -0,0 +1,16 @@ +#ifndef FRICTION_H_INFINITE_MOUSE_MOVEMENT +#define FRICTION_H_INFINITE_MOUSE_MOVEMENT + +/// Allows the mouse to move across the border of the screen and return back to the other side. Is useful for e.g. scaling / rotating an object in the canvas. +void setCursorMovementInfinite(); + +/// Blocks the mouse from going across the border of the screen +void setCursorMovementDefault(); + +/// Allows the mouse only to move 1px and returns it to original state +/// (like in a videogame). Could be useful for e.g. blender-style combo boxes. +/// Should additionally be hidden or change the cursor style, +/// and make this function return values depending on how much the mouse moves. +void setCursorMovementLock(); + +#endif