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