MyGUI  3.2.1
MyGUI_ControllerEdgeHide.cpp
Go to the documentation of this file.
1 /*
2  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3  * Distributed under the MIT License
4  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5  */
6 
7 #include "MyGUI_Precompiled.h"
9 #include "MyGUI_Gui.h"
10 #include "MyGUI_InputManager.h"
11 #include "MyGUI_WidgetManager.h"
12 #include "MyGUI_Widget.h"
13 
14 namespace MyGUI
15 {
16 
17 #ifndef M_PI
18  const float M_PI = 3.141593f;
19 #endif
20 
22  mTime(1.0),
23  mRemainPixels(0),
24  mShadowSize(0),
25  mElapsedTime(0)
26  {
27  }
28 
30  {
31  }
32 
33  void ControllerEdgeHide::prepareItem(Widget* _widget)
34  {
35  recalculateTime(_widget);
36  // вызываем пользовательский делегат для подготовки
37  eventPreAction(_widget, this);
38  }
39 
40  bool ControllerEdgeHide::addTime(Widget* _widget, float _time)
41  {
42  const IntSize& view_size = _widget->getParentSize();
43  // do nothing if we have minimized window
44  if (view_size.width <= 1 && view_size.height <= 1)
45  return true;
46 
47  Widget* keyFocus = InputManager::getInstance().getKeyFocusWidget();
48  Widget* mouseFocus = InputManager::getInstance().getMouseFocusWidget();
49 
50  while ((keyFocus != nullptr) && (_widget != keyFocus))
51  keyFocus = keyFocus->getParent();
52  while ((mouseFocus != nullptr) && (_widget != mouseFocus))
53  mouseFocus = mouseFocus->getParent();
54 
55  // if our widget or its children have focus
56  bool haveFocus = ((keyFocus != nullptr) || (mouseFocus != nullptr)) || (_widget->getVisible() == false);
57 
58  mElapsedTime += haveFocus ? -_time : _time;
59 
60  if (mElapsedTime >= mTime)
61  {
62  mElapsedTime = mTime;
63  }
64  if (mElapsedTime <= 0)
65  {
66  mElapsedTime = 0.0f;
67  return true;
68  }
69 
70  float k = sin(M_PI * mElapsedTime / mTime - M_PI / 2);
71  if (k < 0) k = (-pow(-k, 0.7f) + 1) / 2;
72  else k = (pow(k, 0.7f) + 1) / 2;
73 
74  MyGUI::IntCoord coord = _widget->getCoord();
75  // if widget was moved
76  if (coord != mLastCoord)
77  {
78  // if still moving - leave it alone
79  if (haveFocus)
80  return true;
81  else
82  recalculateTime(_widget);
83  }
84 
85  bool nearBorder = false;
86 
87  if ((coord.left <= 0) && !(coord.right() >= view_size.width - 1))
88  {
89  coord.left = - int( float(coord.width - mRemainPixels - mShadowSize) * k);
90  nearBorder = true;
91  }
92  if ((coord.top <= 0) && !(coord.bottom() >= view_size.height - 1))
93  {
94  coord.top = - int( float(coord.height - mRemainPixels - mShadowSize) * k);
95  nearBorder = true;
96  }
97  if ((coord.right() >= view_size.width - 1) && !(coord.left <= 0))
98  {
99  coord.left = int(float(view_size.width - 1) - float(mRemainPixels) * k - float(coord.width) * (1.f - k));
100  nearBorder = true;
101  }
102  if ((coord.bottom() >= view_size.height - 1) && !(coord.top <= 0))
103  {
104  coord.top = int(float(view_size.height - 1) - float(mRemainPixels) * k - float(coord.height) * (1.f - k));
105  nearBorder = true;
106  }
107 
108  if (nearBorder)
109  {
110  _widget->setCoord(coord);
111  }
112  else
113  {
114  mElapsedTime = 0;
115  }
116  mLastCoord = coord;
117 
118  eventUpdateAction(_widget, this);
119 
120  return true;
121  }
122 
123  void ControllerEdgeHide::setProperty(const std::string& _key, const std::string& _value)
124  {
125  if (_key == "Time")
126  setTime(utility::parseValue<float>(_value));
127  else if (_key == "RemainPixels")
128  setRemainPixels(utility::parseValue<int>(_value));
129  else if (_key == "ShadowSize")
130  setShadowSize(utility::parseValue<int>(_value));
131  }
132 
133  void ControllerEdgeHide::recalculateTime(Widget* _widget)
134  {
135  float k = 0;
136  const MyGUI::IntCoord& coord = _widget->getCoord();
137  const MyGUI::IntSize& view_size = _widget->getParentSize();
138 
139  // check if widget is near any border and not near opposite borders at same time
140  if ((coord.left <= 0) && !(coord.right() >= view_size.width - 1))
141  {
142  k = - (float) coord.left / (coord.width - mRemainPixels - mShadowSize);
143  }
144  else if ((coord.top <= 0) && !(coord.bottom() >= view_size.height - 1))
145  {
146  k = - (float)coord.top / (coord.height - mRemainPixels - mShadowSize);
147  }
148  else if ((coord.right() >= view_size.width - 1) && !(coord.left <= 0))
149  {
150  k = (float)(coord.right() - view_size.width + 1 ) / (coord.width - mRemainPixels);
151  }
152  else if ((coord.bottom() >= view_size.height - 1) && !(coord.top <= 0))
153  {
154  k = (float)(coord.bottom() - view_size.height + 1 ) / (coord.height - mRemainPixels);
155  }
156 
157  //mElapsedTime = (asin(k)/M_PI + 1./2) * mTime;
158  // this is reversed formula from ControllerEdgeHide::addTime k calculation
159  if (k > 0.5f)
160  mElapsedTime = (asin( pow( 2 * k - 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
161  else
162  mElapsedTime = (asin(-pow(-2 * k + 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
163  }
164 
165  void ControllerEdgeHide::setTime(float _value)
166  {
167  mTime = _value;
168  }
169 
171  {
172  mRemainPixels = _value;
173  }
174 
176  {
177  mShadowSize = _value;
178  }
179 
180 } // namespace MyGUI