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