diff --git a/cpp/functions/core/button.cpp b/cpp/functions/core/button.cpp new file mode 100644 index 00000000..9fb9fc13 --- /dev/null +++ b/cpp/functions/core/button.cpp @@ -0,0 +1,74 @@ +#include "core/button.h" +#include "core/tokens.h" +#include + +namespace fn_ui { + +namespace { + +struct VariantColors { + ImVec4 bg; + ImVec4 bg_hover; + ImVec4 bg_active; + ImVec4 text; + ImVec4 border; + float border_size; // 0 = sin borde +}; + +VariantColors colors_for(ButtonVariant v) { + using namespace fn_tokens::colors; + switch (v) { + case ButtonVariant::Primary: + return {primary, primary_hover, primary, text, primary, 0.0f}; + case ButtonVariant::Secondary: + return {surface, surface_hover, surface, text, border, 1.0f}; + case ButtonVariant::Subtle: + return {ImVec4(0,0,0,0), surface_hover, surface, primary, ImVec4(0,0,0,0),0.0f}; + case ButtonVariant::Danger: + return {error, error, error, text, error, 0.0f}; + } + return {surface, surface_hover, surface, text, border, 1.0f}; +} + +ImVec2 padding_for(ButtonSize s) { + using namespace fn_tokens::spacing; + switch (s) { + case ButtonSize::Sm: return ImVec2(md, xs + 2.0f); + case ButtonSize::Md: return ImVec2(lg, sm); + case ButtonSize::Lg: return ImVec2(xl, md); + } + return ImVec2(lg, sm); +} + +float font_scale_for(ButtonSize s) { + return (s == ButtonSize::Lg) ? 1.1f : 1.0f; +} + +} // namespace + +bool button(const char* label, ButtonVariant variant, ButtonSize size) { + using namespace fn_tokens; + + const VariantColors c = colors_for(variant); + const ImVec2 pad = padding_for(size); + const float fs = font_scale_for(size); + + ImGui::PushStyleColor(ImGuiCol_Button, c.bg); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, c.bg_hover); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, c.bg_active); + ImGui::PushStyleColor(ImGuiCol_Text, c.text); + ImGui::PushStyleColor(ImGuiCol_Border, c.border); + ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, radius::sm); + ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, c.border_size); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, pad); + + if (fs != 1.0f) ImGui::SetWindowFontScale(fs); + const bool clicked = ImGui::Button(label); + if (fs != 1.0f) ImGui::SetWindowFontScale(1.0f); + + ImGui::PopStyleVar(3); + ImGui::PopStyleColor(5); + return clicked; +} + +} // namespace fn_ui diff --git a/cpp/functions/core/button.h b/cpp/functions/core/button.h new file mode 100644 index 00000000..7f15a4cb --- /dev/null +++ b/cpp/functions/core/button.h @@ -0,0 +1,30 @@ +#pragma once + +// Button con variantes (primary/secondary/subtle/danger) y tamanos (sm/md/lg) +// usando fn_tokens. Replica el