LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/controller - SlsAnimator.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 104 0.0 %
Date: 2014-04-14 Functions: 0 18 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "controller/SlsAnimator.hxx"
      21             : #include "view/SlideSorterView.hxx"
      22             : #include "View.hxx"
      23             : #include <boost/bind.hpp>
      24             : 
      25             : namespace sd { namespace slidesorter { namespace controller {
      26             : 
      27             : namespace {
      28             :     static const sal_Int32 gnResolution = 25;
      29             : }
      30             : /** Handle one animation function by using a timer for frequent calls to
      31             :     the animations operator().
      32             : */
      33             : class Animator::Animation
      34             : {
      35             : public:
      36             :     Animation (
      37             :         const Animator::AnimationFunctor& rAnimation,
      38             :         const double nStartOffset,
      39             :         const double nDuration,
      40             :         const double nGlobalTime,
      41             :         const Animator::AnimationId nAnimationId,
      42             :         const Animator::FinishFunctor& rFinishFunctor);
      43             :     ~Animation (void);
      44             :     /** Run next animation step.  If animation has reached its end it is
      45             :         expired.
      46             :     */
      47             :     bool Run (const double nGlobalTime);
      48             : 
      49             :     /** Typically called when an animation has finished, but also from
      50             :         Animator::Disposed().  The finish functor is called and the
      51             :         animation is marked as expired to prevent another run.
      52             :     */
      53             :     void Expire (void);
      54             :     bool IsExpired (void);
      55             : 
      56             :     Animator::AnimationFunctor maAnimation;
      57             :     Animator::FinishFunctor maFinishFunctor;
      58             :     const Animator::AnimationId mnAnimationId;
      59             :     const double mnDuration;
      60             :     const double mnEnd;
      61             :     const double mnGlobalTimeAtStart;
      62             :     bool mbIsExpired;
      63             : };
      64             : 
      65             : 
      66             : 
      67             : 
      68           0 : Animator::Animator (SlideSorter& rSlideSorter)
      69             :     : mrSlideSorter(rSlideSorter),
      70             :       maTimer(),
      71             :       mbIsDisposed(false),
      72             :       maAnimations(),
      73             :       maElapsedTime(),
      74             :       mpDrawLock(),
      75           0 :       mnNextAnimationId(0)
      76             : {
      77           0 :     maTimer.SetTimeout(gnResolution);
      78           0 :     maTimer.SetTimeoutHdl(LINK(this,Animator,TimeoutHandler));
      79           0 : }
      80             : 
      81             : 
      82             : 
      83             : 
      84           0 : Animator::~Animator (void)
      85             : {
      86           0 :     if ( ! mbIsDisposed)
      87             :     {
      88             :         OSL_ASSERT(mbIsDisposed);
      89           0 :         Dispose();
      90             :     }
      91           0 : }
      92             : 
      93             : 
      94             : 
      95             : 
      96           0 : void Animator::Dispose (void)
      97             : {
      98           0 :     mbIsDisposed = true;
      99             : 
     100           0 :     AnimationList aCopy (maAnimations);
     101           0 :     AnimationList::const_iterator iAnimation;
     102           0 :     for (iAnimation=aCopy.begin(); iAnimation!=aCopy.end(); ++iAnimation)
     103           0 :         (*iAnimation)->Expire();
     104             : 
     105           0 :     maTimer.Stop();
     106           0 :     if (mpDrawLock)
     107             :     {
     108           0 :         mpDrawLock->Dispose();
     109           0 :         mpDrawLock.reset();
     110           0 :     }
     111           0 : }
     112             : 
     113             : 
     114             : 
     115             : 
     116           0 : Animator::AnimationId Animator::AddAnimation (
     117             :     const AnimationFunctor& rAnimation,
     118             :     const sal_Int32 nStartOffset,
     119             :     const sal_Int32 nDuration,
     120             :     const FinishFunctor& rFinishFunctor)
     121             : {
     122             :     // When the animator is already disposed then ignore this call
     123             :     // silently (well, we show an assertion, but do not throw an exception.)
     124             :     OSL_ASSERT( ! mbIsDisposed);
     125           0 :     if (mbIsDisposed)
     126           0 :         return -1;
     127             : 
     128             :     boost::shared_ptr<Animation> pAnimation (
     129             :         new Animation(
     130             :             rAnimation,
     131             :             nStartOffset / 1000.0,
     132             :             nDuration / 1000.0,
     133           0 :             maElapsedTime.getElapsedTime(),
     134             :             ++mnNextAnimationId,
     135           0 :             rFinishFunctor));
     136           0 :     maAnimations.push_back(pAnimation);
     137             : 
     138           0 :     RequestNextFrame();
     139             : 
     140           0 :     return pAnimation->mnAnimationId;
     141             : }
     142             : 
     143           0 : void Animator::RemoveAnimation (const Animator::AnimationId nId)
     144             : {
     145             :     OSL_ASSERT( ! mbIsDisposed);
     146             : 
     147             :     const AnimationList::iterator iAnimation (::std::find_if(
     148             :         maAnimations.begin(),
     149             :         maAnimations.end(),
     150             :         ::boost::bind(
     151             :             ::std::equal_to<Animator::AnimationId>(),
     152             :             nId,
     153           0 :             ::boost::bind(&Animation::mnAnimationId, _1))));
     154           0 :     if (iAnimation != maAnimations.end())
     155             :     {
     156             :         OSL_ASSERT((*iAnimation)->mnAnimationId == nId);
     157           0 :         (*iAnimation)->Expire();
     158           0 :         maAnimations.erase(iAnimation);
     159             :     }
     160             : 
     161           0 :     if (maAnimations.empty())
     162             :     {
     163             :         // Reset the animation id when we can.
     164           0 :         mnNextAnimationId = 0;
     165             : 
     166             :         // No more animations => we do not have to suppress painting
     167             :         // anymore.
     168           0 :         mpDrawLock.reset();
     169             :     }
     170           0 : }
     171             : 
     172             : 
     173             : 
     174             : 
     175           0 : void Animator::RemoveAllAnimations (void)
     176             : {
     177             :     ::std::for_each(
     178             :         maAnimations.begin(),
     179             :         maAnimations.end(),
     180             :         ::boost::bind(
     181             :             &Animation::Expire,
     182           0 :             _1));
     183           0 :     maAnimations.clear();
     184           0 :     mnNextAnimationId = 0;
     185             : 
     186             :     // No more animations => we do not have to suppress painting
     187             :     // anymore.
     188           0 :     mpDrawLock.reset();
     189           0 : }
     190             : 
     191             : 
     192             : 
     193             : 
     194           0 : bool Animator::ProcessAnimations (const double nTime)
     195             : {
     196           0 :     bool bExpired (false);
     197             : 
     198             :     OSL_ASSERT( ! mbIsDisposed);
     199           0 :     if (mbIsDisposed)
     200           0 :         return bExpired;
     201             : 
     202             : 
     203           0 :     AnimationList aCopy (maAnimations);
     204           0 :     AnimationList::const_iterator iAnimation;
     205           0 :     for (iAnimation=aCopy.begin(); iAnimation!=aCopy.end(); ++iAnimation)
     206             :     {
     207           0 :         bExpired |= (*iAnimation)->Run(nTime);
     208             :     }
     209             : 
     210           0 :     return bExpired;
     211             : }
     212             : 
     213             : 
     214             : 
     215             : 
     216           0 : void Animator::CleanUpAnimationList (void)
     217             : {
     218             :     OSL_ASSERT( ! mbIsDisposed);
     219           0 :     if (mbIsDisposed)
     220           0 :         return;
     221             : 
     222           0 :     AnimationList aActiveAnimations;
     223             : 
     224           0 :     AnimationList::const_iterator iAnimation;
     225           0 :     for (iAnimation=maAnimations.begin(); iAnimation!=maAnimations.end(); ++iAnimation)
     226             :     {
     227           0 :         if ( ! (*iAnimation)->IsExpired())
     228           0 :             aActiveAnimations.push_back(*iAnimation);
     229             :     }
     230             : 
     231           0 :     maAnimations.swap(aActiveAnimations);
     232             : }
     233             : 
     234             : 
     235             : 
     236             : 
     237           0 : void Animator::RequestNextFrame (const double nFrameStart)
     238             : {
     239             :     (void)nFrameStart;
     240           0 :     if ( ! maTimer.IsActive())
     241             :     {
     242             :         // Prevent redraws except for the ones in TimeoutHandler.  While the
     243             :         // Animator is active it will schedule repaints regularly.  Repaints
     244             :         // in between would only lead to visual artifacts.
     245           0 :         mpDrawLock.reset(new view::SlideSorterView::DrawLock(mrSlideSorter));
     246           0 :         maTimer.Start();
     247             :     }
     248           0 : }
     249             : 
     250             : 
     251             : 
     252             : 
     253           0 : IMPL_LINK_NOARG(Animator, TimeoutHandler)
     254             : {
     255           0 :     if (mbIsDisposed)
     256           0 :         return 0;
     257             : 
     258           0 :     if (ProcessAnimations(maElapsedTime.getElapsedTime()))
     259           0 :         CleanUpAnimationList();
     260             : 
     261             :     // Unlock the draw lock.  This should lead to a repaint.
     262           0 :     mpDrawLock.reset();
     263             : 
     264           0 :     if (!maAnimations.empty())
     265           0 :         RequestNextFrame();
     266             : 
     267           0 :     return 0;
     268             : }
     269             : 
     270             : 
     271             : 
     272             : 
     273             : //===== Animator::Animation ===================================================
     274             : 
     275           0 : Animator::Animation::Animation (
     276             :     const Animator::AnimationFunctor& rAnimation,
     277             :     const double nStartOffset,
     278             :     const double nDuration,
     279             :     const double nGlobalTime,
     280             :     const Animator::AnimationId nId,
     281             :     const Animator::FinishFunctor& rFinishFunctor)
     282             :     : maAnimation(rAnimation),
     283             :       maFinishFunctor(rFinishFunctor),
     284             :       mnAnimationId(nId),
     285             :       mnDuration(nDuration),
     286           0 :       mnEnd(nGlobalTime + nDuration + nStartOffset),
     287           0 :       mnGlobalTimeAtStart(nGlobalTime + nStartOffset),
     288           0 :       mbIsExpired(false)
     289             : {
     290           0 :     Run(nGlobalTime);
     291           0 : }
     292             : 
     293             : 
     294             : 
     295             : 
     296           0 : Animator::Animation::~Animation (void)
     297             : {
     298           0 : }
     299             : 
     300             : 
     301             : 
     302             : 
     303           0 : bool Animator::Animation::Run (const double nGlobalTime)
     304             : {
     305           0 :     if ( ! mbIsExpired)
     306             :     {
     307           0 :         if (mnDuration > 0)
     308             :         {
     309           0 :             if (nGlobalTime >= mnEnd)
     310             :             {
     311           0 :                 maAnimation(1.0);
     312           0 :                 Expire();
     313             :             }
     314           0 :             else if (nGlobalTime >= mnGlobalTimeAtStart)
     315             :             {
     316           0 :                 maAnimation((nGlobalTime - mnGlobalTimeAtStart) / mnDuration);
     317             :             }
     318             :         }
     319           0 :         else if (mnDuration < 0)
     320             :         {
     321             :             // Animations without end have to be expired by their owner.
     322           0 :             maAnimation(nGlobalTime);
     323             :         }
     324             :     }
     325             : 
     326           0 :     return mbIsExpired;
     327             : }
     328             : 
     329             : 
     330             : 
     331             : 
     332           0 : void Animator::Animation::Expire (void)
     333             : {
     334           0 :     if ( ! mbIsExpired)
     335             :     {
     336           0 :         mbIsExpired = true;
     337           0 :         if (maFinishFunctor)
     338           0 :             maFinishFunctor();
     339             :     }
     340           0 : }
     341             : 
     342             : 
     343             : 
     344             : 
     345           0 : bool Animator::Animation::IsExpired (void)
     346             : {
     347           0 :     return mbIsExpired;
     348             : }
     349             : 
     350             : 
     351             : 
     352             : 
     353           0 : } } } // end of namespace ::sd::slidesorter::controller
     354             : 
     355             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10