Files
libreoffice/drawinglayer/source/animation/animationtiming.cxx

379 lines
12 KiB
C++
Raw Normal View History

2006-05-12 10:54:47 +00:00
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: animationtiming.cxx,v $
*
* $Revision: 1.6 $
2006-05-12 10:54:47 +00:00
*
* last change: $Author: aw $ $Date: 2008-05-27 14:11:19 $
2006-05-12 10:54:47 +00:00
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_drawinglayer.hxx"
2006-05-12 10:54:47 +00:00
#include <drawinglayer/animation/animationtiming.hxx>
2006-08-09 15:51:16 +00:00
#include <basegfx/numeric/ftools.hxx>
2006-05-12 10:54:47 +00:00
//////////////////////////////////////////////////////////////////////////////
namespace drawinglayer
{
namespace animation
{
//////////////////////////////////////////////////////////////////////////////
2006-10-19 09:40:02 +00:00
AnimationEntry::AnimationEntry()
2006-05-12 10:54:47 +00:00
{
}
2006-10-19 09:40:02 +00:00
AnimationEntry::~AnimationEntry()
2006-05-12 10:54:47 +00:00
{
}
//////////////////////////////////////////////////////////////////////////////
2006-10-19 09:40:02 +00:00
AnimationEntryFixed::AnimationEntryFixed(double fDuration, double fState)
2006-05-12 10:54:47 +00:00
: mfDuration(fDuration),
mfState(fState)
{
}
2006-10-19 09:40:02 +00:00
AnimationEntryFixed::~AnimationEntryFixed()
2006-05-12 10:54:47 +00:00
{
}
2006-10-19 09:40:02 +00:00
AnimationEntry* AnimationEntryFixed::clone() const
2006-05-12 10:54:47 +00:00
{
2006-10-19 09:40:02 +00:00
return new AnimationEntryFixed(mfDuration, mfState);
2006-05-12 10:54:47 +00:00
}
2006-10-19 09:40:02 +00:00
bool AnimationEntryFixed::operator==(const AnimationEntry& rCandidate) const
2006-05-12 10:54:47 +00:00
{
2006-10-19 09:40:02 +00:00
const AnimationEntryFixed* pCompare = dynamic_cast< const AnimationEntryFixed* >(&rCandidate);
2006-05-12 10:54:47 +00:00
return (pCompare
2006-08-09 15:51:16 +00:00
&& basegfx::fTools::equal(mfDuration, pCompare->mfDuration)
&& basegfx::fTools::equal(mfState, pCompare->mfState));
2006-05-12 10:54:47 +00:00
}
2006-10-19 09:40:02 +00:00
double AnimationEntryFixed::getDuration() const
2006-05-12 10:54:47 +00:00
{
return mfDuration;
}
2006-10-19 09:40:02 +00:00
double AnimationEntryFixed::getStateAtTime(double /*fTime*/) const
2006-05-12 10:54:47 +00:00
{
return mfState;
}
2006-10-19 09:40:02 +00:00
double AnimationEntryFixed::getNextEventTime(double fTime) const
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
if(basegfx::fTools::less(fTime, mfDuration))
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
return mfDuration;
}
else
{
return 0.0;
2006-05-12 10:54:47 +00:00
}
}
//////////////////////////////////////////////////////////////////////////////
2006-10-19 09:40:02 +00:00
AnimationEntryLinear::AnimationEntryLinear(double fDuration, double fFrequency, double fStart, double fStop)
2006-05-12 10:54:47 +00:00
: mfDuration(fDuration),
mfFrequency(fFrequency),
mfStart(fStart),
mfStop(fStop)
{
}
2006-10-19 09:40:02 +00:00
AnimationEntryLinear::~AnimationEntryLinear()
2006-05-12 10:54:47 +00:00
{
}
2006-10-19 09:40:02 +00:00
AnimationEntry* AnimationEntryLinear::clone() const
2006-05-12 10:54:47 +00:00
{
2006-10-19 09:40:02 +00:00
return new AnimationEntryLinear(mfDuration, mfFrequency, mfStart, mfStop);
2006-05-12 10:54:47 +00:00
}
2006-10-19 09:40:02 +00:00
bool AnimationEntryLinear::operator==(const AnimationEntry& rCandidate) const
2006-05-12 10:54:47 +00:00
{
2006-10-19 09:40:02 +00:00
const AnimationEntryLinear* pCompare = dynamic_cast< const AnimationEntryLinear* >(&rCandidate);
2006-05-12 10:54:47 +00:00
return (pCompare
2006-08-09 15:51:16 +00:00
&& basegfx::fTools::equal(mfDuration, pCompare->mfDuration)
&& basegfx::fTools::equal(mfStart, pCompare->mfStart)
&& basegfx::fTools::equal(mfStop, pCompare->mfStop));
2006-05-12 10:54:47 +00:00
}
2006-10-19 09:40:02 +00:00
double AnimationEntryLinear::getDuration() const
2006-05-12 10:54:47 +00:00
{
return mfDuration;
}
2006-10-19 09:40:02 +00:00
double AnimationEntryLinear::getStateAtTime(double fTime) const
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
if(basegfx::fTools::more(mfDuration, 0.0))
2006-05-12 10:54:47 +00:00
{
const double fFactor(fTime / mfDuration);
2006-08-09 15:51:16 +00:00
if(fFactor > 1.0)
{
return mfStop;
}
else
{
return mfStart + ((mfStop - mfStart) * fFactor);
}
2006-05-12 10:54:47 +00:00
}
else
{
return mfStart;
}
}
2006-10-19 09:40:02 +00:00
double AnimationEntryLinear::getNextEventTime(double fTime) const
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
if(basegfx::fTools::less(fTime, mfDuration))
2006-05-12 10:54:47 +00:00
{
// use the simple solution: just add the frequency. More correct (but also more
// complicated) would be to calculate the slice of time we are in and when this
// slice will end. For the animations, this makes no quality difference.
fTime += mfFrequency;
2006-08-09 15:51:16 +00:00
if(basegfx::fTools::more(fTime, mfDuration))
2006-05-12 10:54:47 +00:00
{
fTime = mfDuration;
}
2006-08-09 15:51:16 +00:00
return fTime;
}
else
{
return 0.0;
}
2006-05-12 10:54:47 +00:00
}
//////////////////////////////////////////////////////////////////////////////
2006-10-19 09:40:02 +00:00
sal_uInt32 AnimationEntryList::impGetIndexAtTime(double fTime, double &rfAddedTime) const
2006-05-12 10:54:47 +00:00
{
sal_uInt32 nIndex(0L);
2006-08-09 15:51:16 +00:00
while(nIndex < maEntries.size() && basegfx::fTools::lessOrEqual(rfAddedTime + maEntries[nIndex]->getDuration(), fTime))
2006-05-12 10:54:47 +00:00
{
rfAddedTime += maEntries[nIndex++]->getDuration();
}
return nIndex;
}
2006-10-19 09:40:02 +00:00
AnimationEntryList::AnimationEntryList()
2006-05-12 10:54:47 +00:00
: mfDuration(0.0)
{
}
2006-10-19 09:40:02 +00:00
AnimationEntryList::~AnimationEntryList()
2006-05-12 10:54:47 +00:00
{
for(sal_uInt32 a(0L); a < maEntries.size(); a++)
{
delete maEntries[a];
}
}
2006-10-19 09:40:02 +00:00
AnimationEntry* AnimationEntryList::clone() const
2006-05-12 10:54:47 +00:00
{
2006-10-19 09:40:02 +00:00
AnimationEntryList* pNew = new AnimationEntryList();
2006-05-12 10:54:47 +00:00
for(sal_uInt32 a(0L); a < maEntries.size(); a++)
{
pNew->append(*maEntries[a]);
}
return pNew;
}
2006-10-19 09:40:02 +00:00
bool AnimationEntryList::operator==(const AnimationEntry& rCandidate) const
2006-05-12 10:54:47 +00:00
{
2006-10-19 09:40:02 +00:00
const AnimationEntryList* pCompare = dynamic_cast< const AnimationEntryList* >(&rCandidate);
2006-05-12 10:54:47 +00:00
if(pCompare && mfDuration == pCompare->mfDuration)
{
for(sal_uInt32 a(0L); a < maEntries.size(); a++)
{
if(!(*maEntries[a] == *pCompare->maEntries[a]))
{
return false;
}
}
return true;
}
return false;
}
2006-10-19 09:40:02 +00:00
void AnimationEntryList::append(const AnimationEntry& rCandidate)
2006-05-12 10:54:47 +00:00
{
const double fDuration(rCandidate.getDuration());
2006-08-09 15:51:16 +00:00
if(!basegfx::fTools::equalZero(fDuration))
2006-05-12 10:54:47 +00:00
{
maEntries.push_back(rCandidate.clone());
mfDuration += fDuration;
}
}
2006-10-19 09:40:02 +00:00
double AnimationEntryList::getDuration() const
2006-05-12 10:54:47 +00:00
{
return mfDuration;
}
2006-10-19 09:40:02 +00:00
double AnimationEntryList::getStateAtTime(double fTime) const
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
if(!basegfx::fTools::equalZero(mfDuration))
2006-05-12 10:54:47 +00:00
{
double fAddedTime(0.0);
const sal_uInt32 nIndex(impGetIndexAtTime(fTime, fAddedTime));
if(nIndex < maEntries.size())
{
return maEntries[nIndex]->getStateAtTime(fTime - fAddedTime);
}
}
return 0.0;
}
2006-10-19 09:40:02 +00:00
double AnimationEntryList::getNextEventTime(double fTime) const
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
double fNewTime(0.0);
if(!basegfx::fTools::equalZero(mfDuration))
2006-05-12 10:54:47 +00:00
{
double fAddedTime(0.0);
const sal_uInt32 nIndex(impGetIndexAtTime(fTime, fAddedTime));
if(nIndex < maEntries.size())
{
2006-08-09 15:51:16 +00:00
fNewTime = maEntries[nIndex]->getNextEventTime(fTime - fAddedTime) + fAddedTime;
2006-05-12 10:54:47 +00:00
}
}
2006-08-09 15:51:16 +00:00
return fNewTime;
2006-05-12 10:54:47 +00:00
}
//////////////////////////////////////////////////////////////////////////////
2006-10-19 09:40:02 +00:00
AnimationEntryLoop::AnimationEntryLoop(sal_uInt32 nRepeat)
: AnimationEntryList(),
2006-05-12 10:54:47 +00:00
mnRepeat(nRepeat)
{
}
2006-10-19 09:40:02 +00:00
AnimationEntryLoop::~AnimationEntryLoop()
2006-05-12 10:54:47 +00:00
{
}
2006-10-19 09:40:02 +00:00
AnimationEntry* AnimationEntryLoop::clone() const
2006-05-12 10:54:47 +00:00
{
2006-10-19 09:40:02 +00:00
AnimationEntryLoop* pNew = new AnimationEntryLoop(mnRepeat);
2006-05-12 10:54:47 +00:00
for(sal_uInt32 a(0L); a < maEntries.size(); a++)
{
pNew->append(*maEntries[a]);
}
return pNew;
}
2006-10-19 09:40:02 +00:00
bool AnimationEntryLoop::operator==(const AnimationEntry& rCandidate) const
2006-05-12 10:54:47 +00:00
{
2006-10-19 09:40:02 +00:00
const AnimationEntryLoop* pCompare = dynamic_cast< const AnimationEntryLoop* >(&rCandidate);
2006-05-12 10:54:47 +00:00
return (pCompare
2006-08-09 15:51:16 +00:00
&& mnRepeat == pCompare->mnRepeat
2006-10-19 09:40:02 +00:00
&& AnimationEntryList::operator==(rCandidate));
2006-05-12 10:54:47 +00:00
}
2006-10-19 09:40:02 +00:00
double AnimationEntryLoop::getDuration() const
2006-05-12 10:54:47 +00:00
{
return (mfDuration * (double)mnRepeat);
}
2006-10-19 09:40:02 +00:00
double AnimationEntryLoop::getStateAtTime(double fTime) const
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
if(mnRepeat && !basegfx::fTools::equalZero(mfDuration))
2006-05-12 10:54:47 +00:00
{
const sal_uInt32 nCurrentLoop((sal_uInt32)(fTime / mfDuration));
if(nCurrentLoop > mnRepeat)
{
return 1.0;
}
else
{
const double fTimeAtLoopStart((double)nCurrentLoop * mfDuration);
const double fRelativeTime(fTime - fTimeAtLoopStart);
2006-10-19 09:40:02 +00:00
return AnimationEntryList::getStateAtTime(fRelativeTime);
2006-05-12 10:54:47 +00:00
}
}
return 0.0;
}
2006-10-19 09:40:02 +00:00
double AnimationEntryLoop::getNextEventTime(double fTime) const
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
double fNewTime(0.0);
if(mnRepeat && !basegfx::fTools::equalZero(mfDuration))
2006-05-12 10:54:47 +00:00
{
const sal_uInt32 nCurrentLoop((sal_uInt32)(fTime / mfDuration));
if(nCurrentLoop <= mnRepeat)
{
const double fTimeAtLoopStart((double)nCurrentLoop * mfDuration);
const double fRelativeTime(fTime - fTimeAtLoopStart);
2006-10-19 09:40:02 +00:00
const double fNextEventAtLoop(AnimationEntryList::getNextEventTime(fRelativeTime));
2006-05-12 10:54:47 +00:00
2006-08-09 15:51:16 +00:00
if(!basegfx::fTools::equalZero(fNextEventAtLoop))
2006-05-12 10:54:47 +00:00
{
2006-08-09 15:51:16 +00:00
fNewTime = fNextEventAtLoop + fTimeAtLoopStart;
2006-05-12 10:54:47 +00:00
}
}
}
2006-08-09 15:51:16 +00:00
return fNewTime;
2006-05-12 10:54:47 +00:00
}
} // end of namespace animation
} // end of namespace drawinglayer
//////////////////////////////////////////////////////////////////////////////
// eof