File indexing completed on 2024-04-21 04:02:18

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 KWIN4_KWIN4VIEW_H
0009 #define KWIN4_KWIN4VIEW_H
0010 
0011 // own
0012 #include "kwin4global.h"
0013 #include "thememanager.h"
0014 // KDEGames
0015 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
0016 #include <libkdegamesprivate/kgame/kgameio.h>
0017 // Qt
0018 #include <QDataStream>
0019 #include <QGraphicsPixmapItem>
0020 #include <QGraphicsView>
0021 #include <QLinearGradient>
0022 #include <QMouseEvent>
0023 #include <QPoint>
0024 #include <QRect>
0025 #include <QResizeEvent>
0026 #include <QSize>
0027 #include <QTimer>
0028 #include <QWidget>
0029 
0030 // Forward declaration
0031 class DisplayIntro;
0032 class DisplayGame;
0033 class Score;
0034 class ReflectionGraphicsScene;
0035 
0036 /**
0037  * The view object which shows the graphics for the game.
0038  */
0039 class KWin4View : public QGraphicsView, public virtual Themeable
0040 {
0041     Q_OBJECT
0042 
0043 public:
0044     /**
0045      * Constructor for the canvas view.
0046      * @param updateTime    The canvas advance rate
0047      * @param size          The canvas size
0048      * @param scene         The graphics scene
0049      * @param theme         The theme manager
0050      * @param parent        The parent window
0051      */
0052     KWin4View(int updateTime, const QSize &size, ReflectionGraphicsScene *scene, ThemeManager *theme, QWidget *parent = nullptr);
0053 
0054     /**
0055      * Destructor
0056      */
0057     ~KWin4View() override;
0058 
0059     /**
0060      * Main theme manager function. Called when any theme change like
0061      * a new theme or a theme size change occurs. This object needs to
0062      * resize and redraw then.
0063      */
0064     void changeTheme() override;
0065 
0066     /**
0067      * Initial setup of the game view.
0068      */
0069     void initGame(Score *scoreData);
0070 
0071     /**
0072      * Finalize (end) game.
0073      */
0074     void endGame();
0075 
0076     /**
0077      * Displays a move on the game board. This means a piece of
0078      * the given number is moved to the given position, the move
0079      * indicator arrow is switched on and any hints are disabed.
0080      * The move can be performed animated or not.
0081      * @param x          The x position on the game board [0-6]
0082      * @param y          The y position on the game board [0-5]
0083      * @param color      The color [Red,Yellow,Nobody]
0084      * @param xarrow     The x position of the arrow [0-6]
0085      * @param colorarrow The color or the arrow [Red,Yellow,Nobody]
0086      * @param no         The sprite number / move number
0087      * @param animation  True to make an animated move
0088      */
0089     void displayMove(int x, int y, int color, int xarrow, int colorarrow, int no, bool animation);
0090 
0091     /**
0092      * Displays a star on the game board to indicate victorious pieces.
0093      * @param x          The x position on the game board [0-6]
0094      * @param y          The y position on the game board [0-5]
0095      * @param no         The sprite number / move number
0096      */
0097     void displayStar(int x, int y, int no);
0098 
0099     /**
0100      * Displays a hint on the game board to indicate a good move.
0101      * @param x          The x position on the game board [0-6]
0102      * @param y          The y position on the game board [0-5]
0103      */
0104     void displayHint(int x, int y);
0105 
0106     /**
0107      * Enable reflections on the given position. If width or height is zero
0108      * the reflections are disabled.
0109      * @param x      The x position of the reflection  [screen coord]
0110      * @param y      The y position of the reflection  [screen coord]
0111      * @param width  The width of the reflection  [screen coord]
0112      * @param height The height of the reflection  [screen coord]
0113      */
0114     void setReflection(int x, int y, int width, int height);
0115 
0116 Q_SIGNALS:
0117     /**
0118      * Emit this signal if a sprite animation move is finished.
0119      * @param mode  A user-defined parameter.
0120      */
0121     void signalMoveDone(int mode);
0122 
0123     /**
0124      * Emit this signal if a new game is started from the intro display.
0125      * @param startPlayer Color of the starting player
0126      * @param input0      Input device of player 1
0127      * @param input1      Input device of player 2
0128      * @param aiLevel     Level for AI (-1: no change)
0129      */
0130     void signalQuickStart(COLOUR startPlayer, KGameIO::IOMode input0, KGameIO::IOMode input1, int aiLevel);
0131 
0132 public Q_SLOTS:
0133     /**
0134      * The update and advance for the canvas.
0135      *  This is called by a timer at regular intervals.
0136      */
0137     void updateAndAdvance();
0138 
0139     /**
0140      * Handle mouse inputs for the KGame framework.
0141      * @param input     The IO device
0142      * @param stream    The KGame message stream
0143      * @param mouse     The mouse event
0144      * @param eatevent  Set to true if the event was processed
0145      */
0146     void mouseInput(KGameIO *input, QDataStream &stream, QMouseEvent *mouse, bool *eatevent);
0147 
0148     /**
0149      * Handle key inputs for the KGame framework.
0150      * @param input     The IO device
0151      * @param stream    The KGame message stream
0152      * @param key       The key event
0153      * @param eatevent  Set to true if the event was processed
0154      */
0155     void keyInput(KGameIO *input, QDataStream &stream, QKeyEvent *key, bool *eatevent);
0156 
0157     /**
0158      * Animation of a sprite is finished.
0159      * @param item   The item
0160      * @param mode   A user defined mode
0161      */
0162     void moveDone(QGraphicsItem *item, int mode);
0163 
0164     /**
0165      * Rescale the theme (update theme SVG graphics).
0166      */
0167     void rescaleTheme();
0168 
0169 protected:
0170     /**
0171      * Will be called when the widgets contents
0172      * are resized. Resized and rescale game.
0173      * @param e The resize event
0174      */
0175     void resizeEvent(QResizeEvent *e) override;
0176 
0177     /**
0178      * Widget viewport event.
0179      * @param event The event.
0180      */
0181     bool viewportEvent(QEvent *event) override;
0182 
0183     /**
0184      * Overwritten Qt function.
0185      */
0186     void drawItems(QPainter *painter, int numItems, QGraphicsItem *items[], const QStyleOptionGraphicsItem options[]) override;
0187 
0188 private:
0189     // The theme manager
0190     ThemeManager *mTheme;
0191 
0192     // Theme queue
0193     QList<int> mThemeQueue;
0194     // Theme offset queque
0195     QList<QPoint> mThemeOffset;
0196 
0197     // The scene to plot to
0198     ReflectionGraphicsScene *mScene;
0199 
0200     // The advance period of the scene [ms]
0201     int mAdvancePeriod;
0202 
0203     // The intro display engine
0204     DisplayIntro *mIntroDisplay;
0205 
0206     // The game display engine
0207     DisplayGame *mGameDisplay;
0208 
0209     // Status of the game (running or not)
0210     bool mIsRunning;
0211 
0212     // Gradient for the reflection
0213     QLinearGradient mGradient;
0214     // Pixmap for the reflection
0215     QPixmap mGradientPixmap;
0216     // Reflection sprite
0217     QGraphicsPixmapItem *mReflectionSprite;
0218     // Refection size
0219     QRect mReflectionRect;
0220     // Paint pixmap of reflection
0221     QPixmap mReflectPixmap;
0222     // Phase of reflection drawing
0223     int mReflectPhase;
0224 
0225     // Debug frame rate sprite
0226     QGraphicsTextItem *mFrameSprite;
0227     // Average update times
0228     QList<int> mDrawTimes;
0229 
0230     // Update and advance timer
0231     QTimer *mTimer;
0232     // Default update time [ms]
0233     int mDefaultUpdateTime;
0234     // Update slow down factor
0235     double mSlowDownFactor;
0236     // Slow incident counter
0237     int mSlowCnt;
0238 };
0239 
0240 #endif // KWIN4_KWIN4VIEW_H