YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
button.cpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2010 - 2013.
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/button.h"
29 #include "YSLib/Service/yblit.h"
30 #include "YSLib/UI/ygui.h"
31 
33 
35 
36 namespace
37 {
38 
39 using Drawing::Hue;
40 
45 inline Hue
46 change_hue(Hue base_h, Hue h)
47 {
48  base_h += h;
49  return base_h < 360 ? base_h : base_h - 360;
50 }
51 
55 void
56 RectDrawButton(const Graphics& g, Point pt, Size s, Hue base_hue,
57  bool is_pressed = false, bool is_enabled = true)
58 {
59  YAssert(bool(g), "Invalid graphics context found.");
60 
61  DrawRectRoundCorner(g, pt, s, is_enabled ? HSLToColor(
62  {change_hue(base_hue, 25.640625F), 0.493671F, 0.462891F})
63  : FetchGUIState().Colors[Styles::Workspace]);
64  if(YB_LIKELY(s.Width > 2 && s.Height > 2))
65  {
66  yunseq(pt.X += 1, pt.Y += 1, s.Width -= 2, s.Height -= 2);
67  FillRect(g, pt, s, is_enabled ? HSLToColor({change_hue(base_hue,
68  11.304688F), 0.990431F, 0.591797F}) : Color(244, 244, 244));
69  if(is_enabled)
70  {
71  if(s.Width > 2 && s.Height > 2)
72  {
73  Size sz(s.Width - 2, (s.Height - 2) / 2);
74  Point sp(pt.X + 1, pt.Y + 1);
75 
76  FillRect(g, sp, sz, HSLToColor({change_hue(base_hue,
77  39.132872F), 0.920000F, 0.951172F}));
78  sp.Y += sz.Height;
79  if(s.Height % 2 != 0)
80  ++sz.Height;
81  FillRect(g, sp, sz, HSLToColor({change_hue(base_hue,
82  29.523438F), 0.969231F, 0.873047F}));
83  }
84  if(is_pressed)
85  {
86  const Color tc(HSLToColor({change_hue(base_hue, 165), 0.4F,
87  0.16F}));
88 
89  TransformRect(g, pt, s, [=](BitmapPtr dst){
90  const Color d(*dst);
91 
92  *dst = Color(d.GetR() ^ tc.GetR(), d.GetG() ^ tc.GetG(),
93  d.GetB() ^ tc.GetB());
94  });
95  }
96  }
97  }
98 }
99 
100 } // unnamed namespace;
101 
102 
103 Thumb::Thumb(const Rect& r, Hue hue)
104  : Thumb(r, NoBackgroundTag())
105 {
106  Background = std::bind(DrawThumbBackground, std::placeholders::_1,
107  std::ref(*this), hue);
108 }
109 Thumb::Thumb(const Rect& r, NoBackgroundTag)
110  : Control(r, NoBackgroundTag()),
111  bPressed(false)
112 {
113  yunseq(
114  FetchEvent<Enter>(*this) += [this](TouchEventArgs&&){
115  if(!bPressed)
116  {
117  bPressed = true;
118  Invalidate(*this);
119  }
120  },
121  FetchEvent<Leave>(*this) += [this](TouchEventArgs&&){
122  if(bPressed)
123  {
124  bPressed = false;
125  Invalidate(*this);
126  }
127  }
128  );
129 }
130 
131 
132 void
133 DrawThumbBackground(PaintEventArgs&& e, Thumb& tmb, Hue base_hue)
134 {
135  const bool enabled(IsEnabled(tmb));
136  const auto& g(e.Target);
137  const auto& pt(e.Location);
138  Size s(GetSizeOf(tmb));
139 
140  RectDrawButton(g, pt, s, base_hue, tmb.IsPressed(), enabled);
141  if(enabled && IsFocused(tmb) && YB_LIKELY(s.Width > 6 && s.Height > 6))
142  {
143  yunseq(s.Width -= 6, s.Height -= 6);
144  DrawRect(g, pt + Vec(3, 3), s, HSLToColor({base_hue, 1, 0.5F}));
145  }
146  e.ClipArea = Rect(pt, GetSizeOf(tmb));
147 }
148 
149 
150 void
152 {
153  yunseq(
154  FetchEvent<Click>(tmb) += [&](TouchEventArgs&&)
155  {
156  if(const auto pCon = FetchContainerPtr(tmb))
157  Close(*pCon);
158  },
159  FetchEvent<Paint>(tmb) += [&](PaintEventArgs&& e){
160  DrawCross(e.Target, e.Location, GetSizeOf(tmb), IsEnabled(tmb)
161  ? tmb.ForeColor : FetchGUIState().Colors[Styles::Workspace]);
162  }
163  );
164 }
165 
166 
167 Button::Button(const Rect& r, const Drawing::Font& fnt, TextAlignment a)
168  : Button(r, 180, fnt, a)
169 {}
170 Button::Button(const Rect& r, Drawing::Hue h, const Drawing::Font& fnt,
171  TextAlignment a)
172  : Thumb(r, h),
173  MLabel(fnt, a)
174 {}
175 
176 void
177 Button::Refresh(PaintEventArgs&& e)
178 {
179  // NOTE: Partial invalidation made no efficiency improved here.
180  DrawText(GetSizeOf(*this), IsEnabled(*this) ? ForeColor
181  : FetchGUIState().Colors[Styles::Workspace], {e.Target, e.Location,
182  Rect(e.Location, GetSizeOf(*this))});
183 }
184 
186 
187 YSL_END
188