MyGUI  3.2.1
MyGUI_ControllerEdgeHide.cpp
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #include "MyGUI_Precompiled.h"
00008 #include "MyGUI_ControllerEdgeHide.h"
00009 #include "MyGUI_Gui.h"
00010 #include "MyGUI_InputManager.h"
00011 #include "MyGUI_WidgetManager.h"
00012 #include "MyGUI_Widget.h"
00013 
00014 namespace MyGUI
00015 {
00016 
00017 #ifndef M_PI
00018     const float M_PI = 3.141593f;
00019 #endif
00020 
00021     ControllerEdgeHide::ControllerEdgeHide() :
00022         mTime(1.0),
00023         mRemainPixels(0),
00024         mShadowSize(0),
00025         mElapsedTime(0)
00026     {
00027     }
00028 
00029     ControllerEdgeHide::~ControllerEdgeHide()
00030     {
00031     }
00032 
00033     void ControllerEdgeHide::prepareItem(Widget* _widget)
00034     {
00035         recalculateTime(_widget);
00036         // вызываем пользовательский делегат для подготовки
00037         eventPreAction(_widget, this);
00038     }
00039 
00040     bool ControllerEdgeHide::addTime(Widget* _widget, float _time)
00041     {
00042         const IntSize& view_size = _widget->getParentSize();
00043         // do nothing if we have minimized window
00044         if (view_size.width <= 1 && view_size.height <= 1)
00045             return true;
00046 
00047         Widget* keyFocus = InputManager::getInstance().getKeyFocusWidget();
00048         Widget* mouseFocus = InputManager::getInstance().getMouseFocusWidget();
00049 
00050         while ((keyFocus != nullptr) && (_widget != keyFocus))
00051             keyFocus = keyFocus->getParent();
00052         while ((mouseFocus != nullptr) && (_widget != mouseFocus))
00053             mouseFocus = mouseFocus->getParent();
00054 
00055         // if our widget or its children have focus
00056         bool haveFocus = ((keyFocus != nullptr) || (mouseFocus != nullptr)) || (_widget->getVisible() == false);
00057 
00058         mElapsedTime += haveFocus ? -_time : _time;
00059 
00060         if (mElapsedTime >= mTime)
00061         {
00062             mElapsedTime = mTime;
00063         }
00064         if (mElapsedTime <= 0)
00065         {
00066             mElapsedTime = 0.0f;
00067             return true;
00068         }
00069 
00070         float k = sin(M_PI * mElapsedTime / mTime - M_PI / 2);
00071         if (k < 0) k = (-pow(-k, 0.7f) + 1) / 2;
00072         else k = (pow(k, 0.7f) + 1) / 2;
00073 
00074         MyGUI::IntCoord coord = _widget->getCoord();
00075         // if widget was moved
00076         if (coord != mLastCoord)
00077         {
00078             // if still moving - leave it alone
00079             if (haveFocus)
00080                 return true;
00081             else
00082                 recalculateTime(_widget);
00083         }
00084 
00085         bool nearBorder = false;
00086 
00087         if ((coord.left <= 0) && !(coord.right() >= view_size.width - 1))
00088         {
00089             coord.left = - int( float(coord.width - mRemainPixels - mShadowSize) * k);
00090             nearBorder = true;
00091         }
00092         if ((coord.top <= 0) && !(coord.bottom() >= view_size.height - 1))
00093         {
00094             coord.top = - int( float(coord.height - mRemainPixels - mShadowSize) * k);
00095             nearBorder = true;
00096         }
00097         if ((coord.right() >= view_size.width - 1) && !(coord.left <= 0))
00098         {
00099             coord.left = int(float(view_size.width - 1) - float(mRemainPixels) * k - float(coord.width) * (1.f - k));
00100             nearBorder = true;
00101         }
00102         if ((coord.bottom() >= view_size.height - 1) && !(coord.top <= 0))
00103         {
00104             coord.top = int(float(view_size.height - 1) - float(mRemainPixels) * k - float(coord.height) * (1.f - k));
00105             nearBorder = true;
00106         }
00107 
00108         if (nearBorder)
00109         {
00110             _widget->setCoord(coord);
00111         }
00112         else
00113         {
00114             mElapsedTime = 0;
00115         }
00116         mLastCoord = coord;
00117 
00118         eventUpdateAction(_widget, this);
00119 
00120         return true;
00121     }
00122 
00123     void ControllerEdgeHide::setProperty(const std::string& _key, const std::string& _value)
00124     {
00125         if (_key == "Time")
00126             setTime(utility::parseValue<float>(_value));
00127         else if (_key == "RemainPixels")
00128             setRemainPixels(utility::parseValue<int>(_value));
00129         else if (_key == "ShadowSize")
00130             setShadowSize(utility::parseValue<int>(_value));
00131     }
00132 
00133     void ControllerEdgeHide::recalculateTime(Widget* _widget)
00134     {
00135         float k = 0;
00136         const MyGUI::IntCoord& coord = _widget->getCoord();
00137         const MyGUI::IntSize& view_size = _widget->getParentSize();
00138 
00139         // check if widget is near any border and not near opposite borders at same time
00140         if ((coord.left <= 0) && !(coord.right() >= view_size.width - 1))
00141         {
00142             k = - (float) coord.left / (coord.width - mRemainPixels - mShadowSize);
00143         }
00144         else if ((coord.top <= 0) && !(coord.bottom() >= view_size.height - 1))
00145         {
00146             k = - (float)coord.top / (coord.height - mRemainPixels - mShadowSize);
00147         }
00148         else if ((coord.right() >= view_size.width - 1) && !(coord.left <= 0))
00149         {
00150             k = (float)(coord.right() - view_size.width + 1 ) / (coord.width - mRemainPixels);
00151         }
00152         else if ((coord.bottom() >= view_size.height - 1) && !(coord.top <= 0))
00153         {
00154             k = (float)(coord.bottom() - view_size.height + 1 ) / (coord.height - mRemainPixels);
00155         }
00156 
00157         //mElapsedTime = (asin(k)/M_PI + 1./2) * mTime;
00158         // this is reversed formula from ControllerEdgeHide::addTime k calculation
00159         if (k > 0.5f)
00160             mElapsedTime = (asin( pow( 2 * k - 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
00161         else
00162             mElapsedTime = (asin(-pow(-2 * k + 1, 1 / 0.7f)) / M_PI + 1.f / 2) * mTime;
00163     }
00164 
00165     void ControllerEdgeHide::setTime(float _value)
00166     {
00167         mTime = _value;
00168     }
00169 
00170     void ControllerEdgeHide::setRemainPixels(int _value)
00171     {
00172         mRemainPixels = _value;
00173     }
00174 
00175     void ControllerEdgeHide::setShadowSize(int _value)
00176     {
00177         mShadowSize = _value;
00178     }
00179 
00180 } // namespace MyGUI