25Element DefaultTransform(EntryState params) {
36class ButtonBase :
public ComponentBase,
public ButtonOption {
38 explicit ButtonBase(ButtonOption option) : ButtonOption(std::move(option)) {}
42 const bool active = Active();
43 const bool focused = Focused();
44 const bool focused_or_hover = focused || mouse_hover_;
46 float target = focused_or_hover ? 1.f : 0.f;
47 if (target != animator_background_.to()) {
48 SetAnimationTarget(target);
51 const EntryState state{
52 std::string(*label),
false, active, focused_or_hover, Index(),
55 auto element = (transform ? transform : DefaultTransform)
57 element |= AnimatedColorStyle();
65 if (animated_colors.background.enabled) {
67 bgcolor(Color::Interpolate(animation_foreground_,
68 animated_colors.background.inactive,
69 animated_colors.background.active));
71 if (animated_colors.foreground.enabled) {
73 style |
color(Color::Interpolate(animation_foreground_,
74 animated_colors.foreground.inactive,
75 animated_colors.foreground.active));
80 void SetAnimationTarget(
float target) {
81 if (animated_colors.foreground.enabled) {
82 animator_foreground_ = animation::Animator(
83 &animation_foreground_, target, animated_colors.foreground.duration,
84 animated_colors.foreground.function);
86 if (animated_colors.background.enabled) {
87 animator_background_ = animation::Animator(
88 &animation_background_, target, animated_colors.background.duration,
89 animated_colors.background.function);
93 void OnAnimation(animation::Params& p)
override {
94 animator_background_.OnAnimation(p);
95 animator_foreground_.OnAnimation(p);
99 animation_background_ = 0.5F;
100 animation_foreground_ = 0.5F;
101 SetAnimationTarget(1.F);
103 App::PostEventOrExecute(on_click);
106 bool OnEvent(Event event)
override {
107 if (event.is_mouse()) {
108 return OnMouseEvent(event);
111 if (event == Event::Return) {
118 bool OnMouseEvent(Event event) {
120 box_.Contain(event.mouse().x, event.mouse().y) && CaptureMouse(event);
126 if (event.mouse().button == Mouse::Left &&
127 event.mouse().motion == Mouse::Pressed) {
136 bool Focusable() const final {
return true; }
139 bool mouse_hover_ =
false;
141 float animation_background_ = 0;
142 float animation_foreground_ = 0;
143 animation::Animator animator_background_ =
144 animation::Animator(&animation_background_);
145 animation::Animator animator_foreground_ =
146 animation::Animator(&animation_foreground_);
203 std::function<
void()> on_click,
204 ButtonOption option) {
205 option.label = std::move(label);
206 option.on_click = std::move(on_click);
Component Button(ConstStringRef label, std::function< void()> on_click, ButtonOption options=ButtonOption::Simple())
繪製一個按鈕。點擊時執行一個函式。
Element text(std::string_view text)
顯示一段 UTF8 編碼的 Unicode 文字。
Element nothing(Element element)
一個什麼都不做的裝飾器。
Element bold(Element child)
使用粗體字型,用於需要更多強調的元素。
Element inverted(Element child)
添加一個濾鏡,它將反轉前景和背景 顏色。
Element focus(Element child)
將 child 設置為其同級元素中被聚焦的元素。
Element bgcolor(Color color, Element child)
設定元素的背景顏色。
Element color(Color color, Element child)
設定元素的前景顏色。
Element border(Element child)
在元素周圍繪製邊框。
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
std::function< Element(Element)> Decorator
Decorator reflect(Box &box)
std::shared_ptr< ComponentBase > Component