chore: more abstraction and other refactors

This commit is contained in:
Virt 2024-06-28 14:08:13 +02:00
commit fa7cab8717
9 changed files with 263 additions and 175 deletions

19
src/mode/Mode.hpp Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include <hyprutils/math/Vector2D.hpp>
using namespace Hyprutils::Math;
/* specifies when a mode wants to be updated */
enum EModeUpdate {
MOVE, // on mouse move
TICK // on tick (i.e. every frame)
};
class IMode {
public:
/* returns the desired updating strategy for the given mode */
virtual EModeUpdate strategy() = 0;
/* updates the calculations and returns the new angle */
virtual double update(Vector2D pos) = 0;
};

34
src/mode/ModeRotate.cpp Normal file
View file

@ -0,0 +1,34 @@
#include "../globals.hpp"
#include "ModeRotate.hpp"
EModeUpdate CModeRotate::strategy() {
return MOVE;
}
double CModeRotate::update(Vector2D pos) {
static auto* const* PLENGTH = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_LENGTH)->getDataStaticPtr();
// translate to origin
end.x -= pos.x;
end.y -= pos.y;
// normalize
double size = end.size();
end.x /= size;
end.y /= size;
// scale to length
end.x *= **PLENGTH;
end.y *= **PLENGTH;
// calculate angle
double angle = -std::atan(end.x / end.y);
if (end.y > 0) angle += PI;
angle += PI;
// translate back
end.x += pos.x;
end.y += pos.y;
return angle;
}

18
src/mode/ModeRotate.hpp Normal file
View file

@ -0,0 +1,18 @@
#include "Mode.hpp"
#include <hyprutils/math/Vector2D.hpp>
/*
this modes simulates a stick being dragged on one end
this results in a rotating mouse cursor
*/
class CModeRotate : public IMode {
public:
virtual EModeUpdate strategy();
virtual double update(Vector2D pos);
private:
// end of the simulated stick
Vector2D end;
};

56
src/mode/ModeTilt.cpp Normal file
View file

@ -0,0 +1,56 @@
#include "ModeTilt.hpp"
#include "../globals.hpp"
#include <hyprland/src/Compositor.hpp>
double function(double speed) {
static auto const* PFUNCTION = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_FUNCTION)->getDataStaticPtr();
static auto* const* PMASS = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_MASS)->getDataStaticPtr();
double mass = **PMASS;
double result = 0;
if (!strcmp(*PFUNCTION, "linear")) {
result = speed / **PMASS;
} else if (!strcmp(*PFUNCTION, "quadratic")) {
// (1 / m²) * x², is a quadratic function which will reach 1 at m
result = (1.0 / (mass * mass)) * (speed * speed);
result *= (speed > 0 ? 1 : -1);
} else if (!strcmp(*PFUNCTION, "negative_quadratic")) {
float x = std::abs(speed);
// (-1 / m²) * (x - m)² + 1, is a quadratic function with the inverse curvature which will reach 1 at m
result = (-1.0 / (mass * mass)) * ((x - mass) * (x - mass)) + 1;
if (x > mass) result = 1; // need to clamp manually, as the function would decrease again
result *= (speed > 0 ? 1 : -1);
} else
Debug::log(WARN, "[dynamic-cursors] unknown air function specified");
return std::clamp(result, -1.0, 1.0);
}
EModeUpdate CModeTilt::strategy() {
return TICK;
}
double CModeTilt::update(Vector2D pos) {
// create samples array
int max = g_pHyprRenderer->m_pMostHzMonitor->refreshRate / 10; // 100ms worth of history
samples.resize(max);
// capture current sample
samples[samples_index] = Vector2D{pos};
int current = samples_index;
samples_index = (samples_index + 1) % max; // increase for next sample
int first = samples_index;
// calculate speed and tilt
double speed = (samples[current].x - samples[first].x) / 0.1;
return function(speed) * (PI / 3); // 120° in both directions
}

16
src/mode/ModeTilt.hpp Normal file
View file

@ -0,0 +1,16 @@
#include "Mode.hpp"
#include <hyprutils/math/Vector2D.hpp>
#include <vector>
class CModeTilt : public IMode {
public:
virtual EModeUpdate strategy();
virtual double update(Vector2D pos);
private:
// ring buffer of last position samples
std::vector<Vector2D> samples;
int samples_index = 0;
};