animpoint.cpp
author insilmaril
Tue, 30 Mar 2010 17:30:39 +0000
changeset 842 bec082472471
parent 835 31841b366d5e
child 847 43268373032d
permissions -rw-r--r--
Much improved results in FindResultsWidget
     1 #include "animpoint.h"
     2 
     3 #include <math.h>
     4 
     5 AnimPoint::AnimPoint()
     6 {
     7 	init();
     8 }
     9 
    10 void AnimPoint::operator= ( const AnimPoint & other )
    11 {
    12 	copy (other);
    13 }
    14 
    15 void AnimPoint::operator= ( const QPointF & other )
    16 {
    17 	init();
    18 	setX (other.x() );
    19 	setY (other.x() );
    20 }
    21 
    22 bool AnimPoint::operator== ( const QPointF& other )
    23 {
    24 	QPointF p( x(),y());
    25 	return p == other;
    26 }
    27 
    28 bool AnimPoint::operator== ( AnimPoint  other )
    29 {
    30     if (rx() != other.rx() ) return false;
    31     if (ry() != other.ry() ) return false;
    32 	if (startPos != other.startPos) return false;
    33 	if (destPos  != other.destPos) return false;
    34     if (animated != other.animated ) return false;
    35 
    36 	return true;
    37 }
    38 
    39 void AnimPoint::init ()
    40 {
    41 	animated=false;
    42 	n=0;
    43 	startPos=QPointF(0,0);
    44 	destPos=QPointF(0,0);
    45 	vector=QPointF(0,0);
    46 	animTicks=10;
    47 }
    48 
    49 void AnimPoint::copy (AnimPoint other)
    50 {
    51 	setX (other.x() );
    52 	setY (other.x() );
    53 	startPos=other.startPos;
    54 	destPos=other.destPos;
    55 	vector=other.vector;
    56 	animated=other.animated;
    57 	n=other.n;
    58 	animTicks=other.animTicks;
    59 }
    60 
    61 void AnimPoint::setStart(const QPointF &p)
    62 {
    63 	startPos=p;
    64 	initVector();
    65 }
    66 
    67 QPointF AnimPoint::getStart()
    68 {
    69 	return startPos;
    70 }
    71 
    72 
    73 void AnimPoint::setDest(const QPointF &p)
    74 {
    75 	destPos=p;
    76 	initVector();
    77 }
    78 
    79 QPointF AnimPoint::getDest()
    80 {
    81 	return destPos;
    82 }
    83 
    84 void AnimPoint::setTicks (const uint &t)
    85 {
    86 	animTicks=t;
    87 }
    88 
    89 uint AnimPoint::getTicks()
    90 {
    91 	return (uint) animTicks;
    92 }
    93 
    94 void AnimPoint::setAnimated(bool b)
    95 {
    96 	animated=b;
    97 	if (b) n=0;
    98 }
    99 
   100 bool AnimPoint::isAnimated()
   101 {
   102 	return animated;
   103 }
   104 
   105 bool AnimPoint::animate()
   106 {
   107 	if (!animated) return false;
   108 	n++;
   109 	if (n>animTicks)
   110 	{
   111 		vector=QPointF(0,0);
   112 		animated=false;
   113 		setX (destPos.x() );
   114 		setY (destPos.y() );
   115 		return false;
   116 	}
   117 
   118 	// Some math to slow down the movement in the end
   119 	qreal f=1-n/(qreal)animTicks;
   120 	qreal ff=1-f*f*f;
   121 	setX (startPos.x() + vector.x()*ff );
   122 	setY (startPos.y() + vector.y()*ff );
   123 
   124 	return animated;
   125 }
   126 
   127 void AnimPoint::initVector()
   128 {
   129 	vector.setX (destPos.x()-startPos.x() );
   130 	vector.setY (destPos.y()-startPos.y() );
   131 }
   132 
   133