Something went wrong. Try again.
art software for the rest of us
drawing paint image sdl art
Something went wrong. Try again.
C++
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960#include "Label.h"#include <algorithm>
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<Text>(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();}
}