YSTest: Button.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Button.cpp
浏览该文件的文档.
1 /*
2 © 2010-2015, 2018 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YSLib/UI/YModules.h"
29 #include YFM_YSLib_UI_Button
30 #include YFM_YSLib_UI_YGUI
31 #include <ystdex/cast.hpp> // for ystdex::polymorphic_downcast;
32 #include <ystdex/scope_guard.hpp> // for ystdex::swap_guard;
33
34 namespace YSLib
35 {
36
38 using namespace Drawing;
39
40 namespace UI
41 {
42
43 namespace
44 {
45
47 void
48 RectDrawButton(const PaintContext& pc, const Size& s, Hue base_hue,
49 CursorState cursor_state, bool is_enabled, bool is_focused)
50 {
51 const bool inside(cursor_state != CursorState::Outside);
52 const bool is_pressed(cursor_state == CursorState::Pressed);
53 const auto& g(pc.Target);
54 const Rect r(pc.Location, s);
55
56 FillRect(g, pc.ClipArea, r, is_enabled ? (inside ? Color((is_pressed
57 ? HSL(29.2F, .86F, .916F): HSL(30.F, .786F, .945F)).Roll(base_hue))
58 : MakeGray(234)) : MakeGray(239));
59 DrawRect(g, pc.ClipArea, r, is_enabled ? (inside ? Color((is_pressed
60 ? HSL(30.2F, .733F, .618F) : HSL(30.F, .72F, .706F)).Roll(base_hue))
61 : (is_focused ? Color(HSL(30.F, 1.F, .6F).Roll(base_hue))
62 : FetchGUIConfiguration().Colors[Styles::Workspace])) : MakeGray(217));
63 }
64
65 } // unnamed namespace;
66
67
68 Thumb::Thumb(const Rect& r, Hue h)
69 : Thumb(r, ystdex::raw_tag())
70 {
71 using namespace Styles;
72 static const struct Init
73 {
74 Init()
75 {
76 AddHandlers<Thumb>(FetchDefault(), {{ThumbBackground,
77 [](PaintEventArgs&& e){
78 const auto& tmb(
79 ystdex::polymorphic_downcast<Thumb&>(e.GetSender()));
80
81 RectDrawButton(e, GetSizeOf(tmb), tmb.GetHue(),
82 tmb.GetCursorState(), IsEnabled(tmb), IsFocused(tmb));
83 }
84 }});
85 }
86 } init;
87
88 yunseq(hue = h, Background = Painter(typeid(Thumb), ThumbBackground));
89 }
90 Thumb::Thumb(const Rect& r, ystdex::raw_tag)
91 : Control(r),
92 csCurrent(CursorState::Outside)
93 {
94 const auto invalidator([this]{
95 Invalidate(*this);
96 });
97
98 yunseq(
99 FetchEvent<CursorOver>(*this) += [this](CursorEventArgs&& e)
100 {
101 if(csCurrent == CursorState::Outside)
102 {
103 csCurrent = CursorState::Over;
104 Invalidate(e.GetSender());
105 }
106 },
107 FetchEvent<Enter>(*this) += [this](CursorEventArgs&& e){
108 if(!IsPressed() && e.Keys.any())
109 {
110 csCurrent = CursorState::Pressed;
111 Invalidate(*this);
112 }
113 },
114 FetchEvent<Leave>(*this) += [this](CursorEventArgs&& e){
115 if(csCurrent == CursorState::Over || (IsPressed() && e.Keys.any()))
116 {
117 csCurrent = CursorState::Outside;
118 Invalidate(*this);
119 }
120 },
121 FetchEvent<GotFocus>(*this) += invalidator,
122 FetchEvent<LostFocus>(*this) += invalidator
123 );
124 }
125 ImplDeDtor(Thumb)
126
127 void
128 DecorateAsCloseButton(Thumb& tmb)
129 {
130 yunseq(
131 FetchEvent<Click>(tmb) += [&](CursorEventArgs&&)
132 {
133 if(const auto p_con = FetchContainerPtr(tmb))
134 Close(*p_con);
135 },
136 FetchEvent<Paint>(tmb) += [&](PaintEventArgs&& e){
137 DrawCross(e.Target, e.ClipArea, {e.Location, GetSizeOf(tmb)},
138 IsEnabled(tmb) ? tmb.ForeColor
139 : FetchGUIConfiguration().Colors[Styles::Workspace]);
140 }
141 );
142 }
143
144
145 Button::Button(const Rect& r, const Drawing::Font& fnt, Color c,
146 TextAlignment a)
147 : Button(r, 180, fnt, c, a)
148 {}
149 Button::Button(const Rect& r, Hue h, const Drawing::Font& fnt, Color c,
150 TextAlignment a)
151 : Thumb(r, h), MLabel(fnt, c, a)
152 {}
153
154 void
155 Button::Refresh(PaintEventArgs&& e)
156 {
157 ystdex::swap_guard<Color> gd(!IsEnabled(*this), ForeColor, []{
158 return FetchGUIConfiguration().Colors[Styles::Workspace];
159 }());
160
161 // NOTE: Partial invalidation made no efficiency improved here.
162 (*this)(std::move(e));
163 }
164
165 } // namespace UI;
166
167 } // namespace YSLib;
168
YSLib::UI::Thumb::csCurrent
CursorState csCurrent
指针设备光标状态。
Definition: Button.h:70
cast.hpp
C++ 转换模板。
YSLib::Drawing::Color
The boundary is out of the const Rect SDst SPos SPos SPos Color
Definition: YDraw.h:128
YSLib::UI::IsFocused
YF_API bool IsFocused(const IWidget &)
判断部件是否取得焦点。
Definition: YFocus.cpp:38
YSLib::Drawing::FillRect
c YF_API void FillRect(const Graphics &g, const Rect &r, Color c)
填充标准矩形。
Definition: YDraw.cpp:150
CHRLib::c
c
Definition: CharacterProcessing.h:84
YSLib::UI::CursorState
CursorState
指针设备光标相对控件的状态。
Definition: YControl.h:340
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
YSLib::UI::Button::Button
Button(const Rect &={}, const Drawing::Font &={}, Color=Drawing::ColorSpace::Black, TextAlignment=TextAlignment::Center)
构造：使用指定边界、字体、文本颜色和文本对齐样式。
Definition: Button.cpp:145
YSLib::UI::Button
按钮。
Definition: Button.h:127
YSLib::UI::Control
控件。
Definition: YControl.h:271
YSLib::UI::CursorEventArgs
指针设备输入事件参数类。
Definition: YWidgetEvent.h:216
YSLib::UI::GetSizeOf
YB_ATTR_nodiscard YB_PURE pt pt Y YB_ATTR_nodiscard YB_PURE const IWidget &wgt const IWidget &wgt GetSizeOf
无效化：使相对部件的子部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: YWidget.h:165
YSLib::UI::ImplDeDtor
ImplDeDtor(Thumb) void DecorateAsCloseButton(Thumb &tmb)
Definition: Button.cpp:125
YSLib::Drawing::DrawCross
YF_API void DrawCross(const Graphics &, const Rect &, const Rect &, Color)
在指定图形接口上下文中使用指定颜色描画交叉直线段（“×”）。
Definition: YStyle.cpp:244
YSLib::UI::Thumb
基本按钮。
Definition: Button.h:47
YSLib::Drawing::g
The boundary is out of the const Graphics & g
Definition: YDraw.h:161
YSLib::UI::Invalidate
YF_API void Invalidate(IWidget &, const Rect &)
无效化：使相对部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: YWidget.cpp:108
ystdex::state_guard
使用临时状态暂存对象的作用域守卫。
Definition: scope_guard.hpp:273
YSLib::UI::Styles::Workspace
@ Workspace
应用程序工作区背景。
Definition: YStyle.h:314
scope_guard.hpp
作用域守卫。
YSLib::UI::FetchGUIConfiguration
GUIConfiguration & FetchGUIConfiguration()
Definition: YGUI.cpp:517
ystdex::raw_tag
直接构造类型（直接构造重载用）。
Definition: ydef.h:1432
yunseq
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:1535
YSLib::UI::Close
void Close(IWidget &wgt)
Definition: YWidget.cpp:92
YSLib::UI::Thumb::Thumb
Thumb(const Rect &={}, Drawing::Hue=180)
构造：使用指定边界和色调。
YSLib::UI::e
void e
Definition: YWidget.h:356
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:788
YSLib::UI::Thumb::ForeColor
Color ForeColor
默认前景色。
Definition: Button.h:63
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:143
YSLib::UI::CursorState::Pressed
@ Pressed
Color
Pixel Color
Definition: CharRenderer.cpp:59
YSLib::UI::MLabel
标签模块。
Definition: Label.h:62
YModules.h
YSLib 用户界面模块配置文件。
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
YSLib::UI::PaintEventArgs
部件绘制参数。
Definition: YWidgetEvent.h:330
YSLib::UI::CursorState::Over
@ Over
YSLib::UI::Styles::FetchDefault
YB_ATTR_nodiscard YB_PURE YF_API HandlerTable & FetchDefault()
取默认样式处理器表。
Definition: YStyle.cpp:538
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:589
YSLib::UI::CursorState::Outside
@ Outside
YSLib::UI::TextAlignment
TextAlignment
Definition: Label.h:46
YSLib::Drawing::DrawRect
void DrawRect(const Graphics &g, const Rect &bounds, const Rect &r, Color c)
Definition: YDraw.cpp:136
YSLib::a
YB_PURE const _type * a
Definition: YCoreUtilities.h:168
YSLib::UI::Button::Refresh
void Refresh(PaintEventArgs &&) override
刷新：按指定参数绘制界面并更新状态。
Definition: Button.cpp:155