Ticket #5484: Pairs_LocalKit_28022010_2.patch

File Pairs_LocalKit_28022010_2.patch, 7.2 KB (added by stargater, 14 years ago)
  • PairsWindow.cpp

     
    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 }
     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
     28PairsWindow::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
     51PairsWindow::~PairsWindow()
     52{
     53    delete fPairComparing;
     54}
     55
     56
     57void
     58PairsWindow::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                        << TR("You completed the game in ") << fButtonClicks
     121                        << TR(" clicks.\n");
     122
     123                    BAlert* alert = new BAlert("about", 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}