From 204a79ebaf543c99d63e985ff9e99abbc31059b3 Mon Sep 17 00:00:00 2001 From: Virt <41426325+VirtCode@users.noreply.github.com> Date: Fri, 21 Jun 2024 14:32:42 +0200 Subject: [PATCH] feat: first working stick prototype --- src/main.cpp | 4 +++ src/render.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/render.hpp | 19 +++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/render.cpp create mode 100644 src/render.hpp diff --git a/src/main.cpp b/src/main.cpp index 733a93f..84ee21a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,11 +5,13 @@ #include #include "globals.hpp" +#include "render.hpp" typedef void (*origRenderSofwareCursorsFor)(void*, SP, timespec*, CRegion&, std::optional); inline CFunctionHook* g_pRenderSoftwareCursorsForHook = nullptr; void hkRenderSoftwareCursorsFor(void* thisptr, SP pMonitor, timespec* now, CRegion& damage, std::optional overridePos) { + g_pDynamicCursors->render((CPointerManager*) thisptr, pMonitor, now, damage, overridePos); //(*(origRenderSofwareCursorsFor)g_pRenderSoftwareCursorsForHook->m_pOriginal)(thisptr, pMonitor, now, damage, overridePos); } @@ -28,6 +30,8 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { g_pRenderSoftwareCursorsForHook = HyprlandAPI::createFunctionHook(PHANDLE, METHODS[0].address, (void*) &hkRenderSoftwareCursorsFor); g_pRenderSoftwareCursorsForHook->hook(); + g_pDynamicCursors = std::make_unique(); + return {"dynamic-cursors", "The most stupid cursor plugin.", "Virt", "1.1"}; } diff --git a/src/render.cpp b/src/render.cpp new file mode 100644 index 0000000..963dac9 --- /dev/null +++ b/src/render.cpp @@ -0,0 +1,66 @@ +#include "globals.hpp" + +#include +#include + +#define private public +#include +#undef private + +#include "render.hpp" + +void CDynamicCursors::render(CPointerManager* pointers, SP pMonitor, timespec* now, CRegion& damage, std::optional overridePos) { + + if (!pointers->hasCursor()) + return; + + auto state = pointers->stateFor(pMonitor); + + if ((!state->hardwareFailed && state->softwareLocks == 0)) { + return; + } + + auto box = state->box.copy(); + if (overridePos.has_value()) { + box.x = overridePos->x; + box.y = overridePos->y; + } + + if (box.intersection(CBox{{}, {pMonitor->vecSize}}).empty()) + return; + + auto texture = pointers->getCurrentCursorTexture(); + if (!texture) + return; + + box.scale(pMonitor->scale); + box.rot = this->calculate(&pointers->pointerPos); + + g_pHyprOpenGL->renderTextureWithDamage(texture, &box, &damage, 1.F); +} + +double CDynamicCursors::calculate(Vector2D* pos) { + // translate to origin + this->end.x -= pos->x; + this->end.y -= pos->y; + + // normalize + double size = this->end.size(); + this->end.x /= size; + this->end.y /= size; + + // scale to length + this->end.x *= this->size; + this->end.y *= this->size; + + // calculate angle + double angle = -atan(this->end.x / this->end.y); + if (this->end.y > 0) angle += PI; + angle += PI; + + // translate back + this->end.x += pos->x; + this->end.y += pos->y; + + return angle; +} diff --git a/src/render.hpp b/src/render.hpp new file mode 100644 index 0000000..989040a --- /dev/null +++ b/src/render.hpp @@ -0,0 +1,19 @@ +#include "globals.hpp" +#include +#include + +class CDynamicCursors; + +class CDynamicCursors { + public: + void render(CPointerManager* pointers, SP pMonitor, timespec* now, CRegion& damage, std::optional overridePos); + + private: + double size = 10; + // calculates the current angle of the cursor + double calculate(Vector2D* pos); + // this is the end of the virtual stick + Vector2D end; +}; + +inline std::unique_ptr g_pDynamicCursors;