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

Generated by: LCOV version 1.11