YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Selector.cpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2011 - 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/Selector.h"
29 #include "YSLib/Service/yblit.h"
30 #include "YSLib/UI/ygui.h"
31 
33 
35 
36 namespace
37 {
38  void
39  Diminish(Rect& r, SDst off1 = 1, SDst off2 = 2)
40  {
41  YAssert(r.Width > 2 && r.Height > 2, "Boundary is too small.");
42 
43  yunseq(r.X += off1, r.Y += off1,
44  r.Width -= off2, r.Height -= off2);
45  }
46 
47  using namespace Drawing;
48 
49  void
50  RectDrawCheckBox(const Graphics& g, const Rect& r,
51  bool is_pressed = false, bool is_locked = false,
52  bool is_ticked = false, bool is_focused = false,
53  Color c = Color(85, 184, 163))
54  {
55  YAssert(bool(g), "Invalid context found.");
56 
57  DrawRect(g, r, is_focused ? c : Color(85, 134, 223));
58  if(YB_LIKELY(r.Width > 10 && r.Height > 10))
59  {
60  Rect rt(r);
61  Color cs[]{{222, 249, 250}, {177, 223, 253}, {213, 254, 254}};
62  // color3 gradient: 207, 236, 253;
63  // u16 h(rgb2hsl(Color2rgb(c)).h);
64 
65  if(!(is_locked || is_focused))
66  for(auto& c : cs)
67  {
68  hsl_t tmp(ColorToHSL(c));
69 
70  tmp.s /= 4;
71  c = HSLToColor(tmp);
72  }
73  Diminish(rt);
74  DrawRect(g, rt, cs[0]);
75  Diminish(rt);
76  DrawRect(g, rt, cs[1]);
77  Diminish(rt);
78  FillRect(g, rt, cs[2]);
79  }
80  if(is_ticked)
81  {
82  const Color c1(4, 34, 113), c2(108, 166, 208);
83  Point p1(r.X + 2, r.Y + r.Height / 2), p2(r.X + r.Width / 2 - 1,
84  r.Y + r.Height - 3), p3(r.X + r.Width - 2, r.Y + 1);
85 
86  p2 += Vec(0, -1);
87  DrawLineSeg(g, p1 + Vec(1, 0), p2, c2);
88  DrawLineSeg(g, p2, p3 + Vec(-1, 0), c2);
89  p2 += Vec(0, 2);
90  DrawLineSeg(g, p1 + Vec(0, 1), p2, c2);
91  DrawLineSeg(g, p2, p3 + Vec(0, 1), c2);
92  p2 += Vec(0, -1);
93  DrawLineSeg(g, p1, p2, c1);
94  DrawLineSeg(g, p2, p3, c1);
95  }
96  if(is_pressed)
97  TransformRect(g, r, transform_pixel_ex<56, 24, 32>);
98  }
99 }
100 
101 
102 CheckBox::CheckBox(const Rect& r)
103  : Thumb(r, NoBackgroundTag()),
104  bTicked(false)
105 {
106  FetchEvent<Click>(*this) += [this](TouchEventArgs&&){
107  bTicked = !bTicked;
108  Ticked(TickedArgs(*this, bTicked));
109  };
110 }
111 
112 void
113 CheckBox::SetTicked(bool b)
114 {
115  const bool old_tick(bTicked);
116 
117  bTicked = b;
118  if(bTicked != old_tick)
119  Ticked(TickedArgs(*this, b));
120 }
121 
122 void
123 CheckBox::Tick(bool b)
124 {
125  Ticked(TickedArgs(*this, bTicked = b));
126 }
127 
128 void
129 CheckBox::PaintBox(const Graphics& g, const Rect& r)
130 {
131  RectDrawCheckBox(g, r, bPressed, IsFocusedByShell(*this), bTicked,
132  IsFocused(*this));
133 }
134 
135 void
136 CheckBox::Refresh(PaintEventArgs&& e)
137 {
138  PaintBox(e.Target, (e.ClipArea = Rect(e.Location, GetSizeOf(*this))));
139 }
140 
141 
142 CheckButton::CheckButton(const Rect& r)
143  : CheckBox(r)
144 {
145  Margin.Top = 0;
146 }
147 
148 void
150 {
151  const auto& pt(e.Location);
152 
153  PaintBox(e.Target, Rect(pt, 13, 13));
154  Margin.Left += 13;
155  DrawText(GetSizeOf(*this), ForeColor, e);
156  Margin.Left -= 13;
157  e.ClipArea = Rect(pt, GetSizeOf(*this));
158 }
159 
161 
162 YSL_END
163