MyGUI  3.2.2
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 
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 
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
MyGUI::Singleton< InputManager >::getInstance
static InputManager & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI::Widget::getParent
Widget * getParent() const
Definition: MyGUI_Widget.cpp:1275
MyGUI::ControllerEdgeHide::addTime
virtual bool addTime(Widget *_widget, float _time)
Definition: MyGUI_ControllerEdgeHide.cpp:40
MyGUI::ControllerEdgeHide::setProperty
virtual void setProperty(const std::string &_key, const std::string &_value)
Definition: MyGUI_ControllerEdgeHide.cpp:123
MyGUI_Gui.h
MyGUI::types::TSize::height
T height
Definition: MyGUI_TSize.h:21
MyGUI::InputManager::getKeyFocusWidget
Widget * getKeyFocusWidget() const
Definition: MyGUI_InputManager.cpp:633
MyGUI::ControllerEdgeHide::setShadowSize
void setShadowSize(int _value)
Definition: MyGUI_ControllerEdgeHide.cpp:175
MyGUI::types::TCoord::bottom
T bottom() const
Definition: MyGUI_TCoord.h:155
MyGUI::types::TCoord::left
T left
Definition: MyGUI_TCoord.h:22
MyGUI_Widget.h
MyGUI::types::TCoord::top
T top
Definition: MyGUI_TCoord.h:23
MyGUI::ControllerEdgeHide::setTime
void setTime(float _value)
Definition: MyGUI_ControllerEdgeHide.cpp:165
MyGUI::InputManager::getMouseFocusWidget
Widget * getMouseFocusWidget() const
Definition: MyGUI_InputManager.cpp:628
MyGUI::M_PI
const float M_PI
Definition: MyGUI_ControllerEdgeHide.cpp:18
MyGUI::ControllerEdgeHide::prepareItem
virtual void prepareItem(Widget *_widget)
Definition: MyGUI_ControllerEdgeHide.cpp:33
MyGUI::Widget
Widget properties. Skin childs. Widget widget description should be here.
Definition: MyGUI_Widget.h:29
MyGUI::types::TSize::width
T width
Definition: MyGUI_TSize.h:20
MyGUI::Widget::getParentSize
IntSize getParentSize() const
Definition: MyGUI_Widget.cpp:1025
MyGUI::types::TCoord::right
T right() const
Definition: MyGUI_TCoord.h:150
MyGUI::ControllerEdgeHide::~ControllerEdgeHide
virtual ~ControllerEdgeHide()
Definition: MyGUI_ControllerEdgeHide.cpp:29
MyGUI_ControllerEdgeHide.h
MyGUI_Precompiled.h
MyGUI::ControllerItem::eventUpdateAction
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventUpdateAction
Definition: MyGUI_ControllerItem.h:48
MyGUI_InputManager.h
MyGUI::ICroppedRectangle::getCoord
const IntCoord & getCoord() const
Definition: MyGUI_ICroppedRectangle.h:61
MyGUI_WidgetManager.h
MyGUI::ControllerEdgeHide::setRemainPixels
void setRemainPixels(int _value)
Definition: MyGUI_ControllerEdgeHide.cpp:170
MyGUI::ControllerEdgeHide::ControllerEdgeHide
ControllerEdgeHide()
Definition: MyGUI_ControllerEdgeHide.cpp:21
MyGUI::types::TSize< int >
MyGUI::Widget::setCoord
virtual void setCoord(const IntCoord &_value)
Definition: MyGUI_Widget.cpp:689
MyGUI::types::TCoord::width
T width
Definition: MyGUI_TCoord.h:24
MyGUI::Widget::getVisible
bool getVisible() const
Definition: MyGUI_Widget.cpp:1250
MyGUI::types::TCoord< int >
MyGUI::types::TCoord::height
T height
Definition: MyGUI_TCoord.h:25
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::ControllerItem::eventPreAction
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventPreAction
Definition: MyGUI_ControllerItem.h:42