1 /*
2     This file is part of the KDE games kwin4 program
3     SPDX-FileCopyrightText: 2006 Martin Heni <kde@heni-online.de>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef KWIN4_KWIN4VIEW_H
9 #define KWIN4_KWIN4VIEW_H
10 
11 // own
12 #include "thememanager.h"
13 #include "kwin4global.h"
14 // KDEGames
15 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API
16 #include <libkdegamesprivate/kgame/kgameio.h>
17 // Qt
18 #include <QDataStream>
19 #include <QGraphicsPixmapItem>
20 #include <QGraphicsView>
21 #include <QImage>
22 #include <QLinearGradient>
23 #include <QMouseEvent>
24 #include <QPoint>
25 #include <QRect>
26 #include <QResizeEvent>
27 #include <QSize>
28 #include <QTimer>
29 #include <QWidget>
30 
31 // Forward declaration
32 class DisplayIntro;
33 class DisplayGame;
34 class Score;
35 class ReflectionGraphicsScene;
36 
37 /**
38  * The view object which shows the graphics for the game.
39  */
40 class KWin4View : public QGraphicsView, public virtual Themeable
41 {
42   Q_OBJECT
43 
44   public:
45     /** Constructor for the canvas view.
46      *  @param updateTime    The canvas advance rate
47      *  @param size          The canvas size
48      *  @param scene         The graphics scene
49      *  @param theme         The theme manager
50      *  @param parent        The parent window
51      */
52     KWin4View(int updateTime,
53               const QSize &size,
54               ReflectionGraphicsScene* scene,
55               ThemeManager* theme,
56               QWidget* parent = nullptr);
57 
58     /** Destructor
59       */
60     ~KWin4View() override;
61 
62     /** Main theme manager function. Called when any theme change like
63       * a new theme or a theme size change occurs. This object needs to
64       * resize and redraw then.
65       */
66     void changeTheme() override;
67 
68     /** Initial setup of the game view.
69       */
70     void initGame(Score *scoreData);
71 
72     /** Finalize (end) game.
73       */
74     void endGame();
75 
76     /** Displays a move on the game board. This means a piece of
77       * the given number is moved to the given position, the move
78       * indicator arrow is switched on and any hints are disabed.
79       * The move can be performed animated or not.
80       * @param x          The x position on the game board [0-6]
81       * @param y          The y position on the game board [0-5]
82       * @param color      The color [Red,Yellow,Nobody]
83       * @param xarrow     The x position of the arrow [0-6]
84       * @param colorarrow The color or the arrow [Red,Yellow,Nobody]
85       * @param no         The sprite number / move number
86       * @param animation  True to make an animated move
87       */
88     void displayMove(int x, int y, int color, int xarrow, int colorarrow, int no, bool animation);
89 
90     /** Displays a star on the game board to indicate victorious pieces.
91       * @param x          The x position on the game board [0-6]
92       * @param y          The y position on the game board [0-5]
93       * @param no         The sprite number / move number
94       */
95     void displayStar(int x, int y, int no);
96 
97     /** Displays a hint on the game board to indicate a good move.
98       * @param x          The x position on the game board [0-6]
99       * @param y          The y position on the game board [0-5]
100       */
101     void displayHint(int x, int y);
102 
103     /** Enable reflections on the given position. If width or height is zero
104       * the reflections are disabled.
105       * @param x      The x position of the reflection  [screen coord]
106       * @param y      The y position of the reflection  [screen coord]
107       * @param width  The width of the reflection  [screen coord]
108       * @param height The height of the reflection  [screen coord]
109       */
110     void setReflection(int x, int y, int width, int height);
111 
112   Q_SIGNALS:
113     /** Emit this signal if a sprite animation move is finished.
114       * @param mode  A user-defined parameter.
115       */
116     void signalMoveDone(int mode);
117 
118     /** Emit this signal if a new game is started from the intro display.
119       * @param startPlayer Color of the starting player
120       * @param input0      Input device of player 1
121       * @param input1      Input device of player 2
122       * @param aiLevel     Level for AI (-1: no change)
123       */
124     void signalQuickStart(COLOUR startPlayer, KGameIO::IOMode input0, KGameIO::IOMode input1, int aiLevel);
125 
126   public Q_SLOTS:
127     /** The update and advance for the canvas.
128      *  This is called by a timer at regular intervals.
129      */
130     void updateAndAdvance();
131 
132     /** Handle mouse inputs for the KGame framework.
133       * @param input     The IO device
134       * @param stream    The KGame message stream
135       * @param mouse     The mouse event
136       * @param eatevent  Set to true if the event was processed
137       */
138     void mouseInput(KGameIO* input, QDataStream& stream, QMouseEvent* mouse, bool* eatevent);
139 
140     /** Handle key inputs for the KGame framework.
141       * @param input     The IO device
142       * @param stream    The KGame message stream
143       * @param key       The key event
144       * @param eatevent  Set to true if the event was processed
145       */
146     void keyInput(KGameIO* input, QDataStream& stream, QKeyEvent* key, bool* eatevent);
147 
148      /** Animation of a sprite is finished.
149        * @param item   The item
150        * @param mode   A user defined mode
151        */
152      void moveDone(QGraphicsItem* item, int mode);
153 
154      /** Rescale the theme (update theme SVG graphics).
155       */
156     void rescaleTheme();
157 
158 
159   protected:
160     /**
161      * Will be called when the widgets contents
162      * are resized. Resized and rescale game.
163      * @param e The resize event
164      */
165     void resizeEvent(QResizeEvent* e) override;
166 
167     /** Widget viewport event.
168       * @param event The event.
169       */
170     bool viewportEvent ( QEvent * event )   override;
171 
172    /** Overwritten Qt function.
173     */
174     void drawItems(QPainter *painter, int numItems, QGraphicsItem *items[], const QStyleOptionGraphicsItem options[]) override;
175 
176 
177   private:
178     // The theme manager
179     ThemeManager* mTheme;
180 
181     // Theme queue
182     QList<int> mThemeQueue;
183     // Theme offset queque
184     QList<QPoint> mThemeOffset;
185 
186     // The scene to plot to
187     ReflectionGraphicsScene* mScene;
188 
189     // The advance period of the scene [ms]
190     int mAdvancePeriod;
191 
192     // The intro display engine
193     DisplayIntro* mIntroDisplay;
194 
195     // The game display engine
196     DisplayGame* mGameDisplay;
197 
198     // Status of the game (running or not)
199     bool mIsRunning;
200 
201     // Gradient for the reflection
202     QLinearGradient mGradient;
203     // Image for the reflection
204     QImage mGradientImage;
205     // Reflection sprite
206     QGraphicsPixmapItem* mReflectionSprite;
207     // Refection size
208     QRect mReflectionRect;
209     // Paint image of reflection
210     QImage mReflectImage;
211     // Phase of reflection drawing
212     int mReflectPhase;
213 
214     // Debug frame rate sprite
215     QGraphicsTextItem* mFrameSprite;
216     // Average update times
217     QList<int> mDrawTimes;
218 
219     // Update and advance timer
220     QTimer* mTimer;
221     // Default update time [ms]
222     int mDefaultUpdateTime;
223     // Update slow down factor
224     double mSlowDownFactor;
225     // Slow incident counter
226     int mSlowCnt;
227 };
228 
229 #endif // KWIN4_KWIN4VIEW_H
230