feat: stretch mode

This commit is contained in:
Virt 2024-07-02 15:35:37 +02:00
commit 5fee8c5545
18 changed files with 234 additions and 79 deletions

View file

@ -1,19 +1,14 @@
#pragma once
#include <hyprutils/math/Vector2D.hpp>
#include "utils.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;
virtual SModeResult update(Vector2D pos) = 0;
};

View file

@ -5,7 +5,7 @@ EModeUpdate CModeRotate::strategy() {
return MOVE;
}
double CModeRotate::update(Vector2D pos) {
SModeResult CModeRotate::update(Vector2D pos) {
static auto* const* PLENGTH = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_LENGTH)->getDataStaticPtr();
static auto* const* POFFSET = (Hyprlang::FLOAT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_ROTATE_OFFSET)->getDataStaticPtr();
@ -32,5 +32,8 @@ double CModeRotate::update(Vector2D pos) {
end.x += pos.x;
end.y += pos.y;
return angle;
auto result = SModeResult();
result.rotation = angle;
return result;
}

View file

@ -8,7 +8,7 @@ this results in a rotating mouse cursor
class CModeRotate : public IMode {
public:
virtual EModeUpdate strategy();
virtual double update(Vector2D pos);
virtual SModeResult update(Vector2D pos);
private:

40
src/mode/ModeStretch.cpp Normal file
View file

@ -0,0 +1,40 @@
#include "ModeStretch.hpp"
#include "utils.hpp"
#include "../globals.hpp"
#include <hyprland/src/Compositor.hpp>
EModeUpdate CModeStretch::strategy() {
return TICK;
}
SModeResult CModeStretch::update(Vector2D pos) {
static auto const* PFUNCTION = (Hyprlang::STRING const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_STRETCH_FUNCTION)->getDataStaticPtr();
static auto* const* PLIMIT = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, CONFIG_STRETCH_LIMIT)->getDataStaticPtr();
// 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
Vector2D speed = (samples[current] - samples[first]) / 0.1;
double mag = speed.size();
double angle = -std::atan(speed.x / speed.y) + PI;
if (speed.y > 0) angle += PI;
if (mag == 0) angle = 0;
double scale = activation(*PFUNCTION, **PLIMIT, mag);
auto result = SModeResult();
result.stretch.angle = angle;
// we can't do more scaling than that because of how large our buffer around the cursor shape is
result.stretch.magnitude = Vector2D{1.0 - scale * 0.5, 1.0 + scale * 1.0};
return result;
}

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

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

View file

@ -1,43 +1,15 @@
#include "ModeTilt.hpp"
#include "utils.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) {
SModeResult CModeTilt::update(Vector2D pos) {
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();
// create samples array
int max = g_pHyprRenderer->m_pMostHzMonitor->refreshRate / 10; // 100ms worth of history
@ -52,5 +24,7 @@ double CModeTilt::update(Vector2D pos) {
// calculate speed and tilt
double speed = (samples[current].x - samples[first].x) / 0.1;
return function(speed) * (PI / 3); // 120° in both directions
auto result = SModeResult();
result.rotation = activation(*PFUNCTION, **PMASS, speed) * (PI / 3); // 120° in both directions
return result;
}

View file

@ -5,7 +5,7 @@
class CModeTilt : public IMode {
public:
virtual EModeUpdate strategy();
virtual double update(Vector2D pos);
virtual SModeResult update(Vector2D pos);
private:

49
src/mode/utils.cpp Normal file
View file

@ -0,0 +1,49 @@
#include "utils.hpp"
#include <string>
#include <hyprland/src/debug/Log.hpp>
double activation(std::string function, double max, double value) {
double result = 0;
if (function == "linear") {
result = value / max;
} else if (function == "quadratic") {
// (1 / m²) * x², is a quadratic function which will reach 1 at m
result = (1.0 / (max * max)) * (value * value);
result *= (value > 0 ? 1 : -1);
} else if (function == "negative_quadratic") {
float x = std::abs(value);
// (-1 / m²) * (x - m)² + 1, is a quadratic function with the inverse curvature which will reach 1 at m
result = (-1.0 / (max * max)) * ((x - max) * (x - max)) + 1;
if (x > max) result = 1; // need to clamp manually, as the function would decrease again
result *= (value > 0 ? 1 : -1);
} else
Debug::log(WARN, "[dynamic-cursors] unknown air function specified");
return std::clamp(result, -1.0, 1.0);
}
void SModeResult::clamp(double angle, double scale, double stretch) {
if (std::abs(this->rotation) < angle)
this->rotation = 0;
if (std::abs(1 - this->scale) < scale)
this->scale = 1;
if (std::abs(1 - this->stretch.magnitude.x) < stretch && std::abs(1 - this->stretch.magnitude.x) < stretch)
this->stretch.magnitude = Vector2D{1,1};
}
bool SModeResult::hasDifference(SModeResult* other, double angle, double scale, double stretch) {
return
std::abs(other->rotation - this->rotation) > angle ||
std::abs(other->scale - this->scale) > scale ||
std::abs(other->stretch.angle - this->stretch.angle) > angle ||
std::abs(other->stretch.magnitude.x - this->stretch.magnitude.x) > stretch ||
std::abs(other->stretch.magnitude.y - this->stretch.magnitude.y) > stretch;
}

31
src/mode/utils.hpp Normal file
View file

@ -0,0 +1,31 @@
#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)
};
/* result determined by the mode */
struct SModeResult {
// rotation of the shape around hotspot
double rotation = 0;
// uniform scaling of the shape around hotspot
double scale = 1;
// stretch along axis with angle, going through hotspot
struct {
double angle = 0;
Vector2D magnitude = Vector2D{1,1};
} stretch;
void clamp(double angle, double scale, double stretch);
bool hasDifference(SModeResult* other, double angle, double scale, double stretch);
};
double activation(std::string function, double max, double value);