QQuickCanvas renames
[profile/ivi/qtdeclarative.git] / src / quick / items / qquickanimatedsprite.cpp
index 0e37020..8fa4acc 100644 (file)
 #include "qquickspriteengine_p.h"
 #include <QtQuick/private/qsgcontext_p.h>
 #include <private/qsgadaptationlayer_p.h>
+#include <private/qqmlglobal_p.h>
 #include <QtQuick/qsgnode.h>
 #include <QtQuick/qsgtexturematerial.h>
 #include <QtQuick/qsgtexture.h>
-#include <QtQuick/qquickcanvas.h>
+#include <QtQuick/qquickwindow.h>
 #include <QtQml/qqmlinfo.h>
 #include <QFile>
 #include <cmath>
@@ -210,6 +211,7 @@ struct AnimatedSpriteVertices {
     \qmlclass AnimatedSprite QQuickAnimatedSprite
     \inqmlmodule QtQuick 2
     \inherits Item
+    \ingroup qtquick-visual
     \brief Draws a sprite animation
 
     AnimatedSprite provides rendering and control over animations which are provided
@@ -217,7 +219,7 @@ struct AnimatedSpriteVertices {
     frame rate of your display, or manually advance and control the progress.
 
     For details of how a sprite animation is defined see the \l{Sprite Animation} overview.
-    Note that the AnimatedSprite element does not use Sprite elements to define multiple animations,
+    Note that the AnimatedSprite type does not use Sprite types to define multiple animations,
     but instead encapsulates a single animation itself.
 */
 
@@ -376,6 +378,11 @@ QQuickAnimatedSprite::QQuickAnimatedSprite(QQuickItem *parent) :
             this, SLOT(sizeVertices()));
 }
 
+bool QQuickAnimatedSprite::isCurrentFrameChangedConnected()
+{
+    IS_SIGNAL_CONNECTED(this, QQuickAnimatedSprite, currentFrameChanged, (int));
+}
+
 void QQuickAnimatedSprite::reloadImage()
 {
     if (!isComponentComplete())
@@ -393,7 +400,8 @@ void QQuickAnimatedSprite::componentComplete()
 
 void QQuickAnimatedSprite::start()
 {
-    if (m_running)
+    m_running = true;
+    if (!isComponentComplete())
         return;
     m_curLoop = 0;
     m_timestamp.start();
@@ -402,16 +410,16 @@ void QQuickAnimatedSprite::start()
         m_spriteEngine->updateSprites(0);
         m_spriteEngine->start(0);
     }
-    m_running = true;
+    emit currentFrameChanged(0);
     emit runningChanged(true);
     update();
 }
 
 void QQuickAnimatedSprite::stop()
 {
-    if (!m_running)
-        return;
     m_running = false;
+    if (!isComponentComplete())
+        return;
     m_pauseOffset = 0;
     emit runningChanged(false);
 }
@@ -508,7 +516,7 @@ QSGGeometryNode* QQuickAnimatedSprite::buildNode()
     if (image.isNull())
         return 0;
     m_sheetSize = QSizeF(image.size());
-    m_material->texture = canvas()->createTextureFromImage(image);
+    m_material->texture = window()->createTextureFromImage(image);
     m_material->texture->setFiltering(QSGTexture::Linear);
     m_spriteEngine->start(0);
     m_material->animT = 0;
@@ -526,18 +534,19 @@ QSGGeometryNode* QQuickAnimatedSprite::buildNode()
 
     AnimatedSpriteVertices *p = (AnimatedSpriteVertices *) g->vertexData();
 
-    p->v1.tx = 0;
-    p->v1.ty = 0;
+    QRectF texRect = m_material->texture->normalizedTextureSubRect();
 
-    p->v2.tx = 1.0;
-    p->v2.ty = 0;
+    p->v1.tx = texRect.topLeft().x();
+    p->v1.ty = texRect.topLeft().y();
 
-    p->v3.tx = 0;
-    p->v3.ty = 1.0;
+    p->v2.tx = texRect.topRight().x();
+    p->v2.ty = texRect.topRight().y();
 
-    p->v4.tx = 1.0;
-    p->v4.ty = 1.0;
+    p->v3.tx = texRect.bottomLeft().x();
+    p->v3.ty = texRect.bottomLeft().y();
 
+    p->v4.tx = texRect.bottomRight().x();
+    p->v4.ty = texRect.bottomRight().y();
 
     quint16 *indices = g->indexDataAsUShort();
     indices[0] = 0;
@@ -595,6 +604,7 @@ void QQuickAnimatedSprite::prepareNextFrame()
 
     double frameAt; //double just for modf
     qreal progress = 0.0;
+    int lastFrame = m_curFrame;
     if (!m_paused) {
         //Advance State (keeps time for psuedostates)
         m_spriteEngine->updateSprites(timeInt);
@@ -627,6 +637,8 @@ void QQuickAnimatedSprite::prepareNextFrame()
     } else {
         frameAt = m_curFrame;
     }
+    if (m_curFrame != lastFrame && isCurrentFrameChangedConnected())
+        emit currentFrameChanged(m_curFrame);
     if (m_spriteEngine->sprite()->reverse())
         frameAt = (m_spriteEngine->spriteFrames() - 1) - frameAt;
     qreal y = m_spriteEngine->spriteY() / m_sheetSize.height();