LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/controller - SlsAnimator.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 24 103 23.3 %
Date: 2014-11-03 Functions: 6 18 33.3 %
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           0 :     bool IsExpired (void) { return mbIsExpired;}
      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         126 : Animator::Animator (SlideSorter& rSlideSorter)
      66             :     : mrSlideSorter(rSlideSorter),
      67             :       maTimer(),
      68             :       mbIsDisposed(false),
      69             :       maAnimations(),
      70             :       maElapsedTime(),
      71             :       mpDrawLock(),
      72         126 :       mnNextAnimationId(0)
      73             : {
      74         126 :     maTimer.SetTimeout(gnResolution);
      75         126 :     maTimer.SetTimeoutHdl(LINK(this,Animator,TimeoutHandler));
      76         126 : }
      77             : 
      78         252 : Animator::~Animator (void)
      79             : {
      80         126 :     if ( ! mbIsDisposed)
      81             :     {
      82             :         OSL_ASSERT(mbIsDisposed);
      83           0 :         Dispose();
      84             :     }
      85         126 : }
      86             : 
      87         126 : void Animator::Dispose (void)
      88             : {
      89         126 :     mbIsDisposed = true;
      90             : 
      91         126 :     AnimationList aCopy (maAnimations);
      92         126 :     AnimationList::const_iterator iAnimation;
      93         126 :     for (iAnimation=aCopy.begin(); iAnimation!=aCopy.end(); ++iAnimation)
      94           0 :         (*iAnimation)->Expire();
      95             : 
      96         126 :     maTimer.Stop();
      97         126 :     if (mpDrawLock)
      98             :     {
      99           0 :         mpDrawLock->Dispose();
     100           0 :         mpDrawLock.reset();
     101         126 :     }
     102         126 : }
     103             : 
     104           0 : Animator::AnimationId Animator::AddAnimation (
     105             :     const AnimationFunctor& rAnimation,
     106             :     const sal_Int32 nStartOffset,
     107             :     const sal_Int32 nDuration,
     108             :     const FinishFunctor& rFinishFunctor)
     109             : {
     110             :     // When the animator is already disposed then ignore this call
     111             :     // silently (well, we show an assertion, but do not throw an exception.)
     112             :     OSL_ASSERT( ! mbIsDisposed);
     113           0 :     if (mbIsDisposed)
     114           0 :         return -1;
     115             : 
     116             :     boost::shared_ptr<Animation> pAnimation (
     117             :         new Animation(
     118             :             rAnimation,
     119             :             nStartOffset / 1000.0,
     120             :             nDuration / 1000.0,
     121           0 :             maElapsedTime.getElapsedTime(),
     122             :             ++mnNextAnimationId,
     123           0 :             rFinishFunctor));
     124           0 :     maAnimations.push_back(pAnimation);
     125             : 
     126           0 :     RequestNextFrame();
     127             : 
     128           0 :     return pAnimation->mnAnimationId;
     129             : }
     130             : 
     131           0 : void Animator::RemoveAnimation (const Animator::AnimationId nId)
     132             : {
     133             :     OSL_ASSERT( ! mbIsDisposed);
     134             : 
     135             :     const AnimationList::iterator iAnimation (::std::find_if(
     136             :         maAnimations.begin(),
     137             :         maAnimations.end(),
     138             :         ::boost::bind(
     139             :             ::std::equal_to<Animator::AnimationId>(),
     140             :             nId,
     141           0 :             ::boost::bind(&Animation::mnAnimationId, _1))));
     142           0 :     if (iAnimation != maAnimations.end())
     143             :     {
     144             :         OSL_ASSERT((*iAnimation)->mnAnimationId == nId);
     145           0 :         (*iAnimation)->Expire();
     146           0 :         maAnimations.erase(iAnimation);
     147             :     }
     148             : 
     149           0 :     if (maAnimations.empty())
     150             :     {
     151             :         // Reset the animation id when we can.
     152           0 :         mnNextAnimationId = 0;
     153             : 
     154             :         // No more animations => we do not have to suppress painting
     155             :         // anymore.
     156           0 :         mpDrawLock.reset();
     157             :     }
     158           0 : }
     159             : 
     160         126 : void Animator::RemoveAllAnimations (void)
     161             : {
     162             :     ::std::for_each(
     163             :         maAnimations.begin(),
     164             :         maAnimations.end(),
     165             :         ::boost::bind(
     166             :             &Animation::Expire,
     167         126 :             _1));
     168         126 :     maAnimations.clear();
     169         126 :     mnNextAnimationId = 0;
     170             : 
     171             :     // No more animations => we do not have to suppress painting
     172             :     // anymore.
     173         126 :     mpDrawLock.reset();
     174         126 : }
     175             : 
     176           0 : bool Animator::ProcessAnimations (const double nTime)
     177             : {
     178           0 :     bool bExpired (false);
     179             : 
     180             :     OSL_ASSERT( ! mbIsDisposed);
     181           0 :     if (mbIsDisposed)
     182           0 :         return bExpired;
     183             : 
     184           0 :     AnimationList aCopy (maAnimations);
     185           0 :     AnimationList::const_iterator iAnimation;
     186           0 :     for (iAnimation=aCopy.begin(); iAnimation!=aCopy.end(); ++iAnimation)
     187             :     {
     188           0 :         bExpired |= (*iAnimation)->Run(nTime);
     189             :     }
     190             : 
     191           0 :     return bExpired;
     192             : }
     193             : 
     194           0 : void Animator::CleanUpAnimationList (void)
     195             : {
     196             :     OSL_ASSERT( ! mbIsDisposed);
     197           0 :     if (mbIsDisposed)
     198           0 :         return;
     199             : 
     200           0 :     AnimationList aActiveAnimations;
     201             : 
     202           0 :     AnimationList::const_iterator iAnimation;
     203           0 :     for (iAnimation=maAnimations.begin(); iAnimation!=maAnimations.end(); ++iAnimation)
     204             :     {
     205           0 :         if ( ! (*iAnimation)->IsExpired())
     206           0 :             aActiveAnimations.push_back(*iAnimation);
     207             :     }
     208             : 
     209           0 :     maAnimations.swap(aActiveAnimations);
     210             : }
     211             : 
     212           0 : void Animator::RequestNextFrame (const double nFrameStart)
     213             : {
     214             :     (void)nFrameStart;
     215           0 :     if ( ! maTimer.IsActive())
     216             :     {
     217             :         // Prevent redraws except for the ones in TimeoutHandler.  While the
     218             :         // Animator is active it will schedule repaints regularly.  Repaints
     219             :         // in between would only lead to visual artifacts.
     220           0 :         mpDrawLock.reset(new view::SlideSorterView::DrawLock(mrSlideSorter));
     221           0 :         maTimer.Start();
     222             :     }
     223           0 : }
     224             : 
     225           0 : IMPL_LINK_NOARG(Animator, TimeoutHandler)
     226             : {
     227           0 :     if (mbIsDisposed)
     228           0 :         return 0;
     229             : 
     230           0 :     if (ProcessAnimations(maElapsedTime.getElapsedTime()))
     231           0 :         CleanUpAnimationList();
     232             : 
     233             :     // Unlock the draw lock.  This should lead to a repaint.
     234           0 :     mpDrawLock.reset();
     235             : 
     236           0 :     if (!maAnimations.empty())
     237           0 :         RequestNextFrame();
     238             : 
     239           0 :     return 0;
     240             : }
     241             : 
     242             : //===== Animator::Animation ===================================================
     243             : 
     244           0 : Animator::Animation::Animation (
     245             :     const Animator::AnimationFunctor& rAnimation,
     246             :     const double nStartOffset,
     247             :     const double nDuration,
     248             :     const double nGlobalTime,
     249             :     const Animator::AnimationId nId,
     250             :     const Animator::FinishFunctor& rFinishFunctor)
     251             :     : maAnimation(rAnimation),
     252             :       maFinishFunctor(rFinishFunctor),
     253             :       mnAnimationId(nId),
     254             :       mnDuration(nDuration),
     255           0 :       mnEnd(nGlobalTime + nDuration + nStartOffset),
     256           0 :       mnGlobalTimeAtStart(nGlobalTime + nStartOffset),
     257           0 :       mbIsExpired(false)
     258             : {
     259           0 :     Run(nGlobalTime);
     260           0 : }
     261             : 
     262           0 : Animator::Animation::~Animation (void)
     263             : {
     264           0 : }
     265             : 
     266           0 : bool Animator::Animation::Run (const double nGlobalTime)
     267             : {
     268           0 :     if ( ! mbIsExpired)
     269             :     {
     270           0 :         if (mnDuration > 0)
     271             :         {
     272           0 :             if (nGlobalTime >= mnEnd)
     273             :             {
     274           0 :                 maAnimation(1.0);
     275           0 :                 Expire();
     276             :             }
     277           0 :             else if (nGlobalTime >= mnGlobalTimeAtStart)
     278             :             {
     279           0 :                 maAnimation((nGlobalTime - mnGlobalTimeAtStart) / mnDuration);
     280             :             }
     281             :         }
     282           0 :         else if (mnDuration < 0)
     283             :         {
     284             :             // Animations without end have to be expired by their owner.
     285           0 :             maAnimation(nGlobalTime);
     286             :         }
     287             :     }
     288             : 
     289           0 :     return mbIsExpired;
     290             : }
     291             : 
     292           0 : void Animator::Animation::Expire (void)
     293             : {
     294           0 :     if ( ! mbIsExpired)
     295             :     {
     296           0 :         mbIsExpired = true;
     297           0 :         if (maFinishFunctor)
     298           0 :             maFinishFunctor();
     299             :     }
     300           0 : }
     301             : 
     302         114 : } } } // end of namespace ::sd::slidesorter::controller
     303             : 
     304             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10