YSTest: Selector.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

Selector.cpp
浏览该文件的文档.
1 /*
2 © 2011-2016 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_Selector
30 #include YFM_YSLib_Service_YBlit
31 #include YFM_YSLib_UI_YGUI
32 #include <ystdex/cast.hpp> // for ystdex::polymorphic_downcast;
33 #include <ystdex/scope_guard.hpp> // for ystdex::swap_guard;
34
35 namespace YSLib
36 {
37
38 using namespace Drawing;
39
40 namespace UI
41 {
42
43 namespace
44 {
45
47 void
48 RectDrawCheckBox(const PaintContext& pc, const Size& s, Hue base_hue,
49 CursorState cursor_state, bool is_ticked, bool is_enabled)
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& bounds(pc.ClipArea);
55 const Rect r(pc.Location, s);
56
57 FillRect(g, bounds, r, is_enabled ? (inside ? Color((is_pressed
58 ? HSL(30.F, 1.F, .925F) : HSL(30.F, 1.F, .976F)).Roll(base_hue))
59 : ColorSpace::White) : MakeGray(230));
60 DrawRect(g, bounds, r, is_enabled ? (inside ? Color((is_pressed
61 ? HSL(26.5F, 1.F, .435F) : HSL(30.F, 1.F, .6F)).Roll(base_hue))
62 : MakeGray(112)) : MakeGray(188));
63
64 if(is_ticked)
65 DrawTick(g, bounds, r, is_enabled ? ColorSpace::Black : MakeGray(112));
66 }
67
69 void
70 RectDrawRadioBox(const PaintContext& pc, const Size& s, Hue base_hue,
71 CursorState cursor_state, bool is_ticked, bool is_enabled)
72 {
73 const bool inside(cursor_state != CursorState::Outside);
74 const bool is_pressed(cursor_state == CursorState::Pressed);
75 const SDst rad(min(s.Width / 2, s.Height / 2));
76 const auto& g(pc.Target);
77 const auto pt(pc.Location + Size(rad, rad));
78 const Rect& bounds(pc.ClipArea);
79 const Rect r(pc.Location, s);
80
81 FillCircle(g, bounds, pt, rad, is_enabled ? (inside ? Color((is_pressed
82 ? HSL(30.F, 1.F, .925F) : HSL(30.F, 1.F, .976F)).Roll(base_hue))
83 : ColorSpace::White) : MakeGray(230));
84 DrawCircle(g, bounds, pt, rad, is_enabled ? (inside ? Color((is_pressed
85 ? HSL(26.5F, 1.F, .435F) : HSL(30.F, 1.F, .6F)).Roll(base_hue))
86 : MakeGray(112)) : MakeGray(188));
87 // XXX: Minimal size.
88 if(is_ticked && YB_LIKELY(r.Width > 4 && r.Height > 4))
89 FillCircle(g, bounds, pt, rad - 2,
90 is_enabled ? ColorSpace::Black : MakeGray(112));
91 }
92
94 void
95 BoxRefresh(AView& v, MLabel& lbl, PaintEventArgs& e, std::function<void()> f)
96 {
97 {
98 using namespace std::placeholders;
99 ystdex::state_guard<Size, void> gd([&](bool, Size& s) ynothrow{
100 // XXX: Possilble inconsistent state.
101 YSLib::FilterExceptions([&]{
102 SwapSizeOf(v, s);
103 }, yfsig);
104 }, 13, 13);
105
106 f();
107 }
108 {
109 ystdex::swap_guard<SPos, void>
110 gd(lbl.Margin.Left, SPos(lbl.Margin.Left + 13));
111
112 lbl(std::move(e));
113 }
114 }
115
116 } // unnamed namespace;
117
118
119 CheckBox::CheckBox(const Rect& r)
120 : Thumb(r, ystdex::raw_tag()), MCheckBox()
121 {
122 using namespace Styles;
123 static struct Init
124 {
125 Init()
126 {
127 AddHandlers<CheckBox>(FetchDefault(), {{CheckBoxBackground,
128 [](PaintEventArgs&& e){
129 auto& cb(
130 ystdex::polymorphic_downcast<CheckBox&>(e.GetSender()));
131
132 RectDrawCheckBox(e, GetSizeOf(cb), cb.GetHue(),
133 cb.GetCursorState(), cb.IsTicked(), IsEnabled(cb));
134 }
135 }});
136 }
137 } init;
138
139 FetchEvent<Click>(*this) += [this]{
140 Tick(GetState() == Checked ? Unchecked : Checked);
141 };
142 }
143
144 void
145 CheckBox::SetTicked(StateType st)
146 {
147 if(mSelector.State != st)
148 Ticked(TickedArgs(*this, st));
149 }
150
151 void
152 CheckBox::Refresh(PaintEventArgs&& e)
153 {
154 FetchGUIConfiguration().Styles.PaintAsStyle({typeid(CheckBox),
155 CheckBoxBackground}, std::move(e));
156 }
157
158 void
159 CheckBox::Tick(StateType st)
160 {
161 Ticked(TickedArgs(*this, mSelector.State = st));
162 }
163
164
165 CheckButton::CheckButton(const Rect& r)
166 : CheckBox(r)
167 {
168 Margin.Top = 0;
169 }
170
171 void
172 CheckButton::Refresh(PaintEventArgs&& e)
173 {
174 BoxRefresh(GetView(), *this, e, [&, this]{
175 CheckBox::Refresh(std::move(e));
176 });
177 }
178
179
180 void
181 MSharedSelection::ShareTo(MSharedSelection& rb) const
182 {
183 rb.p_selector = p_selector;
184 }
185
186
187 RadioBox::RadioBox(const Rect& r)
188 : Thumb(r, ystdex::raw_tag()), MSharedSelection()
189 {
190 using namespace Styles;
191 static struct Init
192 {
193 Init()
194 {
195 AddHandlers<RadioBox>(FetchDefault(), {{RadioBoxBackground,
196 [](PaintEventArgs&& e){
197 auto& rb(
198 ystdex::polymorphic_downcast<RadioBox&>(e.GetSender()));
199
200 RectDrawRadioBox(e, GetSizeOf(rb), rb.GetHue(),
201 rb.GetCursorState(), rb.IsSelected(), IsEnabled(rb));
202 }
203 }});
204 }
205 } init;
206
207 FetchEvent<Click>(*this) += [this]{
208 SetSelected();
209 };
210 }
211
212 void
213 RadioBox::SetSelected()
214 {
215 const auto p_wgt(GetState());
216
217 if(p_wgt.get() != this)
218 {
219 if(p_wgt)
220 Invalidate(*p_wgt);
221 Select();
222 }
223 }
224
225 void
226 RadioBox::Refresh(PaintEventArgs&& e)
227 {
228 FetchGUIConfiguration().Styles.PaintAsStyle({typeid(RadioBox),
229 RadioBoxBackground}, std::move(e));
230 }
231
232 void
233 RadioBox::Select()
234 {
235 UpdateState(make_observer(this));
236 Selected(SelectedArgs(*this, this));
237 }
238
239 void
240 RadioBox::ShareTo(RadioBox& rb) const
241 {
242 MSharedSelection::ShareTo(rb);
243 }
244
245
246 RadioButton::RadioButton(const Rect& r)
247 : RadioBox(r)
248 {
249 Margin.Top = 0;
250 }
251
252 void
253 RadioButton::Refresh(PaintEventArgs&& e)
254 {
255 BoxRefresh(GetView(), *this, e, [&, this]{
256 RadioBox::Refresh(std::move(e));
257 });
258 }
259
260 } // namespace UI;
261
262 } // namespace YSLib;
263
YSLib::Drawing::GGraphics::Height
const SizeType sGraphics Height
Definition: YGraphics.h:136
YSLib::UI::GMSelector::State
StateType State
状态。
Definition: Selector.h:59
YSLib::UI::RadioBox::Select
void Select()
设置选中状态并触发单选框选中事件。
Definition: Selector.cpp:233
YSLib::UI::FetchGUIConfiguration
GUIConfiguration & FetchGUIConfiguration()
Definition: YGUI.cpp:519
YSLib::UI::MCheckBox::TickedArgs
MSelector::SelectedArgs TickedArgs
选择框选中状态参数类型。
Definition: Selector.h:105
YSLib::UI::CheckBox::CheckBoxBackground
Definition: Selector.h:137
v
byte v
Definition: CharRenderer.cpp:75
YSLib::UI::CheckBox
复选框。
Definition: Selector.h:128
YSLib::UI::MSharedSelection::ShareTo
p_selector State void ShareTo(MSharedSelection &) const
向其它对象覆盖状态。
Definition: Selector.cpp:181
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::UI::RadioButton::Refresh
void Refresh(PaintEventArgs &&) override
刷新：按指定参数绘制界面并更新状态。
Definition: Selector.cpp:253
YSLib::UI::CheckButton::Refresh
void Refresh(PaintEventArgs &&) override
刷新：按指定参数绘制界面并更新状态。
Definition: Selector.cpp:172
cast.hpp
C++ 转换模板。
UI
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: pointer.hpp:326
YSLib::UI::RadioBox::ShareTo
void ShareTo(RadioBox &) const
向其它对象覆盖状态。
Definition: Selector.cpp:240
yfsig
#define yfsig
展开为表示函数签名的预定义变量的宏。
Definition: ydef.h:728
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::RadioBox::Refresh
void Refresh(PaintEventArgs &&) override
刷新：按指定参数绘制界面并更新状态。
Definition: Selector.cpp:226
YSLib::UI::Styles::FetchDefault
YF_API HandlerTable & FetchDefault()
取默认样式处理器表。
Definition: YStyle.cpp:533
CHRLib::f
f(d, s)
YSLib::UI::CheckBox::Refresh
void Refresh(PaintEventArgs &&) override
刷新：按指定参数绘制界面并更新状态。
Definition: Selector.cpp:152
YSLib::UI::MCheckBox::mSelector
MSelector mSelector
Definition: Selector.h:109
YSLib::Drawing::Color
The boundary is out of the const Rect SDst SPos SPos SPos Color
Definition: YDraw.h:127
YSLib::UI::MSharedSelection
单选框模块。
Definition: Selector.h:225
YSLib::Drawing::DrawTick
YF_API void DrawTick(const Graphics &, const Rect &, const Rect &, Color, Color)
Definition: YStyle.cpp:257
YSLib::UI::MLabel::Margin
Drawing::Padding Margin
文本和容器的间距。
Definition: Label.h:79
YSLib::UI::RadioBox
单选框。
Definition: Selector.h:282
YSLib::Drawing::Padding::Top
SPos Top
Definition: YGDI.h:52
YSLib::Drawing::FillRect
c YF_API void FillRect(const Graphics &g, const Rect &r, Color c)
填充标准矩形。
Definition: YDraw.cpp:150
YSLib::UI::Thumb
基本按钮。
Definition: Button.h:46
YSLib::UI::Invalidate
YF_API void Invalidate(IWidget &, const Rect &)
无效化：使相对部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: YWidget.cpp:108
YSLib::UI::SwapSizeOf
YF_API void SwapSizeOf(AView &, Size &)
交换指定视图的大小。
Definition: YWidgetView.cpp:63
YSLib::UI::CursorState
CursorState
指针设备光标相对控件的状态。
Definition: YControl.h:344
platform::SDst
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:44
YSLib::Drawing::g
The boundary is out of the const Graphics & g
Definition: YDraw.h:161
YSLib::FilterExceptions
bool FilterExceptions(_func f, const char *desc={}, RecordLevel lv=Alert, ExceptionTracer trace=ExtractAndTrace) ynothrow
调用函数并过滤宿主异常。
Definition: YException.h:203
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex
特性测试宏。
Definition: ydef.h:822
YSLib::UI::CheckBox::Tick
void Tick(StateType)
设置选中状态并触发复选框选中事件。
Definition: Selector.cpp:159
Drawing
ystdex::min
const _type & min(const _type &a, const _type &b, _fComp comp=less< _type >())
取较小的元素。
Definition: algorithm.hpp:527
platform::SPos
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:43
YSLib::UI::PaintEventArgs
部件绘制参数。
Definition: YWidgetEvent.h:323
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
YSLib::UI::e
void e
Definition: YWidget.h:352
ystdex::state_guard
使用临时状态暂存对象的作用域守护。
Definition: scope_guard.hpp:273
YSLib::UI::CheckButton::CheckButton
CheckButton(const Rect &={})
构造：使用指定边界。
Definition: Selector.cpp:165
YModules.h
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
YSLib::UI::MCheckBox
复选框模块。
Definition: Selector.h:88
YSLib::UI::RadioBox::Selected
Selected
Definition: Selector.h:304
YSLib::Drawing::DrawRect
void DrawRect(const Graphics &g, const Rect &bounds, const Rect &r, Color c)
Definition: YDraw.cpp:136
YSLib::Drawing::Rect
屏幕标准矩形：表示屏幕矩形区域。
Definition: YGDIBase.h:540
YSLib::UI::MCheckBox::StateType
enum SelectedState:yimpl(size_t){Unchecked=0, Checked=1, Partial=2} StateType
选择框选中状态类型。
Definition: Selector.h:99
YSLib::UI::CheckBox::GetState
GetState()
YSLib::Drawing::DrawCircle
bounds c YF_API void DrawCircle(const Graphics &g, const Rect &bounds, const Point &, SDst, Color)
描画圆形。
Definition: YDraw.cpp:180
YSLib::Consoles::Black
Definition: YConsole.h:53
YSLib::UI::CheckBox::CheckBox
CheckBox(const Rect &={})
构造：使用指定边界。
Definition: Selector.cpp:119
scope_guard.hpp
作用域守护。
YSLib::UI::Widget::GetView
GetView()) DefGetter(const ImplI(IWidget)
YSLib::Drawing::bounds
The boundary is out of the const Rect & bounds
Definition: YDraw.h:127
YSLib::UI::MSharedSelection::SelectedArgs
MSelector::SelectedArgs SelectedArgs
单选框选中状态参数类型。
Definition: Selector.h:234
YSLib::UI::CheckBox::SetTicked
void SetTicked(StateType)
设置选中状态并检查复选框选中事件。
Definition: Selector.cpp:145
YB_LIKELY
#define YB_LIKELY(_expr)
Definition: ydef.h:463
YSLib::UI::RadioBox::RadioBox
RadioBox(const Rect &={})
构造：使用指定边界。
Definition: Selector.cpp:187
YSLib::UI::GetSizeOf
pt pt Y const IWidget &wgt const IWidget &wgt GetSizeOf
无效化：使相对部件的子部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: YWidget.h:161
YSLib::UI::MSharedSelection::p_selector
shared_ptr< MSelector > p_selector
共享状态。
Definition: Selector.h:243
YSLib::UI::RadioButton::RadioButton
RadioButton(const Rect &={})
构造：使用指定边界。
Definition: Selector.cpp:246
YSLib::UI::RadioBox::RadioBoxBackground
Definition: Selector.h:288
YSLib::UI::CursorState::Pressed
YSLib::Drawing::FillCircle
YF_API void FillCircle(const Graphics &g, const Rect &bounds, const Point &, SDst, Color)
填充圆形。
Definition: YDraw.cpp:202
YSLib::UI::CheckBox::Ticked
Ticked
Definition: Selector.h:161
YSLib::Consoles::White
Definition: YConsole.h:68
YSLib::UI::RadioBox::SetSelected
void SetSelected()
设置选中状态并检查单选框选中事件。
Definition: Selector.cpp:213
YSLib::UI::RadioBox::GetState
GetState().get()