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