#include "IconButton.h" #include "../Assets.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) { 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->SetRoundness(8.0); previousColor = drawColor; UI::Size textSize = Text::Measure(label); 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 = std::make_unique(textX, textY, label); float iconX = globalBounds.X + padding.Left; float iconY = globalBounds.Y + padding.Top + (contentH - iconSize.height - 4) * 0.5f; // for some reason this is centered static TTF_Font *iconFont; if (!iconFont) iconFont = TTF_OpenFont(Assets::Path("fonts/materialSymbolsRounded.ttf").data(), 24); iconText = std::make_unique(iconX, iconY, icon, iconFont); } void IconButton::Measure(Size /*availableSpace*/) { UI::Size textSize = Text::Measure(label); desiredSize.width = std::clamp(textSize.width + 8 + 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(); } }