Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widgets: add Shape + minor image improve #266

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ void CConfigManager::init() {
m_config.addSpecialConfigValue("background", "vibrancy", Hyprlang::FLOAT{0.1686});
m_config.addSpecialConfigValue("background", "vibrancy_darkness", Hyprlang::FLOAT{0.05});

m_config.addSpecialCategory("shape", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true});
m_config.addSpecialConfigValue("shape", "monitor", Hyprlang::STRING{""});
m_config.addSpecialConfigValue("shape", "size", Hyprlang::VEC2{100, 100});
m_config.addSpecialConfigValue("shape", "rounding", Hyprlang::INT{0});
m_config.addSpecialConfigValue("shape", "border_size", Hyprlang::INT{0});
m_config.addSpecialConfigValue("shape", "border_color", Hyprlang::INT{0xFFDDDDDD});
m_config.addSpecialConfigValue("shape", "color", Hyprlang::INT{0xFF111111});
m_config.addSpecialConfigValue("shape", "position", Hyprlang::VEC2{0, 0});
m_config.addSpecialConfigValue("shape", "halign", Hyprlang::STRING{"center"});
m_config.addSpecialConfigValue("shape", "valign", Hyprlang::STRING{"center"});
m_config.addSpecialConfigValue("shape", "rotate", Hyprlang::FLOAT{0});
m_config.addSpecialConfigValue("shape", "xray", Hyprlang::INT{0});
SHADOWABLE("shape");

m_config.addSpecialCategory("image", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true});
m_config.addSpecialConfigValue("image", "monitor", Hyprlang::STRING{""});
m_config.addSpecialConfigValue("image", "path", Hyprlang::STRING{""});
Expand Down Expand Up @@ -169,6 +183,30 @@ std::vector<CConfigManager::SWidgetConfig> CConfigManager::getWidgetConfigs() {
// clang-format on
}

//
keys = m_config.listKeysForSpecialCategory("shape");
for (auto& k : keys) {
// clang-format off
result.push_back(CConfigManager::SWidgetConfig{
"shape",
std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("shape", "monitor", k.c_str())),
{
{"size", m_config.getSpecialConfigValue("shape", "size", k.c_str())},
{"rounding", m_config.getSpecialConfigValue("shape", "rounding", k.c_str())},
{"border_size", m_config.getSpecialConfigValue("shape", "border_size", k.c_str())},
{"border_color", m_config.getSpecialConfigValue("shape", "border_color", k.c_str())},
{"color", m_config.getSpecialConfigValue("shape", "color", k.c_str())},
{"position", m_config.getSpecialConfigValue("shape", "position", k.c_str())},
{"halign", m_config.getSpecialConfigValue("shape", "halign", k.c_str())},
{"valign", m_config.getSpecialConfigValue("shape", "valign", k.c_str())},
{"rotate", m_config.getSpecialConfigValue("shape", "rotate", k.c_str())},
{"xray", m_config.getSpecialConfigValue("shape", "xray", k.c_str())},
SHADOWABLE("shape"),
}
});
// clang-format on
}

//
keys = m_config.listKeysForSpecialCategory("image");
for (auto& k : keys) {
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "widgets/Background.hpp"
#include "widgets/Label.hpp"
#include "widgets/Image.hpp"
#include "widgets/Shape.hpp"

inline const float fullVerts[] = {
1, 0, // top right
Expand Down Expand Up @@ -324,6 +325,8 @@ std::vector<std::unique_ptr<IWidget>>* CRenderer::getOrCreateWidgetsFor(const CS
widgets[surf].emplace_back(std::make_unique<CPasswordInputField>(surf->size, c.values, surf->output->stringPort));
} else if (c.type == "label") {
widgets[surf].emplace_back(std::make_unique<CLabel>(surf->size, c.values, surf->output->stringPort));
} else if (c.type == "shape") {
widgets[surf].emplace_back(std::make_unique<CShape>(surf->size, c.values));
} else if (c.type == "image") {
const std::string PATH = std::any_cast<Hyprlang::STRING>(c.values.at("path"));

Expand Down
21 changes: 13 additions & 8 deletions src/renderer/widgets/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,34 @@ bool CImage::draw(const SRenderData& data) {

if (!imageFB.isAllocated()) {

const Vector2D TEXSIZE = asset->texture.m_vSize;
const float SCALEX = size / TEXSIZE.x;
const float SCALEY = size / TEXSIZE.y;
const Vector2D IMAGEPOS = {border, border};
const Vector2D BORDERPOS = {0.0, 0.0};
const Vector2D TEXSIZE = asset->texture.m_vSize;
const float SCALEX = size / TEXSIZE.x;
const float SCALEY = size / TEXSIZE.y;

// image with borders offset
CBox texbox = {{border, border}, TEXSIZE};
// image with borders offset, with extra pixel for anti-aliasing when rotated
CBox texbox = {angle == 0 ? IMAGEPOS : IMAGEPOS + Vector2D{1.0, 1.0}, TEXSIZE};

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 = {{}, {texbox.w + border * 2.0, texbox.h + border * 2.0}};
CBox borderBox = {angle == 0 ? BORDERPOS : BORDERPOS + Vector2D{1.0, 1.0}, texbox.size() + IMAGEPOS * 2.0};

borderBox.round();
imageFB.alloc(borderBox.w, borderBox.h, true);

const Vector2D FBSIZE = angle == 0 ? borderBox.size() : borderBox.size() + Vector2D{2.0, 2.0};

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->renderRect(borderBox, color, ALLOWROUND ? rounding : std::min(borderBox.w, borderBox.h) / 2.0);
g_pRenderer->renderRect(borderBox, color, ALLOWROUND ? (rounding == 0 ? 0 : rounding + std::round(border / M_PI)) : std::min(borderBox.w, borderBox.h) / 2.0);

texbox.round();
g_pRenderer->renderTexture(texbox, asset->texture, 1.0, ALLOWROUND ? rounding : std::min(texbox.w, texbox.h) / 2.0, WL_OUTPUT_TRANSFORM_NORMAL);
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/widgets/Label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ bool CLabel::draw(const SRenderData& data) {
}
}

shadow.draw(data);

// calc pos
pos = posFromHVAlign(viewport, asset->texture.m_vSize, configPos, halign, valign, angle);

CBox box = {pos.x, pos.y, asset->texture.m_vSize.x, asset->texture.m_vSize.y};
box.rot = angle;
shadow.draw(data);
g_pRenderer->renderTexture(box, asset->texture, data.opacity);

return false;
Expand Down
108 changes: 108 additions & 0 deletions src/renderer/widgets/Shape.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "Shape.hpp"
#include "../Renderer.hpp"
#include <cmath>

CShape::CShape(const Vector2D& viewport_, const std::unordered_map<std::string, std::any>& props) : shadow(this, props, viewport_) {

size = std::any_cast<Hyprlang::VEC2>(props.at("size"));
rounding = std::any_cast<Hyprlang::INT>(props.at("rounding"));
border = std::any_cast<Hyprlang::INT>(props.at("border_size"));
color = std::any_cast<Hyprlang::INT>(props.at("color"));
borderColor = std::any_cast<Hyprlang::INT>(props.at("border_color"));
pos = std::any_cast<Hyprlang::VEC2>(props.at("position"));
halign = std::any_cast<Hyprlang::STRING>(props.at("halign"));
valign = std::any_cast<Hyprlang::STRING>(props.at("valign"));
angle = std::any_cast<Hyprlang::FLOAT>(props.at("rotate"));
xray = std::any_cast<Hyprlang::INT>(props.at("xray"));

viewport = viewport_;
angle = angle * M_PI / 180.0;

const Vector2D VBORDER = {border, border};
const Vector2D REALSIZE = size + VBORDER * 2.0;

pos = posFromHVAlign(viewport, xray ? size : (angle == 0 ? REALSIZE : REALSIZE + Vector2D{2.0, 2.0}), pos, halign, valign, xray ? 0 : angle);

if (xray) {
shapeBox = {pos, size};
borderBox = {pos - VBORDER, REALSIZE};
} else {
shapeBox = {pos + VBORDER, size};
borderBox = {pos, REALSIZE};
}
}

bool CShape::draw(const SRenderData& data) {

if (firstRender) {
firstRender = false;
shadow.markShadowDirty();
}

shadow.draw(data);

CColor borderCol = borderColor;
borderCol.a *= data.opacity;

const auto MINHALFBORDER = std::min(borderBox.w, borderBox.h) / 2.0;

if (xray) {
if (border > 0) {
const int PIROUND = std::min(MINHALFBORDER, std::round(border * M_PI));
g_pRenderer->renderRect(borderBox, borderCol, rounding == -1 ? PIROUND : std::clamp(rounding, 0, PIROUND));
}

glEnable(GL_SCISSOR_TEST);
glScissor(shapeBox.x, shapeBox.y, shapeBox.width, shapeBox.height);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);

return data.opacity < 1.0;
}

const auto MINHALFSHAPE = std::min(shapeBox.w, shapeBox.h) / 2.0;
const bool ALLOWROUND = rounding > -1 && rounding < MINHALFSHAPE;
const int ROUNDSHAPE = ALLOWROUND ? rounding : MINHALFSHAPE;
const int ROUNDBORDER = ALLOWROUND ? (rounding == 0 ? 0 : rounding + std::round(border / M_PI)) : MINHALFBORDER;

if (angle == 0) {
CColor shapeCol = color;
shapeCol.a *= data.opacity;

if (border > 0)
g_pRenderer->renderRect(borderBox, borderCol, ROUNDBORDER);

g_pRenderer->renderRect(shapeBox, shapeCol, ROUNDSHAPE);

return data.opacity < 1.0;
}

if (!shapeFB.isAllocated()) {
borderBox.x = 1.0;
borderBox.y = 1.0;
shapeBox.x = border + 1.0;
shapeBox.y = border + 1.0;

shapeFB.alloc(borderBox.width + 2.0, borderBox.height + 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->renderRect(borderBox, borderColor, ROUNDBORDER);

g_pRenderer->renderRect(shapeBox, color, ROUNDSHAPE);
g_pRenderer->popFb();
}

CTexture* tex = &shapeFB.m_cTex;
CBox texbox = {pos, tex->m_vSize};

texbox.round();
texbox.rot = angle;

g_pRenderer->renderTexture(texbox, *tex, data.opacity, 0, WL_OUTPUT_TRANSFORM_FLIPPED_180);

return data.opacity < 1.0;
}
38 changes: 38 additions & 0 deletions src/renderer/widgets/Shape.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include "IWidget.hpp"
#include "../../helpers/Vector2D.hpp"
#include "../../helpers/Color.hpp"
#include "../../helpers/Box.hpp"
#include "Shadowable.hpp"
#include <string>
#include <unordered_map>
#include <any>

class CShape : public IWidget {
public:
CShape(const Vector2D& viewport, const std::unordered_map<std::string, std::any>& props);

virtual bool draw(const SRenderData& data);

private:
CFramebuffer shapeFB;

int rounding;
double border;
double angle;
CColor color;
CColor borderColor;
Vector2D size;
Vector2D pos;
CBox shapeBox;
CBox borderBox;
bool xray;

std::string halign, valign;

bool firstRender = true;

Vector2D viewport;
CShadowable shadow;
};
Loading