File indexing completed on 2024-05-26 07:56:06

0001 /*
0002     This file is part of the KDE games lskat 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 PIXMAP_SPRITE_H
0009 #define PIXMAP_SPRITE_H
0010 
0011 // Qt includes
0012 #include <QPointF>
0013 #include <QGraphicsPixmapItem>
0014 
0015 // Local includes
0016 #include "thememanager.h"
0017 
0018 /**
0019  * This sprite is used to display a pixmap on the canvas.
0020  * It can be animated and it is themable.
0021  */
0022 class PixmapSprite : public QGraphicsPixmapItem, public virtual Themable
0023 {
0024 public:
0025     /**
0026      * Constructor for the sprite.
0027      * @param advancePeriod The advance period time [ms]
0028      * @param no            A user defined ID number
0029      * @param scene        The graphics scene
0030      */
0031     PixmapSprite(int advancePeriod, int no, QGraphicsScene *scene);
0032 
0033     /**
0034      * Constructor for the sprite.
0035      * @param id            The theme file ID string
0036      * @param theme         The theme manager
0037      * @param advancePeriod The advance period time [ms]
0038      * @param no            A user defined ID number
0039      * @param scene        The graphics scene
0040      */
0041     PixmapSprite(const QString &id, ThemeManager *theme, int advancePeriod, int no, QGraphicsScene *scene);
0042 
0043     /**
0044      * Possible animation states of the sprite
0045      */
0046     enum AnimationState {Idle, Animated};
0047 
0048     /**
0049      * Standard QGI advance function.
0050      * @param phase The advance phase
0051      */
0052     void advance(int phase) override;
0053 
0054     /**
0055      * Retrieve the type of QGI. This item is UserType+3
0056      * @return The type of item.
0057      */
0058     int type() const override {return QGraphicsItem::UserType + 3;}
0059 
0060     /**
0061      * Retrieve the user defined sprite number (i.e. which PixmapSprite)
0062      * @return The sprite numbers.
0063      */
0064     int number() {return mNo;}
0065 
0066     /**
0067      * Main theme manager function. Called when any theme change like
0068      * a new theme or a theme size change occurs. This object needs to
0069      * resize and redraw then.
0070      */
0071     void changeTheme() override;
0072 
0073     /**
0074      * Choose a pixmap frame of this sprite. If the setting is forced a
0075      * redraw is even performed when the frame number stays the same. This
0076      * is important for theme changes.
0077      * @param no    The new frame number (0..)
0078      * @param force Force redraw
0079      */
0080     void setFrame(int no, bool force = false);
0081 
0082     /**
0083      * Initialize and start a frame animation between the start and end frame.
0084      * The delay between the frames is given in [ms]. After the last frame
0085      * is displayed the animation starts with the first frame.
0086      * @param start   The start frame number
0087      * @param end     The end frame number
0088      * @param delay   Delay between a frame change [ms]
0089      */
0090     void setAnimation(int start, int end, int delay);
0091 
0092     /**
0093      * Start or stop an animation.
0094      * @param status True to start and false to stop the animation
0095      */
0096     void setAnimation(bool status);
0097 
0098     /**
0099      * Set/Move the sprite to the given position. The position is
0100      * given in relative coordinates [0..1, 0..1] to its parent object.
0101      * Thus the position is scaled with the scale from the theme manager.
0102      * @param pos  The position of the sprite [0..1, 0..1]
0103      */
0104     void setPosition(const QPointF &pos);
0105 
0106     /**
0107      * Reads a theme configuration item of type double and of the given name.
0108      * @deprecated This is debug only
0109      * @param item  The theme configuration item name
0110      * @return The read double value.
0111      */
0112     double getDoubleValue(const QString &item);
0113 
0114     /**
0115      * Set whether the sprite should respect a theme offset (default: true)
0116      * or not (false), that is, it is handled by its parent item.
0117      * @param status True: Handle theme offset, False: don't
0118      */
0119     void setOffsetStatus(bool status);
0120 
0121 protected:
0122     /** The user defined sprite number.*/
0123     int mNo;
0124 
0125     /** The canvas advance period (e.g. 25) [ms] */
0126     int mAdvancePeriod;
0127 
0128     /** The theme id. */
0129     QString mId;
0130 
0131     /** The state of the animation. */
0132     AnimationState mAnimationState;
0133 
0134     /** The position of the sprite [rel 0..1, rel 0..1] */
0135     QPointF mStart;
0136 
0137     /** The first frame in the frame animation. */
0138     int mStartFrame;
0139 
0140     /** The last frame in the frame animation. */
0141     int mEndFrame;
0142 
0143     /** The delay between two frames in the frame animation. */
0144     int mDelay;
0145 
0146     /** The current running time for the animation. */
0147     int mTime;
0148 
0149     /** The current running frame number for the animation. */
0150     int mCurrentFrame;
0151 
0152     /** Storage of the frame pixmaps. */
0153     QList<QPixmap> mFrames;
0154 
0155     /** Storage of the frame pixmap offset points. */
0156     QList<QPointF> mHotspots;
0157 
0158     /** Offset status. */
0159     bool mOffsetStatus;
0160 };
0161 
0162 #endif