-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring pour pouvoir ajouter facilement le tronçonnage de table
- Loading branch information
dguihal@gmail.com
committed
Oct 17, 2013
1 parent
5a8d623
commit 5ac8821
Showing
9 changed files
with
346 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
#include "qqhuntpixmapitem.h" | ||
|
||
#include "core/qutetools.h" | ||
|
||
#include <qmath.h> | ||
|
||
#include <QGraphicsScene> | ||
#include <QPixmap> | ||
#include <QPropertyAnimation> | ||
|
||
#define BASE_ANIM_PIX QString(":/anims/") | ||
|
||
QQHuntPixmapItem::QQHuntPixmapItem(QString srcBouchot, QString postId, QObject *parent) : | ||
QObject(parent) | ||
{ | ||
setCacheMode(ItemCoordinateCache); | ||
m_srcBouchot = srcBouchot; | ||
m_postId = postId; | ||
|
||
m_animation = new QSequentialAnimationGroup(this); | ||
connect(m_animation, SIGNAL(finished()), this, SLOT(animate())); | ||
|
||
m_speedVec(X_VALUE) = 1.0; | ||
m_speedVec(Y_VALUE) = 0.0; | ||
|
||
m_maxAnimPixmapNumInc = 1; | ||
m_minAnimPixmapNumInc = 1; | ||
m_animPixmapNum = 1; | ||
m_animPixmapNumInc = true; | ||
|
||
m_animPixmapTimerEnabled = true; | ||
m_animPixmapTimer.setInterval(200); | ||
m_animPixmapTimer.setSingleShot(false); | ||
connect(&m_animPixmapTimer, SIGNAL(timeout()), this, SLOT(animateMove())); | ||
} | ||
|
||
QQHuntPixmapItem::~QQHuntPixmapItem() | ||
{ | ||
delete m_animation; | ||
} | ||
|
||
#define SPEED_FACTOR 200 | ||
#define MAX_DURATION 3000 | ||
#define MIN_DURATION 1000 | ||
|
||
void QQHuntPixmapItem::animate() | ||
{ | ||
m_animation->clear(); | ||
m_listPixmapProp.clear(); | ||
int timeMs = QuteTools::randInt(MIN_DURATION, MAX_DURATION); | ||
|
||
//Recuperation des bornes | ||
QRectF sceneRect = scene()->sceneRect(); | ||
float maxX = sceneRect.width() - boundingRect().width(); | ||
float maxY = sceneRect.height() - boundingRect().height(); | ||
|
||
//Et du point initial | ||
QPointF curPos = pos(); | ||
|
||
//Calcul du nouveau vecteur vitesse | ||
float angle = (((float) qrand()) / INT_MAX) * M_PI_2; | ||
angle -= M_PI_4; | ||
|
||
QQMatrix2x2 rotMatrix; | ||
rotMatrix(0, 0) = qCos(angle); | ||
rotMatrix(0, 1) = qSin(angle); | ||
rotMatrix(1, 0) = 0.0 - rotMatrix(0,1); | ||
rotMatrix(1, 1) = rotMatrix(0,0); | ||
|
||
m_speedVec = rotMatrix * m_speedVec; | ||
|
||
//Decoupage du temps d'animation en tranche selon les rencontres avec les bords. | ||
while(timeMs > 0) | ||
{ | ||
QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos"); | ||
pAnimation->setStartValue(curPos); | ||
connect(pAnimation, SIGNAL(finished()), this, SLOT(loadNextPixMap())); | ||
|
||
float angle = qAcos(m_speedVec(X_VALUE)); // 0 <= angle <= Pi | ||
QQPixmapProp pixmapProp = animPixmapProp(angle); | ||
m_listPixmapProp.append(pixmapProp); | ||
|
||
QQMatrix1x2 resSpeedVec = m_speedVec * ((float) SPEED_FACTOR) | ||
* (timeMs / (float) (MAX_DURATION - MIN_DURATION)); | ||
float destPosX = curPos.x() + resSpeedVec(X_VALUE); | ||
float destPosY = curPos.y() + resSpeedVec(Y_VALUE); | ||
|
||
float xFactor = 1; | ||
if(destPosX < 0) | ||
xFactor = (0.0 - curPos.x() / (destPosX - curPos.x())); | ||
else if(destPosX > maxX) | ||
xFactor = (maxX - curPos.x()) / (destPosX - curPos.x()); | ||
|
||
float yFactor = 1; | ||
if(destPosY < 0) | ||
yFactor = (0.0 - curPos.y() / (destPosY - curPos.y())); | ||
else if(destPosY > maxY) | ||
yFactor = (maxY - curPos.y()) / (destPosY - curPos.y()); | ||
|
||
//qDebug() << "xFactor" << xFactor << "yFactor" << yFactor; | ||
//Collision de bord detectee | ||
if(xFactor < 1 || yFactor < 1) | ||
{ | ||
float realtime = 0; | ||
if(xFactor <= yFactor) // on touche gauche/droite avant haut/bas | ||
{ | ||
realtime = timeMs * xFactor; | ||
curPos = QPointF(curPos.x() + (destPosX - curPos.x()) * xFactor, | ||
curPos.y() + (destPosY - curPos.y()) * xFactor); | ||
|
||
m_speedVec(X_VALUE) = 0.0 - m_speedVec(X_VALUE); | ||
if(xFactor == yFactor) | ||
m_speedVec(Y_VALUE) = 0.0 - m_speedVec(Y_VALUE); | ||
} | ||
else if(xFactor > yFactor) // on touche haut/bas avant gauche/droite | ||
{ | ||
realtime = timeMs * yFactor; | ||
curPos = QPointF(curPos.x() + (destPosX - curPos.x()) * yFactor, | ||
curPos.y() + (destPosY - curPos.y()) * yFactor); | ||
|
||
m_speedVec(Y_VALUE) = 0.0 - m_speedVec(Y_VALUE); | ||
} | ||
|
||
pAnimation->setDuration(realtime); | ||
timeMs -= realtime; | ||
pAnimation->setEndValue(curPos); | ||
} | ||
else | ||
{ | ||
pAnimation->setDuration(timeMs); | ||
timeMs = 0; | ||
pAnimation->setEndValue(QPointF(destPosX, destPosY)); | ||
} | ||
m_animation->addAnimation(pAnimation); | ||
} | ||
loadNextPixMap(); | ||
m_animation->start(QAbstractAnimation::KeepWhenStopped); | ||
} | ||
|
||
#define FALL_SPEED_FACTOR 5 | ||
void QQHuntPixmapItem::animateKill() | ||
{ | ||
m_animation->disconnect(); | ||
m_animation->clear(); | ||
m_listPixmapProp.clear(); | ||
m_animPixmapTimerEnabled = false; | ||
m_animPixmapTimer.stop(); | ||
|
||
QPointF curPos = pos(); | ||
float maxY = scene()->sceneRect().height() - boundingRect().height(); | ||
|
||
QPropertyAnimation *pAnimation = new QPropertyAnimation(this, "pos"); | ||
pAnimation->setStartValue(curPos); | ||
connect(pAnimation, SIGNAL(finished()), this, SLOT(loadNextPixMap())); | ||
pAnimation->setDuration(1000); | ||
pAnimation->setEndValue(curPos); | ||
m_animation->addAnimation(pAnimation); | ||
|
||
m_listPixmapProp.append(killPixmapProp()); | ||
m_animPixmapNumInc = false; | ||
|
||
pAnimation = new QPropertyAnimation(this, "pos"); | ||
pAnimation->setStartValue(curPos); | ||
connect(pAnimation, SIGNAL(finished()), this, SLOT(deleteLater())); | ||
QPointF endPos = curPos; | ||
endPos.setY(maxY); | ||
pAnimation->setDuration((maxY - curPos.y()) * FALL_SPEED_FACTOR); | ||
pAnimation->setEndValue(endPos); | ||
m_animation->addAnimation(pAnimation); | ||
|
||
m_listPixmapProp.append(fallPixmapProp()); | ||
|
||
loadNextPixMap(); | ||
m_animation->start(QAbstractAnimation::KeepWhenStopped); | ||
} | ||
|
||
|
||
void QQHuntPixmapItem::animateMove() | ||
{ | ||
if(m_currPixmapName.contains("%1")) | ||
{ | ||
setPixmap(QPixmap(m_currPixmapName.arg(m_animPixmapNum))); | ||
nextMoveStep(); | ||
} | ||
else | ||
setPixmap(QPixmap(m_currPixmapName)); | ||
} | ||
|
||
void QQHuntPixmapItem::loadNextPixMap() | ||
{ | ||
if(! m_listPixmapProp.isEmpty()) | ||
{ | ||
m_animPixmapTimer.stop(); | ||
QQPixmapProp pixmapProp = m_listPixmapProp.at(0); | ||
m_listPixmapProp.remove(0); | ||
m_currPixmapName = BASE_ANIM_PIX.append(pixmapProp.m_pixmapName); | ||
|
||
animateMove(); | ||
QPixmap p = pixmap(); | ||
|
||
int scaleX = pixmapProp.m_mirrorX ? -1 : 1; | ||
int scaleY = pixmapProp.m_mirrorY ? -1 : 1; | ||
setTransformOriginPoint(p.width() / 2, p.height() / 2); | ||
setTransform(QTransform().scale(scaleX, scaleY).rotateRadians(pixmapProp.m_rotationAngle, Qt::ZAxis)); | ||
setPixmap(p); | ||
if(m_animPixmapTimerEnabled) | ||
m_animPixmapTimer.start(); | ||
} | ||
} | ||
|
||
void QQHuntPixmapItem::nextMoveStep() | ||
{ | ||
if(m_animPixmapNumInc) | ||
{ | ||
m_animPixmapNum++; | ||
if(m_animPixmapNum == m_maxAnimPixmapNumInc) | ||
m_animPixmapNumInc = false; | ||
} | ||
else | ||
{ | ||
m_animPixmapNum--; | ||
if(m_animPixmapNum == m_minAnimPixmapNumInc) | ||
m_animPixmapNumInc = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#ifndef QQHUNTPIXMAPITEM_H | ||
#define QQHUNTPIXMAPITEM_H | ||
|
||
#include <QGenericMatrix> | ||
#include <QGraphicsPixmapItem> | ||
#include <QSequentialAnimationGroup> | ||
#include <QTimer> | ||
#include <QVector> | ||
|
||
typedef QGenericMatrix<1, 2, float> QQMatrix1x2; | ||
#define X_VALUE 0,0 | ||
#define Y_VALUE 1,0 | ||
|
||
typedef QGenericMatrix<2, 2, float> QQMatrix2x2; | ||
|
||
class QQHuntPixmapItem : public QObject, public QGraphicsPixmapItem | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(QPointF pos READ pos WRITE setPos) | ||
public: | ||
explicit QQHuntPixmapItem(QString srcBouchot, QString postId, QObject *parent = 0); | ||
~QQHuntPixmapItem(); | ||
|
||
|
||
QString bouchotName() const { return m_srcBouchot; } | ||
QString postId() const { return m_postId; } | ||
|
||
signals: | ||
|
||
public slots: | ||
void animate(); | ||
void animateKill(); | ||
void animateMove(); | ||
void loadNextPixMap(); | ||
|
||
protected: | ||
|
||
class QQPixmapProp | ||
{ | ||
public: | ||
explicit QQPixmapProp() : m_pixmapName() { m_rotationAngle = 0.0, m_mirrorX = false, m_mirrorY = false; } | ||
QString m_pixmapName; | ||
float m_rotationAngle; | ||
bool m_mirrorX; | ||
bool m_mirrorY; | ||
}; | ||
|
||
virtual QQPixmapProp animPixmapProp(float angle) = 0; | ||
virtual QQPixmapProp fallPixmapProp() = 0; | ||
virtual QQPixmapProp killPixmapProp() = 0; | ||
void nextMoveStep(); | ||
|
||
QSequentialAnimationGroup *m_animation; | ||
QTimer m_animPixmapTimer; | ||
bool m_animPixmapTimerEnabled; | ||
QVector<QQPixmapProp> m_listPixmapProp; | ||
int m_maxAnimPixmapNumInc; | ||
int m_minAnimPixmapNumInc; | ||
int m_animPixmapNum; | ||
bool m_animPixmapNumInc; | ||
QString m_currPixmapName; | ||
|
||
QQMatrix1x2 m_speedVec; | ||
|
||
QString m_srcBouchot; | ||
QString m_postId; | ||
}; | ||
|
||
#endif // QQHUNTPIXMAPITEM_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
Oops, something went wrong.