Source

cocos2d-qt / Srcs / action.cpp

Full commit
#include "action.h"

Action::Action() {
}

Action* Action::FuncCall(QObject* targeted,const char *slot) {
    Action *newAction = new Action;
    newAction->slotCall = slot;
    newAction->actionTypeV = actionTypeFuncCall;
    newAction->target = targeted;
    return newAction;
}

Action* Action::moveTo(float duration,qreal x,qreal y) {
    Action *newAction = new Action;
    newAction->actionTypeV = actionTypeMoveTo;
    newAction->endValue[0] = x;
    newAction->endValue[1] = y;
    newAction->animation.setDuration(1000*duration);
    newAction->animation.setPropertyName("pos");
    return newAction;
}

Action* Action::moveBy(float duration,qreal x,qreal y) {
    Action *newAction = new Action;
    newAction->actionTypeV = actionTypeMoveBy;
    newAction->endValue[0] = x;
    newAction->endValue[1] = y;
    newAction->animation.setDuration(1000*duration);
    newAction->animation.setPropertyName("pos");
    return newAction;

}

Action* Action::scaleTo(float duration,qreal scale) {
    Action *newAction = new Action;
    newAction->actionTypeV = actionTypeScaleTo;
    newAction->endValue[0] = scale;
    newAction->animation.setDuration(1000*duration);
    newAction->animation.setPropertyName("scale");
    return newAction;

}

Action* Action::scaleBy(float duration,qreal scale) {
    Action *newAction = new Action;
    newAction->actionTypeV = actionTypeScaleBy;
    newAction->endValue[0] = scale;
    newAction->animation.setDuration(1000*duration);
    newAction->animation.setPropertyName("scale");
    return newAction;

}

Action* Action::fadeTo(float duration,qreal value) {
    Action *newAction = new Action;
    newAction->actionTypeV = actionTypeFadeTo;
    newAction->endValue[0] = value;
    newAction->animation.setDuration(1000*duration);
    newAction->animation.setPropertyName("opacity");
    return newAction;

}

Action* Action::fadeBy(float duration,qreal value) {
    Action *newAction = new Action;
    newAction->actionTypeV = actionTypeFadeBy;
    newAction->endValue[0] = value;
    newAction->animation.setDuration(1000*duration);
    newAction->animation.setPropertyName("opacity");
    return newAction;

}

Action* Action::rotateTo(float duration,qreal angle) {
    Action *newAction = new Action;
    newAction->actionTypeV = actionTypeRotateTo;
    newAction->endValue[0] = angle;
    newAction->animation.setDuration(1000*duration);
    newAction->animation.setPropertyName("rotate");
    return newAction;

}

Action* Action::rotateBy(float duration,qreal angle) {
    Action *newAction = new Action;
    newAction->actionTypeV = actionTypeRotateBy;
    newAction->endValue[0] = angle;
    newAction->animation.setDuration(1000*duration);
    newAction->animation.setPropertyName("rotate");
    return newAction;

}


Action* Action::easein(Action* withAction,int rate) {
    if (rate < 0) {
        rate = 0;
        qDebug() << "GameEngine :: easing rate should have a range between 0 and 5, range has been set to 0 for you this time";
    }
    if (rate > 5) {
        rate = 5;
        qDebug() << "GameEngine :: easing rate should have a range between 0 and 5, range has been set to 5 for you this time";
    }
    if (rate == 0)
        withAction->animation.setEasingCurve(QEasingCurve::Linear);
    if (rate == 1)
        withAction->animation.setEasingCurve(QEasingCurve::InQuad);
    if (rate == 2)
        withAction->animation.setEasingCurve(QEasingCurve::InCubic);
    if (rate == 3)
        withAction->animation.setEasingCurve(QEasingCurve::InQuart);
    if (rate == 4)
        withAction->animation.setEasingCurve(QEasingCurve::InCirc);
    if (rate == 5)
        withAction->animation.setEasingCurve(QEasingCurve::InBack);

    return withAction;
}

Action* Action::easeout(Action* withAction,int rate) {
    if (rate < 0) {
        rate = 0;
        qDebug() << "GameEngine :: easing rate should have a range between 0 and 5, range has been set to 0 for you this time";
    }
    if (rate > 5) {
        rate = 5;
        qDebug() << "GameEngine :: easing rate should have a range between 0 and 5, range has been set to 5 for you this time";
    }
    if (rate == 0)
        withAction->animation.setEasingCurve(QEasingCurve::Linear);
    if (rate == 1)
        withAction->animation.setEasingCurve(QEasingCurve::OutQuad);
    if (rate == 2)
        withAction->animation.setEasingCurve(QEasingCurve::OutCubic);
    if (rate == 3)
        withAction->animation.setEasingCurve(QEasingCurve::OutQuart);
    if (rate == 4)
        withAction->animation.setEasingCurve(QEasingCurve::OutCirc);
    if (rate == 5)
        withAction->animation.setEasingCurve(QEasingCurve::OutBack);
    return withAction;
}

Action* Action::easeinout(Action* withAction,int rate) {
    if (rate < 0) {
        rate = 0;
        qDebug() << "GameEngine :: easing rate should have a range between 0 and 5, range has been set to 0 for you this time";
    }
    if (rate > 5) {
        rate = 5;
        qDebug() << "GameEngine :: easing rate should have a range between 0 and 5, range has been set to 5 for you this time";
    }
    if (rate == 0)
        withAction->animation.setEasingCurve(QEasingCurve::Linear);
    if (rate == 1)
        withAction->animation.setEasingCurve(QEasingCurve::InOutQuad);
    if (rate == 2)
        withAction->animation.setEasingCurve(QEasingCurve::InOutCubic);
    if (rate == 3)
        withAction->animation.setEasingCurve(QEasingCurve::InOutQuart);
    if (rate == 4)
        withAction->animation.setEasingCurve(QEasingCurve::InOutCirc);
    if (rate == 5)
        withAction->animation.setEasingCurve(QEasingCurve::InOutBack);
    return withAction;
}


void Action::setTarget(Node *targeted) {
    if (actionTypeV == actionTypeFuncCall) {
        connect(this,SIGNAL(animationFinished(Action*)),target,slotCall);
        delete_self();
    }
    else {
    target = targeted;
    qreal newY;
    qreal newX;
    switch (actionTypeV) {
    case actionTypeMoveTo :
        animation.setStartValue(targeted->pos());
        //here we are setting the new position
        animation.setEndValue(QPointF(endValue[0],endValue[1]));
        break;
    case actionTypeMoveBy :
        animation.setStartValue(targeted->pos());
        //here we'll calculate the new point as it's moveBy, we'll add the value to the original
        newX = endValue[0] + targeted->pos().x();
        newY = endValue[1] + targeted->pos().y();
        animation.setEndValue(QPointF(newX,newY));
        break;
    case actionTypeFadeTo :
        animation.setStartValue(targeted->opacity());
        //here we'll set the new scale
        animation.setEndValue(endValue[0]);
        break;
    case actionTypeFadeBy :
        animation.setStartValue(targeted->opacity());
        //here we'll calculate the new scale as it's multiplied by original scale
        animation.setEndValue(endValue[0]+targeted->opacity());
        break;
    case actionTypeScaleTo:
        animation.setStartValue(targeted->scale());
        //here we'll set the new scale
        animation.setEndValue(endValue[0]);
        break;
    case actionTypeScaleBy :
        animation.setStartValue(targeted->scale());
        //here we'll calculate the new scale as it's multiplied by original scale
        animation.setEndValue(endValue[0]*targeted->scale());
        break;
    case actionTypeRotateTo :
        animation.setStartValue(targeted->rotation());
        //here we'll set the new scale
        animation.setEndValue(endValue[0]);
        break;
    case actionTypeRotateBy :
        animation.setStartValue(targeted->rotation());
        //here we'll calculate the new scale as it's multiplied by original scale
        animation.setEndValue(endValue[0]+targeted->rotation());
        break;
    default:
        emit animationFinished(this);
        break;
    }
    animation.setTargetObject(target);
    animation.start();
    //here we are connecting it to delete itself slot, and emit done signal
    connect(&animation,SIGNAL(finished()),this,SLOT(delete_self()));
}

}


void Action::delete_self() {
        emit animationFinished(this);
}

void Action::stopAnimation() {
    animation.stop();
}