fix: reset mode if it has changed

fixes #25
This commit is contained in:
Virt 2024-10-10 13:04:46 +02:00
commit 3ff4c2a053
9 changed files with 37 additions and 5 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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;
};

View file

@ -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};
}

View file

@ -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:

View file

@ -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;
}

View file

@ -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:

View file

@ -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;
}

View file

@ -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: