xref: /haiku/src/apps/pairs/PairsWindow.cpp (revision 4f2fd49bdc6078128b1391191e4edac647044c3d)
1 /*
2  * Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "PairsWindow.h"
7 
8 #include <stdio.h>
9 
10 #include <Application.h>
11 #include <MessageRunner.h>
12 #include <Button.h>
13 #include <Alert.h>
14 #include <TextView.h>
15 #include <String.h>
16 
17 #include "Pairs.h"
18 #include "PairsGlobal.h"
19 #include "PairsView.h"
20 #include "PairsTopButton.h"
21 
22 
23 PairsWindow::PairsWindow()
24 	: BWindow(BRect(100, 100, 405, 405), "Pairs", B_TITLED_WINDOW,
25 		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE
26 			| B_NOT_RESIZABLE | B_NOT_ZOOMABLE),
27 	  fPairComparing(NULL),
28 	  fIsFirstClick(true),
29 	  fIsPairsActive(true),
30 	  fPairCard(0),
31 	  fPairCardTmp(0),
32 	  fButtonTmp(0),
33 	  fButton(0),
34 	  fButtonClicks(0),
35 	  fFinishPairs(0)
36 {
37 	fPairsView = new PairsView(Bounds().InsetByCopy(0, 0).OffsetToSelf(0, 0),
38 		"PairsView", B_FOLLOW_NONE);
39 	fPairsView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
40 	AddChild(fPairsView);
41 }
42 
43 
44 PairsWindow::~PairsWindow()
45 {
46 	delete fPairComparing;
47 }
48 
49 
50 void
51 PairsWindow::MessageReceived(BMessage* message)
52 {
53 	switch (message->what) {
54 		case kMsgCardButton:
55 			if (fIsPairsActive) {
56 				fButtonClicks++;
57 
58 				int32 num;
59 				if (message->FindInt32("ButtonNum", &num) < B_OK)
60 					break;
61 
62 				// look what Icon is behind a button
63 				for (int h = 0; h < 16; h++) {
64 					if (fPairsView->GetIconFromPos(h) == num) {
65 						fPairCard = (h % 8);
66 						fButton = fPairsView->GetIconFromPos(h);
67 						break;
68 					}
69 				}
70 
71 				// gameplay
72 				fPairsView->fDeckCard[fButton]->Hide();
73 
74 				if (fIsFirstClick) {
75 					fPairCardTmp = fPairCard;
76 					fButtonTmp = fButton;
77 				} else {
78 					delete fPairComparing;
79 						// message of message runner might not have arrived
80 						// yet, so it is deleted here to prevent any leaking
81 						// just in case
82 					BMessage message(kMsgPairComparing);
83 					fPairComparing = new BMessageRunner(BMessenger(this),
84 						&message,  5 * 100000L, 1);
85 					fIsPairsActive = false;
86 				}
87 
88 				fIsFirstClick = !fIsFirstClick;
89 			}
90 			break;
91 
92 			case kMsgPairComparing:
93 				delete fPairComparing;
94 				fPairComparing = NULL;
95 
96 				fIsPairsActive = true;
97 
98 				if (fPairCard == fPairCardTmp) {
99 					fFinishPairs++;
100 				} else {
101 					fPairsView->fDeckCard[fButton]->Show();
102 					fPairsView->fDeckCard[fButtonTmp]->Show();
103 				}
104 
105 				// game end and results
106 				if (fFinishPairs == 8) {
107 					BString strAbout;
108 					strAbout
109 						<< "Pairs\n"
110 						<< "\twritten by Ralf Schülke\n"
111 						<< "\tCopyright 2008, Haiku Inc.\n"
112 						<< "\n"
113 						<< "You completed the game in " << fButtonClicks
114 						<< " clicks.\n";
115 
116 					BAlert* alert = new BAlert("about", strAbout.String(),
117 						"New game", "Quit game");
118 
119 					BTextView* view = alert->TextView();
120 					BFont font;
121 
122 					view->SetStylable(true);
123 
124 					view->GetFont(&font);
125 					font.SetSize(18);
126 					font.SetFace(B_BOLD_FACE);
127 					view->SetFontAndColor(0, 6, &font);
128 					view->ResizeToPreferred();
129 
130 					if (alert->Go() == 0) {
131 						// New game
132 						fButtonClicks = 0;
133 						fFinishPairs = 0;
134 						fPairsView->CreateGameBoard();
135 					} else {
136 						// Quit game
137 						be_app->PostMessage(B_QUIT_REQUESTED);
138 					}
139 				}
140 				break;
141 
142 		default:
143 			BWindow::MessageReceived(message);
144 			break;
145 	}
146 }
147