Skip to content

Commit

Permalink
widgets: algin rect outline radius with inner border radius (#614)
Browse files Browse the repository at this point in the history
* widget: add utility functions to calculate borders and make em static

* image: use the rounding util functions

* input-field: use the rounding util functions

* shape: use the rounding util functions
  • Loading branch information
PaideiaDilemma authored Dec 29, 2024
1 parent 820eaff commit d94cc3a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
19 changes: 19 additions & 0 deletions src/renderer/widgets/IWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ Vector2D IWidget::posFromHVAlign(const Vector2D& viewport, const Vector2D& size,
return pos;
}

int IWidget::roundingForBox(const CBox& box, int roundingConfig) {
const int MINHALFBOX = std::min(box.w, box.h) / 2.0;
if (roundingConfig == -1)
return MINHALFBOX;

return std::clamp(roundingConfig, 0, MINHALFBOX);
}

int IWidget::roundingForBorderBox(const CBox& borderBox, int roundingConfig, int thickness) {
const int MINHALFBORDER = std::min(borderBox.w, borderBox.h) / 2.0;
if (roundingConfig == -1)
return MINHALFBORDER;

else if (roundingConfig == 0)
return 0;

return std::clamp(roundingConfig + thickness, 0, MINHALFBORDER);
}

static void replaceAllAttempts(std::string& str) {

const size_t ATTEMPTS = g_pAuth->m_iFailedAttempts;
Expand Down
10 changes: 6 additions & 4 deletions src/renderer/widgets/IWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ class IWidget {
};
virtual ~IWidget() = default;

virtual bool draw(const SRenderData& data) = 0;
virtual bool draw(const SRenderData& data) = 0;

virtual Vector2D posFromHVAlign(const Vector2D& viewport, const Vector2D& size, const Vector2D& offset, const std::string& halign, const std::string& valign,
const double& ang = 0);
static Vector2D posFromHVAlign(const Vector2D& viewport, const Vector2D& size, const Vector2D& offset, const std::string& halign, const std::string& valign,
const double& ang = 0);
static int roundingForBox(const CBox& box, int roundingConfig);
static int roundingForBorderBox(const CBox& borderBox, int roundingConfig, int thickness);

struct SFormatResult {
std::string formatted;
Expand All @@ -23,5 +25,5 @@ class IWidget {
bool allowForceUpdate = false;
};

virtual SFormatResult formatString(std::string in);
static SFormatResult formatString(std::string in);
};
11 changes: 5 additions & 6 deletions src/renderer/widgets/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,25 @@ bool CImage::draw(const SRenderData& data) {
texbox.w *= std::max(SCALEX, SCALEY);
texbox.h *= std::max(SCALEX, SCALEY);

const bool ALLOWROUND = rounding > -1 && rounding < std::min(texbox.w, texbox.h) / 2.0;

// plus borders if any
CBox borderBox = {angle == 0 ? BORDERPOS : BORDERPOS + Vector2D{1.0, 1.0}, texbox.size() + IMAGEPOS * 2.0};

borderBox.round();

const Vector2D FBSIZE = angle == 0 ? borderBox.size() : borderBox.size() + Vector2D{2.0, 2.0};
const Vector2D FBSIZE = angle == 0 ? borderBox.size() : borderBox.size() + Vector2D{2.0, 2.0};
const int ROUND = roundingForBox(texbox, rounding);
const int BORDERROUND = roundingForBorderBox(borderBox, rounding, border);

imageFB.alloc(FBSIZE.x, FBSIZE.y, true);
g_pRenderer->pushFb(imageFB.m_iFb);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);

if (border > 0)
g_pRenderer->renderBorder(borderBox, color, border, ALLOWROUND ? (rounding == 0 ? 0 : rounding + std::round(border / M_PI)) : std::min(borderBox.w, borderBox.h) / 2.0,
1.0);
g_pRenderer->renderBorder(borderBox, color, border, BORDERROUND, 1.0);

texbox.round();
g_pRenderer->renderTexture(texbox, asset->texture, 1.0, ALLOWROUND ? rounding : std::min(texbox.w, texbox.h) / 2.0, HYPRUTILS_TRANSFORM_NORMAL);
g_pRenderer->renderTexture(texbox, asset->texture, 1.0, ROUND, HYPRUTILS_TRANSFORM_NORMAL);
g_pRenderer->popFb();
}

Expand Down
9 changes: 5 additions & 4 deletions src/renderer/widgets/PasswordInputField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ bool CPasswordInputField::draw(const SRenderData& data) {
fontCol.a *= fade.a * data.opacity;

if (outThick > 0) {
const auto OUTERROUND = rounding == -1 ? outerBox.h / 2.0 : rounding;
g_pRenderer->renderBorder(outerBox, outerGrad, outThick, OUTERROUND, fade.a * data.opacity);
const int BORDERROUND = roundingForBorderBox(outerBox, rounding, outThick);
g_pRenderer->renderBorder(outerBox, outerGrad, outThick, BORDERROUND, fade.a * data.opacity);

if (passwordLength != 0 && hiddenInputState.enabled && !fade.animated && data.opacity == 1.0) {
CBox outerBoxScaled = outerBox;
Expand All @@ -226,13 +226,14 @@ bool CPasswordInputField::draw(const SRenderData& data) {
outerBoxScaled.x += outerBoxScaled.w;
glEnable(GL_SCISSOR_TEST);
glScissor(outerBoxScaled.x, outerBoxScaled.y, outerBoxScaled.w, outerBoxScaled.h);
g_pRenderer->renderBorder(outerBox, hiddenInputState.lastColor, outThick, OUTERROUND, fade.a * data.opacity);
g_pRenderer->renderBorder(outerBox, hiddenInputState.lastColor, outThick, BORDERROUND, fade.a * data.opacity);
glScissor(0, 0, viewport.x, viewport.y);
glDisable(GL_SCISSOR_TEST);
}
}

g_pRenderer->renderRect(inputFieldBox, innerCol, rounding == -1 ? inputFieldBox.h / 2.0 : rounding - outThick - 1);
const int ROUND = roundingForBox(inputFieldBox, rounding);
g_pRenderer->renderRect(inputFieldBox, innerCol, ROUND);

if (!hiddenInputState.enabled && !g_pHyprlock->m_bFadeStarted) {
const int RECTPASSSIZE = std::nearbyint(inputFieldBox.h * dots.size * 0.5f) * 2.f;
Expand Down
9 changes: 5 additions & 4 deletions src/renderer/widgets/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,19 @@ bool CShape::draw(const SRenderData& data) {
}

if (!shapeFB.isAllocated()) {
const auto MINHALFSHAPE = std::min(shapeBox.w, shapeBox.h) / 2.0;
const bool ALLOWROUND = rounding > -1 && rounding < MINHALFSHAPE;
const int ROUND = roundingForBox(shapeBox, rounding);
const int BORDERROUND = roundingForBorderBox(borderBox, rounding, border);
Debug::log(LOG, "round: {}, borderround: {}", ROUND, BORDERROUND);

shapeFB.alloc(borderBox.width + borderBox.x * 2.0, borderBox.height + borderBox.y * 2.0, true);
g_pRenderer->pushFb(shapeFB.m_iFb);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);

if (border > 0)
g_pRenderer->renderBorder(borderBox, borderGrad, border, ALLOWROUND ? (rounding == 0 ? 0 : rounding + std::round(border / M_PI)) : MINHALFBORDER, 1.0);
g_pRenderer->renderBorder(borderBox, borderGrad, border, BORDERROUND, 1.0);

g_pRenderer->renderRect(shapeBox, color, ALLOWROUND ? rounding : MINHALFSHAPE);
g_pRenderer->renderRect(shapeBox, color, ROUND);
g_pRenderer->popFb();
}

Expand Down

0 comments on commit d94cc3a

Please sign in to comment.