MyGUI: MyGUI_ControllerEdgeHide.cpp Source File

MyGUI 3.4.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"
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 void ControllerEdgeHide::prepareItem(Widget* _widget)
23 {
24 recalculateTime(_widget);
25 // вызываем пользовательский делегат для подготовки
26 eventPreAction(_widget, this);
27 }
28
29 bool ControllerEdgeHide::addTime(Widget* _widget, float _time)
30 {
31 const IntSize& view_size = _widget->getParentSize();
32 // do nothing if we have minimized window
33 if (view_size.width <= 1 && view_size.height <= 1)
34 return true;
35
36 Widget* keyFocus = InputManager::getInstance().getKeyFocusWidget();
37 Widget* mouseFocus = InputManager::getInstance().getMouseFocusWidget();
38
39 while ((keyFocus != nullptr) && (_widget != keyFocus))
40 keyFocus = keyFocus->getParent();
41 while ((mouseFocus != nullptr) && (_widget != mouseFocus))
42 mouseFocus = mouseFocus->getParent();
43
44 // if our widget or its children have focus
45 bool haveFocus = ((keyFocus != nullptr) || (mouseFocus != nullptr)) || (!_widget->getVisible());
46
47 mElapsedTime += haveFocus ? -_time : _time;
48
49 if (mElapsedTime >= mTime)
50 {
51 mElapsedTime = mTime;
52 }
53 if (mElapsedTime <= 0)
54 {
55 mElapsedTime = 0.0f;
56 return true;
57 }
58
59 float k = std::sin(M_PI * mElapsedTime / mTime - M_PI / 2);
60 if (k < 0)
61 k = (-std::pow(-k, 0.7f) + 1) / 2;
62 else
63 k = (std::pow(k, 0.7f) + 1) / 2;
64
65 MyGUI::IntCoord coord = _widget->getCoord();
66 // if widget was moved
67 if (coord != mLastCoord)
68 {
69 // if still moving - leave it alone
70 if (haveFocus)
71 return true;
72 recalculateTime(_widget);
73 }
74
75 bool nearBorder = false;
76
77 bool behindLeft = coord.left <= 0;
78 bool behindRight = coord.right() >= view_size.width - 1;
79 bool behindTop = coord.top <= 0;
80 bool behindBottom = coord.bottom() >= view_size.height - 1;
81 if (behindLeft && !behindRight)
82 {
83 coord.left = -int(float(coord.width - mRemainPixels - mShadowSize) * k);
84 nearBorder = true;
85 }
86 if (behindTop && !behindBottom)
87 {
88 coord.top = -int(float(coord.height - mRemainPixels - mShadowSize) * k);
89 nearBorder = true;
90 }
91 if (behindRight && !behindLeft)
92 {
93 coord.left = int(float(view_size.width - 1) - float(mRemainPixels) * k - float(coord.width) * (1.f - k));
94 nearBorder = true;
95 }
96 if (behindBottom && !behindTop)
97 {
98 coord.top = int(float(view_size.height - 1) - float(mRemainPixels) * k - float(coord.height) * (1.f - k));
99 nearBorder = true;
100 }
101
102 if (nearBorder)
103 {
104 _widget->setCoord(coord);
105 }
106 else
107 {
108 mElapsedTime = 0;
109 }
110 mLastCoord = coord;
111
112 eventUpdateAction(_widget, this);
113
114 return true;
115 }
116
117 void ControllerEdgeHide::setProperty(std::string_view _key, std::string_view _value)
118 {
119 if (_key == "Time")
120 setTime(utility::parseValue<float>(_value));
121 else if (_key == "RemainPixels")
122 setRemainPixels(utility::parseValue<int>(_value));
123 else if (_key == "ShadowSize")
124 setShadowSize(utility::parseValue<int>(_value));
125 }
126
127 void ControllerEdgeHide::recalculateTime(Widget* _widget)
128 {
129 float k = 0;
130 const MyGUI::IntCoord& coord = _widget->getCoord();
131 const MyGUI::IntSize& view_size = _widget->getParentSize();
132
133 bool behindLeft = coord.left <= 0;
134 bool behindRight = coord.right() >= view_size.width - 1;
135 bool behindTop = coord.top <= 0;
136 bool behindBottom = coord.bottom() >= view_size.height - 1;
137 // check if widget is near any border and not near opposite borders at same time
138 if (behindLeft && !behindRight)
139 {
140 k = -(float)coord.left / (coord.width - mRemainPixels - mShadowSize);
141 }
142 else if (behindTop && !behindBottom)
143 {
144 k = -(float)coord.top / (coord.height - mRemainPixels - mShadowSize);
145 }
146 else if (behindRight && !behindLeft)
147 {
148 k = (float)(coord.right() - view_size.width + 1) / (coord.width - mRemainPixels);
149 }
150 else if (behindBottom && !behindTop)
151 {
152 k = (float)(coord.bottom() - view_size.height + 1) / (coord.height - mRemainPixels);
153 }
154
155 //mElapsedTime = (asin(k)/M_PI + 1./2) * mTime;
156 // this is reversed formula from ControllerEdgeHide::addTime k calculation
157 if (k > 0.5f)
158 mElapsedTime = (std::asin(std::pow(2 * k - 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
159 else
160 mElapsedTime = (std::asin(-std::pow(-2 * k + 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
161 }
162
163 void ControllerEdgeHide::setTime(float _value)
164 {
165 mTime = _value;
166 }
167
168 void ControllerEdgeHide::setRemainPixels(int _value)
169 {
170 mRemainPixels = _value;
171 }
172
173 void ControllerEdgeHide::setShadowSize(int _value)
174 {
175 mShadowSize = _value;
176 }
177
178} // 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:168
MyGUI::ControllerEdgeHide::setShadowSize
void setShadowSize(int _value)
Definition MyGUI_ControllerEdgeHide.cpp:173
MyGUI::ControllerEdgeHide::setTime
void setTime(float _value)
Definition MyGUI_ControllerEdgeHide.cpp:163
MyGUI::ControllerEdgeHide::addTime
bool addTime(Widget *_widget, float _time) override
Definition MyGUI_ControllerEdgeHide.cpp:29
MyGUI::ControllerEdgeHide::prepareItem
void prepareItem(Widget *_widget) override
Definition MyGUI_ControllerEdgeHide.cpp:22
MyGUI::ControllerEdgeHide::setProperty
void setProperty(std::string_view _key, std::string_view _value) override
Definition MyGUI_ControllerEdgeHide.cpp:117
MyGUI::ControllerItem::eventPreAction
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventPreAction
Definition MyGUI_ControllerItem.h:41
MyGUI::ControllerItem::eventUpdateAction
EventPairAddParameter< EventHandle_WidgetPtr, EventHandle_WidgetPtrControllerItemPtr > eventUpdateAction
Definition MyGUI_ControllerItem.h:47
MyGUI::ICroppedRectangle::getCoord
const IntCoord & getCoord() const
Definition MyGUI_ICroppedRectangle.h:56
MyGUI::InputManager::getKeyFocusWidget
Widget * getKeyFocusWidget() const
Definition MyGUI_InputManager.cpp:643
MyGUI::InputManager::getMouseFocusWidget
Widget * getMouseFocusWidget() const
Definition MyGUI_InputManager.cpp:638
MyGUI::InputManager::getInstance
static InputManager & getInstance()
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:40
MyGUI::Widget::getParent
Widget * getParent() const
Definition MyGUI_Widget.cpp:1352
MyGUI::Widget::getParentSize
IntSize getParentSize() const
Definition MyGUI_Widget.cpp:1082
MyGUI::Widget::getVisible
bool getVisible() const
Definition MyGUI_Widget.cpp:1327
MyGUI::Widget::setCoord
void setCoord(const IntCoord &_coord) override
Definition MyGUI_Widget.cpp:730
MyGUI::utility::parseValue
T parseValue(std::string_view _value)
Definition MyGUI_StringUtility.h:60
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::M_PI
const float M_PI
Definition MyGUI_ControllerEdgeHide.cpp:20
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
MyGUI::IntSize
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
MyGUI::types::TCoord::height
T height
Definition MyGUI_TCoord.h:23
MyGUI::types::TCoord::right
T right() const
Definition MyGUI_TCoord.h:125
MyGUI::types::TCoord::width
T width
Definition MyGUI_TCoord.h:22
MyGUI::types::TCoord::top
T top
Definition MyGUI_TCoord.h:21
MyGUI::types::TCoord::left
T left
Definition MyGUI_TCoord.h:20
MyGUI::types::TCoord::bottom
T bottom() const
Definition MyGUI_TCoord.h:130
MyGUI::types::TSize::width
T width
Definition MyGUI_TSize.h:19
MyGUI::types::TSize::height
T height
Definition MyGUI_TSize.h:20