YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Selector.cpp
浏览该文件的文档.
1 /*
2  © 2011-2014 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>
33 
34 namespace YSLib
35 {
36 
37 using namespace Drawing;
38 
39 namespace UI
40 {
41 
42 namespace
43 {
44 
46 void
47 RectDrawCheckBox(const PaintContext& pc, const Size& s, Hue base_hue,
48  CursorState cursor_state, bool is_ticked, bool is_enabled)
49 {
50  const bool inside(cursor_state != CursorState::Outside);
51  const bool is_pressed(cursor_state == CursorState::Pressed);
52  const SDst rad(min(s.Width, s.Height));
53  const auto& g(pc.Target);
54  const auto pt(pc.Location + Size(rad, rad));
55  const auto& bounds(pc.ClipArea);
56  const Rect r(pc.Location, s);
57 
58  FillRect(g, bounds, r, is_enabled ? (inside ? RollColor(is_pressed
59  ? hsl_t{30.F, 1.F, .925F} : hsl_t{30.F, 1.F, .976F}, base_hue)
60  : ColorSpace::White) : MakeGray(230));
61  DrawRect(g, bounds, r, is_enabled ? (inside ? RollColor(is_pressed
62  ? hsl_t{26.5F, 1.F, .435F} : hsl_t{30.F, 1.F, .6F}, base_hue)
63  : MakeGray(112)) : MakeGray(188));
64 
65  if(is_ticked)
66  DrawTick(g, bounds, r, is_enabled ? ColorSpace::Black : MakeGray(112));
67 }
68 
70 void
71 RectDrawRadioBox(const PaintContext& pc, const Size& s, Hue base_hue,
72  CursorState cursor_state, bool is_ticked, bool is_enabled)
73 {
74  const bool inside(cursor_state != CursorState::Outside);
75  const bool is_pressed(cursor_state == CursorState::Pressed);
76  const SDst rad(min(s.Width / 2, s.Height / 2));
77  const auto& g(pc.Target);
78  const auto pt(pc.Location + Size(rad, rad));
79  const auto& bounds(pc.ClipArea);
80  const Rect r(pc.Location, s);
81 
82  FillCircle(g, bounds, pt, rad, is_enabled ? (inside ? RollColor(is_pressed
83  ? hsl_t{30.F, 1.F, .925F} : hsl_t{30.F, 1.F, .976F}, base_hue)
84  : ColorSpace::White) : MakeGray(230));
85  DrawCircle(g, bounds, pt, rad, is_enabled ? (inside ? RollColor(is_pressed
86  ? hsl_t{26.5F, 1.F, .435F} : hsl_t{30.F, 1.F, .6F}, base_hue)
87  : MakeGray(112)) : MakeGray(188));
88  // XXX: Minimal size.
89  if(is_ticked && YB_LIKELY(r.Width > 4 && r.Height > 4))
90  FillCircle(g, bounds, pt, rad - 2,
91  is_enabled ? ColorSpace::Black : MakeGray(112));
92 }
93 
94 } // unnamed namespace;
95 
96 
98  : Thumb(r, ystdex::raw_tag()), MCheckBox()
99 {
100  using namespace Styles;
101  static struct Init
102  {
103  Init()
104  {
105  AddHandlers<CheckBox>(FetchDefault(), {{CheckBoxBackground,
106  [](PaintEventArgs&& e){
107  auto& cb(
108  ystdex::polymorphic_downcast<CheckBox&>(e.GetSender()));
109 
110  RectDrawCheckBox(e, GetSizeOf(cb), cb.GetHue(),
111  cb.GetCursorState(), cb.IsTicked(), IsEnabled(cb));
112  }
113  }});
114  }
115  } init;
116 
117  FetchEvent<Click>(*this) += [this]{
118  Tick(GetState() == Checked ? Unchecked : Checked);
119  };
120 }
121 
122 void
124 {
125  if(mSelector.State != st)
126  Ticked(TickedArgs(*this, st));
127 }
128 
129 void
131 {
132  FetchGUIState().Styles.PaintAsStyle({typeid(CheckBox), CheckBoxBackground},
133  std::move(e));
134 }
135 
136 void
138 {
139  Ticked(TickedArgs(*this, mSelector.State = st));
140 }
141 
142 
144  : CheckBox(r)
145 {
146  Margin.Top = 0;
147 }
148 
149 void
151 {
152  auto& view(GetView());
153  const Size s(GetSizeOf(*this));
154 
155  view.GetSizeRef() = {13, 13};
156  CheckBox::Refresh(std::move(e));
157  view.GetSizeRef() = s;
158  Margin.Left += 13;
159  DrawText(s, ForeColor, e);
160  Margin.Left -= 13;
161 }
162 
163 
164 void
165 MSharedSelection::ShareTo(MSharedSelection& rb) const
166 {
167  rb.p_selector = p_selector;
168 }
169 
170 
172  : Thumb(r, ystdex::raw_tag()), MSharedSelection()
173 {
174  using namespace Styles;
175  static struct Init
176  {
177  Init()
178  {
179  AddHandlers<RadioBox>(FetchDefault(), {{RadioBoxBackground,
180  [](PaintEventArgs&& e){
181  auto& rb(
182  ystdex::polymorphic_downcast<RadioBox&>(e.GetSender()));
183 
184  RectDrawRadioBox(e, GetSizeOf(rb), rb.GetHue(),
185  rb.GetCursorState(), rb.IsSelected(), IsEnabled(rb));
186  }
187  }});
188  }
189  } init;
190 
191  FetchEvent<Click>(*this) += [this]{
192  SetSelected();
193  };
194 }
195 
196 void
198 {
199  const auto p_wgt(GetState());
200 
201  if(p_wgt != this)
202  {
203  if(p_wgt)
204  Invalidate(*p_wgt);
205  Select();
206  }
207 }
208 
209 void
211 {
212  FetchGUIState().Styles.PaintAsStyle({typeid(RadioBox), RadioBoxBackground},
213  std::move(e));
214 }
215 
216 void
218 {
219  UpdateState(this);
220  Selected(SelectedArgs(*this, this));
221 }
222 
223 void
225 {
226  MSharedSelection::ShareTo(rb);
227 }
228 
229 
231  : RadioBox(r)
232 {
233  Margin.Top = 0;
234 }
235 
236 void
238 {
239  auto& view(GetView());
240  const Size s(GetSizeOf(*this));
241 
242  view.GetSizeRef() = {13, 13};
243  RadioBox::Refresh(std::move(e));
244  view.GetSizeRef() = s;
245  Margin.Left += 13;
246  DrawText(s, ForeColor, e);
247  Margin.Left -= 13;
248 }
249 
250 } // namespace UI;
251 
252 } // namespace YSLib;
253 
void ShareTo(RadioBox &) const
向其它对象覆盖状态。
Definition: Selector.cpp:224
YF_API HandlerTable & FetchDefault()
取默认样式处理器表。
Definition: ystyle.cpp:346
pt pt Y const IWidget &wgt const IWidget &wgt GetSizeOf
无效化:使相对于部件的子部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: ywidget.h:156
shared_ptr< MSelector > p_selector
Definition: Selector.h:246
复选框模块。
Definition: Selector.h:90
YF_API void FillCircle(const Graphics &, const Rect &, const Point &, SDst, Color c)
填充圆形。
Definition: ydraw.cpp:195
YF_API GUIState & FetchGUIState()
取默认图形用户界面公共状态。
Definition: ygui.cpp:442
void SetSelected()
设置选中状态并检查单选框选中事件。
Definition: Selector.cpp:197
YF_API void FillRect(const Graphics &g, const Rect &, Color c)
填充标准矩形。
Definition: ydraw.cpp:146
YF_API void Invalidate(IWidget &, const Rect &)
无效化:使相对于部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: ywidget.cpp:111
复选框。
Definition: Selector.h:132
部件绘制参数。
Definition: ywgtevt.h:276
enum SelectedState:yimpl(size_t){Unchecked=0, Checked=1, Partial=2} StateType
选择框选中状态类型。
Definition: Selector.h:101
MSelector::SelectedArgs TickedArgs
选择框选中状态参数类型。
Definition: Selector.h:107
void Refresh(PaintEventArgs &&) override
刷新:按指定参数绘制界面并更新状态。
Definition: Selector.cpp:210
基本按钮。
Definition: button.h:46
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
RadioBox(const Rect &={})
构造:使用指定边界。
Definition: Selector.cpp:171
YF_API void DrawTick(const Graphics &, const Rect &, const Rect &, Color, Color)
Definition: ystyle.cpp:138
单选框。
Definition: Selector.h:285
void Select()
设置选中状态并触发单选框选中事件。
Definition: Selector.cpp:217
C++ 转换模板。
单选框模块。
Definition: Selector.h:226
CursorState
指针设备光标相对于控件的状态。
Definition: ycontrol.h:345
void Refresh(PaintEventArgs &&) override
刷新:按指定参数绘制界面并更新状态。
Definition: Selector.cpp:237
MSelector::SelectedArgs SelectedArgs
单选框选中状态参数类型。
Definition: Selector.h:235
bounds c YF_API void DrawCircle(const Graphics &, const Rect &, const Point &, SDst, Color c)
描画圆形。
Definition: ydraw.cpp:173
void SetTicked(StateType)
设置选中状态并检查复选框选中事件。
Definition: Selector.cpp:123
void Refresh(PaintEventArgs &&) override
刷新:按指定参数绘制界面并更新状态。
Definition: Selector.cpp:130
CheckButton(const Rect &={})
构造:使用指定边界。
Definition: Selector.cpp:143
YF_API void DrawText(const Graphics &g, TextState &ts, const String &str, bool line_wrap)
绘制文本。
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
MSelector mSelector
Definition: Selector.h:113
bool IsEnabled(const IWidget &wgt)
判断部件是否为可用的控件。
Definition: ycontrol.h:86
Drawing::Padding Margin
文本和容器的间距。
Definition: label.h:62
void Tick(StateType)
设置选中状态并触发复选框选中事件。
Definition: Selector.cpp:137
bounds & r
Definition: ydraw.h:220
c yconstfn g
Definition: ystyle.h:104
#define YB_LIKELY(expr)
Definition: ydef.h:297
Color ForeColor
默认前景色。
Definition: ywidget.h:375
void Refresh(PaintEventArgs &&) override
刷新:按指定参数绘制界面并更新状态。
Definition: Selector.cpp:150
empty_base<> raw_tag
直接构造类型(直接构造重载用)。
Definition: ydef.h:665
AController *controller_ptr Renderer *renderer_ptr View *view_ptr GetView()) DefGetterMem(const ynothrow
YF_API Color RollColor(hsl_t, Hue)
色调偏移。
Definition: ystyle.cpp:238
CheckBox(const Rect &={})
构造:使用指定边界。
Definition: Selector.cpp:97
屏幕区域大小。
Definition: ygdibase.h:249
Styles::StyleMap Styles
样式映射。
Definition: ygui.h:129
p1 p1 p2 p2 c YF_API void DrawRect(const Graphics &g, const Rect &bounds, const Point &pt, const Size &s, Color c)
描画标准矩形。
Definition: ydraw.cpp:131
RadioButton(const Rect &={})
构造:使用指定边界。
Definition: Selector.cpp:230