]> Shamusworld >> Repos - warehouse-man-deluxe/blob - src/mainwin.cpp
Converted from Qt4 to Qt5.
[warehouse-man-deluxe] / src / mainwin.cpp
1 //
2 // mainwin.cpp: Implementation of the MainWin class
3 //
4 //   JLH = James Hammons <jlhamm@acm.org>
5 //
6 // Who  When        What
7 // ---  ----------  ------------------------------------------------------------
8 // JLH  03/01/2014  Created this file
9 //
10
11 // TO DO:
12 //
13
14 #include "mainwin.h"
15
16 #include <stdlib.h>                     // For rand()
17 #include <time.h>                       // For time()
18 #include "gameboard.h"
19 #include "editorwindow.h"       // Actual mouse/drawing window
20 #include "gamewidget.h"         // Actual mouse/drawing window
21 //#include "resource.h"
22 //#include "optiondlg.h"                // Options dialog class
23 //#include "statdlg.h"          // Statistics dialog class
24
25
26 MainWin::MainWin()
27 {
28         // We create the EditorWindow 1st because the GameWidget will crash if there
29         // is no level data loaded...
30         editorWindow = new EditorWindow(this);
31         gameWidget = new GameWidget(this);
32         setCentralWidget(gameWidget);
33         gameWidget->setFocusPolicy(Qt::StrongFocus);    // Without this, it gets no keys
34         setWindowTitle("Warehouse Man Deluxe");
35         setWindowIcon(QIcon(":/wmd-icon.png"));
36
37         editorWindow->show();
38
39         newGame = CreateAction(tr("&New"), tr("New Game"), tr("Start a new game of Warehouse Man Deluxe"), QIcon(), QKeySequence(tr("ctrl+n")));
40         connect(newGame, SIGNAL(triggered()), this, SLOT(HandleNewGame()));
41
42         undoAction = CreateAction(tr("&Undo"), tr(""), tr(""), QIcon(), QKeySequence(tr("ctrl+z")));
43         connect(undoAction, SIGNAL(triggered()), this, SLOT(HandleUndo()));
44
45         resetLevel = CreateAction(tr("&Reset Level"), tr("Reset Level"), tr("Start the current level over"), QIcon(), QKeySequence(tr("ctrl+r")));
46         connect(resetLevel, SIGNAL(triggered()), this, SLOT(HandleReset()));
47
48         skipLevel = CreateAction(tr("&Skip Level"), tr("Skip Level"), tr("Skip the current level"), QIcon(), QKeySequence(tr("ctrl+k")));
49         connect(skipLevel, SIGNAL(triggered()), this, SLOT(HandleSkipLevel()));
50
51         gameOptions = CreateAction(tr("&Options..."), tr("Options"), tr("Configure Warehouse Man Deluxe's options"), QIcon(), QKeySequence(tr("ctrl+o")));
52 //      connect(gameOptions, SIGNAL(triggered()), this, SLOT(OnGameOptions()));
53
54         gameStats = CreateAction(tr("&Stats..."), tr("Game Stats"), tr("Show game stats"), QIcon(), QKeySequence(tr("ctrl+s")));
55 //      connect(gameStats, SIGNAL(triggered()), this, SLOT(OnGameStats()));
56
57         appExit = CreateAction(tr("E&xit"), tr("Exit"), tr("Quit playing Warehouse Man Deluxe..."), QIcon(), QKeySequence(tr("ctrl+q")));
58         connect(appExit, SIGNAL(triggered()), this, SLOT(close()));
59
60         appAbout = CreateAction(tr("&About"), tr("About"), tr("Display program information..."), QIcon(), QKeySequence(tr("")));
61         connect(appAbout, SIGNAL(triggered()), this, SLOT(AboutGame()));
62
63         // Get signals from the gameboard to update scores...
64 //      connect(gameBoard, SIGNAL(UpdateScore(int)), this, SLOT(OnUpdateScore(int)));
65         connect(gameWidget, SIGNAL(GameWasWon()), this, SLOT(WeHaveAWinner()));
66
67         // Connect editor to game widget...
68         connect(editorWindow, SIGNAL(SetupLevel(Level *)), gameWidget,
69                 SLOT(HandlePlayGameFromEditor(Level *)));
70
71
72         QMenu * menu = menuBar()->addMenu(tr("&Game"));
73         menu->addAction(newGame);
74         menu->addSeparator();
75 //      menu->addAction(gamePlay);
76         menu->addAction(undoAction);
77         menu->addAction(resetLevel);
78         menu->addAction(skipLevel);
79         menu->addSeparator();
80         menu->addAction(gameOptions);
81         menu->addAction(gameStats);
82         menu->addSeparator();
83         menu->addAction(appExit);
84
85         menu = menuBar()->addMenu(tr("&Help"));
86         menu->addAction(appAbout);
87
88 //      statusBar()->addPermanentWidget(gameBoard->score);
89         statusBar()->showMessage(tr("Ready"));
90
91         QSettings settings("Underground Software", "Warehouse Man Deluxe");
92         QPoint mainWinPosition = settings.value("pos", QPoint(200, 100)).toPoint();
93         move(mainWinPosition);
94         QSize mainWinSize = settings.value("size", QSize(500, 500)).toSize();
95         resize(mainWinSize);
96
97 /*      nCardBack = settings.value("CardBack", BACK4).toInt();
98         m_nBackground = settings.value("Background", 0).toInt();
99         bSoundOn = settings.value("EnableSound", true).toBool();
100         m_bAnimationsOn = settings.value("EnableAnimations", true).toBool();
101         bAutoRemove = settings.value("AutoRemove", true).toBool();
102         scoringOn = settings.value("EnableScoring", false).toBool();
103         m_bCumulativeOn = settings.value("CumulativeScore", false).toBool();
104         m_bVegasStyle = settings.value("VegasScoring", false).toBool();
105         m_bDrawThree = settings.value("DrawThree", false).toBool();
106         m_nWins = settings.value("NumWins", 0).toInt();
107         m_nLosses = settings.value("NumLosses", 0).toInt();
108         m_nHiScore = settings.value("HiScore", 0).toInt();
109         m_nVHiScore = settings.value("VegasHiScore", 0).toInt();
110         m_nStreakW = settings.value("WinStreak", 0).toInt();
111         m_nStreakL = settings.value("LoseStreak", 0).toInt();
112         m_nStreakC = settings.value("CurrentStreak", 0).toInt();
113         m_bWonLastGame = settings.value("WonLastGame", false).toBool();
114         m_nScore = settings.value("LastScore", -52).toInt();*/
115
116 //      gameBoard->CreateBackground();
117 //      InitializeAudio();
118
119 //      mouseDown = false;
120 //      myDeck.SetCardWidth(CARD_WIDTH);
121 //      myDeck.SetCardHeight(CARD_HEIGHT);
122 //
123 //      for(int i=0; i<52; i++)
124 //              myDeck.SetCardPosition(i, QPoint(0, 0));
125
126         ResetGame();
127 //      allowUserInput = true;
128 //      m_bFreeCard = false;
129 //      m_pFirstCard = NULL;
130 }
131
132
133 MainWin::~MainWin()
134 {
135 //      ShutDownAudio();
136         delete editorWindow;
137 }
138
139
140 //
141 // Consolidates action creation from a multi-step process to a single-step one.
142 //
143 QAction * MainWin::CreateAction(QString name, QString tooltip, QString statustip,
144         QIcon icon, QKeySequence key, bool checkable/*= false*/)
145 {
146         QAction * action = new QAction(icon, name, this);
147         action->setToolTip(tooltip);
148         action->setStatusTip(statustip);
149         action->setShortcut(key);
150         action->setCheckable(checkable);
151
152         return action;
153 }
154
155
156 //
157 // This is essentially the same as the previous function, but this allows more
158 // than one key sequence to be added as key shortcuts.
159 //
160 QAction * MainWin::CreateAction(QString name, QString tooltip, QString statustip,
161         QIcon icon, QKeySequence key1, QKeySequence key2, bool checkable/*= false*/)
162 {
163         QAction * action = new QAction(icon, name, this);
164         action->setToolTip(tooltip);
165         action->setStatusTip(statustip);
166         QList<QKeySequence> keyList;
167         keyList.append(key1);
168         keyList.append(key2);
169         action->setShortcuts(keyList);
170         action->setCheckable(checkable);
171
172         return action;
173 }
174
175
176 void MainWin::closeEvent(QCloseEvent * event)
177 {
178 /*      if (allowUserInput && m_bTouchedGame)
179         {
180                 gameBoard->HandleStatistics();
181                 m_bWonLastGame = false;
182         }*/
183
184         QSettings settings("Underground Software", "Warehouse Man Deluxe");
185         settings.setValue("pos", pos());
186         settings.setValue("size", size());
187
188 /*
189         settings.setValue("CardBack", nCardBack);
190         settings.setValue("Background", m_nBackground);
191         settings.setValue("EnableSound", bSoundOn);
192         settings.setValue("EnableAnimations", m_bAnimationsOn);
193         settings.setValue("AutoRemove", bAutoRemove);
194         settings.setValue("EnableScoring", scoringOn);
195         settings.setValue("CumulativeScore", m_bCumulativeOn);
196         settings.setValue("VegasScoring", m_bVegasStyle);
197         settings.setValue("DrawThree", m_bDrawThree);
198         settings.setValue("NumWins", m_nWins);
199         settings.setValue("NumLosses", m_nLosses);
200         settings.setValue("HiScore", m_nHiScore);
201         settings.setValue("VegasHiScore", m_nVHiScore);
202         settings.setValue("WinStreak", m_nStreakW);
203         settings.setValue("LoseStreak", m_nStreakL);
204         settings.setValue("CurrentStreak", m_nStreakC);
205         settings.setValue("WonLastGame", m_bWonLastGame);
206         settings.setValue("LastScore", m_nScore);*/
207
208         event->accept();
209 }
210
211
212 /*
213 void MainWin::OnUpdateScore(int n)
214 {
215         if (!scoringOn)
216                 return;
217
218         QString s;
219
220         if (m_bVegasStyle)
221                 s = QString(tr("Score: $%1")).arg(n);
222         else
223                 s = QString(tr("Score: %1")).arg(n);
224
225         gameBoard->score->setText(s);
226 printf("OUS: n=%i, s=%s\n", n, s.toUtf8().data());
227 }
228 */
229
230 void MainWin::ResetGame(void)
231 {
232         gameWidget->level = 1;
233
234 /*      myDeck.Shuffle();
235
236         for(int i=0; i<7; i++)
237                 for(int j=0; j<20; j++)
238                         solBoard[i][j] = -1; // Empty the board...
239
240         int card = 0;
241         myDeck.SetAllFaceDown();
242
243         for(int i=0; i<7; i++)       // Fill the tableaux
244         {
245                 for(int j=i; j<7; j++)
246                 {
247                         solBoard[6 - i][6 - j] = card;
248
249                         if (i == j)
250                                 myDeck.SetCardFaceUp(card);
251
252                         card++;
253                 }
254         }
255
256         for(int i=0; i<24; i++)   // Fill the stack
257                 stack1[i] = card++;
258
259         stack1p = 23;
260         stack2p = -1;
261         nAce[0] = nAce[1] = nAce[2] = nAce[3] = -1;
262
263         hitStack = hitDiscard = false;
264         nHitAce = 0;
265         m_bTouchedGame = false;
266
267         if (!m_bVegasStyle)
268                 m_nScore = 0;
269         else
270         {
271                 if (!m_bCumulativeOn)
272                         m_nScore = -52;
273                 else
274                         m_nScore -= 52;
275         }
276
277         m_nExhaustedDeck = 0;
278         m_bShowX = false;
279         m_bCanUndo = false;
280
281         OnUpdateScore(m_nScore);*/
282 }
283
284
285 void MainWin::HandleNewGame(void)
286 {
287 //      ResetGame();
288         gameWidget->level = 0;
289         gameWidget->NextLevel();
290 }
291
292
293 void MainWin::AboutGame(void)
294 {
295         QMessageBox::about(this, tr("About Warehouse Man Deluxe"), tr("Warehouse Man Deluxe Version 1.0\n\nCopyright (C) 2014 Underground Software\n\nWritten by James Hammons"));
296 }
297
298
299 void MainWin::WeHaveAWinner(void)
300 {
301         QMessageBox::information(this, "Warehouse Man Deluxe", "Congratulations!\nYou beat this level!\nNow try the next level!");
302         gameWidget->NextLevel();
303 }
304
305
306 void MainWin::HandleReset(void)
307 {
308         gameWidget->ResetLevel();
309 }
310
311
312 void MainWin::HandleSkipLevel(void)
313 {
314         gameWidget->NextLevel();
315 }
316
317
318 void MainWin::HandleUndo(void)
319 {
320         gameWidget->UndoLastMove();
321 }
322
323
324 #if 0
325 void MainWin::OnNewGame(void) 
326 {
327         if (allowUserInput && m_bTouchedGame)   // Player didn't win...
328         {
329                 gameBoard->HandleStatistics();
330                 m_bWonLastGame = false;
331         }
332
333         ResetGame();
334         gameBoard->update();
335         allowUserInput = true;
336
337 #if HAVE_SOUND
338         if (bSoundOn)
339         {
340                 PlaySound(IDW_CARDKNOCK);
341                 gameBoard->Pause(300);
342                 PlaySound(IDW_SHUFFLE);
343         }
344 #endif
345 }
346
347
348 void MainWin::OnGameOptions(void)
349 {
350         OptionDlg dlg;
351
352         dlg.m_cardBack = nCardBack - BACK1;
353         dlg.m_nBackground = m_nBackground;
354         dlg.m_bCheck1 = bSoundOn;
355         dlg.m_bCheck2 = m_bAnimationsOn;
356         dlg.m_bCheck3 = bAutoRemove;
357         dlg.m_bCheck4 = scoringOn;
358         dlg.m_bCheck5 = m_bCumulativeOn;
359         dlg.m_nRadio1 = m_bVegasStyle;
360         dlg.m_nRadio3 = m_bDrawThree;
361
362         dlg.UpdateUI();
363
364         if (dlg.exec() == QDialog::Rejected)
365                 return;
366
367         dlg.UpdateData();
368
369         nCardBack = dlg.m_cardBack + BACK1;
370         m_nBackground = dlg.m_nBackground;
371         bSoundOn = (bool)dlg.m_bCheck1;
372         m_bAnimationsOn = (bool)dlg.m_bCheck2;
373         bAutoRemove = (bool)dlg.m_bCheck3;
374         m_bCumulativeOn = (bool)dlg.m_bCheck5;
375
376         // Reset game if Vegas Style/Draw Three changes at all...
377         bool bChanged =  (bool)((m_bVegasStyle != (bool)dlg.m_nRadio1)
378                 || (m_bDrawThree != (bool)dlg.m_nRadio3) || (scoringOn != (bool)dlg.m_bCheck4));
379
380         scoringOn = (bool)dlg.m_bCheck4;
381         m_bVegasStyle = (bool)dlg.m_nRadio1;
382         m_bDrawThree = (bool)dlg.m_nRadio3;
383
384         if (bChanged && m_bTouchedGame)
385                 OnNewGame();  // Probably should warn of this...
386
387         gameBoard->CreateBackground();
388         gameBoard->update();
389 }
390
391
392 void MainWin::OnGameStats(void)
393 {
394         StatDlg dlg;
395
396 /*      dlg.m_nWins = m_nWins;
397         dlg.m_nLosses = m_nLosses;
398         dlg.m_nHiScore = m_nHiScore;
399         dlg.m_nVHiScore = m_nVHiScore;
400         dlg.m_nStreakW = m_nStreakW;
401         dlg.m_nStreakL = m_nStreakL;
402         dlg.m_nStreakC = m_nStreakC;*/
403
404         dlg.exec();
405
406         // Since it's possible to reset the stats, we grab the fields back here...
407 /*      m_nWins = dlg.m_nWins;
408         m_nLosses = dlg.m_nLosses;
409         m_nHiScore = dlg.m_nHiScore;
410         m_nVHiScore = dlg.m_nVHiScore;
411         m_nStreakW = dlg.m_nStreakW;
412         m_nStreakL = dlg.m_nStreakL;
413         m_nStreakC = dlg.m_nStreakC;*/
414 }
415
416
417 void MainWin::OnundoAction(void)
418 {
419         if (m_bCanUndo)
420         {
421                 for(int i=0; i<7; i++)
422                         for(int j=0; j<20; j++)
423                                 solBoard[i][j] = solBoardUndo[i][j];
424
425                 for(int i=0; i<24; i++)
426                         stack1[i] = stack1Undo[i], stack2[i] = stack2Undo[i];
427
428                 for(int i=0; i<4; i++)
429                         nAce[i] = nAceUndo[i];
430
431                 stack1p = stack1pUndo, stack2p = stack2pUndo;
432                 m_nScore = m_nScoreUndo;
433                 m_nExhaustedDeck = m_nExhaustedDeckUndo;
434                 m_nNumToSplay = m_nNumToSplayUndo;
435                 m_bShowX = m_bShowXUndo;
436
437                 OnUpdateScore(m_nScore);
438         }
439
440         m_bCanUndo = false;
441         gameBoard->update();  // wil wok?
442 }
443
444
445 void MainWin::OnGamePlay(void)
446 {
447         // Play a card from discard deck...
448         //NOTE: can reduce size by eliminating duplicate code in OnLButtonDown...
449         m_bCanUndo = true;
450
451         if (stack1p == -1)  // I.e. it's empty...
452         {
453                 // Scoring...
454                 if ((m_bDrawThree && (m_nExhaustedDeck == 3))
455                         || (!m_bDrawThree && (m_nExhaustedDeck == 1)))
456                 {
457                         if (!m_bVegasStyle)
458                         {
459                                 m_nExhaustedDeck = 0;
460                                 m_nScore -= (m_bDrawThree ? 20 : 100);
461
462                                 if (m_nScore < 0)
463                                         m_nScore = 0;
464
465                                 OnUpdateScore(m_nScore);
466                         }
467                 }
468
469                 if (!m_bVegasStyle || (m_bVegasStyle && ((m_bDrawThree && (m_nExhaustedDeck < 3))
470                                 || (!m_bDrawThree && (m_nExhaustedDeck < 1)))))
471                 {
472                         for(; stack2p!=-1; stack2p--)
473                                 stack1[++stack1p] = stack2[stack2p];
474
475 #if HAVE_SOUND
476                         if (bSoundOn)
477                                 PlaySound(IDW_CARDKNOCK);
478 #endif
479                 }
480         }
481         else
482         {
483                 m_nNumToSplay = 0;
484
485                 for(int i=0; i<(m_bDrawThree ? 3 : 1); i++)
486                 {
487                         if (stack1p != -1)  // I.e., there are cards left...
488                         {
489                                 stack2[++stack2p] = stack1[stack1p--];
490
491 #if HAVE_SOUND
492                                 if (bSoundOn)
493                                 {
494                                         PlaySound(IDW_CARDFLIP);
495                                         // Pause
496                                         gameBoard->Pause(55);
497                                 }
498 #endif
499
500                                 m_nNumToSplay++;
501                         }
502                 }
503
504                 if (stack1p == -1)  // This is done here because it's right!
505                 {
506                         m_nExhaustedDeck++;
507
508                         if (m_bVegasStyle && ((m_bDrawThree && (m_nExhaustedDeck == 3))
509                                 || (!m_bDrawThree && (m_nExhaustedDeck == 1))))
510                                 m_bShowX = true;
511                 }
512         }
513
514         if (bAutoRemove)
515                 gameBoard->HandleAutoRemove();
516
517         gameBoard->update();
518 }
519
520
521 #endif
522