xref: /haiku/src/apps/pairs/PairsWindow.cpp (revision c9ad965c81b08802fed0827fd1dd16f45297928a)
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 	CenterOnScreen();
43 }
44 
45 
46 PairsWindow::~PairsWindow()
47 {
48 	delete fPairComparing;
49 }
50 
51 
52 void
53 PairsWindow::MessageReceived(BMessage* message)
54 {
55 	switch (message->what) {
56 		case kMsgCardButton:
57 			if (fIsPairsActive) {
58 				fButtonClicks++;
59 
60 				int32 num;
61 				if (message->FindInt32("ButtonNum", &num) < B_OK)
62 					break;
63 
64 				// look what Icon is behind a button
65 				for (int h = 0; h < 16; h++) {
66 					if (fPairsView->GetIconFromPos(h) == num) {
67 						fPairCard = (h % 8);
68 						fButton = fPairsView->GetIconFromPos(h);
69 						break;
70 					}
71 				}
72 
73 				// gameplay
74 				fPairsView->fDeckCard[fButton]->Hide();
75 
76 				if (fIsFirstClick) {
77 					fPairCardTmp = fPairCard;
78 					fButtonTmp = fButton;
79 				} else {
80 					delete fPairComparing;
81 						// message of message runner might not have arrived
82 						// yet, so it is deleted here to prevent any leaking
83 						// just in case
84 					BMessage message(kMsgPairComparing);
85 					fPairComparing = new BMessageRunner(BMessenger(this),
86 						&message,  5 * 100000L, 1);
87 					fIsPairsActive = false;
88 				}
89 
90 				fIsFirstClick = !fIsFirstClick;
91 			}
92 			break;
93 
94 			case kMsgPairComparing:
95 				delete fPairComparing;
96 				fPairComparing = NULL;
97 
98 				fIsPairsActive = true;
99 
100 				if (fPairCard == fPairCardTmp) {
101 					fFinishPairs++;
102 				} else {
103 					fPairsView->fDeckCard[fButton]->Show();
104 					fPairsView->fDeckCard[fButtonTmp]->Show();
105 				}
106 
107 				// game end and results
108 				if (fFinishPairs == 8) {
109 					BString strAbout;
110 					strAbout
111 						<< "Pairs\n"
112 						<< "\twritten by Ralf Schülke\n"
113 						<< "\tCopyright 2008, Haiku Inc.\n"
114 						<< "\n"
115 						<< "You completed the game in " << fButtonClicks
116 						<< " clicks.\n";
117 
118 					BAlert* alert = new BAlert("about", strAbout.String(),
119 						"New game", "Quit game");
120 
121 					BTextView* view = alert->TextView();
122 					BFont font;
123 
124 					view->SetStylable(true);
125 
126 					view->GetFont(&font);
127 					font.SetSize(18);
128 					font.SetFace(B_BOLD_FACE);
129 					view->SetFontAndColor(0, 6, &font);
130 					view->ResizeToPreferred();
131 
132 					if (alert->Go() == 0) {
133 						// New game
134 						fButtonClicks = 0;
135 						fFinishPairs = 0;
136 						fPairsView->CreateGameBoard();
137 					} else {
138 						// Quit game
139 						be_app->PostMessage(B_QUIT_REQUESTED);
140 					}
141 				}
142 				break;
143 
144 		default:
145 			BWindow::MessageReceived(message);
146 			break;
147 	}
148 }
149