#include "IconButton.h" #include "Widgets/Widget.h" namespace UI { IconButton::IconButton(const std::string& icon, const std::string& label, std::function onClick) : Button(label, onClick), icon(icon) { normalColor = Color(0.0f, 0.0f, 0.0f, 0.0f); borderRoundness = 8.0f; padding = Thickness(8,4,8,4); verticalAlignment = VerticalAlignment::Stretch; } void IconButton::RefreshPrimitives() { bgRect = std::make_unique(globalBounds.X, globalBounds.Y, globalBounds.Width,globalBounds.Height); Color drawColor = normalColor; if (pressed) { drawColor = activeColor; } else if (hovered) { drawColor = hoverColor; } bgRect->SetColor(drawColor.r, drawColor.g, drawColor.b, drawColor.a); bgRect->SetBorderColor(borderColor.r, borderColor.g, borderColor.b, borderColor.a); bgRect->SetBorderWidth(borderThickness); bgRect->SetRoundness(borderRoundness); previousColor = drawColor; UI::Size textSize = label.size() ? Text::Measure(label) : Size(0,0); UI::Size iconSize(24,24); //float contentW = std::max(0.0f, globalBounds.Width - padding.HorizontalTotal()); float contentH = std::max(0.0f, globalBounds.Height - padding.VerticalTotal()); float textX = globalBounds.X + padding.Left + iconSize.width + 8; float textY = globalBounds.Y + padding.Top + std::max(0.0f, (contentH - textSize.height) * 0.5f); labelText = label.size() ? std::make_unique(textX, textY, label) : nullptr; float iconX = globalBounds.X + padding.Left; float iconY = globalBounds.Y + padding.Top + (contentH - iconSize.height - 4) * 0.5f; // for some reason this is centered iconText = std::make_unique(iconX, iconY, icon, Text::GetIconFont()); } void IconButton::Measure(Size /*availableSpace*/) { UI::Size textSize = Text::Measure(label); desiredSize.width = std::clamp(textSize.width + (label.size() ? 8 : 0) + 24+ padding.HorizontalTotal() + margin.HorizontalTotal(), minSize.width, maxSize.width); desiredSize.height = std::clamp(24 + padding.VerticalTotal() + margin.VerticalTotal(), minSize.height, maxSize.height); } void IconButton::Draw() { if (!visible) return; if (!bgRect || IsDirty(DirtyFlags::Layout)) { RefreshPrimitives(); } // Determine primitive color based on input interaction state // TODO: needs dedup Color drawColor = normalColor; if (pressed) { drawColor = activeColor; } else if (hovered) { drawColor = hoverColor; } if (drawColor != previousColor) { bgRect->SetColor(drawColor.r, drawColor.g, drawColor.b, drawColor.a); previousColor = drawColor; } if (bgRect) { bgRect->Render(); } if (labelText) { labelText->Render(); } if (iconText) { iconText->Render(); } Widget::Draw(); } }