#include "Label.h" #include namespace UI { Label::Label(const std::string& label) : label(label) { focusable = true; padding = Thickness(0.0f, 0.0f); minSize = Size(0.0f, 0.0f); horizontalAlignment = HorizontalAlignment::Left; } void Label::SetLabel(const std::string& newLabel) { label = newLabel; MarkDirty(DirtyFlags::Layout | DirtyFlags::Render); } void Label::RefreshPrimitives() { UI::Size textSize = Text::Measure(label); float contentH = std::max(0.0f, globalBounds.Height - padding.VerticalTotal()); float textX = globalBounds.X + padding.Left; float textY = globalBounds.Y + padding.Top + std::max(0.0f, (contentH - textSize.height) * 0.5f); labelText = std::make_unique(textX, textY, label); labelText->SetColor(textColor.r, textColor.g, textColor.b, textColor.a); } void Label::Measure(Size /*availableSpace*/) { UI::Size textSize = Text::Measure(label); desiredSize.width = std::clamp(textSize.width + padding.HorizontalTotal() + margin.HorizontalTotal(), minSize.width, maxSize.width); desiredSize.height = std::clamp(textSize.height + padding.VerticalTotal() + margin.VerticalTotal(), minSize.height, maxSize.height); } void Label::Arrange(Rect finalBounds) { Rect oldGlobal = globalBounds; bool doWeRefreshMaybe = IsDirty(DirtyFlags::Render | DirtyFlags::Layout); Widget::Arrange(finalBounds); if (doWeRefreshMaybe || globalBounds != oldGlobal) { RefreshPrimitives(); } } void Label::Draw() { if (!visible) return; if (!labelText || IsDirty(DirtyFlags::Layout)) { RefreshPrimitives(); } if (labelText) { labelText->Render(); } Widget::Draw(); } }