/
SilovAndrey
/
easel_gui
Обзор
Документация
Войти
/
SilovAndrey
/
easel_gui
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/button.cpp
85 строк
3 KB
Andrey HomePC
первая рабочая версия
21 июл 2026, 11:50
21 июл 2026, 11:50
38b8d4a
Код
Авторство
О чём код?
module; #include <functional> #include <memory> #include <thorvg.h> module Easel.GUI; import Easel.GUI.Log; namespace easel::gui { WidgetPtr button(const char* text, std::function<void()> onClick) { struct ButtonWidget : Widget { std::string m_text; tvg::Shape* m_bg; tvg::Text* m_label; std::function<void()> m_onClick; rect m_bounds; float m_radius = 0, m_padding = 0; bool m_hovered = false; ButtonWidget(const char* t, std::function<void()> cb) : m_text(t), m_onClick(std::move(cb)) {} void init(tvg::Canvas* c, Theme const& th, rect const& b) override { m_bounds = b; m_radius = th.radius; m_padding = th.padding; log_info("init Button %f %f %f %f", b.left, b.top, b.right, b.bottom); m_bg = tvg::Shape::gen(); m_bg->fill(th.button_bg.red, th.button_bg.green, th.button_bg.blue, th.button_bg.alpha); if (m_radius > 0) m_bg->appendRect(b.left, b.top, b.width(), b.height(), m_radius, m_radius); else m_bg->appendRect(b.left, b.top, b.width(), b.height()); c->add(m_bg); m_label = tvg::Text::gen(); m_label->text(m_text.c_str()); m_label->font(th.font.c_str()); m_label->size(th.size_font); m_label->fill(th.text_color.red, th.text_color.green, th.text_color.blue); auto in = b.inset(m_padding); m_label->translate(in.left, in.top); c->add(m_label); } bool resize(rect const& b, Theme const&) override { if (m_bounds == b) return false; m_bounds = b; m_bg->reset(); if (m_radius > 0) m_bg->appendRect(b.left, b.top, b.width(), b.height(), m_radius, m_radius); else m_bg->appendRect(b.left, b.top, b.width(), b.height()); auto in = b.inset(m_padding); m_label->translate(in.left, in.top); return true; } bool on_event(Event const& e, Theme const& th) override { //if (!m_bounds.includes(e.pos)) return false; if (e.type == Event::Hover) { m_hovered = !m_hovered; auto& c = m_hovered ? th.button_hover : th.button_bg; m_bg->fill(c.red, c.green, c.blue, c.alpha); return true; } if (e.type == Event::Click && m_onClick) { m_onClick(); return false; } return false; } float preferred_width(Theme const& th) const override { return m_text.size() * th.size_font * 1.6f + m_padding * 2; } float preferred_height(Theme const& th) const override { return th.size_font * 3.0f + m_padding * 2; } }; return std::make_unique<ButtonWidget>(text, std::move(onClick)); } } // namespace easel::gui