File: displaygame.cpp

package info (click to toggle)
kfourinline 4:18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,784 kB
  • sloc: cpp: 7,339; sh: 8; makefile: 5
file content (436 lines) | stat: -rw-r--r-- 11,612 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
   This file is part of the KDE games kwin4 program
   Copyright (c) 2006 Martin Heni <kde@heni-online.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Header includes
#include "displaygame.h"

// Standard includes
#include <cmath>
#include <cassert>

// Qt includes
#include <QColor>
#include <QPixmap>
#include <QPoint>

// KDE includes
#include "kfourinline_debug.h"

// Local includes
#include "thememanager.h"
#include "piecesprite.h"
#include "pixmapsprite.h"
#include "scoresprite.h"
#include "spritenotify.h"
#include "kwin4doc.h"
#include "kwin4view.h"

#include "reflectiongraphicsscene.h"



// Constructor for the display
DisplayGame::DisplayGame(ReflectionGraphicsScene* scene, ThemeManager* theme, QGraphicsView* parent)
           : Themeable(QStringLiteral("gamedisplay"),theme), QObject(parent)
{
  // Store arguments as attributes
  mScene         = scene;
  mView          = parent;
  mTheme         = theme;
  
    // Choose a background color
  scene->setBackgroundBrush(QColor(0,0,128));

  // Clear storage of all sprites
  mSprites.clear();

  // Create piece sprites 
  for (int i=0; i<42; i++)
  {
    PieceSprite* sprite = new PieceSprite(QStringLiteral("piece"), mTheme, i, mScene);
    if (!sprite) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << "piece";
    mSprites.append(sprite);
    mPieces.append(sprite);
    sprite->hide();
  }

  // Create stars
  for (int i=0;i<4;i++)
  {
    PixmapSprite* sprite = new PixmapSprite(QStringLiteral("star"), mTheme, i, mScene);
    if (!sprite) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << "star";
    mSprites.append(sprite);
    mStars.append(sprite);
    sprite->hide();
  }

  // Create board
  mBoard = new PixmapSprite(QStringLiteral("board"), mTheme, 0, mScene);
  if (!mBoard) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << "board";
  mSprites.append(mBoard);
  mBoard->hide();

  // Create hint
  mHint = new PixmapSprite(QStringLiteral("hint"), mTheme, 0, mScene);
  if (!mHint) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << "hint";
  mSprites.append(mHint);
  mHint->hide();

  // Create Game Over
  // mGameOver = new PixmapSprite("gameover", mTheme, 0, mScene);
  // if (!mGameOver) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << "gameover";
  // mSprites.append(mGameOver);
  // mGameOver->hide();

  // Create score board
  mScoreBoard = new ScoreSprite(QStringLiteral("scoreboard"), mTheme, 0, mScene);
  if (!mScoreBoard) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << "scoreboard";
  mSprites.append(mScoreBoard);
  mScoreBoard->hide();


  // Create movement indication arrows
  for (int i=0; i<7; i++)
  {
    PixmapSprite* arrow = new PixmapSprite(QStringLiteral("arrow"), mTheme, i, mScene);
    if (!arrow) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << "arrow";
    mSprites.append(arrow);
    mArrows.append(arrow);
    arrow->hide();
  }

  // Static decoration
  KConfigGroup config = thememanager()->config(id());
  QStringList deco = config.readEntry("decoration", QStringList());
  for (int i = 0; i < deco.size(); i++)
  {
    PixmapSprite* sprite = new PixmapSprite(deco.at(i), mTheme, i, mScene);
    if (!sprite) qCCritical(KFOURINLINE_LOG) << "Cannot load sprite" << deco.at(i);
    mSprites.append(sprite);
    sprite->show();
  }

  // Animation timer
  mTimer = new QTimer(this);
  connect(mTimer, &QTimer::timeout, this, &DisplayGame::advance);

  // Redraw
  if (theme) theme->updateTheme(this);

}


// Destructor: clean up
DisplayGame::~DisplayGame()
{
  delete mTimer;
  qDeleteAll(mSprites);
}


// Called by thememanager when theme or theme geometry changes. Redraw and resize
// this display.
void DisplayGame::changeTheme()
{
  // Retrieve theme data
  KConfigGroup config = thememanager()->config(id());
  QPointF board_pos    = config.readEntry("board-pos", QPointF(1.0,1.0));
  QPointF arrow_pos    = config.readEntry("arrow-pos", QPointF(1.0,1.0));
  QPointF board_spread = config.readEntry("board-spread", QPointF(1.0,1.0));

  // Retrieve background pixmap
  QString bgsvgid = config.readEntry("background-svgid");
  QPixmap pixmap  = thememanager()->getPixmap(bgsvgid, mScene->sceneRect().size().toSize());
  mScene->setBackgroundBrush(pixmap);
  QRectF pos = mBoard->sceneBoundingRect();
  

  // Move movement arrows
  for (int i=0; i<7; i++)
  {
    PixmapSprite* arrow = mArrows.value(i);
    QPointF to   = QPointF(board_spread.x()*i + arrow_pos.x(),
                           board_spread.y()*0 + arrow_pos.y());
    arrow->setPosition(to);

  }
  
  // Move piece sprites 
  for (int i=0; i<42; i++)
  {
    PieceSprite* piece = mPieces.value(i);
    int x = piece->logicalPos().x();
    int y = piece->logicalPos().y();
    QPointF to   = QPointF(board_spread.x()*x + board_pos.x(),
                           board_spread.y()*y + board_pos.y());
    piece->setPosition(to);
  }
  
  // Move hint
  {
    int x = mHint->logicalPos().x();
    int y = mHint->logicalPos().y();
    QPointF to   = QPointF(board_spread.x()*x + board_pos.x(),
                           board_spread.y()*y + board_pos.y());
    mHint->setPosition(to);
  }
  
  
  // Move stars
  for (int i=0;i<4;i++)
  {
    PixmapSprite* star = mStars.value(i);
    int x = star->logicalPos().x();
    int y = star->logicalPos().y();
    QPointF to  = QPointF(board_spread.x()*x    + board_pos.x(),
                          board_spread.y()*y    + board_pos.y());
    star->setPosition(to);
  }
  

  // Check whether the theme uses reflection handling
  if( config.readEntry("use-reflection", false))
  {
     dynamic_cast<KWin4View*>(mView)->setReflection((int)pos.x(), (int)(pos.y()+pos.height()), 
                          (int)(mScene->sceneRect().width() - pos.x()), (int)(pos.height()*0.3));
  }
  else
  {
    // Zero width disables the reflections
     dynamic_cast<KWin4View*>(mView)->setReflection(0,0,0,0);
  }
  
  mView->update();
}


// Start a new game. Initialize graphics.
void DisplayGame::start()
{
        // Run timer (unused)
  // mTimer->setSingleShot(true);
  // mTimer->start(0);


  // Show decoration
  mBoard->show();
  mScoreBoard->show();

  
  // Show movement arrows
  for (int i=0; i<7; i++)
  {
    mArrows.value(i)->setFrame(0);
    mArrows.value(i)->show();
  }

  // Hide piece sprites 
  for (int i=0; i<42; i++)
  {
    mPieces.value(i)->hide();
  }
  
  // Hide stars
  for (int i=0;i<4;i++)
  {
    mStars.value(i)->setAnimation(false);
    mStars.value(i)->hide();
  }

}


// Run game animation
void DisplayGame::advance()
{
         // Currently unused
}


// Display end game sprite
void DisplayGame::displayEnd()
{
  // assert(mGameOver != 0);
  // mGameOver->show();
}


// Set the movement indicator arrows above the game board
void DisplayGame::displayArrow(int x,int color)
{
  // Set all arrows back to frame 0
  for (int i=0; i<7; i++)
  {
    mArrows.value(i)->setFrame(0);
  }

  // Check for no color 
  if (color==Nobody)
  {
    return ;
  }

  // Make sure the frames are chosen properly
  if (color==Yellow) mArrows.value(x)->setFrame(1);
  else mArrows.value(x)->setFrame(2);
}


// Set a game HINT sprite
void DisplayGame::displayHint(int x, int y, bool show)
{
  // Invert height
  y=5-y;

  // Check for removal of the sprite
  if (!show)
  {
    mHint->hide();
    return;
  }

  // Retrieve theme data
  KConfigGroup config   = thememanager()->config(id());
  QPointF board_pos     = config.readEntry("board-pos", QPointF(1.0,1.0));
  QPointF board_spread  = config.readEntry("board-spread", QPointF(1.0,1.0));

  QPointF to   = QPointF(board_spread.x()*x + board_pos.x(),
                         board_spread.y()*y + board_pos.y());
  mHint->setLogicalPos(QPoint(x,y));                       
  mHint->setPosition(to);
  mHint->show();
}


// Set a game piece, red or yellow or hidden depending on 'color'
SpriteNotify* DisplayGame::displayPiece(int x, int y, int color, int no, bool animation)
{
  // Invert height
  y=5-y;

  // Get piece
  PieceSprite *sprite = mPieces.value(no);
  assert(sprite != 0);

  // Check for removal of sprite
  if (color==Nobody)
  {
    sprite->hide();
    return 0;
  }

  // Retrieve theme data
  KConfigGroup config   = thememanager()->config(id());
  QPointF board_pos     = config.readEntry("board-pos", QPointF(1.0,1.0));
  QPointF board_spread  = config.readEntry("board-spread", QPointF(1.0,1.0));
  double velocity       = config.readEntry("move-velocity", 0.8);

  // Make sure the frames are ok
  int frame;
  if (color==Yellow) frame = 0;
  else frame = 1;


  // Just draw the sprites or show an movement animation?
  if (animation)
  {
    QPointF from = QPointF(board_spread.x()*x    + board_pos.x(),
                           board_spread.y()*(-1) + board_pos.y());
    QPointF to   = QPointF(board_spread.x()*x    + board_pos.x(),
                           board_spread.y()*y    + board_pos.y());
    sprite->setFrame(frame);
    sprite->startLinear(from, to, velocity);
    // For theme store (x,y) board coordinate 
    sprite->setLogicalPos(QPoint(x, y));
  }
  else
  {
    QPointF to   = QPointF(board_spread.x()*x + board_pos.x(),
                           board_spread.y()*y + board_pos.y());
    sprite->setFrame(frame);
    sprite->setPosition(to);
    // For theme store (x,y) board coordinate 
    sprite->setLogicalPos(QPoint(x, y));
  }

  sprite->show();
  return sprite->notify();
}


// Draw Star Sprites as winning indicator
void DisplayGame::displayStar(int x,int y,int no)
{
  // Invert height
  y=5-y;
  PixmapSprite* star = mStars.value(no-1);
  assert(star != 0);

  // Retrieve theme data
  KConfigGroup config  = thememanager()->config(id());
  QPointF board_pos    = config.readEntry("board-pos", QPointF(1.0,1.0));
  QPointF board_spread = config.readEntry("board-spread", QPointF(1.0,1.0));

  QPointF pos  = QPointF(board_spread.x()*x    + board_pos.x(),
                         board_spread.y()*y    + board_pos.y());
  star->setAnimation(true);
  star->setLogicalPos(QPoint(x,y));
  star->setPosition(pos);
  star->show();
}


// Return the mouse mapped to the board or bar item so that a
// move 0..6 is generated. -1 means an illegal position.
int DisplayGame::mapMouseToMove(const QPoint &pos)
{
  // Error?
  if (!mBoard) return -1;

  // Find which arrow the mouse is closest to. This way
  // all board scaling become irrelevant. An alternative
  // would be to calculate the position using board_pos and
  // board_spread.
  for (int i=0; i<7; i++)
  {
    PixmapSprite* arrow = mArrows.value(i);
    int width           = int(arrow->boundingRect().width());
    int relPos          = int(arrow->mapFromParent(QPointF(pos)).x());
    // Found matching arrow
    if (relPos > 0 && relPos<= width)
    {
      return i;
    }
  }

  // Nothing found
  return -1;
}



// Retrieve the score sprite.
ScoreSprite* DisplayGame::score()
{
  return mScoreBoard;
}