MyGUI 3.5.3
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
14namespace MyGUI
15{
16
17#ifdef M_PI
18 #undef M_PI
19#endif
20 const float M_PI = 3.141593f;
21
22 void ControllerEdgeHide::prepareItem(Widget* _widget)
23 {
24 recalculateTime(_widget);
25 eventPreAction(_widget, this);
26 }
27
28 bool ControllerEdgeHide::addTime(Widget* _widget, float _time)
29 {
30 const IntSize& view_size = _widget->getParentSize();
31 // do nothing if we have minimized window
32 if (view_size.width <= 1 && view_size.height <= 1)
33 return true;
34
35 Widget* keyFocus = InputManager::getInstance().getKeyFocusWidget();
36 Widget* mouseFocus = InputManager::getInstance().getMouseFocusWidget();
37
38 while ((keyFocus != nullptr) && (_widget != keyFocus))
39 keyFocus = keyFocus->getParent();
40 while ((mouseFocus != nullptr) && (_widget != mouseFocus))
41 mouseFocus = mouseFocus->getParent();
42
43 // if our widget or its children have focus
44 bool haveFocus = ((keyFocus != nullptr) || (mouseFocus != nullptr)) || (!_widget->getVisible());
45
46 mElapsedTime += haveFocus ? -_time : _time;
47
48 if (mElapsedTime >= mTime)
49 {
50 mElapsedTime = mTime;
51 }
52 if (mElapsedTime <= 0)
53 {
54 mElapsedTime = 0.0f;
55 return true;
56 }
57
58 float k = std::sin(M_PI * mElapsedTime / mTime - M_PI / 2);
59 if (k < 0)
60 k = (-std::pow(-k, 0.7f) + 1) / 2;
61 else
62 k = (std::pow(k, 0.7f) + 1) / 2;
63
64 MyGUI::IntCoord coord = _widget->getCoord();
65 // if widget was moved
66 if (coord != mLastCoord)
67 {
68 // if still moving - leave it alone
69 if (haveFocus)
70 return true;
71 recalculateTime(_widget);
72 }
73
74 bool nearBorder = false;
75
76 bool behindLeft = coord.left <= 0;
77 bool behindRight = coord.right() >= view_size.width - 1;
78 bool behindTop = coord.top <= 0;
79 bool behindBottom = coord.bottom() >= view_size.height - 1;
80 if (behindLeft && !behindRight)
81 {
82 coord.left = -int(float(coord.width - mRemainPixels - mShadowSize) * k);
83 nearBorder = true;
84 }
85 if (behindTop && !behindBottom)
86 {
87 coord.top = -int(float(coord.height - mRemainPixels - mShadowSize) * k);
88 nearBorder = true;
89 }
90 if (behindRight && !behindLeft)
91 {
92 coord.left = int(float(view_size.width - 1) - float(mRemainPixels) * k - float(coord.width) * (1.f - k));
93 nearBorder = true;
94 }
95 if (behindBottom && !behindTop)
96 {
97 coord.top = int(float(view_size.height - 1) - float(mRemainPixels) * k - float(coord.height) * (1.f - k));
98 nearBorder = true;
99 }
100
101 if (nearBorder)
102 {
103 _widget->setCoord(coord);
104 }
105 else
106 {
107 mElapsedTime = 0;
108 }
109 mLastCoord = coord;
110
111 eventUpdateAction(_widget, this);
112
113 return true;
114 }
115
116 void ControllerEdgeHide::setProperty(std::string_view _key, std::string_view _value)
117 {
118 if (_key == "Time")
119 setTime(utility::parseValue<float>(_value));
120 else if (_key == "RemainPixels")
121 setRemainPixels(utility::parseValue<int>(_value));
122 else if (_key == "ShadowSize")
123 setShadowSize(utility::parseValue<int>(_value));
124 }
125
126 void ControllerEdgeHide::recalculateTime(Widget* _widget)
127 {
128 float k = 0;
129 const MyGUI::IntCoord& coord = _widget->getCoord();
130 const MyGUI::IntSize& view_size = _widget->getParentSize();
131
132 bool behindLeft = coord.left <= 0;
133 bool behindRight = coord.right() >= view_size.width - 1;
134 bool behindTop = coord.top <= 0;
135 bool behindBottom = coord.bottom() >= view_size.height - 1;
136 // check if widget is near any border and not near opposite borders at same time
137 if (behindLeft && !behindRight)
138 {
139 k = -(float)coord.left / (coord.width - mRemainPixels - mShadowSize);
140 }
141 else if (behindTop && !behindBottom)
142 {
143 k = -(float)coord.top / (coord.height - mRemainPixels - mShadowSize);
144 }
145 else if (behindRight && !behindLeft)
146 {
147 k = (float)(coord.right() - view_size.width + 1) / (coord.width - mRemainPixels);
148 }
149 else if (behindBottom && !behindTop)
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
T parseValue(std::string_view value)
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:35
types::TSize< int > IntSize
Definition MyGUI_Types.h:29