MyGUI  3.4.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 #ifdef M_PI
18 #undef M_PI
19 #endif
20  const float M_PI = 3.141593f;
21 
23  mTime(1.0),
24  mRemainPixels(0),
25  mShadowSize(0),
26  mElapsedTime(0)
27  {
28  }
29 
31  {
32  recalculateTime(_widget);
33  // вызываем пользовательский делегат для подготовки
34  eventPreAction(_widget, this);
35  }
36 
37  bool ControllerEdgeHide::addTime(Widget* _widget, float _time)
38  {
39  const IntSize& view_size = _widget->getParentSize();
40  // do nothing if we have minimized window
41  if (view_size.width <= 1 && view_size.height <= 1)
42  return true;
43 
46 
47  while ((keyFocus != nullptr) && (_widget != keyFocus))
48  keyFocus = keyFocus->getParent();
49  while ((mouseFocus != nullptr) && (_widget != mouseFocus))
50  mouseFocus = mouseFocus->getParent();
51 
52  // if our widget or its children have focus
53  bool haveFocus = ((keyFocus != nullptr) || (mouseFocus != nullptr)) || (_widget->getVisible() == false);
54 
55  mElapsedTime += haveFocus ? -_time : _time;
56 
57  if (mElapsedTime >= mTime)
58  {
59  mElapsedTime = mTime;
60  }
61  if (mElapsedTime <= 0)
62  {
63  mElapsedTime = 0.0f;
64  return true;
65  }
66 
67  float k = std::sin(M_PI * mElapsedTime / mTime - M_PI / 2);
68  if (k < 0) k = (-std::pow(-k, 0.7f) + 1) / 2;
69  else k = (std::pow(k, 0.7f) + 1) / 2;
70 
71  MyGUI::IntCoord coord = _widget->getCoord();
72  // if widget was moved
73  if (coord != mLastCoord)
74  {
75  // if still moving - leave it alone
76  if (haveFocus)
77  return true;
78  else
79  recalculateTime(_widget);
80  }
81 
82  bool nearBorder = false;
83 
84  if ((coord.left <= 0) && !(coord.right() >= view_size.width - 1))
85  {
86  coord.left = - int( float(coord.width - mRemainPixels - mShadowSize) * k);
87  nearBorder = true;
88  }
89  if ((coord.top <= 0) && !(coord.bottom() >= view_size.height - 1))
90  {
91  coord.top = - int( float(coord.height - mRemainPixels - mShadowSize) * k);
92  nearBorder = true;
93  }
94  if ((coord.right() >= view_size.width - 1) && !(coord.left <= 0))
95  {
96  coord.left = int(float(view_size.width - 1) - float(mRemainPixels) * k - float(coord.width) * (1.f - k));
97  nearBorder = true;
98  }
99  if ((coord.bottom() >= view_size.height - 1) && !(coord.top <= 0))
100  {
101  coord.top = int(float(view_size.height - 1) - float(mRemainPixels) * k - float(coord.height) * (1.f - k));
102  nearBorder = true;
103  }
104 
105  if (nearBorder)
106  {
107  _widget->setCoord(coord);
108  }
109  else
110  {
111  mElapsedTime = 0;
112  }
113  mLastCoord = coord;
114 
115  eventUpdateAction(_widget, this);
116 
117  return true;
118  }
119 
120  void ControllerEdgeHide::setProperty(const std::string& _key, const std::string& _value)
121  {
122  if (_key == "Time")
123  setTime(utility::parseValue<float>(_value));
124  else if (_key == "RemainPixels")
125  setRemainPixels(utility::parseValue<int>(_value));
126  else if (_key == "ShadowSize")
127  setShadowSize(utility::parseValue<int>(_value));
128  }
129 
130  void ControllerEdgeHide::recalculateTime(Widget* _widget)
131  {
132  float k = 0;
133  const MyGUI::IntCoord& coord = _widget->getCoord();
134  const MyGUI::IntSize& view_size = _widget->getParentSize();
135 
136  // check if widget is near any border and not near opposite borders at same time
137  if ((coord.left <= 0) && !(coord.right() >= view_size.width - 1))
138  {
139  k = - (float) coord.left / (coord.width - mRemainPixels - mShadowSize);
140  }
141  else if ((coord.top <= 0) && !(coord.bottom() >= view_size.height - 1))
142  {
143  k = - (float)coord.top / (coord.height - mRemainPixels - mShadowSize);
144  }
145  else if ((coord.right() >= view_size.width - 1) && !(coord.left <= 0))
146  {
147  k = (float)(coord.right() - view_size.width + 1 ) / (coord.width - mRemainPixels);
148  }
149  else if ((coord.bottom() >= view_size.height - 1) && !(coord.top <= 0))
150  {
151  k = (float)(coord.bottom() - view_size.height + 1 ) / (coord.height - mRemainPixels);
152  }
153 
154  //mElapsedTime = (asin(k)/M_PI + 1./2) * mTime;
155  // this is reversed formula from ControllerEdgeHide::addTime k calculation
156  if (k > 0.5f)
157  mElapsedTime = (std::asin( std::pow( 2 * k - 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
158  else
159  mElapsedTime = (std::asin(-std::pow(-2 * k + 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
160  }
161 
162  void ControllerEdgeHide::setTime(float _value)
163  {
164  mTime = _value;
165  }
166 
168  {
169  mRemainPixels = _value;
170  }
171 
173  {
174  mShadowSize = _value;
175  }
176 
177 } // namespace MyGUI
void setProperty(const std::string &_key, const std::string &_value) override
bool addTime(Widget *_widget, float _time) override
void prepareItem(Widget *_widget) override
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventPreAction
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventUpdateAction
const IntCoord & getCoord() const
Widget * getKeyFocusWidget() const
Widget * getMouseFocusWidget() const
static InputManager & getInstance()
widget description should be here.
Definition: MyGUI_Widget.h:37
Widget * getParent() const
void setCoord(const IntCoord &_value) override
IntSize getParentSize() const
bool getVisible() const
const float M_PI