File indexing completed on 2023-09-24 08:14:02

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