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