animpoint.cpp
author insilmaril
Mon May 05 13:46:42 2008 +0000 (2008-05-05)
changeset 692 c5e3cb54b9db
parent 461 b0d72eb511c9
child 721 12958f987bcf
permissions -rw-r--r--
Some code cleanup and experimental stuff to support animations later
     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 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 animated;
   108 	n++;
   109 	if (n>animTicks)
   110 	{
   111 		vector=QPointF(0,0);
   112 		animated=false;
   113 		return animated;
   114 	}
   115 
   116 	setX (startPos.x() + vector.x()*sqrt(n/animTicks) );
   117 	setY (startPos.y() + vector.y()*sqrt(n/animTicks) );
   118 	/*
   119 	setX (startPos.x() + vector.x()*(n/animTicks) );
   120 	setY (startPos.y() + vector.y()*(n/animTicks) );
   121 	*/
   122 	return animated;
   123 }
   124 
   125 void AnimPoint::initVector()
   126 {
   127 	vector.setX (destPos.x()-startPos.x() );
   128 	vector.setY (destPos.y()-startPos.y() );
   129 }
   130 
   131