Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite mouse movement for S R shortcuts #362

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/ui/misc/infinitemousemovement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "infinitemousemovement.h"

#include <QCursor>
#include <QScreen>
#include <QPoint>
#include <QSize>

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() {}
16 changes: 16 additions & 0 deletions src/ui/misc/infinitemousemovement.h
Original file line number Diff line number Diff line change
@@ -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