#include "Label.hpp" #include "../../helpers/Color.hpp" #include #include "../Renderer.hpp" #include "../../helpers/Log.hpp" void replaceAll(std::string& str, const std::string& from, const std::string& to) { if (from.empty()) return; size_t pos = 0; while ((pos = str.find(from, pos)) != std::string::npos) { str.replace(pos, from.length(), to); pos += to.length(); } } std::string CLabel::formatString(std::string in) { replaceAll(in, "$USER", std::string{getlogin()}); return in; } CLabel::CLabel(const Vector2D& viewport_, const std::unordered_map& props) { std::string labelPreFormat = std::any_cast(props.at("text")); std::string fontFamily = std::any_cast(props.at("font_family")); CColor labelColor = std::any_cast(props.at("color")); int fontSize = std::any_cast(props.at("font_size")); CAsyncResourceGatherer::SPreloadRequest request; request.id = std::string{"label:"} + std::to_string((uintptr_t)this); resourceID = request.id; request.asset = formatString(labelPreFormat); request.type = CAsyncResourceGatherer::eTargetType::TARGET_TEXT; request.props["font_family"] = fontFamily; request.props["color"] = labelColor; request.props["font_size"] = fontSize; g_pRenderer->asyncResourceGatherer->requestAsyncAssetPreload(request); auto POS__ = std::any_cast(props.at("position")); pos = {POS__.x, POS__.y}; viewport = viewport_; label = request.asset; halign = std::any_cast(props.at("halign")); valign = std::any_cast(props.at("valign")); } bool CLabel::draw(const SRenderData& data) { if (!asset) { asset = g_pRenderer->asyncResourceGatherer->getAssetByID(resourceID); if (!asset) return true; // calc pos 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}; g_pRenderer->renderTexture(box, asset->texture, data.opacity); return false; }