From 3585324e4ffd7771abca67fd151904b4cc9fccd3 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Mon, 19 Feb 2024 22:45:59 +0000 Subject: [PATCH] input-field: add halign valign and pos --- src/config/ConfigManager.cpp | 6 ++++++ src/renderer/widgets/IWidget.hpp | 7 ++++++- src/renderer/widgets/Label.cpp | 18 +----------------- src/renderer/widgets/PasswordInputField.cpp | 4 +++- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 634cf62..f130b80 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -34,6 +34,9 @@ void CConfigManager::init() { m_config.addSpecialConfigValue("input-field", "outline_thickness", Hyprlang::INT{4}); m_config.addSpecialConfigValue("input-field", "fade_on_empty", Hyprlang::INT{1}); m_config.addSpecialConfigValue("input-field", "font_color", Hyprlang::INT{0xFF000000}); + m_config.addSpecialConfigValue("input-field", "halign", Hyprlang::STRING{"center"}); + m_config.addSpecialConfigValue("input-field", "valign", Hyprlang::STRING{"center"}); + m_config.addSpecialConfigValue("input-field", "position", Hyprlang::VEC2{0, -20}); m_config.addSpecialCategory("label", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true}); m_config.addSpecialConfigValue("label", "monitor", Hyprlang::STRING{""}); @@ -91,6 +94,9 @@ std::vector CConfigManager::getWidgetConfigs() { {"outline_thickness", m_config.getSpecialConfigValue("input-field", "outline_thickness", k.c_str())}, {"fade_on_empty", m_config.getSpecialConfigValue("input-field", "fade_on_empty", k.c_str())}, {"font_color", m_config.getSpecialConfigValue("input-field", "font_color", k.c_str())}, + {"halign", m_config.getSpecialConfigValue("input-field", "halign", k.c_str())}, + {"valign", m_config.getSpecialConfigValue("input-field", "valign", k.c_str())}, + {"position", m_config.getSpecialConfigValue("input-field", "position", k.c_str())}, } }); // clang-format on diff --git a/src/renderer/widgets/IWidget.hpp b/src/renderer/widgets/IWidget.hpp index 45a6e63..37810b3 100644 --- a/src/renderer/widgets/IWidget.hpp +++ b/src/renderer/widgets/IWidget.hpp @@ -1,10 +1,15 @@ #pragma once +#include "../../helpers/Vector2D.hpp" +#include + class IWidget { public: struct SRenderData { float opacity = 1; }; - 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); }; \ No newline at end of file diff --git a/src/renderer/widgets/Label.cpp b/src/renderer/widgets/Label.cpp index 8daf1c4..e7d43d0 100644 --- a/src/renderer/widgets/Label.cpp +++ b/src/renderer/widgets/Label.cpp @@ -54,23 +54,7 @@ bool CLabel::draw(const SRenderData& data) { return true; // calc pos - if (halign == "center") - pos.x += viewport.x / 2.0 - asset->texture.m_vSize.x / 2.0; - else if (halign == "left") - pos.x += 0; - else if (halign == "right") - pos.x += viewport.x - asset->texture.m_vSize.x; - else if (halign != "none") - Debug::log(ERR, "Label: invalid halign {}", halign); - - if (valign == "center") - pos.y += viewport.y / 2.0 - asset->texture.m_vSize.y / 2.0; - else if (valign == "top") - pos.y += viewport.y - asset->texture.m_vSize.y; - else if (valign == "bottom") - pos.y += asset->texture.m_vSize.y; - else if (valign != "none") - Debug::log(ERR, "Label: invalid halign {}", halign); + pos = posFromHVAlign(viewport, asset->texture.m_vSize, pos, halign, valign); } CBox box = {pos.x, pos.y, asset->texture.m_vSize.x, asset->texture.m_vSize.y}; diff --git a/src/renderer/widgets/PasswordInputField.cpp b/src/renderer/widgets/PasswordInputField.cpp index b544575..c686e25 100644 --- a/src/renderer/widgets/PasswordInputField.cpp +++ b/src/renderer/widgets/PasswordInputField.cpp @@ -5,12 +5,14 @@ CPasswordInputField::CPasswordInputField(const Vector2D& viewport, const std::unordered_map& props) { size = std::any_cast(props.at("size")); - pos = viewport / 2.f - size / 2.f; inner = std::any_cast(props.at("inner_color")); outer = std::any_cast(props.at("outer_color")); out_thick = std::any_cast(props.at("outline_thickness")); fadeOnEmpty = std::any_cast(props.at("fade_on_empty")); font = std::any_cast(props.at("font_color")); + pos = std::any_cast(props.at("position")); + + pos = posFromHVAlign(viewport, size, pos, std::any_cast(props.at("halign")), std::any_cast(props.at("valign"))); } void CPasswordInputField::updateFade() {