Back to index

unity  6.0.0
unity-mt-grab-handle-group.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Sam Spilsbury <sam.spilsbury@canonical.com>
00017  */
00018 
00019 #include "unity-mt-grab-handle-group.h"
00020 
00021 unsigned int unity::MT::FADE_MSEC = 0;
00022 
00023 void
00024 unity::MT::GrabHandleGroup::show(unsigned int handles)
00025 {
00026   for(const unity::MT::GrabHandle::Ptr & handle : mHandles)
00027     if (handles & handle->id ())
00028       handle->show();
00029 
00030   mState = State::FADE_IN;
00031 }
00032 
00033 void
00034 unity::MT::GrabHandleGroup::hide()
00035 {
00036   for(const unity::MT::GrabHandle::Ptr & handle : mHandles)
00037     handle->hide();
00038 
00039   mState = State::FADE_OUT;
00040 }
00041 
00042 void
00043 unity::MT::GrabHandleGroup::raiseHandle(const boost::shared_ptr <const unity::MT::GrabHandle> &h)
00044 {
00045   mOwner->raiseGrabHandle (h);
00046 }
00047 
00048 bool
00049 unity::MT::GrabHandleGroup::animate(unsigned int msec)
00050 {
00051   mMoreAnimate = false;
00052 
00053   switch (mState)
00054   {
00055     case State::FADE_IN:
00056 
00057       mOpacity += ((float) msec / (float) unity::MT::FADE_MSEC) * std::numeric_limits <unsigned short>::max ();
00058 
00059       if (mOpacity >= std::numeric_limits <unsigned short>::max ())
00060       {
00061         mOpacity = std::numeric_limits <unsigned short>::max ();
00062         mState = State::NONE;
00063       }
00064       break;
00065     case State::FADE_OUT:
00066       mOpacity -= ((float) msec / (float) unity::MT::FADE_MSEC) * std::numeric_limits <unsigned short>::max ();
00067 
00068       if (mOpacity <= 0)
00069       {
00070         mOpacity = 0;
00071         mState = State::NONE;
00072       }
00073       break;
00074     default:
00075       break;
00076   }
00077 
00078   mMoreAnimate = mState != State::NONE;
00079 
00080   return mMoreAnimate;
00081 }
00082 
00083 int
00084 unity::MT::GrabHandleGroup::opacity()
00085 {
00086   return mOpacity;
00087 }
00088 
00089 bool
00090 unity::MT::GrabHandleGroup::visible()
00091 {
00092   return mOpacity > 0.0f;
00093 }
00094 
00095 bool
00096 unity::MT::GrabHandleGroup::needsAnimate()
00097 {
00098   return mMoreAnimate;
00099 }
00100 
00101 void
00102 unity::MT::GrabHandleGroup::relayout(const nux::Geometry& rect, bool hard)
00103 {
00104   /* Each grab handle at each vertex, eg:
00105    *
00106    * 1 - topleft
00107    * 2 - top
00108    * 3 - topright
00109    * 4 - right
00110    * 5 - bottom-right
00111    * 6 - bottom
00112    * 7 - bottom-left
00113    * 8 - left
00114    */
00115 
00116   const float pos[9][2] =
00117   {
00118     {0.0f, 0.0f}, {0.5f, 0.0f}, {1.0f, 0.0f},
00119     {1.0f, 0.5f}, {1.0f, 1.0f},
00120     {0.5f, 1.0f}, {0.0f, 1.0f}, {0.0f, 0.5f},
00121     {0.5f, 0.5f} /* middle */
00122   };
00123 
00124   for (unsigned int i = 0; i < NUM_HANDLES; i++)
00125   {
00126     unity::MT::GrabHandle::Ptr & handle = mHandles.at(i);
00127 
00128     handle->reposition (rect.x + rect.width * pos[i][0] -
00129                         handle->width () / 2,
00130                         rect.y + rect.height * pos[i][1] -
00131                         handle->height () / 2,
00132                         unity::MT::PositionSet | (hard ? unity::MT::PositionLock : 0));
00133   }
00134 }
00135 
00136 unity::MT::GrabHandleGroup::GrabHandleGroup(GrabHandleWindow *owner,
00137                                             std::vector <unity::MT::TextureSize>  &textures) :
00138   mState(State::NONE),
00139   mOpacity(0.0f),
00140   mMoreAnimate(false),
00141   mOwner(owner)
00142 {
00143 }
00144 
00145 unity::MT::GrabHandleGroup::Ptr
00146 unity::MT::GrabHandleGroup::create (GrabHandleWindow *owner,
00147                                     std::vector<unity::MT::TextureSize> &textures)
00148 {
00149     unity::MT::GrabHandleGroup::Ptr p = unity::MT::GrabHandleGroup::Ptr (new unity::MT::GrabHandleGroup (owner, textures));
00150     for (unsigned int i = 0; i < NUM_HANDLES; i++)
00151       p->mHandles.push_back(unity::MT::GrabHandle::create (textures.at(i).first,
00152                                                            textures.at(i).second.width,
00153                                                            textures.at(i).second.height,
00154                                                            p,
00155                                                            handlesMask.find (i)->second));
00156     return p;
00157 }
00158 
00159 unity::MT::GrabHandleGroup::~GrabHandleGroup()
00160 {
00161   for (unity::MT::GrabHandle::Ptr & handle : mHandles)
00162     handle->damage (nux::Geometry (handle->x (),
00163                                    handle->y (),
00164                                    handle->width (),
00165                                    handle->height ()));
00166 }
00167 
00168 void
00169 unity::MT::GrabHandleGroup::requestMovement (int x,
00170                                              int y,
00171                                         unsigned int direction,
00172                                         unsigned int button)
00173 {
00174   mOwner->requestMovement (x, y, direction, button);
00175 }
00176 
00177 std::vector <unity::MT::TextureLayout>
00178 unity::MT::GrabHandleGroup::layout(unsigned int handles)
00179 {
00180   std::vector <unity::MT::TextureLayout> layout;
00181 
00182   for(const unity::MT::GrabHandle::Ptr & handle : mHandles)
00183     if (handle->id () & handles)
00184       layout.push_back (handle->layout ());
00185 
00186   return layout;
00187 }
00188 
00189 void
00190 unity::MT::GrabHandleGroup::forEachHandle (const std::function <void (const unity::MT::GrabHandle::Ptr &)> &f)
00191 {
00192   for (unity::MT::GrabHandle::Ptr &h : mHandles)
00193     f (h);
00194 }