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