diff options
| author | Vaxry <43317083+vaxerski@users.noreply.github.com> | 2023-11-04 17:03:05 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-04 17:03:05 +0000 |
| commit | 55b4f84fea33adee3ec4b0a69c2bca6e7a1f5754 (patch) | |
| tree | be7db4f841b4ac049527daae01e683701bf92b92 /src/helpers | |
| parent | 73e78f05ad5cafa20ac5bf177c94ac9ecca37097 (diff) | |
Internal: Hyprland box implementation (#3755)
* box impl
* remove unused operators
* missed applyfromwlr
Diffstat (limited to 'src/helpers')
| -rw-r--r-- | src/helpers/Box.cpp | 104 | ||||
| -rw-r--r-- | src/helpers/Box.hpp | 66 | ||||
| -rw-r--r-- | src/helpers/MiscFunctions.cpp | 7 | ||||
| -rw-r--r-- | src/helpers/MiscFunctions.hpp | 1 | ||||
| -rw-r--r-- | src/helpers/Monitor.cpp | 4 | ||||
| -rw-r--r-- | src/helpers/Monitor.hpp | 2 | ||||
| -rw-r--r-- | src/helpers/Region.cpp | 6 | ||||
| -rw-r--r-- | src/helpers/Region.hpp | 6 | ||||
| -rw-r--r-- | src/helpers/SubsurfaceTree.cpp | 7 | ||||
| -rw-r--r-- | src/helpers/Vector2D.hpp | 3 | ||||
| -rw-r--r-- | src/helpers/WLClasses.hpp | 6 |
11 files changed, 192 insertions, 20 deletions
diff --git a/src/helpers/Box.cpp b/src/helpers/Box.cpp new file mode 100644 index 00000000..450871cb --- /dev/null +++ b/src/helpers/Box.cpp @@ -0,0 +1,104 @@ +#include "Box.hpp" +wlr_box CBox::wlr() { + CBox rounded = roundInternal(); + m_bWlrBox = wlr_box{(int)rounded.x, (int)rounded.y, (int)rounded.w, (int)rounded.h}; + return m_bWlrBox; +} + +wlr_box* CBox::pWlr() { + CBox rounded = roundInternal(); + m_bWlrBox = wlr_box{(int)rounded.x, (int)rounded.y, (int)rounded.w, (int)rounded.h}; + return &m_bWlrBox; +} + +CBox& CBox::scale(double scale) { + x *= scale; + y *= scale; + w *= scale; + h *= scale; + + return *this; +} + +CBox& CBox::scale(const Vector2D& scale) { + x *= scale.x; + y *= scale.y; + w *= scale.x; + h *= scale.y; + + return *this; +} + +CBox& CBox::translate(const Vector2D& vec) { + x += vec.x; + y += vec.y; + + return *this; +} + +Vector2D CBox::middle() const { + return Vector2D{x + w / 2.0, y + h / 2.0}; +} + +bool CBox::containsPoint(const Vector2D& vec) const { + return VECINRECT(vec, x, y, x + w, y + h); +} + +bool CBox::empty() const { + return w == 0 || h == 0; +} + +CBox& CBox::applyFromWlr() { + x = m_bWlrBox.x; + y = m_bWlrBox.y; + w = m_bWlrBox.width; + h = m_bWlrBox.height; + + return *this; +} + +CBox& CBox::round() { + float newW = x + w - std::floor(x); + float newH = y + h - std::floor(y); + x = std::floor(x); + y = std::floor(y); + w = std::floor(newW); + h = std::floor(newH); + + return *this; +} + +CBox& CBox::transform(const wl_output_transform t, double w, double h) { + wlr_box_transform(&m_bWlrBox, pWlr(), t, w, h); + applyFromWlr(); + + return *this; +} + +CBox& CBox::addExtents(const SWindowDecorationExtents& e) { + x -= e.topLeft.x; + y -= e.topLeft.y; + w += e.topLeft.x + e.bottomRight.x; + h += e.topLeft.y + e.bottomRight.y; + + return *this; +} + +CBox& CBox::scaleFromCenter(double scale) { + double oldW = w, oldH = h; + + w *= scale; + h *= scale; + + x -= (w - oldW) / 2.0; + y -= (h - oldH) / 2.0; + + return *this; +} + +CBox CBox::roundInternal() { + float newW = x + w - std::floor(x); + float newH = y + h - std::floor(y); + + return CBox{std::floor(x), std::floor(y), std::floor(newW), std::floor(newH)}; +}
\ No newline at end of file diff --git a/src/helpers/Box.hpp b/src/helpers/Box.hpp new file mode 100644 index 00000000..7a8aa939 --- /dev/null +++ b/src/helpers/Box.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include <wlr/util/box.h> +#include "Vector2D.hpp" +#include "../SharedDefs.hpp" + +class CBox { + public: + CBox(double x_, double y_, double w_, double h_) { + x = x_; + y = y_; + w = w_; + h = h_; + } + + CBox() { + w = 0; + h = 0; + } + + CBox(const wlr_box& box) { + x = box.x; + y = box.y; + w = box.width; + h = box.height; + } + + CBox(const double d) { + x = d; + y = d; + w = d; + h = d; + } + + wlr_box wlr(); + wlr_box* pWlr(); + + CBox& applyFromWlr(); + CBox& scale(double scale); + CBox& scaleFromCenter(double scale); + CBox& scale(const Vector2D& scale); + CBox& translate(const Vector2D& vec); + CBox& round(); + CBox& transform(const wl_output_transform t, double w, double h); + CBox& addExtents(const SWindowDecorationExtents& e); + + Vector2D middle() const; + + bool containsPoint(const Vector2D& vec) const; + bool empty() const; + + double x = 0, y = 0; + union { + double w; + double width; + }; + union { + double h; + double height; + }; + + private: + CBox roundInternal(); + + wlr_box m_bWlrBox; +};
\ No newline at end of file diff --git a/src/helpers/MiscFunctions.cpp b/src/helpers/MiscFunctions.cpp index b65f4a87..b3a8ade7 100644 --- a/src/helpers/MiscFunctions.cpp +++ b/src/helpers/MiscFunctions.cpp @@ -185,13 +185,6 @@ std::string escapeJSONStrings(const std::string& str) { return oss.str(); } -void scaleBox(wlr_box* box, float scale) { - box->width = std::round(box->width * scale); - box->height = std::round(box->height * scale); - box->x = std::round(box->x * scale); - box->y = std::round(box->y * scale); -} - std::string removeBeginEndSpacesTabs(std::string str) { if (str.empty()) return str; diff --git a/src/helpers/MiscFunctions.hpp b/src/helpers/MiscFunctions.hpp index 499aa90f..3bb07f79 100644 --- a/src/helpers/MiscFunctions.hpp +++ b/src/helpers/MiscFunctions.hpp @@ -16,7 +16,6 @@ struct SCallstackFrameInfo { std::string absolutePath(const std::string&, const std::string&); void addWLSignal(wl_signal*, wl_listener*, void* pOwner, const std::string& ownerString); std::string escapeJSONStrings(const std::string& str); -void scaleBox(wlr_box*, float); std::string removeBeginEndSpacesTabs(std::string); bool isNumber(const std::string&, bool allowfloat = false); bool isDirection(const std::string&); diff --git a/src/helpers/Monitor.cpp b/src/helpers/Monitor.cpp index 6d6b40e0..9dbd2a3e 100644 --- a/src/helpers/Monitor.cpp +++ b/src/helpers/Monitor.cpp @@ -317,14 +317,14 @@ void CMonitor::addDamage(const CRegion* rg) { addDamage(const_cast<CRegion*>(rg)->pixman()); } -void CMonitor::addDamage(const wlr_box* box) { +void CMonitor::addDamage(const CBox* box) { static auto* const PZOOMFACTOR = &g_pConfigManager->getConfigValuePtr("misc:cursor_zoom_factor")->floatValue; if (*PZOOMFACTOR != 1.f && g_pCompositor->getMonitorFromCursor() == this) { wlr_damage_ring_add_whole(&damage); g_pCompositor->scheduleFrameForMonitor(this); } - if (wlr_damage_ring_add_box(&damage, box)) + if (wlr_damage_ring_add_box(&damage, const_cast<CBox*>(box)->pWlr())) g_pCompositor->scheduleFrameForMonitor(this); } diff --git a/src/helpers/Monitor.hpp b/src/helpers/Monitor.hpp index 5e861b50..10790b5e 100644 --- a/src/helpers/Monitor.hpp +++ b/src/helpers/Monitor.hpp @@ -113,7 +113,7 @@ class CMonitor { void onDisconnect(); void addDamage(const pixman_region32_t* rg); void addDamage(const CRegion* rg); - void addDamage(const wlr_box* box); + void addDamage(const CBox* box); void setMirror(const std::string&); bool isMirror(); float getDefaultScale(); diff --git a/src/helpers/Region.cpp b/src/helpers/Region.cpp index 5515e71e..fdf8a242 100644 --- a/src/helpers/Region.cpp +++ b/src/helpers/Region.cpp @@ -21,6 +21,10 @@ CRegion::CRegion(wlr_box* box) { pixman_region32_init_rect(&m_rRegion, box->x, box->y, box->width, box->height); } +CRegion::CRegion(CBox* box) { + pixman_region32_init_rect(&m_rRegion, box->x, box->y, box->w, box->h); +} + CRegion::CRegion(pixman_box32_t* box) { pixman_region32_init_rect(&m_rRegion, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1); } @@ -100,7 +104,7 @@ std::vector<pixman_box32_t> CRegion::getRects() const { return result; } -wlr_box CRegion::getExtents() { +CBox CRegion::getExtents() { pixman_box32_t* box = pixman_region32_extents(&m_rRegion); return {box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1}; } diff --git a/src/helpers/Region.hpp b/src/helpers/Region.hpp index cc745f9e..94b639c9 100644 --- a/src/helpers/Region.hpp +++ b/src/helpers/Region.hpp @@ -15,6 +15,8 @@ class CRegion { CRegion(double x, double y, double w, double h); /* Create from a wlr_box */ CRegion(wlr_box* box); + /* Create from a CBox */ + CRegion(CBox* box); /* Create from a pixman_box32_t */ CRegion(pixman_box32_t* box); @@ -43,7 +45,7 @@ class CRegion { CRegion& translate(const Vector2D& vec); CRegion& invert(pixman_box32_t* box); CRegion& scale(float scale); - wlr_box getExtents(); + CBox getExtents(); bool containsPoint(const Vector2D& vec) const; bool empty() const; Vector2D closestPoint(const Vector2D& vec) const; @@ -51,7 +53,7 @@ class CRegion { std::vector<pixman_box32_t> getRects() const; pixman_region32_t* pixman() { - return &m_rRegion; + return &m_rRegion; } private: diff --git a/src/helpers/SubsurfaceTree.cpp b/src/helpers/SubsurfaceTree.cpp index c3716100..84ba5cbb 100644 --- a/src/helpers/SubsurfaceTree.cpp +++ b/src/helpers/SubsurfaceTree.cpp @@ -98,8 +98,9 @@ void SubsurfaceTree::destroySurfaceTree(SSurfaceTreeNode* pNode) { // damage if (pNode->pSurface && pNode->pSurface->exists()) { - wlr_box extents = {}; - wlr_surface_get_extends(pNode->pSurface->wlr(), &extents); + CBox extents = {}; + wlr_surface_get_extends(pNode->pSurface->wlr(), extents.pWlr()); + extents.applyFromWlr(); int lx = 0, ly = 0; addSurfaceGlobalOffset(pNode, &lx, &ly); @@ -198,7 +199,7 @@ void Events::listener_unmapSubsurface(void* owner, void* data) { int lx = 0, ly = 0; addSurfaceGlobalOffset(PNODE, &lx, &ly); - wlr_box extents = {lx, ly, 0, 0}; + CBox extents = {lx, ly, 0, 0}; extents.width = PNODE->pSurface->wlr()->current.width; extents.height = PNODE->pSurface->wlr()->current.height; diff --git a/src/helpers/Vector2D.hpp b/src/helpers/Vector2D.hpp index 3c5d4a2c..6112b8e4 100644 --- a/src/helpers/Vector2D.hpp +++ b/src/helpers/Vector2D.hpp @@ -22,6 +22,9 @@ class Vector2D { Vector2D operator-(const Vector2D& a) const { return Vector2D(this->x - a.x, this->y - a.y); } + Vector2D operator-() const { + return Vector2D(-this->x, -this->y); + } Vector2D operator*(const float& a) const { return Vector2D(this->x * a, this->y * a); } diff --git a/src/helpers/WLClasses.hpp b/src/helpers/WLClasses.hpp index e8647426..f7efbe98 100644 --- a/src/helpers/WLClasses.hpp +++ b/src/helpers/WLClasses.hpp @@ -33,7 +33,7 @@ struct SLayerSurface { DYNLISTENER(commitLayerSurface); DYNLISTENER(newPopup); - wlr_box geometry = {0, 0, 0, 0}; + CBox geometry = {0, 0, 0, 0}; Vector2D position; zwlr_layer_shell_v1_layer layer; @@ -65,12 +65,12 @@ class CMonitor; struct SRenderData { CMonitor* pMonitor; timespec* when; - int x, y; + double x, y; // for iters void* data = nullptr; wlr_surface* surface = nullptr; - int w, h; + double w, h; // for rounding bool dontRound = true; |
