File indexing completed on 2024-07-14 04:00:05

0001 /*
0002     This file is part of the KDE games kwin4 program
0003     SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef PIECE_SPRITE_H
0009 #define PIECE_SPRITE_H
0010 
0011 // own
0012 #include "pixmapsprite.h"
0013 #include "spritenotify.h"
0014 #include "thememanager.h"
0015 // Qt
0016 #include <QGraphicsPixmapItem>
0017 #include <QPointF>
0018 
0019 /**
0020  * The sprite for a pixmap piece on the canvas. The pixmap can be
0021  * animated and moving.
0022  */
0023 class PieceSprite : public PixmapSprite
0024 {
0025 public:
0026     /**
0027      * Constructor for the sprite.
0028      */
0029     PieceSprite(const QString &id, ThemeManager *theme, int no, QGraphicsScene *canvas);
0030 
0031     /**
0032      * Destructor
0033      */
0034     ~PieceSprite() override;
0035 
0036     /**
0037      * Possible animation states of the sprite
0038      */
0039     enum MovementState { Idle, LinearMove };
0040 
0041     /**
0042      * Standard QGI advance function.
0043      * @param phase The advance phase
0044      */
0045     void advance(int phase) override;
0046 
0047     /**
0048      * Retrieve the type of QGI. This item is UserType+2
0049      * @return The type of item.
0050      */
0051     int type() const override
0052     {
0053         return QGraphicsItem::UserType + 2;
0054     }
0055 
0056     /**
0057      * Standard Themeable function. It is called when the theme item
0058      * needs to completely refresh itself.
0059      */
0060     void changeTheme() override;
0061 
0062     /**
0063      * Retrieve the sprite notification object. This object indicates the
0064      * end of a movement.
0065      * @return The notification object.
0066      */
0067     SpriteNotify *notify()
0068     {
0069         return mNotify;
0070     }
0071 
0072     /**
0073      * Start a linear movement from the current position to the given
0074      * end position with the given velocity.
0075      * @param end      The end position in relative coordinates [rel 0..1, rel 0..1]
0076      * @param velocity The velocity of the move in [relative units/sec.]
0077      */
0078     void startLinear(QPointF end, double velocity);
0079 
0080     /**
0081      * Start a linear movement from the start point to the given
0082      * end position with the given velocity.
0083      * @param start    The start position in relative coordinates [rel 0..1, rel 0..1]
0084      * @param end      The end position in relative coordinates [rel 0..1, rel 0..1]
0085      * @param velocity The velocity of the move in [relative units/sec.]
0086      */
0087     void startLinear(QPointF start, QPointF end, double velocity);
0088 
0089 private:
0090     /// The duration of the movement
0091     double mDuration;
0092 
0093     /// The state of the movement
0094     MovementState mMovementState;
0095 
0096     /// The end points of the movement [rel]
0097     QPointF mEnd;
0098 
0099     /// The movement sprite notifier
0100     SpriteNotify *mNotify;
0101 };
0102 
0103 #endif