MyGUI  3.2.1
MyGUI_ControllerPosition.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_ControllerPosition.h"
00009 #include "MyGUI_Gui.h"
00010 #include "MyGUI_InputManager.h"
00011 #include "MyGUI_WidgetManager.h"
00012 #include "MyGUI_Widget.h"
00013 #include "MyGUI_ActionController.h"
00014 
00015 namespace MyGUI
00016 {
00017 
00018     ControllerPosition::ControllerPosition() :
00019         mTime(1),
00020         mElapsedTime(0),
00021         mCalcPosition(false),
00022         mCalcSize(false)
00023     {
00024     }
00025 
00026     ControllerPosition::~ControllerPosition()
00027     {
00028     }
00029 
00030     void ControllerPosition::setCoord(const IntCoord& _destCoord)
00031     {
00032         mDestCoord = _destCoord;
00033         mCalcPosition = true;
00034         mCalcSize = true;
00035     }
00036 
00037     void ControllerPosition::setSize(const IntSize& _destSize)
00038     {
00039         mDestCoord.width = _destSize.width;
00040         mDestCoord.height = _destSize.height;
00041         mCalcPosition = false;
00042         mCalcSize = true;
00043     }
00044 
00045     void ControllerPosition::setPosition(const IntPoint& _destPoint)
00046     {
00047         mDestCoord.left = _destPoint.left;
00048         mDestCoord.top = _destPoint.top;
00049         mCalcPosition = true;
00050         mCalcSize = false;
00051     }
00052 
00053     void ControllerPosition::prepareItem(Widget* _widget)
00054     {
00055         MYGUI_DEBUG_ASSERT(mTime > 0, "Time must be > 0");
00056 
00057         mStartCoord = _widget->getCoord();
00058 
00059         // вызываем пользовательский делегат для подготовки
00060         eventPreAction(_widget, this);
00061     }
00062 
00063     bool ControllerPosition::addTime(Widget* _widget, float _time)
00064     {
00065         mElapsedTime += _time;
00066 
00067         if (mElapsedTime < mTime)
00068         {
00069             IntCoord coord;
00070             eventFrameAction(mStartCoord, mDestCoord, coord, mElapsedTime / mTime);
00071             if (mCalcPosition)
00072             {
00073                 if (mCalcSize) _widget->setCoord(coord);
00074                 else _widget->setPosition(coord.point());
00075             }
00076             else if (mCalcSize) _widget->setSize(coord.size());
00077 
00078             // вызываем пользовательский делегат обновления
00079             eventUpdateAction(_widget, this);
00080 
00081             return true;
00082         }
00083 
00084         // поставить точно в конец
00085         IntCoord coord;
00086         eventFrameAction(mStartCoord, mDestCoord, coord, 1.0f);
00087         if (mCalcPosition)
00088         {
00089             if (mCalcSize) _widget->setCoord(coord);
00090             else _widget->setPosition(coord.point());
00091         }
00092         else if (mCalcSize) _widget->setSize(coord.size());
00093 
00094         // вызываем пользовательский делегат обновления
00095         eventUpdateAction(_widget, this);
00096 
00097         // вызываем пользовательский делегат пост обработки
00098         eventPostAction(_widget, this);
00099 
00100         return false;
00101     }
00102 
00103     void ControllerPosition::setProperty(const std::string& _key, const std::string& _value)
00104     {
00105         if (_key == "Time")
00106             setTime(utility::parseValue<float>(_value));
00107         else if (_key == "Coord")
00108             setCoord(utility::parseValue<IntCoord>(_value));
00109         else if (_key == "Size")
00110             setSize(utility::parseValue<IntSize>(_value));
00111         else if (_key == "Position")
00112             setPosition(utility::parseValue<IntPoint>(_value));
00113         else if (_key == "Function")
00114             setFunction(_value);
00115     }
00116 
00117     void ControllerPosition::setFunction(const std::string& _value)
00118     {
00119         if (_value == "Inertional")
00120             setAction(MyGUI::newDelegate(action::inertionalMoveFunction));
00121         else if (_value == "Accelerated")
00122             setAction(MyGUI::newDelegate(action::acceleratedMoveFunction<30>));
00123         else if (_value == "Slowed")
00124             setAction(MyGUI::newDelegate(action::acceleratedMoveFunction<4>));
00125         else if (_value == "Jump")
00126             setAction(MyGUI::newDelegate(action::jumpMoveFunction<5>));
00127     }
00128 
00129     void ControllerPosition::setTime(float _value)
00130     {
00131         mTime = _value;
00132     }
00133 
00134     void ControllerPosition::setAction(FrameAction::IDelegate* _value)
00135     {
00136         eventFrameAction = _value;
00137     }
00138 
00139 } // namespace MyGUI