hyprland-plugins/hyprtrails/trail.cpp

334 lines
14 KiB
C++
Raw Permalink Normal View History

2023-11-04 01:39:14 +01:00
#include "trail.hpp"
#include <hyprland/src/Compositor.hpp>
2024-03-20 04:02:10 +01:00
#include <hyprland/src/desktop/Window.hpp>
2023-11-04 01:39:14 +01:00
#include "globals.hpp"
void CTrail::onTick() {
2024-02-18 16:30:21 +01:00
static auto* const PHISTORYSTEP = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprtrails:history_step")->getDataStaticPtr();
static auto* const PHISTORYPOINTS = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprtrails:history_points")->getDataStaticPtr();
2023-11-04 01:39:14 +01:00
m_iTimer++;
2024-04-27 14:03:46 +02:00
const auto PWINDOW = m_pWindow.lock();
2024-02-18 16:30:21 +01:00
if (m_iTimer > **PHISTORYSTEP) {
2024-04-27 14:03:46 +02:00
m_dLastGeoms.push_front({box{(float)PWINDOW->m_vRealPosition.value().x, (float)PWINDOW->m_vRealPosition.value().y, (float)PWINDOW->m_vRealSize.value().x,
(float)PWINDOW->m_vRealSize.value().y},
2023-11-04 01:39:14 +01:00
std::chrono::system_clock::now()});
2024-02-18 16:30:21 +01:00
while (m_dLastGeoms.size() > **PHISTORYPOINTS)
2023-11-04 18:28:57 +01:00
m_dLastGeoms.pop_back();
2023-11-04 01:39:14 +01:00
m_iTimer = 0;
}
if (m_bNeedsDamage) {
g_pHyprRenderer->damageBox(&m_bLastBox);
m_bNeedsDamage = false;
}
}
2024-04-27 14:03:46 +02:00
CTrail::CTrail(PHLWINDOW pWindow) : IHyprWindowDecoration(pWindow), m_pWindow(pWindow) {
2024-03-02 20:38:48 +01:00
m_vLastWindowPos = pWindow->m_vRealPosition.value();
m_vLastWindowSize = pWindow->m_vRealSize.value();
2023-11-04 01:39:14 +01:00
2024-04-22 16:57:18 +02:00
pTickCb = HyprlandAPI::registerCallbackDynamic(PHANDLE, "trailTick", [this](void* self, SCallbackInfo& info, std::any data) { this->onTick(); });
2023-11-04 01:39:14 +01:00
}
CTrail::~CTrail() {
damageEntire();
2024-04-22 16:57:18 +02:00
HyprlandAPI::unregisterCallback(PHANDLE, pTickCb);
2023-11-04 01:39:14 +01:00
}
2023-11-11 15:39:46 +01:00
SDecorationPositioningInfo CTrail::getPositioningInfo() {
return {DECORATION_POSITION_ABSOLUTE};
2023-11-04 18:28:57 +01:00
}
2023-11-04 01:39:14 +01:00
2023-11-11 15:39:46 +01:00
void CTrail::onPositioningReply(const SDecorationPositioningReply& reply) {
; // ignored
2023-11-04 18:28:57 +01:00
}
2023-11-04 01:39:14 +01:00
void scaleBox2(box& box, float coeff) {
float hwl = (box.w - (box.w * coeff)) / 2.0;
float hhl = (box.h - (box.h * coeff)) / 2.0;
box.w *= coeff;
box.h *= coeff;
box.x += hwl;
box.y += hhl;
}
Vector2D vecForT(const Vector2D& a, const Vector2D& b, const float& t) {
const Vector2D vec_PQ = b - a;
return Vector2D{a + vec_PQ * t};
}
Vector2D vecForBezierT(const float& t, const std::vector<Vector2D>& verts) {
std::vector<Vector2D> pts;
for (size_t vertexIndex = 0; vertexIndex < verts.size() - 1; vertexIndex++) {
Vector2D p = verts[vertexIndex];
pts.push_back(vecForT(p, verts[vertexIndex + 1], t));
}
if (pts.size() > 1)
return vecForBezierT(t, pts);
else
return pts[0];
}
void CTrail::draw(PHLMONITOR pMonitor, const float &a) {
2024-04-27 14:03:46 +02:00
if (!validMapped(m_pWindow))
2023-11-04 18:28:57 +01:00
return;
2023-11-04 01:39:14 +01:00
2024-04-27 14:03:46 +02:00
const auto PWINDOW = m_pWindow.lock();
2024-07-13 15:10:57 +02:00
if (!PWINDOW->m_sWindowData.decorate.valueOrDefault())
2023-11-04 18:28:57 +01:00
return;
2023-11-04 01:39:14 +01:00
2024-02-18 16:30:21 +01:00
static auto* const PBEZIERSTEP = (Hyprlang::FLOAT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprtrails:bezier_step")->getDataStaticPtr();
static auto* const PPOINTSPERSTEP = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprtrails:points_per_step")->getDataStaticPtr();
static auto* const PCOLOR = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprtrails:color")->getDataStaticPtr();
2023-11-04 01:39:14 +01:00
2024-02-18 16:30:21 +01:00
const CColor COLOR = **PCOLOR;
2023-11-04 01:39:14 +01:00
2023-11-04 18:28:57 +01:00
if (m_dLastGeoms.size() < 2)
return;
2023-11-04 01:39:14 +01:00
2024-04-27 14:03:46 +02:00
box thisbox =
box{(float)PWINDOW->m_vRealPosition.value().x, (float)PWINDOW->m_vRealPosition.value().y, (float)PWINDOW->m_vRealSize.value().x, (float)PWINDOW->m_vRealSize.value().y};
CBox wlrbox = {thisbox.x - pMonitor->vecPosition.x, thisbox.y - pMonitor->vecPosition.y, thisbox.w, thisbox.h};
wlrbox.scale(pMonitor->scale).round();
2023-11-04 01:39:14 +01:00
2023-11-04 18:28:57 +01:00
g_pHyprOpenGL->scissor((CBox*)nullptr); // allow the entire window and stencil to render
2023-11-04 01:39:14 +01:00
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, -1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2024-04-27 14:03:46 +02:00
g_pHyprOpenGL->renderRect(&wlrbox, CColor(0, 0, 0, 0), PWINDOW->rounding() * pMonitor->scale);
2023-11-04 01:39:14 +01:00
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_NOTEQUAL, 1, -1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
2024-09-25 12:08:10 +02:00
CBox monbox = {0, 0, g_pHyprOpenGL->m_RenderData.pMonitor->vecTransformedSize.x, g_pHyprOpenGL->m_RenderData.pMonitor->vecTransformedSize.y};
2023-11-04 01:39:14 +01:00
2024-09-25 12:08:10 +02:00
Mat3x3 matrix = g_pHyprOpenGL->m_RenderData.monitorProjection.projectBox(monbox, wlTransformToHyprutils(invertTransform(WL_OUTPUT_TRANSFORM_NORMAL)), monbox.rot);
Mat3x3 glMatrix = g_pHyprOpenGL->m_RenderData.projection.copy().multiply(matrix);
2023-11-04 01:39:14 +01:00
g_pHyprOpenGL->blend(true);
glUseProgram(g_pGlobalState->trailShader.program);
#ifndef GLES2
2024-09-25 12:08:10 +02:00
glUniformMatrix3fv(g_pGlobalState->trailShader.proj, 1, GL_TRUE, glMatrix.getMatrix().data());
2023-11-04 01:39:14 +01:00
#else
2024-09-25 12:08:10 +02:00
glMatrix.transpose();
glUniformMatrix3fv(g_pGlobalState->trailShader.proj, 1, GL_FALSE, glMatrix.getMatrix().data());
2023-11-04 01:39:14 +01:00
#endif
2023-11-04 18:28:57 +01:00
std::vector<point2> points;
2023-11-04 01:39:14 +01:00
std::vector<Vector2D> bezierPts;
std::vector<Vector2D> pointsForBezier;
2023-11-04 18:28:57 +01:00
std::vector<float> agesForBezier;
2023-11-04 01:39:14 +01:00
2023-11-04 18:28:57 +01:00
float originalCoeff = 50;
2023-11-04 01:39:14 +01:00
2023-11-04 18:28:57 +01:00
auto msFrom = [](std::chrono::system_clock::time_point t) -> float {
2023-11-04 01:39:14 +01:00
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now() - t).count() / 1000.0;
};
auto dist = [&](const point2& a, const point2& b) -> float {
Vector2D diff = Vector2D{a.x - b.x, a.y - b.y} * pMonitor->vecSize;
2023-11-04 01:39:14 +01:00
return std::sqrt(diff.x * diff.x + diff.y * diff.y);
};
2023-11-04 18:28:57 +01:00
float msMaxToLast = msFrom(m_dLastGeoms.back().second);
2023-11-04 01:39:14 +01:00
2023-11-04 18:28:57 +01:00
float dists[2] = {0, 0};
2023-11-04 01:39:14 +01:00
2023-11-04 18:28:57 +01:00
Vector2D mainVec = {originalCoeff / pMonitor->vecSize.x, originalCoeff / pMonitor->vecSize.y};
2024-04-27 14:03:46 +02:00
Vector2D windowMiddle = PWINDOW->middle() - pMonitor->vecPosition;
2023-11-04 01:39:14 +01:00
points.push_back(
Vector2D{cos(0) * mainVec.x - sin(0) * mainVec.y + windowMiddle.x / pMonitor->vecSize.x, sin(0) * mainVec.x + cos(0) * mainVec.y + windowMiddle.y / pMonitor->vecSize.y});
points.push_back(Vector2D{cos(-M_PI_2) * mainVec.x - sin(-M_PI_2) * mainVec.y + windowMiddle.x / pMonitor->vecSize.x,
sin(-M_PI_2) * mainVec.x + cos(-M_PI_2) * mainVec.y + windowMiddle.y / pMonitor->vecSize.y});
points.push_back(Vector2D{cos(M_PI_2) * mainVec.x - sin(M_PI_2) * mainVec.y + windowMiddle.x / pMonitor->vecSize.x,
sin(M_PI_2) * mainVec.x + cos(M_PI_2) * mainVec.y + windowMiddle.y / pMonitor->vecSize.y});
points.push_back(Vector2D{cos(M_PI) * mainVec.x - sin(M_PI) * mainVec.y + windowMiddle.x / pMonitor->vecSize.x,
sin(M_PI) * mainVec.x + cos(M_PI) * mainVec.y + windowMiddle.y / pMonitor->vecSize.y});
2023-11-04 01:39:14 +01:00
pointsForBezier.push_back(windowMiddle);
agesForBezier.push_back(0);
for (size_t i = 0; i < m_dLastGeoms.size(); i += 1) {
box box = m_dLastGeoms[i].first;
box.x -= pMonitor->vecPosition.x;
box.y -= pMonitor->vecPosition.y;
Vector2D middle = {box.x + box.w / 2.0, box.y + box.h / 2.0};
2023-11-04 18:28:57 +01:00
if (middle == pointsForBezier[pointsForBezier.size() - 1])
continue;
2023-11-04 01:39:14 +01:00
pointsForBezier.push_back(middle);
agesForBezier.push_back(msFrom(m_dLastGeoms[i].second));
}
if (pointsForBezier.size() < 3) {
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glDisable(GL_STENCIL_TEST);
glStencilMask(-1);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
2023-11-04 18:28:57 +01:00
g_pHyprOpenGL->scissor((CBox*)nullptr);
2023-11-04 01:39:14 +01:00
return;
}
2023-11-04 18:28:57 +01:00
float maxAge = agesForBezier.back();
2024-02-18 16:30:21 +01:00
float tCoeff = **PBEZIERSTEP;
int pointsPerBezier = **PPOINTSPERSTEP;
2023-11-04 01:39:14 +01:00
bezierPts.push_back(vecForBezierT(0, pointsForBezier));
for (float t = tCoeff; t <= 1.0; t += tCoeff) {
bezierPts.push_back(vecForBezierT(t, pointsForBezier));
2023-11-04 18:28:57 +01:00
const Vector2D& lastbezier = bezierPts.back();
2023-11-04 01:39:14 +01:00
const Vector2D& lastprevbezier = bezierPts[bezierPts.size() - 2];
for (size_t i = 1; i < pointsPerBezier + 1; ++i) {
2023-11-04 18:28:57 +01:00
const float bezierPointStep = (1.0 / (pointsPerBezier + 2));
const Vector2D& middle = vecForT(lastprevbezier, lastbezier, bezierPointStep * (i + 1));
const Vector2D& lastmiddle = vecForT(lastprevbezier, lastbezier, bezierPointStep * i);
2023-11-04 01:39:14 +01:00
2023-11-04 18:28:57 +01:00
Vector2D vecNormal = {middle.x - lastmiddle.x, middle.y - lastmiddle.y};
2023-11-04 01:39:14 +01:00
// normalize vec
float invlen = 1.0 / std::sqrt(vecNormal.x * vecNormal.x + vecNormal.y * vecNormal.y);
vecNormal.x *= invlen;
vecNormal.y *= invlen;
// make sure it points up
// vecNormal.y = std::abs(vecNormal.y);
// extend by coeff
float ageCoeff = t * (agesForBezier.size() - 1);
float ageFloor = std::floor(ageCoeff);
2023-11-04 18:28:57 +01:00
float ageCeil = std::ceil(ageCoeff);
float approxAge =
agesForBezier[static_cast<int>(ageFloor)] + (agesForBezier[static_cast<int>(ageCeil)] - agesForBezier[static_cast<int>(ageFloor)]) * (ageCoeff - ageFloor);
float coeff = originalCoeff * (1.0 - (approxAge / maxAge));
2023-11-04 01:39:14 +01:00
Vector2D newVec = {vecNormal.x * coeff / pMonitor->vecSize.x, vecNormal.y * coeff / pMonitor->vecSize.y};
2023-11-04 18:28:57 +01:00
if ((newVec.x == 0 && newVec.y == 0) || std::isnan(newVec.x) || std::isnan(newVec.y))
continue;
2023-11-04 01:39:14 +01:00
// rotate by 90 and -90 and add middle
points.push_back(Vector2D{cos(M_PI_2) * newVec.x - sin(M_PI_2) * newVec.y + middle.x / pMonitor->vecSize.x,
sin(M_PI_2) * newVec.x + cos(M_PI_2) * newVec.y + middle.y / pMonitor->vecSize.y});
points.push_back(Vector2D{cos(-M_PI_2) * newVec.x - sin(-M_PI_2) * newVec.y + middle.x / pMonitor->vecSize.x,
sin(-M_PI_2) * newVec.x + cos(-M_PI_2) * newVec.y + middle.y / pMonitor->vecSize.y});
2023-11-04 01:39:14 +01:00
}
}
2024-04-27 14:03:46 +02:00
box thisboxopengl = box{(PWINDOW->m_vRealPosition.value().x - pMonitor->vecPosition.x) / pMonitor->vecSize.x,
(PWINDOW->m_vRealPosition.value().y - pMonitor->vecPosition.y) / pMonitor->vecSize.y,
(PWINDOW->m_vRealPosition.value().x + PWINDOW->m_vRealSize.value().x) / pMonitor->vecSize.x,
(PWINDOW->m_vRealPosition.value().y + PWINDOW->m_vRealSize.value().y) / pMonitor->vecSize.y};
2023-11-04 01:39:14 +01:00
glUniform4f(g_pGlobalState->trailShader.gradient, thisboxopengl.x, thisboxopengl.y, thisboxopengl.w, thisboxopengl.h);
glUniform4f(g_pGlobalState->trailShader.color, COLOR.r, COLOR.g, COLOR.b, COLOR.a);
2023-11-04 18:28:57 +01:00
CBox transformedBox = monbox;
2024-09-25 12:08:10 +02:00
transformedBox.transform(wlTransformToHyprutils(invertTransform(g_pHyprOpenGL->m_RenderData.pMonitor->transform)), g_pHyprOpenGL->m_RenderData.pMonitor->vecTransformedSize.x,
g_pHyprOpenGL->m_RenderData.pMonitor->vecTransformedSize.y);
2023-11-04 01:39:14 +01:00
glVertexAttribPointer(g_pGlobalState->trailShader.posAttrib, 2, GL_FLOAT, GL_FALSE, 0, (float*)points.data());
glEnableVertexAttribArray(g_pGlobalState->trailShader.posAttrib);
if (g_pHyprOpenGL->m_RenderData.clipBox.width != 0 && g_pHyprOpenGL->m_RenderData.clipBox.height != 0) {
CRegion damageClip{g_pHyprOpenGL->m_RenderData.clipBox.x, g_pHyprOpenGL->m_RenderData.clipBox.y, g_pHyprOpenGL->m_RenderData.clipBox.width,
g_pHyprOpenGL->m_RenderData.clipBox.height};
damageClip.intersect(g_pHyprOpenGL->m_RenderData.damage);
if (!damageClip.empty()) {
for (auto& RECT : damageClip.getRects()) {
g_pHyprOpenGL->scissor(&RECT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, points.size());
}
}
} else {
for (auto& RECT : g_pHyprOpenGL->m_RenderData.damage.getRects()) {
g_pHyprOpenGL->scissor(&RECT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, points.size());
}
}
glDisableVertexAttribArray(g_pGlobalState->trailShader.posAttrib);
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
glDisable(GL_STENCIL_TEST);
glStencilMask(-1);
glStencilFunc(GL_ALWAYS, 1, 0xFF);
2023-11-04 18:28:57 +01:00
g_pHyprOpenGL->scissor((CBox*)nullptr);
2023-11-04 01:39:14 +01:00
// calculate damage
float minX = 9999999;
float minY = 9999999;
float maxX = -9999999;
float maxY = -9999999;
for (auto& p : points) {
2023-11-04 18:28:57 +01:00
if (p.x < minX)
minX = p.x;
if (p.y < minY)
minY = p.y;
if (p.x > maxX)
maxX = p.x;
if (p.y > maxY)
maxY = p.y;
2023-11-04 01:39:14 +01:00
}
// bring back to global coords
minX *= pMonitor->vecSize.x;
minY *= pMonitor->vecSize.y;
maxX *= pMonitor->vecSize.x;
maxY *= pMonitor->vecSize.y;
2023-11-04 01:39:14 +01:00
2023-11-04 18:28:57 +01:00
m_bLastBox.x = minX + pMonitor->vecPosition.x;
m_bLastBox.y = minY + pMonitor->vecPosition.y;
m_bLastBox.width = maxX - minX;
2023-11-04 01:39:14 +01:00
m_bLastBox.height = maxY - minY;
m_bNeedsDamage = true;
}
2023-11-04 18:28:57 +01:00
eDecorationType CTrail::getDecorationType() {
return DECORATION_CUSTOM;
}
2023-11-04 01:39:14 +01:00
2024-04-27 14:03:46 +02:00
void CTrail::updateWindow(PHLWINDOW pWindow) {
2024-04-03 13:09:31 +02:00
const auto PWORKSPACE = pWindow->m_pWorkspace;
2023-11-04 01:39:14 +01:00
2024-03-02 20:38:48 +01:00
const auto WORKSPACEOFFSET = PWORKSPACE && !pWindow->m_bPinned ? PWORKSPACE->m_vRenderOffset.value() : Vector2D();
2023-11-04 01:39:14 +01:00
2024-03-02 20:38:48 +01:00
m_vLastWindowPos = pWindow->m_vRealPosition.value() + WORKSPACEOFFSET;
m_vLastWindowSize = pWindow->m_vRealSize.value();
2023-11-04 01:39:14 +01:00
damageEntire();
}
void CTrail::damageEntire() {
2023-11-04 18:28:57 +01:00
CBox dm = {(int)(m_vLastWindowPos.x - m_seExtents.topLeft.x), (int)(m_vLastWindowPos.y - m_seExtents.topLeft.y),
(int)(m_vLastWindowSize.x + m_seExtents.topLeft.x + m_seExtents.bottomRight.x), (int)m_seExtents.topLeft.y};
2023-11-04 01:39:14 +01:00
g_pHyprRenderer->damageBox(&dm);
}