From 3ff4c2a053f7673b3b8cd45ada0886cbda13ebcc Mon Sep 17 00:00:00 2001 From: Virt <41426325+VirtCode@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:04:46 +0200 Subject: [PATCH] fix: reset mode if it has changed fixes #25 --- src/cursor.cpp | 5 +++++ src/cursor.hpp | 2 ++ src/mode/Mode.hpp | 4 +++- src/mode/ModeRotate.cpp | 10 ++++++++++ src/mode/ModeRotate.hpp | 1 + src/mode/ModeStretch.cpp | 9 +++++++-- src/mode/ModeStretch.hpp | 1 + src/mode/ModeTilt.cpp | 9 +++++++-- src/mode/ModeTilt.hpp | 1 + 9 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/cursor.cpp b/src/cursor.cpp index e0663b6..b59a355 100644 --- a/src/cursor.cpp +++ b/src/cursor.cpp @@ -405,9 +405,14 @@ void CDynamicCursors::calculate(EModeUpdate type) { // calculate angle and zoom if (mode) { + // reset mode if it has changed + if (mode != lastMode) mode->reset(); + if (mode->strategy() == type) resultMode = mode->update(g_pPointerManager->pointerPos); } else resultMode = SModeResult(); + lastMode = mode; + if (**PSHAKE) { if (type == TICK) resultShake = shake.update(g_pPointerManager->pointerPos); diff --git a/src/cursor.hpp b/src/cursor.hpp index b8cb7f9..b7200d8 100644 --- a/src/cursor.hpp +++ b/src/cursor.hpp @@ -55,8 +55,10 @@ class CDynamicCursors { CModeRotate rotate; CModeTilt tilt; CModeStretch stretch; + /* returns the current mode, nullptr if none is selected */ IMode* currentMode(); + IMode* lastMode; // used to reset the mode if it was switched (to prune stale data) // shake CShake shake; diff --git a/src/mode/Mode.hpp b/src/mode/Mode.hpp index 9dccc8d..f48fd0d 100644 --- a/src/mode/Mode.hpp +++ b/src/mode/Mode.hpp @@ -9,8 +9,10 @@ class IMode { public: /* returns the desired updating strategy for the given mode */ virtual EModeUpdate strategy() = 0; - /* updates the calculations and returns the new angle */ + /* updates the calculations and returns the new result */ virtual SModeResult update(Vector2D pos) = 0; + /* reset the internal stuff of the mode */ + virtual void reset() = 0; /* called on warp, an update will be sent afterwards (probably) */ virtual void warp(Vector2D old, Vector2D pos) = 0; }; diff --git a/src/mode/ModeRotate.cpp b/src/mode/ModeRotate.cpp index 0369e24..21cb298 100644 --- a/src/mode/ModeRotate.cpp +++ b/src/mode/ModeRotate.cpp @@ -13,6 +13,12 @@ SModeResult CModeRotate::update(Vector2D pos) { auto length = g_pShapeRuleHandler->getIntOr(CONFIG_ROTATE_LENGTH, **PLENGTH); auto offset = g_pShapeRuleHandler->getFloatOr(CONFIG_ROTATE_OFFSET, **POFFSET); + // this mode has just started, start at upright orientation + if (end.y == 0 && end.x == 0) { + end.x = pos.x; + end.y = pos.y + length; + } + // translate to origin end.x -= pos.x; end.y -= pos.y; @@ -49,3 +55,7 @@ SModeResult CModeRotate::update(Vector2D pos) { void CModeRotate::warp(Vector2D old, Vector2D pos) { end += (pos - old); } + +void CModeRotate::reset() { + end = {0, 0}; +} diff --git a/src/mode/ModeRotate.hpp b/src/mode/ModeRotate.hpp index 44b6ef9..7932472 100644 --- a/src/mode/ModeRotate.hpp +++ b/src/mode/ModeRotate.hpp @@ -9,6 +9,7 @@ class CModeRotate : public IMode { public: virtual EModeUpdate strategy(); virtual SModeResult update(Vector2D pos); + virtual void reset(); virtual void warp(Vector2D old, Vector2D pos); private: diff --git a/src/mode/ModeStretch.cpp b/src/mode/ModeStretch.cpp index ef4c930..c75851e 100644 --- a/src/mode/ModeStretch.cpp +++ b/src/mode/ModeStretch.cpp @@ -15,10 +15,10 @@ SModeResult CModeStretch::update(Vector2D pos) { // create samples array int max = g_pHyprRenderer->m_pMostHzMonitor->refreshRate / 10; // 100ms worth of history - samples.resize(max); + samples.resize(max, pos); // capture current sample - samples[samples_index] = Vector2D{pos}; + samples[samples_index] = pos; int current = samples_index; samples_index = (samples_index + 1) % max; // increase for next sample int first = samples_index; @@ -47,3 +47,8 @@ void CModeStretch::warp(Vector2D old, Vector2D pos) { for (auto& sample : samples) sample += delta; } + +void CModeStretch::reset() { + samples.clear(); + samples_index = 0; +} diff --git a/src/mode/ModeStretch.hpp b/src/mode/ModeStretch.hpp index 3c9fa76..60c3f0b 100644 --- a/src/mode/ModeStretch.hpp +++ b/src/mode/ModeStretch.hpp @@ -6,6 +6,7 @@ class CModeStretch : public IMode { public: virtual EModeUpdate strategy(); virtual SModeResult update(Vector2D pos); + virtual void reset(); virtual void warp(Vector2D old, Vector2D pos); private: diff --git a/src/mode/ModeTilt.cpp b/src/mode/ModeTilt.cpp index 05a6475..4a1b546 100644 --- a/src/mode/ModeTilt.cpp +++ b/src/mode/ModeTilt.cpp @@ -15,10 +15,10 @@ SModeResult CModeTilt::update(Vector2D pos) { // create samples array int max = g_pHyprRenderer->m_pMostHzMonitor->refreshRate / 10; // 100ms worth of history - samples.resize(max); + samples.resize(max, pos); // capture current sample - samples[samples_index] = Vector2D{pos}; + samples[samples_index] = pos; int current = samples_index; samples_index = (samples_index + 1) % max; // increase for next sample int first = samples_index; @@ -37,3 +37,8 @@ void CModeTilt::warp(Vector2D old, Vector2D pos) { for (auto& sample : samples) sample += delta; } + +void CModeTilt::reset() { + samples.clear(); + samples_index = 0; +} diff --git a/src/mode/ModeTilt.hpp b/src/mode/ModeTilt.hpp index fd57d01..15c79d8 100644 --- a/src/mode/ModeTilt.hpp +++ b/src/mode/ModeTilt.hpp @@ -6,6 +6,7 @@ class CModeTilt : public IMode { public: virtual EModeUpdate strategy(); virtual SModeResult update(Vector2D pos); + virtual void reset(); virtual void warp(Vector2D old, Vector2D pos); private: