MyGUI: MyGUI_ControllerEdgeHide.cpp Source File

MyGUI 3.4.2

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

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"
8#include "MyGUI_ControllerEdgeHide.h"
9#include "MyGUI_Gui.h"
10#include "MyGUI_InputManager.h"
11#include "MyGUI_WidgetManager.h"
12#include "MyGUI_Widget.h"
13
14namespace MyGUI
15{
16
17#ifdef M_PI
18#undef M_PI
19#endif
20 const float M_PI = 3.141593f;
21
22 ControllerEdgeHide::ControllerEdgeHide() :
23 mTime(1.0),
24 mRemainPixels(0),
25 mShadowSize(0),
26 mElapsedTime(0)
27 {
28 }
29
30 void ControllerEdgeHide::prepareItem(Widget* _widget)
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
44 Widget* keyFocus = InputManager::getInstance().getKeyFocusWidget();
45 Widget* mouseFocus = InputManager::getInstance().getMouseFocusWidget();
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
167 void ControllerEdgeHide::setRemainPixels(int _value)
168 {
169 mRemainPixels = _value;
170 }
171
172 void ControllerEdgeHide::setShadowSize(int _value)
173 {
174 mShadowSize = _value;
175 }
176
177} // namespace MyGUI
MyGUI_ControllerEdgeHide.h
MyGUI_Gui.h
MyGUI_InputManager.h
MyGUI_Precompiled.h
MyGUI_Widget.h
MyGUI_WidgetManager.h
MyGUI::ControllerEdgeHide::setRemainPixels
void setRemainPixels(int _value)
Definition MyGUI_ControllerEdgeHide.cpp:167
MyGUI::ControllerEdgeHide::setProperty
void setProperty(const std::string &_key, const std::string &_value) override
Definition MyGUI_ControllerEdgeHide.cpp:120
MyGUI::ControllerEdgeHide::setShadowSize
void setShadowSize(int _value)
Definition MyGUI_ControllerEdgeHide.cpp:172
MyGUI::ControllerEdgeHide::setTime
void setTime(float _value)
Definition MyGUI_ControllerEdgeHide.cpp:162
MyGUI::ControllerEdgeHide::addTime
bool addTime(Widget *_widget, float _time) override
Definition MyGUI_ControllerEdgeHide.cpp:37
MyGUI::ControllerEdgeHide::prepareItem
void prepareItem(Widget *_widget) override
Definition MyGUI_ControllerEdgeHide.cpp:30
MyGUI::ControllerEdgeHide::ControllerEdgeHide
ControllerEdgeHide()
Definition MyGUI_ControllerEdgeHide.cpp:22
MyGUI::ControllerItem::eventPreAction
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventPreAction
Definition MyGUI_ControllerItem.h:42
MyGUI::ControllerItem::eventUpdateAction
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventUpdateAction
Definition MyGUI_ControllerItem.h:48
MyGUI::ICroppedRectangle::getCoord
const IntCoord & getCoord() const
Definition MyGUI_ICroppedRectangle.h:61
MyGUI::InputManager::getKeyFocusWidget
Widget * getKeyFocusWidget() const
Definition MyGUI_InputManager.cpp:652
MyGUI::InputManager::getMouseFocusWidget
Widget * getMouseFocusWidget() const
Definition MyGUI_InputManager.cpp:647
MyGUI::InputManager::getInstance
static InputManager & getInstance()
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:37
MyGUI::Widget::getParent
Widget * getParent() const
Definition MyGUI_Widget.cpp:1275
MyGUI::Widget::setCoord
void setCoord(const IntCoord &_value) override
Definition MyGUI_Widget.cpp:686
MyGUI::Widget::getParentSize
IntSize getParentSize() const
Definition MyGUI_Widget.cpp:1019
MyGUI::Widget::getVisible
bool getVisible() const
Definition MyGUI_Widget.cpp:1250
MyGUI::utility::parseValue
T parseValue(const std::string &_value)
Definition MyGUI_StringUtility.h:112
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::M_PI
const float M_PI
Definition MyGUI_ControllerEdgeHide.cpp:20
MyGUI::types::TCoord< int >
MyGUI::types::TCoord::height
T height
Definition MyGUI_TCoord.h:25
MyGUI::types::TCoord::right
T right() const
Definition MyGUI_TCoord.h:150
MyGUI::types::TCoord::width
T width
Definition MyGUI_TCoord.h:24
MyGUI::types::TCoord::top
T top
Definition MyGUI_TCoord.h:23
MyGUI::types::TCoord::left
T left
Definition MyGUI_TCoord.h:22
MyGUI::types::TCoord::bottom
T bottom() const
Definition MyGUI_TCoord.h:155
MyGUI::types::TSize< int >
MyGUI::types::TSize::width
T width
Definition MyGUI_TSize.h:20
MyGUI::types::TSize::height
T height
Definition MyGUI_TSize.h:21