src/hbcore/effects/hbeffecttranslate.cpp
changeset 5 627c4a0fd0e7
parent 1 f7ac710697a9
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
    32 
    32 
    33 HbEffectTranslateAnimation::HbEffectTranslateAnimation(
    33 HbEffectTranslateAnimation::HbEffectTranslateAnimation(
    34     HbEffectGroup *group,
    34     HbEffectGroup *group,
    35     HbEffectTranslate *effect,
    35     HbEffectTranslate *effect,
    36     int duration) :
    36     int duration) :
    37         HbEffectAnimation(group),
    37     HbEffectAnimation(group),
    38         effect(effect)
    38     effect(effect)
    39 {
    39 {
    40     setDuration(duration);
    40     setDuration(duration);
    41 }
    41 }
    42 
    42 
    43 void HbEffectTranslateAnimation::setOriginalPos(qreal pos)
    43 void HbEffectTranslateAnimation::setOriginalPos(qreal pos)
    63 
    63 
    64 HbEffectTranslate::HbEffectTranslate(
    64 HbEffectTranslate::HbEffectTranslate(
    65     const QList<HbEffectFxmlParamData> &data,
    65     const QList<HbEffectFxmlParamData> &data,
    66     QGraphicsItem *item,
    66     QGraphicsItem *item,
    67     HbEffectGroup *group) :
    67     HbEffectGroup *group) :
    68         HbEffectAbstract(0, item, group),
    68     HbEffectAbstract(0, item, group),
    69         mAnimationX(0),
    69     mAnimationX(0),
    70         mAnimationY(0)
    70     mAnimationY(0)
    71 {
    71 {
    72     // Default values if something is not passed in FXML
    72     // Default values if something is not passed in FXML
    73     int durationX = 0;
    73     int durationX = 0;
    74     int durationY = 0;
    74     int durationY = 0;
    75 
    75 
    76     QEasingCurve curveShapeX = QEasingCurve::Linear;
    76     QEasingCurve curveShapeX = QEasingCurve::Linear;
    77     QEasingCurve curveShapeY = QEasingCurve::Linear;
    77     QEasingCurve curveShapeY = QEasingCurve::Linear;
    78 
    78 
    79     const HbEffectFxmlParamData* paramX = 0;
    79     const HbEffectFxmlParamData *paramX = 0;
    80     const HbEffectFxmlParamData* paramY = 0;
    80     const HbEffectFxmlParamData *paramY = 0;
    81 
    81 
    82     // Handle FXML parameters
    82     // Handle FXML parameters
    83     Q_FOREACH(const HbEffectFxmlParamData &param, data) {
    83     Q_FOREACH(const HbEffectFxmlParamData & param, data) {
    84 	    if (param.name() == FXML_KEYWORD_TRANSLATION_X) {
    84         if (param.name() == FXML_KEYWORD_TRANSLATION_X) {
    85             mKeyFrameListX = param.keyFrames();
    85             mKeyFrameListX = param.keyFrames();
    86 
    86 
    87             HbEffectUtils::resolveFxmlDuration(durationX, param);
    87             HbEffectUtils::resolveFxmlDuration(durationX, param);
    88             HbEffectUtils::resolveFxmlCurveShape(curveShapeX, param);
    88             HbEffectUtils::resolveFxmlCurveShape(curveShapeX, param);
    89 			mStartXRef = param.startRef();
    89             mStartXRef = param.startRef();
    90 			mEndXRef = param.endRef();
    90             mEndXRef = param.endRef();
    91 			mStartX = param.getAttribute(FXML_KEYWORD_START);
    91             mStartX = param.getAttribute(FXML_KEYWORD_START);
    92 			mEndX = param.getAttribute(FXML_KEYWORD_END);
    92             mEndX = param.getAttribute(FXML_KEYWORD_END);
    93 
    93 
    94             // Only if "start ref" and "end ref" are not used, read parameter's "ref" attribute
    94             // Only if "start ref" and "end ref" are not used, read parameter's "ref" attribute
    95             if (mStartXRef.isEmpty() && mEndXRef.isEmpty()) {
    95             if (mStartXRef.isEmpty() && mEndXRef.isEmpty()) {
    96                 mParamRefX = param.getAttribute(FXML_PARAM_REF);
    96                 mParamRefX = param.getAttribute(FXML_PARAM_REF);
    97             }
    97             }
    98             paramX = &param;
    98             paramX = &param;
    99 	    }
    99         } else if (param.name() == FXML_KEYWORD_TRANSLATION_Y) {
   100 	    else if (param.name() == FXML_KEYWORD_TRANSLATION_Y) {
       
   101             mKeyFrameListY = param.keyFrames();
   100             mKeyFrameListY = param.keyFrames();
   102 
   101 
   103             HbEffectUtils::resolveFxmlDuration(durationY, param);
   102             HbEffectUtils::resolveFxmlDuration(durationY, param);
   104             HbEffectUtils::resolveFxmlCurveShape(curveShapeY, param);
   103             HbEffectUtils::resolveFxmlCurveShape(curveShapeY, param);
   105 			mStartYRef = param.startRef();
   104             mStartYRef = param.startRef();
   106 			mEndYRef = param.endRef();
   105             mEndYRef = param.endRef();
   107 			mStartY = param.getAttribute(FXML_KEYWORD_START);
   106             mStartY = param.getAttribute(FXML_KEYWORD_START);
   108 			mEndY = param.getAttribute(FXML_KEYWORD_END);
   107             mEndY = param.getAttribute(FXML_KEYWORD_END);
   109 
   108 
   110             // Only if "start ref" and "end ref" are not used, read parameter's "ref" attribute
   109             // Only if "start ref" and "end ref" are not used, read parameter's "ref" attribute
   111             if (mStartYRef.isEmpty() && mEndYRef.isEmpty()) {
   110             if (mStartYRef.isEmpty() && mEndYRef.isEmpty()) {
   112                 mParamRefY = param.getAttribute(FXML_PARAM_REF);
   111                 mParamRefY = param.getAttribute(FXML_PARAM_REF);
   113             }
   112             }
   114             paramY = &param;
   113             paramY = &param;
   115 	    }
   114         }
   116     }
   115     }
   117 
   116 
   118     if (durationX > 0) {
   117     if (durationX > 0) {
   119         mAnimationX = new HbEffectTranslateAnimation(group, this, durationX);
   118         mAnimationX = new HbEffectTranslateAnimation(group, this, durationX);
   120         mAnimationX->addLooping(paramX);
   119         mAnimationX->addLooping(paramX);
   128         mAnimationY->mCurve = curveShapeY;
   127         mAnimationY->mCurve = curveShapeY;
   129     }
   128     }
   130 }
   129 }
   131 
   130 
   132 /*  Deletes the effect and rewinds the animation so that next animation can start,
   131 /*  Deletes the effect and rewinds the animation so that next animation can start,
   133     rewind is necessary as the general use case is that the effect is restarted before 
   132     rewind is necessary as the general use case is that the effect is restarted before
   134     being complete, in which case the rewind would transform the item and prepare for
   133     being complete, in which case the rewind would transform the item and prepare for
   135     the next effect,otherwise would result in flicker.
   134     the next effect,otherwise would result in flicker.
   136 */
   135 */
   137 HbEffectTranslate::~HbEffectTranslate()
   136 HbEffectTranslate::~HbEffectTranslate()
   138 {
   137 {
   159 
   158 
   160     const QRectF &extRect = group()->extRect();
   159     const QRectF &extRect = group()->extRect();
   161 
   160 
   162     // StartX
   161     // StartX
   163     qreal value = HbEffectUtils::resolveFxmlRef(
   162     qreal value = HbEffectUtils::resolveFxmlRef(
   164         mStartXRef, mStartX, &valueOk, item(), HbEffectUtils::Position, extRect);
   163                       mStartXRef, mStartX, &valueOk, item(), HbEffectUtils::Position, extRect);
   165     if (valueOk) {
   164     if (valueOk) {
   166         mStartXValue = value;
   165         mStartXValue = value;
   167     } else {
   166     } else {
   168         // Failed to resolve parameters, use item's current position.
   167         // Failed to resolve parameters, use item's current position.
   169         mStartXValue = item()->pos().x();
   168         mStartXValue = item()->pos().x();
   170     }
   169     }
   171     
   170 
   172     // StartY
   171     // StartY
   173     value = HbEffectUtils::resolveFxmlRef(
   172     value = HbEffectUtils::resolveFxmlRef(
   174         mStartYRef, mStartY, &valueOk, item(), HbEffectUtils::Position, extRect);
   173                 mStartYRef, mStartY, &valueOk, item(), HbEffectUtils::Position, extRect);
   175     if (valueOk) {
   174     if (valueOk) {
   176         mStartYValue = value;
   175         mStartYValue = value;
   177     } else {
   176     } else {
   178         // Failed to resolve parameters, use item's current position.
   177         // Failed to resolve parameters, use item's current position.
   179         mStartYValue = item()->pos().y();
   178         mStartYValue = item()->pos().y();
   180     }
   179     }
   181 
   180 
   182     // EndX
   181     // EndX
   183     value = HbEffectUtils::resolveFxmlRef(
   182     value = HbEffectUtils::resolveFxmlRef(
   184         mEndXRef, mEndX, &valueOk, item(), HbEffectUtils::Position, extRect);
   183                 mEndXRef, mEndX, &valueOk, item(), HbEffectUtils::Position, extRect);
   185     if (valueOk) {
   184     if (valueOk) {
   186         mEndXValue = value;
   185         mEndXValue = value;
   187     } else {
   186     } else {
   188         // Failed to resolve parameters, use item's current position.
   187         // Failed to resolve parameters, use item's current position.
   189         mEndXValue = item()->pos().x();
   188         mEndXValue = item()->pos().x();
   190     }
   189     }
   191 
   190 
   192     // EndY
   191     // EndY
   193     value = HbEffectUtils::resolveFxmlRef(
   192     value = HbEffectUtils::resolveFxmlRef(
   194         mEndYRef, mEndY, &valueOk, item(), HbEffectUtils::Position, extRect);
   193                 mEndYRef, mEndY, &valueOk, item(), HbEffectUtils::Position, extRect);
   195     if (valueOk) {
   194     if (valueOk) {
   196         mEndYValue = value;
   195         mEndYValue = value;
   197     } else {
   196     } else {
   198         // Failed to resolve parameters, use item's current position.
   197         // Failed to resolve parameters, use item's current position.
   199         mEndYValue = item()->pos().y();
   198         mEndYValue = item()->pos().y();
   205         mAnimationX->setEndValue(QVariant(mEndXValue));
   204         mAnimationX->setEndValue(QVariant(mEndXValue));
   206 
   205 
   207         bool startEndRefUsed = !mStartXRef.isEmpty() && !mEndXRef.isEmpty();
   206         bool startEndRefUsed = !mStartXRef.isEmpty() && !mEndXRef.isEmpty();
   208 
   207 
   209         // Set keyframes in animation
   208         // Set keyframes in animation
   210         foreach(const HbKeyFrame &kf, mKeyFrameListX) {
   209         foreach(const HbKeyFrame & kf, mKeyFrameListX) {
   211             // If start and end references are used,
   210             // If start and end references are used,
   212             // value at given step is (1-c)*startX + c*endX where c is the keyframe coefficient value
   211             // value at given step is (1-c)*startX + c*endX where c is the keyframe coefficient value
   213             if (startEndRefUsed) {
   212             if (startEndRefUsed) {
   214                 value = (1 - kf.val) * mStartXValue + kf.val * mEndXValue;
   213                 value = (1 - kf.val) * mStartXValue + kf.val * mEndXValue;
   215             }
   214             }
   216             // Otherwise c defines the value
   215             // Otherwise c defines the value
   217             else {
   216             else {
   218                 value = HbEffectUtils::resolveFxmlRef(
   217                 value = HbEffectUtils::resolveFxmlRef(
   219                     mParamRefX, QString().setNum(kf.val), &valueOk, item(), HbEffectUtils::Position, extRect);
   218                             mParamRefX, QString().setNum(kf.val), &valueOk, item(), HbEffectUtils::Position, extRect);
   220                 if (!valueOk) {
   219                 if (!valueOk) {
   221                     // Default to item's position if failed
   220                     // Default to item's position if failed
   222                     value = item()->pos().x();
   221                     value = item()->pos().x();
   223                 }
   222                 }
   224             }
   223             }
   240         mAnimationY->setEndValue(QVariant(mEndYValue));
   239         mAnimationY->setEndValue(QVariant(mEndYValue));
   241 
   240 
   242         bool startEndRefUsed = !mStartYRef.isEmpty() && !mEndYRef.isEmpty();
   241         bool startEndRefUsed = !mStartYRef.isEmpty() && !mEndYRef.isEmpty();
   243 
   242 
   244         // Set keyframes in animation
   243         // Set keyframes in animation
   245         foreach(const HbKeyFrame &kf, mKeyFrameListY) {
   244         foreach(const HbKeyFrame & kf, mKeyFrameListY) {
   246             // If start and end references are used,
   245             // If start and end references are used,
   247             // value at given step is (1-c)*startY + c*endY where c is the keyframe coefficient value
   246             // value at given step is (1-c)*startY + c*endY where c is the keyframe coefficient value
   248             if (startEndRefUsed) {
   247             if (startEndRefUsed) {
   249                 value = (1 - kf.val) * mStartYValue + kf.val * mEndYValue;
   248                 value = (1 - kf.val) * mStartYValue + kf.val * mEndYValue;
   250             }
   249             }
   251             // Otherwise c defines the relative value to parameter's reference
   250             // Otherwise c defines the relative value to parameter's reference
   252             else {
   251             else {
   253                 value = HbEffectUtils::resolveFxmlRef(
   252                 value = HbEffectUtils::resolveFxmlRef(
   254                     mParamRefY, QString().setNum(kf.val), &valueOk, item(), HbEffectUtils::Position, extRect);
   253                             mParamRefY, QString().setNum(kf.val), &valueOk, item(), HbEffectUtils::Position, extRect);
   255                 if (!valueOk) {
   254                 if (!valueOk) {
   256                     // Default to item's position if failed
   255                     // Default to item's position if failed
   257                     value = item()->pos().y();
   256                     value = item()->pos().y();
   258                 }
   257                 }
   259             }
   258             }
   269         }
   268         }
   270     }
   269     }
   271 }
   270 }
   272 
   271 
   273 void HbEffectTranslate::setStartState(QTransform &transform)
   272 void HbEffectTranslate::setStartState(QTransform &transform)
   274 {   
   273 {
   275     qreal translateX = mAnimationX ? mStartXValue - mAnimationX->originalPos : 0;
   274     qreal translateX = mAnimationX ? mStartXValue - mAnimationX->originalPos : 0;
   276     qreal translateY = mAnimationY ? mStartYValue - mAnimationY->originalPos : 0;
   275     qreal translateY = mAnimationY ? mStartYValue - mAnimationY->originalPos : 0;
   277 
   276 
   278     QTransform newTransform;
   277     QTransform newTransform;
   279     newTransform.translate(translateX, translateY);
   278     newTransform.translate(translateX, translateY);