File indexing completed on 2024-04-14 04:02:24

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 CARD_SPRITE_H
0009 #define CARD_SPRITE_H
0010 
0011 // Qt includes
0012 #include <QGraphicsPixmapItem>
0013 #include <QList>
0014 #include <QPixmap>
0015 #include <QPoint>
0016 #include <QPointF>
0017 
0018 // Local includes
0019 #include "thememanager.h"
0020 #include "deck.h"
0021 
0022 /**
0023  * The sprite for a card on the canvas.
0024  */
0025 class CardSprite : public QGraphicsPixmapItem, public virtual Themable
0026 {
0027 public:
0028     /**
0029      * Constructor for the sprite.
0030      * @param suite         The suite of the card to use [Club, ...]
0031      * @param type          The card type [Ace, ...]
0032      * @param theme         The theme manager
0033      * @param advancePeriod The scene advance period [ms]
0034      * @param scene         The scene object
0035      */
0036     CardSprite(const Suite suite, const CardType type, ThemeManager *theme, int advancePeriod, QGraphicsScene *scene);
0037 
0038     /**
0039      * Possible animation states of the sprite
0040      */
0041     enum AnimationState {Idle, Turning, Moving, Removing, ShuffleMove};
0042 
0043     /**
0044      * Main theme manager function. Called when any theme change like
0045      * a new theme or a theme size change occurs. This object needs to
0046      * resize and redraw then.
0047      */
0048     void changeTheme() override;
0049 
0050     /**
0051      * Display the card front pixmap image
0052      */
0053     void setFrontside();
0054 
0055     /**
0056      * Display the card back pixmap image
0057      */
0058     void setBackside();
0059 
0060     /**
0061      * Standard advance method
0062      * @param phase Advance phase
0063      */
0064     void advance(int phase) override;
0065 
0066     /**
0067      * Stop all movement and animation.
0068      */
0069     void stop();
0070 
0071     /**
0072      * Turn the card to frontside or backside.
0073      * @param front True (turn to frontside) or false (turn to backside)
0074      */
0075     void setTurning(bool front);
0076 
0077     /**
0078      * Move the sprite slowly to the target area. Stop
0079      * movement if it arrived there.
0080      * @param pos  Target position [0..1]
0081      * @param time Time for the move [ms]
0082      */
0083     void setMove(QPointF pos, double time);
0084 
0085     /**
0086      * Set the position of the sprite.
0087      * @param pos  Target position [0..1]
0088      */
0089     void setPosition(QPointF pos);
0090 
0091     /**
0092      * Move the sprite slowly to the target area. Stop
0093      * movement and hide sprite if it arrived there.
0094      * @param pos  Target position [0..1]
0095      * @param time Time for the move [ms]
0096      */
0097     void setRemove(QPointF pos, double time);
0098 
0099     /**
0100      * Delay before moving, then move the sprite fast to the target area.
0101      * Stop movement and depending on the last argument turn backside/frontside
0102      * sprite if it arrived there.
0103      * @param pos   Target position [0..1]
0104      * @param delay The Move start delay [ms]
0105      * @param front Show frontside(true) or backside
0106      */
0107     void setShuffleMove(QPointF pos, double delay, bool front);
0108 
0109     /**
0110      * Set the current frame.
0111      * @param no    The frame number 0..n-1
0112      * @param force Force redraw
0113      */
0114     void setFrame(int no, bool force = false);
0115 
0116     /**
0117      * Retrieve the maximum frame number.
0118      * @return The frame count.
0119      */
0120     int count();
0121 
0122     /**
0123      * Retrieve the card id, a combination of suite and card type.
0124      * @return The card id.
0125      */
0126     int cardid() {return mCardType * 4 + mSuite;}
0127 
0128     /**
0129      * Retrieve the current frame number.
0130      * @return The frame number 0..n-1.
0131      */
0132     int frame() {return mCurrentFrame;}
0133 
0134     /**
0135      * Check whether the sprite is idle,
0136      * @return True if idle.
0137      */
0138     bool isIdle() {return mAnimationState == Idle;}
0139 
0140 protected:
0141     /**
0142      * Calculate a sprite frame (for turning sprites).
0143      * Called when the frame is asked for.
0144      * @param no The frame number.
0145      */
0146     void calcFrame(int no);
0147 
0148     /**
0149      * Create the pixmap for one frame of the frame turning animation.
0150      * @param front The front pixmap
0151      * @param back  The back pixmap
0152      * @param curNo Which number in the animation sequence [0..count]
0153      * @param count How many frames in the animation
0154      * @return The calculated pixmap.
0155      */
0156     QPixmap createCard(const QPixmap &front, const QPixmap &back, int curNo, int count);
0157 
0158     /**
0159      * Set target position and calculate moving speed.
0160      * @param pos  Target [0..1, 0..1]
0161      * @param time The movement speed [ms]
0162      */
0163     void calcTargetAndSpeed(QPointF pos, double time);
0164 
0165     /**
0166      * Perform a move by a delta given by the sprites velocity.
0167      * @return True if the target position is reached false otherwise.
0168      */
0169     bool deltaMove();
0170 
0171 private:
0172     // Type of animation
0173     AnimationState mAnimationState;
0174     // Counter of animation [ms]
0175     double mTime;
0176     // Target point for movement
0177     QPointF mMoveTarget;
0178     // Movement speed X
0179     double mMoveSpeedX;
0180     // Movement speed Y
0181     double mMoveSpeedY;
0182     // Front/Backside flag
0183     bool mFrontFlag;
0184     // Store our pixmap array
0185     QList<QPixmap> mFrames;
0186     // Store our pixmap offset values
0187     QList<QPointF> mHotspots;
0188     // The current frame number
0189     int mCurrentFrame;
0190 
0191     // Sprite advance period [ms]
0192     int mAdvancePeriod;
0193     // Card width
0194     double mWidth;
0195     // Suite of card
0196     Suite mSuite;
0197     // Type of card
0198     CardType mCardType;
0199 
0200     // The position of the sprite [rel 0..1, rel 0..1]
0201     QPointF mStart;
0202 };
0203 
0204 #endif