YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
button.cpp
浏览该文件的文档.
1 /*
2  © 2010-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_Button
30 #include YFM_YSLib_UI_YGUI
31 #include <ystdex/cast.hpp>
32 
33 namespace YSLib
34 {
35 
37 using namespace Drawing;
38 
39 namespace UI
40 {
41 
42 namespace
43 {
44 
46 void
47 RectDrawButton(const PaintContext& pc, const Size& s, Hue base_hue,
48  CursorState cursor_state, bool is_enabled, bool is_focused)
49 {
50  const bool inside(cursor_state != CursorState::Outside);
51  const bool is_pressed(cursor_state == CursorState::Pressed);
52  const auto& g(pc.Target);
53  const Rect r(pc.Location, s);
54 
55  FillRect(g, pc.ClipArea, r, is_enabled ? (inside ? RollColor(is_pressed
56  ? hsl_t{29.2F, .86F, .916F} : hsl_t{30.F, .786F, .945F}, base_hue)
57  : MakeGray(234)) : MakeGray(239));
58  DrawRect(g, pc.ClipArea, r, is_enabled ? (inside ? RollColor(is_pressed
59  ? hsl_t{30.2F, .733F, .618F} : hsl_t{30.F, .72F, .706F}, base_hue)
60  : (is_focused ? RollColor({30.F, 1.F, .6F}, base_hue)
61  : FetchGUIState().Colors[Styles::Workspace])) : MakeGray(217));
62 }
63 
64 } // unnamed namespace;
65 
66 
67 Thumb::Thumb(const Rect& r, Hue h)
68  : Thumb(r, ystdex::raw_tag())
69 {
70  using namespace Styles;
71  static struct Init
72  {
73  Init()
74  {
75  AddHandlers<Thumb>(FetchDefault(), {{ThumbBackground,
76  [](PaintEventArgs&& e){
77  const auto& tmb(
78  ystdex::polymorphic_downcast<Thumb&>(e.GetSender()));
79 
80  RectDrawButton(e, GetSizeOf(tmb), tmb.GetHue(),
81  tmb.GetCursorState(), IsEnabled(tmb), IsFocused(tmb));
82  }
83  }});
84  }
85  } init;
86 
87  yunseq(hue = h, Background = Painter(typeid(Thumb), ThumbBackground));
88 }
90  : Control(r),
91  csCurrent(CursorState::Outside)
92 {
93  const auto invalidator([this]{
94  Invalidate(*this);
95  });
96 
97  yunseq(
98  FetchEvent<CursorOver>(*this) += [this](CursorEventArgs&& e)
99  {
101  {
103  Invalidate(e.GetSender());
104  }
105  },
106  FetchEvent<Enter>(*this) += [this](CursorEventArgs&& e){
107  if(!IsPressed() && e.Keys.any())
108  {
110  Invalidate(e.GetSender());
111  }
112  },
113  FetchEvent<Leave>(*this) += [this](CursorEventArgs&& e){
114  if(csCurrent == CursorState::Over || (IsPressed() && e.Keys.any()))
115  {
117  Invalidate(e.GetSender());
118  }
119  },
120  FetchEvent<GotFocus>(*this) += invalidator,
121  FetchEvent<LostFocus>(*this) += invalidator
122  );
123 }
124 
125 
126 void
128 {
129  yunseq(
130  FetchEvent<Click>(tmb) += [&](CursorEventArgs&&)
131  {
132  if(const auto pCon = FetchContainerPtr(tmb))
133  Close(*pCon);
134  },
135  FetchEvent<Paint>(tmb) += [&](PaintEventArgs&& e){
136  DrawCross(e.Target, e.ClipArea, {e.Location, GetSizeOf(tmb)},
137  IsEnabled(tmb) ? tmb.ForeColor
138  : FetchGUIState().Colors[Styles::Workspace]);
139  }
140  );
141 }
142 
143 
145  : Button(r, 180, fnt, a)
146 {}
147 Button::Button(const Rect& r, Hue h, const Drawing::Font& fnt, TextAlignment a)
148  : Thumb(r, h),
149  MLabel(fnt, a)
150 {}
151 
152 void
154 {
155  // NOTE: Partial invalidation made no efficiency improved here.
156  DrawText(GetSizeOf(*this), IsEnabled(*this) ? ForeColor
157  : FetchGUIState().Colors[Styles::Workspace], std::move(e));
158 }
159 
160 } // namespace UI;
161 
162 } // namespace YSLib;
163 
YF_API HandlerTable & FetchDefault()
取默认样式处理器表。
Definition: ystyle.cpp:346
pt pt Y const IWidget &wgt const IWidget &wgt GetSizeOf
无效化:使相对于部件的子部件的指定区域在直接和间接的窗口缓冲区中无效。
Definition: ywidget.h:156
YF_API void DrawCross(const Graphics &, const Rect &, const Rect &, Color)
在指定图形接口上下文中使用指定颜色描画交叉直线段(“×”)。
Definition: ystyle.cpp:125
YF_API GUIState & FetchGUIState()
取默认图形用户界面公共状态。
Definition: ygui.cpp:442
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
Thumb(const Rect &={}, Drawing::Hue=180)
构造:使用指定边界和色调。
部件绘制参数。
Definition: ywgtevt.h:276
按钮。
Definition: button.h:116
基本按钮。
Definition: button.h:46
控件。
Definition: ycontrol.h:275
C++ 转换模板。
CursorState
指针设备光标相对于控件的状态。
Definition: ycontrol.h:345
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
YF_API bool IsFocused(const IWidget &)
判断部件是否取得焦点。
Definition: yfocus.cpp:38
CursorState csCurrent
指针设备光标状态。
Definition: button.h:64
YF_API void DrawText(const Graphics &g, TextState &ts, const String &str, bool line_wrap)
绘制文本。
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
bool IsEnabled(const IWidget &wgt)
判断部件是否为可用的控件。
Definition: ycontrol.h:86
应用程序工作区背景。
Definition: ystyle.h:185
TextAlignment
Definition: label.h:44
空基类模板。
Definition: ydef.h:658
bounds & r
Definition: ydraw.h:220
标签模块。
Definition: label.h:58
c yconstfn g
Definition: ystyle.h:104
字体:字模,包含字型、样式和大小。
Definition: Font.h:546
Color ForeColor
默认前景色。
Definition: ywidget.h:375
empty_base<> raw_tag
直接构造类型(直接构造重载用)。
Definition: ydef.h:665
virtual void Refresh(PaintEventArgs &&)
刷新:按指定参数绘制界面并更新状态。
Definition: ywidget.cpp:264
YF_API Color RollColor(hsl_t, Hue)
色调偏移。
Definition: ystyle.cpp:238
Button(const Rect &={}, const Drawing::Font &={}, TextAlignment=TextAlignment::Center)
构造:使用指定边界、字体和文本对齐样式。
Definition: button.cpp:144
指针设备输入事件参数类。
Definition: ywgtevt.h:183
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
void DecorateAsCloseButton(Thumb &tmb)
Definition: button.cpp:127
void Close(IWidget &wgt)
Definition: ywidget.cpp:95