2007-06-08 07:24:19 +02:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2007 Aaron Seigo <aseigo@kde.org>
|
2007-07-06 18:26:48 +02:00
|
|
|
* 2007 Alexis Ménard <darktears31@gmail.com>
|
2007-06-08 07:24:19 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 21:06:18 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
2007-06-08 07:24:19 +02:00
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "phase.h"
|
|
|
|
|
|
|
|
#include <QGraphicsItem>
|
|
|
|
|
|
|
|
#include <KConfig>
|
|
|
|
#include <KConfigGroup>
|
|
|
|
#include <KService>
|
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
|
|
|
|
#include "animator.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2007-10-12 22:26:07 +02:00
|
|
|
static const qreal MIN_TICK_RATE = 40;
|
2007-06-25 08:01:34 +02:00
|
|
|
|
2007-06-08 07:24:19 +02:00
|
|
|
struct AnimationState
|
|
|
|
{
|
|
|
|
QGraphicsItem* item;
|
|
|
|
Phase::Animation animation;
|
2007-06-25 08:01:34 +02:00
|
|
|
Phase::CurveShape curve;
|
|
|
|
int interval;
|
|
|
|
int currentInterval;
|
2007-06-19 23:56:03 +02:00
|
|
|
int frames;
|
2007-06-25 08:01:34 +02:00
|
|
|
int currentFrame;
|
2007-06-19 23:56:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ElementAnimationState
|
|
|
|
{
|
|
|
|
QGraphicsItem* item;
|
2007-06-25 08:01:34 +02:00
|
|
|
Phase::CurveShape curve;
|
2007-06-19 23:56:03 +02:00
|
|
|
Phase::ElementAnimation animation;
|
2007-06-25 08:01:34 +02:00
|
|
|
int interval;
|
|
|
|
int currentInterval;
|
2007-06-19 23:56:03 +02:00
|
|
|
int frames;
|
|
|
|
int currentFrame;
|
|
|
|
int id;
|
|
|
|
QPixmap pixmap;
|
2007-06-08 07:24:19 +02:00
|
|
|
};
|
|
|
|
|
2007-07-04 11:36:50 +02:00
|
|
|
struct MovementState
|
|
|
|
{
|
|
|
|
QGraphicsItem* item;
|
|
|
|
Phase::CurveShape curve;
|
|
|
|
Phase::Movement movement;
|
|
|
|
int interval;
|
|
|
|
int currentInterval;
|
|
|
|
int frames;
|
|
|
|
int currentFrame;
|
2007-10-11 22:19:51 +02:00
|
|
|
QPoint start;
|
2007-07-04 11:36:50 +02:00
|
|
|
QPoint destination;
|
|
|
|
};
|
|
|
|
|
2007-10-12 09:25:24 +02:00
|
|
|
struct CustomAnimationState
|
|
|
|
{
|
|
|
|
Phase::CurveShape curve;
|
|
|
|
int frames;
|
|
|
|
int currentFrame;
|
|
|
|
int interval;
|
|
|
|
int currentInterval;
|
|
|
|
Phase::AnimId id;
|
|
|
|
QObject* receiver;
|
|
|
|
char* slot;
|
|
|
|
};
|
|
|
|
|
2007-06-08 07:24:19 +02:00
|
|
|
class Phase::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Private()
|
2007-06-19 23:56:03 +02:00
|
|
|
: animator(0),
|
2007-06-25 08:01:34 +02:00
|
|
|
animId(0),
|
|
|
|
timerId(0)
|
2007-06-08 07:24:19 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Private()
|
|
|
|
{
|
2007-06-25 08:01:34 +02:00
|
|
|
qDeleteAll(animatedItems);
|
|
|
|
qDeleteAll(animatedElements);
|
2007-10-12 09:25:24 +02:00
|
|
|
qDeleteAll(movingItems);
|
|
|
|
|
|
|
|
QMutableMapIterator<AnimId, CustomAnimationState*> it(customAnims);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
delete it.value()->slot;
|
|
|
|
delete it.value();
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
// Animator is a QObject
|
|
|
|
// and we don't own the items
|
|
|
|
}
|
|
|
|
|
2007-10-12 09:25:24 +02:00
|
|
|
qreal calculateProgress(int frames, int currentFrame)
|
|
|
|
{
|
|
|
|
qreal progress = frames;
|
|
|
|
progress = currentFrame / progress;
|
|
|
|
progress = qMin(1.0, qMax(0.0, progress));
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
void performAnimation(qreal amount, const AnimationState* state)
|
|
|
|
{
|
|
|
|
switch (state->animation) {
|
|
|
|
case Phase::Appear:
|
|
|
|
animator->appear(amount, state->item);
|
|
|
|
break;
|
|
|
|
case Phase::Disappear:
|
|
|
|
animator->disappear(amount, state->item);
|
2007-07-27 18:34:01 +02:00
|
|
|
if (amount >= 1) {
|
|
|
|
state->item->hide();
|
|
|
|
}
|
2007-06-25 08:01:34 +02:00
|
|
|
break;
|
|
|
|
case Phase::Activate:
|
|
|
|
animator->activate(amount, state->item);
|
|
|
|
break;
|
|
|
|
case Phase::FrameAppear:
|
|
|
|
animator->frameAppear(amount, state->item, QRegion()); //FIXME: what -is- the frame region?
|
|
|
|
break;
|
|
|
|
}
|
2007-06-08 07:24:19 +02:00
|
|
|
}
|
2007-07-27 18:34:01 +02:00
|
|
|
|
|
|
|
void performMovement(qreal amount, const MovementState* state)
|
2007-07-06 18:26:48 +02:00
|
|
|
{
|
|
|
|
switch (state->movement) {
|
|
|
|
case Phase::SlideIn:
|
2007-10-12 09:25:24 +02:00
|
|
|
//kDebug() << "performMovement, SlideIn";
|
2007-10-11 22:19:51 +02:00
|
|
|
animator->slideIn(amount, state->item, state->start, state->destination);
|
2007-07-06 18:26:48 +02:00
|
|
|
break;
|
|
|
|
case Phase::SlideOut:
|
2007-10-12 09:25:24 +02:00
|
|
|
//kDebug() << "performMovement, SlideOut";
|
2007-10-11 22:19:51 +02:00
|
|
|
animator->slideOut(amount, state->item, state->start, state->destination);
|
2007-07-06 18:26:48 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-06-08 07:24:19 +02:00
|
|
|
Animator* animator;
|
2007-06-19 23:56:03 +02:00
|
|
|
int animId;
|
2007-06-25 08:01:34 +02:00
|
|
|
int timerId;
|
|
|
|
QTime time;
|
2007-06-08 07:24:19 +02:00
|
|
|
|
|
|
|
//TODO: eventually perhaps we should allow multiple animations simulataneously
|
|
|
|
// which would imply changing this to a QMap<QGraphicsItem*, QList<QTimeLine*> >
|
|
|
|
// and really making the code fun ;)
|
2007-06-25 08:01:34 +02:00
|
|
|
QMap<QGraphicsItem*, AnimationState*> animatedItems;
|
2007-07-04 11:36:50 +02:00
|
|
|
QMap<QGraphicsItem*, MovementState*> movingItems;
|
2007-06-25 08:01:34 +02:00
|
|
|
QMap<Phase::AnimId, ElementAnimationState*> animatedElements;
|
2007-10-12 09:25:24 +02:00
|
|
|
QMap<AnimId, CustomAnimationState*> customAnims;
|
2007-06-08 07:24:19 +02:00
|
|
|
};
|
|
|
|
|
2007-06-17 22:05:43 +02:00
|
|
|
class PhaseSingleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Phase self;
|
|
|
|
};
|
|
|
|
|
|
|
|
K_GLOBAL_STATIC( PhaseSingleton, privateSelf )
|
|
|
|
|
|
|
|
Phase* Phase::self()
|
|
|
|
{
|
|
|
|
return &privateSelf->self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-08 07:24:19 +02:00
|
|
|
Phase::Phase(QObject * parent)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Phase::~Phase()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Phase::appletDestroyed(QObject* o)
|
|
|
|
{
|
|
|
|
QGraphicsItem* item = dynamic_cast<QGraphicsItem*>(o);
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
QMap<QGraphicsItem*, AnimationState*>::iterator it = d->animatedItems.find(item);
|
|
|
|
if (it != d->animatedItems.end()) {
|
|
|
|
delete it.value();
|
|
|
|
d->animatedItems.erase(it);
|
2007-07-04 11:36:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMap<QGraphicsItem*, MovementState*>::iterator it2 = d->movingItems.find(item);
|
|
|
|
if (it2 != d->movingItems.end()) {
|
|
|
|
delete it2.value();
|
|
|
|
d->movingItems.erase(it2);
|
2007-06-08 07:24:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-12 09:25:24 +02:00
|
|
|
void Phase::customAnimReceiverDestroyed(QObject* o)
|
|
|
|
{
|
|
|
|
QMutableMapIterator<AnimId, CustomAnimationState*> it(d->customAnims);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
if (it.next().value()->receiver == o) {
|
|
|
|
delete it.value()->slot;
|
|
|
|
delete it.value();
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
void Phase::animateItem(QGraphicsItem* item, Animation animation)
|
2007-06-08 07:24:19 +02:00
|
|
|
{
|
2007-08-20 23:33:48 +02:00
|
|
|
//kDebug();
|
2007-06-25 08:01:34 +02:00
|
|
|
// get rid of any existing animations on this item.
|
|
|
|
//TODO: shoudl we allow multiple anims per item?
|
|
|
|
QMap<QGraphicsItem*, AnimationState*>::iterator it = d->animatedItems.find(item);
|
|
|
|
if (it != d->animatedItems.end()) {
|
2007-06-08 07:24:19 +02:00
|
|
|
delete it.value();
|
2007-06-25 08:01:34 +02:00
|
|
|
d->animatedItems.erase(it);
|
2007-06-08 07:24:19 +02:00
|
|
|
}
|
|
|
|
|
2007-06-24 23:36:00 +02:00
|
|
|
int frames = d->animator->framesPerSecond(animation);
|
2007-06-08 07:24:19 +02:00
|
|
|
|
|
|
|
if (frames < 1) {
|
2007-06-25 08:01:34 +02:00
|
|
|
// evidently this animator doesn't have an implementation
|
|
|
|
// for this Animation
|
2007-06-08 07:24:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
AnimationState* state = new AnimationState;
|
|
|
|
state->item = item;
|
|
|
|
state->animation = animation;
|
|
|
|
state->curve = d->animator->curve(animation);
|
2007-06-19 23:56:03 +02:00
|
|
|
//TODO: variance in times based on the value of animation
|
2007-06-25 08:01:34 +02:00
|
|
|
state->frames = frames / 3;
|
|
|
|
state->currentFrame = 0;
|
|
|
|
state->interval = 333 / state->frames;
|
2007-10-12 22:26:07 +02:00
|
|
|
state->interval = (state->interval / MIN_TICK_RATE) * MIN_TICK_RATE;
|
2007-06-25 08:01:34 +02:00
|
|
|
state->currentInterval = state->interval;
|
|
|
|
|
|
|
|
d->animatedItems[item] = state;
|
|
|
|
d->performAnimation(0, state);
|
|
|
|
|
|
|
|
if (!d->timerId) {
|
2007-10-12 22:26:07 +02:00
|
|
|
d->timerId = startTimer(MIN_TICK_RATE);
|
2007-06-25 08:01:34 +02:00
|
|
|
d->time.restart();
|
2007-06-08 07:24:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 10:06:27 +02:00
|
|
|
void Phase::moveItem(QGraphicsItem* item, Movement movement, const QPoint &destination)
|
2007-07-04 11:36:50 +02:00
|
|
|
{
|
2007-10-11 22:19:51 +02:00
|
|
|
kDebug();
|
2007-07-06 18:26:48 +02:00
|
|
|
QMap<QGraphicsItem*, MovementState*>::iterator it = d->movingItems.find(item);
|
|
|
|
if (it != d->movingItems.end()) {
|
|
|
|
delete it.value();
|
|
|
|
d->movingItems.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
int frames = d->animator->framesPerSecond(movement);
|
|
|
|
if (frames < 1) {
|
|
|
|
// evidently this animator doesn't have an implementation
|
|
|
|
// for this Animation
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MovementState* state = new MovementState;
|
2007-10-11 22:19:51 +02:00
|
|
|
state->destination = destination;
|
|
|
|
state->start = item->pos().toPoint();
|
2007-07-06 18:26:48 +02:00
|
|
|
state->item = item;
|
|
|
|
state->movement = movement;
|
|
|
|
state->curve = d->animator->curve(movement);
|
|
|
|
//TODO: variance in times based on the value of animation
|
|
|
|
state->frames = frames / 2;
|
|
|
|
state->currentFrame = 0;
|
2007-10-12 01:20:20 +02:00
|
|
|
state->interval = 250 / state->frames;
|
2007-10-12 22:26:07 +02:00
|
|
|
state->interval = (state->interval / MIN_TICK_RATE) * MIN_TICK_RATE;
|
2007-07-06 18:26:48 +02:00
|
|
|
state->currentInterval = state->interval;
|
|
|
|
|
|
|
|
d->movingItems[item] = state;
|
|
|
|
d->performMovement(0, state);
|
|
|
|
|
|
|
|
if (!d->timerId) {
|
2007-10-12 22:26:07 +02:00
|
|
|
d->timerId = startTimer(MIN_TICK_RATE);
|
2007-07-06 18:26:48 +02:00
|
|
|
d->time.restart();
|
|
|
|
}
|
2007-07-04 11:36:50 +02:00
|
|
|
}
|
|
|
|
|
2007-10-12 09:25:24 +02:00
|
|
|
Phase::AnimId Phase::customAnimation(int frames, int duration, Phase::CurveShape curve,
|
|
|
|
QObject* receiver, const char* slot)
|
|
|
|
{
|
|
|
|
if (frames < 1 || duration < 1 || !receiver || !slot) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomAnimationState *state = new CustomAnimationState;
|
|
|
|
state->id = ++d->animId;
|
|
|
|
state->frames = frames;
|
|
|
|
state->currentFrame = 0;
|
|
|
|
state->curve = curve;
|
|
|
|
state->interval = duration / qreal(state->frames);
|
2007-10-12 22:26:07 +02:00
|
|
|
state->interval = (state->interval / MIN_TICK_RATE) * MIN_TICK_RATE;
|
2007-10-12 09:25:24 +02:00
|
|
|
state->currentInterval = state->interval;
|
|
|
|
state->receiver = receiver;
|
|
|
|
state->slot = qstrdup(slot);
|
|
|
|
|
|
|
|
d->customAnims[state->id] = state;
|
|
|
|
|
2007-10-12 22:26:07 +02:00
|
|
|
connect(receiver, SIGNAL(destroyed(QObject*)),
|
2007-10-12 09:25:24 +02:00
|
|
|
this, SLOT(customAnimReceiverDestroyed(QObject*)));
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(receiver, slot, Q_ARG(qreal, 0));
|
|
|
|
|
|
|
|
if (!d->timerId) {
|
2007-10-12 22:26:07 +02:00
|
|
|
d->timerId = startTimer(MIN_TICK_RATE);
|
2007-10-12 09:25:24 +02:00
|
|
|
d->time.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
return state->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Phase::stopCustomAnimation(AnimId id)
|
|
|
|
{
|
|
|
|
QMap<AnimId, CustomAnimationState*>::iterator it = d->customAnims.find(id);
|
|
|
|
if (it != d->customAnims.end()) {
|
|
|
|
delete it.value()->slot;
|
|
|
|
delete it.value();
|
|
|
|
d->customAnims.erase(it);
|
|
|
|
}
|
|
|
|
//kDebug() << "stopCustomAnimation(AnimId " << id << ") done";
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
Phase::AnimId Phase::animateElement(QGraphicsItem *item, ElementAnimation animation)
|
2007-06-19 23:56:03 +02:00
|
|
|
{
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "startElementAnimation(AnimId " << animation << ")";
|
2007-06-25 08:01:34 +02:00
|
|
|
ElementAnimationState *state = new ElementAnimationState;
|
|
|
|
state->item = item;
|
|
|
|
state->curve = d->animator->curve(animation);
|
|
|
|
state->animation = animation;
|
2007-06-19 23:56:03 +02:00
|
|
|
//TODO: variance in times based on the value of animation
|
2007-06-25 08:01:34 +02:00
|
|
|
state->frames = d->animator->framesPerSecond(animation) / 5;
|
|
|
|
state->currentFrame = 0;
|
|
|
|
state->interval = 200 / state->frames;
|
2007-10-12 22:26:07 +02:00
|
|
|
state->interval = (state->interval / MIN_TICK_RATE) * MIN_TICK_RATE;
|
2007-06-25 08:01:34 +02:00
|
|
|
state->currentInterval = state->interval;
|
|
|
|
state->id = ++d->animId;
|
|
|
|
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "animateElement " << animation << ", interval: " << state->interval << ", frames: " << state->frames;
|
2007-06-25 08:01:34 +02:00
|
|
|
bool needTimer = true;
|
|
|
|
if (state->frames < 1) {
|
|
|
|
state->frames = 1;
|
|
|
|
state->currentFrame = 1;
|
|
|
|
needTimer = false;
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
d->animatedElements[state->id] = state;
|
|
|
|
state->item->update();
|
2007-06-19 23:56:03 +02:00
|
|
|
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "startElementAnimation(AnimId " << animation << ") returning " << state->id;
|
2007-06-25 08:01:34 +02:00
|
|
|
if (needTimer && !d->timerId) {
|
|
|
|
// start a 20fps timer;
|
|
|
|
//TODO: should be started at the maximum frame rate needed only?
|
2007-10-12 22:26:07 +02:00
|
|
|
d->timerId = startTimer(MIN_TICK_RATE);
|
2007-06-25 08:01:34 +02:00
|
|
|
d->time.restart();
|
|
|
|
}
|
|
|
|
return state->id;
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Phase::stopElementAnimation(AnimId id)
|
|
|
|
{
|
2007-06-25 08:01:34 +02:00
|
|
|
QMap<AnimId, ElementAnimationState*>::iterator it = d->animatedElements.find(id);
|
|
|
|
if (it != d->animatedElements.end()) {
|
|
|
|
delete it.value();
|
|
|
|
d->animatedElements.erase(it);
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "stopElementAnimation(AnimId " << id << ") done";
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Phase::setAnimationPixmap(AnimId id, const QPixmap &pixmap)
|
|
|
|
{
|
2007-06-25 08:01:34 +02:00
|
|
|
QMap<AnimId, ElementAnimationState*>::iterator it = d->animatedElements.find(id);
|
2007-06-19 23:56:03 +02:00
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
if (it == d->animatedElements.end()) {
|
2007-08-02 15:12:59 +02:00
|
|
|
kDebug() << "Phase::setAnimationPixmap(" << id << ") found no entry for it!";
|
2007-06-19 23:56:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
it.value()->pixmap = pixmap;
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap Phase::animationResult(AnimId id)
|
|
|
|
{
|
2007-06-25 08:01:34 +02:00
|
|
|
QMap<AnimId, ElementAnimationState*>::const_iterator it = d->animatedElements.find(id);
|
2007-06-19 23:56:03 +02:00
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
if (it == d->animatedElements.constEnd()) {
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "Phase::animationResult(" << id << ") found no entry for it!";
|
2007-06-19 23:56:03 +02:00
|
|
|
return QPixmap();
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
ElementAnimationState* state = it.value();
|
|
|
|
qreal progress = state->frames;
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "Phase::animationResult(" << id << " at " << progress;
|
2007-06-25 08:01:34 +02:00
|
|
|
progress = state->currentFrame / progress;
|
|
|
|
progress = qMin(1.0, qMax(0.0, progress));
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "Phase::animationResult(" << id << " at " << progress;
|
2007-06-19 23:56:03 +02:00
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
switch (state->animation) {
|
2007-06-19 23:56:03 +02:00
|
|
|
case ElementAppear:
|
2007-06-25 08:01:34 +02:00
|
|
|
return d->animator->elementAppear(progress, state->pixmap);
|
2007-06-19 23:56:03 +02:00
|
|
|
break;
|
|
|
|
case ElementDisappear:
|
2007-06-25 08:01:34 +02:00
|
|
|
return d->animator->elementDisappear(progress, state->pixmap);
|
2007-06-19 23:56:03 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
return state->pixmap;
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
void Phase::timerEvent(QTimerEvent *event)
|
2007-06-19 23:56:03 +02:00
|
|
|
{
|
2007-06-25 08:01:34 +02:00
|
|
|
Q_UNUSED(event)
|
|
|
|
bool animationsRemain = false;
|
2007-10-12 22:26:07 +02:00
|
|
|
int elapsed = MIN_TICK_RATE;
|
2007-06-25 08:01:34 +02:00
|
|
|
if (d->time.elapsed() > elapsed) {
|
|
|
|
elapsed = d->time.elapsed();
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
2007-06-25 08:01:34 +02:00
|
|
|
d->time.restart();
|
2007-08-02 15:12:59 +02:00
|
|
|
//kDebug() << "timeEvent, elapsed time: " << elapsed;
|
2007-06-25 08:01:34 +02:00
|
|
|
|
|
|
|
foreach (AnimationState* state, d->animatedItems) {
|
|
|
|
if (state->currentInterval <= elapsed) {
|
|
|
|
// we need to step forward!
|
|
|
|
state->currentFrame += qMax(1, elapsed / state->interval);
|
|
|
|
|
|
|
|
if (state->currentFrame < state->frames) {
|
2007-10-12 09:25:24 +02:00
|
|
|
qreal progress = d->calculateProgress(state->frames, state->currentFrame);
|
2007-07-06 18:26:48 +02:00
|
|
|
d->performAnimation(progress, state);
|
2007-06-25 08:01:34 +02:00
|
|
|
state->currentInterval = state->interval;
|
|
|
|
//TODO: calculate a proper interval based on the curve
|
|
|
|
state->interval *= 1 - progress;
|
|
|
|
animationsRemain = true;
|
|
|
|
} else {
|
|
|
|
d->performAnimation(1, state);
|
|
|
|
d->animatedItems.erase(d->animatedItems.find(state->item));
|
2007-07-27 18:34:01 +02:00
|
|
|
emit animationComplete(state->item, state->animation);
|
2007-06-25 08:01:34 +02:00
|
|
|
delete state;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state->currentInterval -= elapsed;
|
|
|
|
animationsRemain = true;
|
|
|
|
}
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
|
|
|
|
2007-07-04 11:36:50 +02:00
|
|
|
foreach (MovementState* state, d->movingItems) {
|
2007-07-06 18:26:48 +02:00
|
|
|
if (state->currentInterval <= elapsed) {
|
|
|
|
// we need to step forward!
|
|
|
|
state->currentFrame += qMax(1, elapsed / state->interval);
|
|
|
|
|
|
|
|
if (state->currentFrame < state->frames) {
|
2007-10-12 09:25:24 +02:00
|
|
|
d->performMovement(d->calculateProgress(state->frames, state->currentFrame), state);
|
|
|
|
//TODO: calculate a proper interval based on the curve
|
2007-07-06 18:26:48 +02:00
|
|
|
state->currentInterval = state->interval;
|
|
|
|
animationsRemain = true;
|
|
|
|
} else {
|
|
|
|
d->performMovement(1, state);
|
|
|
|
d->movingItems.erase(d->movingItems.find(state->item));
|
2007-07-17 21:23:33 +02:00
|
|
|
emit movementComplete(state->item);
|
2007-07-06 18:26:48 +02:00
|
|
|
delete state;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state->currentInterval -= elapsed;
|
|
|
|
animationsRemain = true;
|
|
|
|
}
|
2007-07-04 11:36:50 +02:00
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
foreach (ElementAnimationState* state, d->animatedElements) {
|
|
|
|
if (state->currentFrame == state->frames) {
|
|
|
|
// since we keep element animations around until they are
|
|
|
|
// removed, we will end up with finished animations in the queue;
|
|
|
|
// just skip them
|
|
|
|
//TODO: should we move them to a separate QMap?
|
|
|
|
continue;
|
|
|
|
}
|
2007-06-19 23:56:03 +02:00
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
if (state->currentInterval <= elapsed) {
|
|
|
|
// we need to step forward!
|
|
|
|
/* kDebug() << "stepping forwards element anim " << state->id << " from " << state->currentFrame
|
|
|
|
<< " by " << qMax(1, elapsed / state->interval) << " to "
|
|
|
|
<< state->currentFrame + qMax(1, elapsed / state->interval) << endl;*/
|
|
|
|
state->currentFrame += qMax(1, elapsed / state->interval);
|
|
|
|
state->item->update();
|
|
|
|
|
|
|
|
if (state->currentFrame < state->frames) {
|
|
|
|
state->currentInterval = state->interval;
|
|
|
|
//TODO: calculate a proper interval based on the curve
|
2007-10-12 09:25:24 +02:00
|
|
|
state->interval *= 1 - d->calculateProgress(state->frames, state->currentFrame);
|
2007-06-25 08:01:34 +02:00
|
|
|
animationsRemain = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state->currentInterval -= elapsed;
|
2007-10-12 09:25:24 +02:00
|
|
|
animationsRemain = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (CustomAnimationState *state, d->customAnims) {
|
|
|
|
if (state->currentInterval <= elapsed) {
|
|
|
|
// advance the frame
|
|
|
|
state->currentFrame += qMax(1, elapsed / state->interval);
|
2007-10-12 22:26:07 +02:00
|
|
|
kDebug() << "custom anim for" << state->receiver << "to slot" << state->slot
|
|
|
|
<< "with interval of" << state->interval << "at frame" << state->currentFrame;
|
2007-10-12 09:25:24 +02:00
|
|
|
|
|
|
|
if (state->currentFrame < state->frames) {
|
2007-10-12 22:26:07 +02:00
|
|
|
kDebug () << "not the final frame";
|
2007-10-12 09:25:24 +02:00
|
|
|
//TODO: calculate a proper interval based on the curve
|
|
|
|
state->currentInterval = state->interval;
|
|
|
|
animationsRemain = true;
|
|
|
|
// signal the object
|
|
|
|
QMetaObject::invokeMethod(state->receiver, state->slot,
|
|
|
|
Q_ARG(qreal,
|
|
|
|
d->calculateProgress(state->frames, state->currentFrame)));
|
|
|
|
} else {
|
|
|
|
QMetaObject::invokeMethod(state->receiver, state->slot, Q_ARG(qreal, 1));
|
|
|
|
d->customAnims.erase(d->customAnims.find(state->id));
|
|
|
|
delete state->slot;
|
|
|
|
delete state;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state->currentInterval -= elapsed;
|
2007-06-25 08:01:34 +02:00
|
|
|
animationsRemain = true;
|
|
|
|
}
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
|
|
|
|
2007-06-25 08:01:34 +02:00
|
|
|
if (!animationsRemain && d->timerId) {
|
|
|
|
killTimer(d->timerId);
|
|
|
|
d->timerId = 0;
|
2007-06-19 23:56:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-08 07:24:19 +02:00
|
|
|
void Phase::init()
|
|
|
|
{
|
|
|
|
KConfig c("plasmarc");
|
|
|
|
KConfigGroup cg(&c, "Phase");
|
2007-06-20 08:06:47 +02:00
|
|
|
QString pluginName = cg.readEntry("animator", "default");
|
2007-06-08 07:24:19 +02:00
|
|
|
|
|
|
|
if (!pluginName.isEmpty()) {
|
|
|
|
QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(pluginName);
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/Animator", constraint);
|
|
|
|
|
|
|
|
if (!offers.isEmpty()) {
|
2007-08-29 05:06:48 +02:00
|
|
|
QString error;
|
2007-09-27 01:06:17 +02:00
|
|
|
d->animator = offers.first()->createInstance<Plasma::Animator>(0, QVariantList(), &error);
|
2007-08-29 05:06:48 +02:00
|
|
|
if (!d->animator) {
|
|
|
|
kDebug() << "Could not load requested animator " << offers.first() << ". Error given: " << error;
|
|
|
|
}
|
2007-06-08 07:24:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->animator) {
|
|
|
|
d->animator = new Animator(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include <phase.moc>
|