MyGUI  3.2.0
MyGUI_ControllerEdgeHide.cpp
Go to the documentation of this file.
1 
6 /*
7  This file is part of MyGUI.
8 
9  MyGUI is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  MyGUI is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_Gui.h"
25 #include "MyGUI_InputManager.h"
26 #include "MyGUI_WidgetManager.h"
27 #include "MyGUI_Widget.h"
28 
29 namespace MyGUI
30 {
31 
32 #ifndef M_PI
33  const float M_PI = 3.141593f;
34 #endif
35 
37  mTime(1.0),
38  mRemainPixels(0),
39  mShadowSize(0),
40  mElapsedTime(0)
41  {
42  }
43 
45  {
46  }
47 
48  void ControllerEdgeHide::prepareItem(Widget* _widget)
49  {
50  recalculateTime(_widget);
51  // вызываем пользовательский делегат для подготовки
52  eventPreAction(_widget);
53  }
54 
55  bool ControllerEdgeHide::addTime(Widget* _widget, float _time)
56  {
57  const IntSize& view_size = _widget->getParentSize();
58  // do nothing if we have minimized window
59  if (view_size.width <= 1 && view_size.height <= 1)
60  return true;
61 
62  Widget* keyFocus = InputManager::getInstance().getKeyFocusWidget();
63  Widget* mouseFocus = InputManager::getInstance().getMouseFocusWidget();
64 
65  while ((keyFocus != nullptr) && (_widget != keyFocus))
66  keyFocus = keyFocus->getParent();
67  while ((mouseFocus != nullptr) && (_widget != mouseFocus))
68  mouseFocus = mouseFocus->getParent();
69 
70  // if our widget or its children have focus
71  bool haveFocus = ((keyFocus != nullptr) || (mouseFocus != nullptr)) || (_widget->getVisible() == false);
72 
73  mElapsedTime += haveFocus ? -_time : _time;
74 
75  if (mElapsedTime >= mTime)
76  {
77  mElapsedTime = mTime;
78  }
79  if (mElapsedTime <= 0)
80  {
81  mElapsedTime = 0.0f;
82  return true;
83  }
84 
85  float k = sin(M_PI * mElapsedTime / mTime - M_PI / 2);
86  if (k < 0) k = (-pow(-k, 0.7f) + 1) / 2;
87  else k = (pow(k, 0.7f) + 1) / 2;
88 
89  MyGUI::IntCoord coord = _widget->getCoord();
90  // if widget was moved
91  if (coord != mLastCoord)
92  {
93  // if still moving - leave it alone
94  if (haveFocus)
95  return true;
96  else
97  recalculateTime(_widget);
98  }
99 
100  bool nearBorder = false;
101 
102  if ((coord.left <= 0) && !(coord.right() >= view_size.width - 1))
103  {
104  coord.left = - int( float(coord.width - mRemainPixels - mShadowSize) * k);
105  nearBorder = true;
106  }
107  if ((coord.top <= 0) && !(coord.bottom() >= view_size.height - 1))
108  {
109  coord.top = - int( float(coord.height - mRemainPixels - mShadowSize) * k);
110  nearBorder = true;
111  }
112  if ((coord.right() >= view_size.width - 1) && !(coord.left <= 0))
113  {
114  coord.left = int(float(view_size.width - 1) - float(mRemainPixels) * k - float(coord.width) * (1.f - k));
115  nearBorder = true;
116  }
117  if ((coord.bottom() >= view_size.height - 1) && !(coord.top <= 0))
118  {
119  coord.top = int(float(view_size.height - 1) - float(mRemainPixels) * k - float(coord.height) * (1.f - k));
120  nearBorder = true;
121  }
122 
123  if (nearBorder)
124  {
125  _widget->setCoord(coord);
126  }
127  else
128  {
129  mElapsedTime = 0;
130  }
131  mLastCoord = coord;
132 
133  eventUpdateAction(_widget);
134 
135  return true;
136  }
137 
138  void ControllerEdgeHide::setProperty(const std::string& _key, const std::string& _value)
139  {
140  if (_key == "Time")
141  setTime(utility::parseValue<float>(_value));
142  else if (_key == "RemainPixels")
143  setRemainPixels(utility::parseValue<int>(_value));
144  else if (_key == "ShadowSize")
145  setShadowSize(utility::parseValue<int>(_value));
146  }
147 
148  void ControllerEdgeHide::recalculateTime(Widget* _widget)
149  {
150  float k = 0;
151  const MyGUI::IntCoord& coord = _widget->getCoord();
152  const MyGUI::IntSize& view_size = _widget->getParentSize();
153 
154  // check if widget is near any border and not near opposite borders at same time
155  if ((coord.left <= 0) && !(coord.right() >= view_size.width - 1))
156  {
157  k = - (float) coord.left / (coord.width - mRemainPixels - mShadowSize);
158  }
159  else if ((coord.top <= 0) && !(coord.bottom() >= view_size.height - 1))
160  {
161  k = - (float)coord.top / (coord.height - mRemainPixels - mShadowSize);
162  }
163  else if ((coord.right() >= view_size.width - 1) && !(coord.left <= 0))
164  {
165  k = (float)(coord.right() - view_size.width + 1 ) / (coord.width - mRemainPixels);
166  }
167  else if ((coord.bottom() >= view_size.height - 1) && !(coord.top <= 0))
168  {
169  k = (float)(coord.bottom() - view_size.height + 1 ) / (coord.height - mRemainPixels);
170  }
171 
172  //mElapsedTime = (asin(k)/M_PI + 1./2) * mTime;
173  // this is reversed formula from ControllerEdgeHide::addTime k calculation
174  if (k > 0.5f)
175  mElapsedTime = (asin( pow( 2 * k - 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
176  else
177  mElapsedTime = (asin(-pow(-2 * k + 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
178  }
179 
180  void ControllerEdgeHide::setTime(float _value)
181  {
182  mTime = _value;
183  }
184 
186  {
187  mRemainPixels = _value;
188  }
189 
191  {
192  mShadowSize = _value;
193  }
194 
195 } // namespace MyGUI
delegates::CMultiDelegate1< Widget * > eventUpdateAction
const IntCoord & getCoord() const
types::TSize< int > IntSize
Definition: MyGUI_Types.h:44
const float M_PI
static InputManager & getInstance()
virtual void setProperty(const std::string &_key, const std::string &_value)
IntSize getParentSize() const
Widget * getMouseFocusWidget() const
Widget * getParent() const
Widget * getKeyFocusWidget() const
delegates::CMultiDelegate1< Widget * > eventPreAction