Ticket #5484: Pairs_LocalKit_27022010.patch

File Pairs_LocalKit_27022010.patch, 27.6 KB (added by stargater, 14 years ago)
  • Jamfile

     
    1 SubDir HAIKU_TOP src apps pairs ;
    2 
    3 UseLibraryHeaders icon ;
    4 
    5 Application Pairs :
    6     Pairs.cpp
    7     PairsWindow.cpp
    8     PairsView.cpp
    9     PairsTopButton.cpp
    10 
    11     : be $(TARGET_LIBSTDC++)
    12     : Pairs.rdef
    13     ;
     1SubDir HAIKU_TOP src apps pairs ;
     2
     3UseLibraryHeaders icon ;
     4
     5Application Pairs :
     6    Pairs.cpp
     7    PairsWindow.cpp
     8    PairsView.cpp
     9    PairsTopButton.cpp
     10
     11    : be liblocale.so $(TARGET_LIBSTDC++)
     12    : Pairs.rdef
     13;
     14
     15DoCatalogs Pairs :
     16    x-vnd.Haiku-Pairs
     17    :
     18    PairsWindow.cpp
     19    : en.catalog
     20;
  • Pairs.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 <stdlib.h>
    7 
    8 #include <Application.h>
    9 
    10 #include "Pairs.h"
    11 #include "PairsWindow.h"
    12 
    13 const char* kSignature = "application/x-vnd.Haiku-Pairs";
    14 
    15 
    16 Pairs::Pairs()
    17     : BApplication(kSignature),
    18       fWindow(NULL)
    19 {
    20 }
    21 
    22 
    23 Pairs::~Pairs()
    24 {
    25 }
    26 
    27 
    28 void
    29 Pairs::ReadyToRun()
    30 {
    31     fWindow = new PairsWindow();
    32     fWindow->Show();
    33 }
    34 
    35 
    36 void
    37 Pairs::RefsReceived(BMessage* message)
    38 {
    39     fWindow->PostMessage(message);
    40 }
    41 
    42 
    43 void
    44 Pairs::MessageReceived(BMessage* message)
    45 {
    46     BApplication::MessageReceived(message);
    47 }
    48 
    49 
    50 int
    51 main(void)
    52 {
    53     Pairs pairs;
    54     pairs.Run();
    55 
    56     return 0;
    57 }
     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 <stdlib.h>
     7
     8#include <Application.h>
     9#include <Catalog.h>
     10#include <Locale.h>
     11
     12#include "Pairs.h"
     13#include "PairsWindow.h"
     14
     15const char* kSignature = "application/x-vnd.Haiku-Pairs";
     16
     17
     18Pairs::Pairs()
     19    : BApplication(kSignature),
     20      fWindow(NULL)
     21{
     22    be_locale->GetAppCatalog(&fCatalog);
     23}
     24
     25
     26Pairs::~Pairs()
     27{
     28}
     29
     30
     31void
     32Pairs::ReadyToRun()
     33{
     34    fWindow = new PairsWindow();
     35    fWindow->Show();
     36}
     37
     38
     39void
     40Pairs::RefsReceived(BMessage* message)
     41{
     42    fWindow->PostMessage(message);
     43}
     44
     45
     46void
     47Pairs::MessageReceived(BMessage* message)
     48{
     49    BApplication::MessageReceived(message);
     50}
     51
     52
     53int
     54main(void)
     55{
     56    Pairs pairs;
     57    pairs.Run();
     58
     59    return 0;
     60}
  • Pairs.h

     
    1 /*
    2  * Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
    3  * Distributed under the terms of the MIT License.
    4  */
    5 #ifndef PAIRS_H
    6 #define PAIRS_H
    7 
    8 #include <Application.h>
    9 
    10 extern const char* kSignature;
    11 
    12 class BMessage;
    13 class PairsWindow;
    14 
    15 class Pairs : public BApplication {
    16 public:
    17             Pairs();
    18             virtual ~Pairs();
    19            
    20             virtual void ReadyToRun();
    21             virtual void RefsReceived(BMessage* message);
    22             virtual void MessageReceived(BMessage* message);
    23 
    24 private:
    25         PairsWindow* fWindow;
    26 };
    27 
    28 #endif  // PAIRS_H
     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#ifndef PAIRS_H
     7#define PAIRS_H
     8
     9#include <Application.h>
     10#include <Catalog.h>
     11
     12extern const char* kSignature;
     13
     14class BMessage;
     15class PairsWindow;
     16
     17class Pairs : public BApplication {
     18public:
     19            Pairs();
     20            virtual ~Pairs();
     21           
     22            virtual void    ReadyToRun();
     23            virtual void    RefsReceived(BMessage* message);
     24            virtual void    MessageReceived(BMessage* message);
     25
     26private:
     27            PairsWindow*    fWindow;
     28            BCatalog        fCatalog;
     29
     30};
     31
     32#endif  // PAIRS_H
  • PairsGlobal.h

     
    1 /*
    2  * Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
    3  * Distributed under the terms of the MIT License.
    4  */
    5 #ifndef PAIRS_GLOBAL_H
    6 #define PAIRS_GLOBAL_H
    7 
    8 #include <SupportDefs.h>
    9 
    10 
    11 const uint32 kMsgCardButton = 'card';
    12 const uint32 kMsgPairComparing = 'pcom';
    13 const int kBitmapSize = 64;
    14 
    15 
    16 #endif // PAIRS_GLOBAL_H
     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#ifndef PAIRS_GLOBAL_H
     7#define PAIRS_GLOBAL_H
     8
     9#include <SupportDefs.h>
     10
     11
     12const uint32 kMsgCardButton = 'card';
     13const uint32 kMsgPairComparing = 'pcom';
     14const int kBitmapSize = 64;
     15
     16
     17#endif // PAIRS_GLOBAL_H
  • PairsTopButton.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 <stdio.h>
    7 #include <unistd.h>
    8 
    9 #include <Button.h>
    10 
    11 #include "PairsTopButton.h"
    12 #include "PairsGlobal.h"
    13 
    14 
    15 TopButton::TopButton(int x, int y, BMessage* message)
    16     : BButton(BRect(x, y, x + kBitmapSize, y + kBitmapSize), "top_button",
    17         "?", message)
    18 {
    19     SetFontSize(54);
    20 }
    21 
    22 
    23 TopButton::~TopButton()
    24 {
    25 }
     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 <stdio.h>
     7#include <unistd.h>
     8
     9#include <Button.h>
     10
     11#include "PairsTopButton.h"
     12#include "PairsGlobal.h"
     13
     14
     15TopButton::TopButton(int x, int y, BMessage* message)
     16    : BButton(BRect(x, y, x + kBitmapSize, y + kBitmapSize), "top_button",
     17        "?", message)
     18{
     19    SetFontSize(54);
     20}
     21
     22
     23TopButton::~TopButton()
     24{
     25}
  • PairsTopButton.h

     
    1 /*
    2  * Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
    3  * Distributed under the terms of the MIT License.
    4  */
    5 #ifndef PAIRS_TOP_BUTTON_H
    6 #define PAIRS_TOP_BUTTON_H
    7 
    8 #include <OS.h>
    9 
    10 class BButton;
    11 
    12 class TopButton : public BButton {
    13 public:
    14             TopButton(int x, int y, BMessage* message);
    15             virtual ~TopButton();
    16 };
    17 
    18 #endif  // PAIRS_TOP_BUTTON_H
     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#ifndef PAIRS_TOP_BUTTON_H
     7#define PAIRS_TOP_BUTTON_H
     8
     9#include <OS.h>
     10
     11class BButton;
     12
     13class TopButton : public BButton {
     14public:
     15            TopButton(int x, int y, BMessage* message);
     16            virtual ~TopButton();
     17};
     18
     19#endif  // PAIRS_TOP_BUTTON_H
  • PairsView.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 "PairsView.h"
    7 
    8 #include <stdio.h>
    9 #include <stdlib.h>
    10 #include <string.h>
    11 
    12 #include <Alert.h>
    13 #include <Application.h>
    14 #include <Bitmap.h>
    15 #include <Button.h>
    16 #include <Directory.h>
    17 #include <Entry.h>
    18 #include <FindDirectory.h>
    19 #include <IconUtils.h>
    20 #include <List.h>
    21 #include <Node.h>
    22 #include <NodeInfo.h>
    23 #include <Path.h>
    24 
    25 #include "Pairs.h"
    26 #include "PairsGlobal.h"
    27 #include "PairsTopButton.h"
    28 
    29 // TODO: support custom board sizes
    30 
    31 PairsView::PairsView(BRect frame, const char* name, uint32 resizingMode)
    32     : BView(frame, name, resizingMode, B_WILL_DRAW)
    33 {
    34     // init bitmap pointers
    35     for (int i = 0; i < 8; i++)
    36         fCard[i] = NULL;
    37 
    38     CreateGameBoard();
    39     _SetPairsBoard();
    40 }
    41 
    42 
    43 void
    44 PairsView::CreateGameBoard()
    45 {
    46     // Show hidden buttons
    47     for (int32 i = 0; i < CountChildren(); i++) {
    48         BView* child = ChildAt(i);
    49         if (child->IsHidden())
    50             child->Show();
    51     }
    52     _GenerateCardPos();
    53 }
    54 
    55 
    56 PairsView::~PairsView()
    57 {
    58     for (int i = 0; i < 8; i++)
    59         delete fCard[i];
    60 
    61     for (int i = 0; i < 16; i++)
    62         delete fDeckCard[i];
    63 }
    64 
    65 
    66 void
    67 PairsView::AttachedToWindow()
    68 {
    69     MakeFocus(true);
    70 }
    71 
    72 
    73 bool
    74 PairsView::_HasBitmap(BList& bitmaps, BBitmap* bitmap)
    75 {
    76     // TODO: if this takes too long, we could build a hash value for each
    77     // bitmap in a separate list
    78     for (int32 i = bitmaps.CountItems(); i-- > 0;) {
    79         BBitmap* item = (BBitmap*)bitmaps.ItemAtFast(i);
    80         if (!memcmp(item->Bits(), bitmap->Bits(), item->BitsLength()))
    81             return true;
    82     }
    83 
    84     return false;
    85 }
    86 
    87 
    88 void
    89 PairsView::_ReadRandomIcons()
    90 {
    91     // TODO: maybe read the icons only once at startup
    92 
    93     // clean out any previous icons
    94     for (int i = 0; i < 8; i++) {
    95         delete fCard[i];
    96         fCard[i] = NULL;
    97     }
    98 
    99     BDirectory appsDirectory;
    100     BDirectory prefsDirectory;
    101 
    102     BPath path;
    103     if (find_directory(B_BEOS_APPS_DIRECTORY, &path) == B_OK)
    104         appsDirectory.SetTo(path.Path());
    105     if (find_directory(B_BEOS_PREFERENCES_DIRECTORY, &path) == B_OK)
    106         prefsDirectory.SetTo(path.Path());
    107 
    108     // read vector icons from apps and prefs folder and put them
    109     // into a BList as BBitmaps
    110     BList bitmaps;
    111 
    112     BEntry entry;
    113     while (appsDirectory.GetNextEntry(&entry) == B_OK
    114         || prefsDirectory.GetNextEntry(&entry) == B_OK) {
    115 
    116         BNode node(&entry);
    117         BNodeInfo nodeInfo(&node);
    118 
    119         if (nodeInfo.InitCheck() < B_OK)
    120             continue;
    121 
    122         uint8* data;
    123         size_t size;
    124         type_code type;
    125 
    126         if (nodeInfo.GetIcon(&data, &size, &type) < B_OK)
    127             continue;
    128 
    129         if (type != B_VECTOR_ICON_TYPE) {
    130             delete[] data;
    131             continue;
    132         }
    133 
    134         BBitmap* bitmap = new BBitmap(
    135             BRect(0, 0, kBitmapSize - 1, kBitmapSize - 1), 0, B_RGBA32);
    136         if (BIconUtils::GetVectorIcon(data, size, bitmap) < B_OK) {
    137             delete[] data;
    138             delete bitmap;
    139             continue;
    140         }
    141 
    142         delete[] data;
    143 
    144         if (_HasBitmap(bitmaps, bitmap) || !bitmaps.AddItem(bitmap))
    145             delete bitmap;
    146         else if (bitmaps.CountItems() >= 128) {
    147             // this is enough to choose from, stop eating memory...
    148             break;
    149         }
    150     }
    151 
    152     // pick eight random bitmaps from the ones we got in the list
    153     srand((unsigned)time(0));
    154 
    155     for (int i = 0; i < 8; i++) {
    156         int32 index = rand() % bitmaps.CountItems();
    157         fCard[i] = (BBitmap*)bitmaps.RemoveItem(index);
    158         if (fCard[i] == NULL) {
    159             BAlert* alert = new BAlert("fatal", "Pairs did not find enough "
    160                 "vector icons in the system, it needs at least eight.",
    161                 "OK", NULL, NULL, B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
    162             alert->Go();
    163             exit(1);
    164         }
    165     }
    166 
    167     // delete the remaining bitmaps from the list
    168     while (BBitmap* bitmap = (BBitmap*)bitmaps.RemoveItem(0L))
    169         delete bitmap;
    170 }
    171 
    172 
    173 void
    174 PairsView::_SetPairsBoard()
    175 {
    176     for (int i = 0; i < 16; i++) {
    177         fButtonMessage = new BMessage(kMsgCardButton);
    178         fButtonMessage->AddInt32("ButtonNum", i);
    179 
    180         int x =  i % 4 * (kBitmapSize + 10) + 10;
    181         int y =  i / 4 * (kBitmapSize + 10) + 10;
    182 
    183         fDeckCard[i] = new TopButton(x, y, fButtonMessage);
    184         AddChild(fDeckCard[i]);
    185     }
    186 }
    187 
    188 
    189 void
    190 PairsView::_GenerateCardPos()
    191 {
    192     _ReadRandomIcons();
    193 
    194     srand((unsigned)time(0));
    195 
    196     int positions[16];
    197     for (int i = 0; i < 16; i++)
    198         positions[i] = i;
    199 
    200     for (int i = 16; i >= 1; i--) {
    201         int index = rand() % i;
    202 
    203         fRandPos[16-i] = positions[index];
    204 
    205         for (int j = index; j < i - 1; j++)
    206             positions[j] = positions[j + 1];
    207     }
    208 
    209     for (int i = 0; i < 16; i++) {
    210         fPosX[i] = (fRandPos[i]) % 4 * (kBitmapSize + 10) + 10;
    211         fPosY[i] = (fRandPos[i]) / 4 * (kBitmapSize + 10) + 10;
    212     }
    213 }
    214 
    215 
    216 void
    217 PairsView::Draw(BRect updateRect)
    218 {
    219     SetDrawingMode(B_OP_ALPHA);
    220 
    221     // draw rand pair 1 & 2
    222     for (int i = 0; i < 16; i++)
    223         DrawBitmap(fCard[i % 8], BPoint(fPosX[i], fPosY[i]));
    224 }
    225 
    226 
    227 int
    228 PairsView::GetIconFromPos(int pos)
    229 {
    230     return fRandPos[pos];
    231 }
     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 "PairsView.h"
     7
     8#include <stdio.h>
     9#include <stdlib.h>
     10#include <string.h>
     11
     12#include <Alert.h>
     13#include <Application.h>
     14#include <Bitmap.h>
     15#include <Button.h>
     16#include <Directory.h>
     17#include <Entry.h>
     18#include <FindDirectory.h>
     19#include <IconUtils.h>
     20#include <List.h>
     21#include <Node.h>
     22#include <NodeInfo.h>
     23#include <Path.h>
     24
     25#include "Pairs.h"
     26#include "PairsGlobal.h"
     27#include "PairsTopButton.h"
     28
     29// TODO: support custom board sizes
     30
     31PairsView::PairsView(BRect frame, const char* name, uint32 resizingMode)
     32    : BView(frame, name, resizingMode, B_WILL_DRAW)
     33{
     34    // init bitmap pointers
     35    for (int i = 0; i < 8; i++)
     36        fCard[i] = NULL;
     37
     38    CreateGameBoard();
     39    _SetPairsBoard();
     40}
     41
     42
     43void
     44PairsView::CreateGameBoard()
     45{
     46    // Show hidden buttons
     47    for (int32 i = 0; i < CountChildren(); i++) {
     48        BView* child = ChildAt(i);
     49        if (child->IsHidden())
     50            child->Show();
     51    }
     52    _GenerateCardPos();
     53}
     54
     55
     56PairsView::~PairsView()
     57{
     58    for (int i = 0; i < 8; i++)
     59        delete fCard[i];
     60
     61    for (int i = 0; i < 16; i++)
     62        delete fDeckCard[i];
     63}
     64
     65
     66void
     67PairsView::AttachedToWindow()
     68{
     69    MakeFocus(true);
     70}
     71
     72
     73bool
     74PairsView::_HasBitmap(BList& bitmaps, BBitmap* bitmap)
     75{
     76    // TODO: if this takes too long, we could build a hash value for each
     77    // bitmap in a separate list
     78    for (int32 i = bitmaps.CountItems(); i-- > 0;) {
     79        BBitmap* item = (BBitmap*)bitmaps.ItemAtFast(i);
     80        if (!memcmp(item->Bits(), bitmap->Bits(), item->BitsLength()))
     81            return true;
     82    }
     83
     84    return false;
     85}
     86
     87
     88void
     89PairsView::_ReadRandomIcons()
     90{
     91    // TODO: maybe read the icons only once at startup
     92
     93    // clean out any previous icons
     94    for (int i = 0; i < 8; i++) {
     95        delete fCard[i];
     96        fCard[i] = NULL;
     97    }
     98
     99    BDirectory appsDirectory;
     100    BDirectory prefsDirectory;
     101
     102    BPath path;
     103    if (find_directory(B_BEOS_APPS_DIRECTORY, &path) == B_OK)
     104        appsDirectory.SetTo(path.Path());
     105    if (find_directory(B_BEOS_PREFERENCES_DIRECTORY, &path) == B_OK)
     106        prefsDirectory.SetTo(path.Path());
     107
     108    // read vector icons from apps and prefs folder and put them
     109    // into a BList as BBitmaps
     110    BList bitmaps;
     111
     112    BEntry entry;
     113    while (appsDirectory.GetNextEntry(&entry) == B_OK
     114        || prefsDirectory.GetNextEntry(&entry) == B_OK) {
     115
     116        BNode node(&entry);
     117        BNodeInfo nodeInfo(&node);
     118
     119        if (nodeInfo.InitCheck() < B_OK)
     120            continue;
     121
     122        uint8* data;
     123        size_t size;
     124        type_code type;
     125
     126        if (nodeInfo.GetIcon(&data, &size, &type) < B_OK)
     127            continue;
     128
     129        if (type != B_VECTOR_ICON_TYPE) {
     130            delete[] data;
     131            continue;
     132        }
     133
     134        BBitmap* bitmap = new BBitmap(
     135            BRect(0, 0, kBitmapSize - 1, kBitmapSize - 1), 0, B_RGBA32);
     136        if (BIconUtils::GetVectorIcon(data, size, bitmap) < B_OK) {
     137            delete[] data;
     138            delete bitmap;
     139            continue;
     140        }
     141
     142        delete[] data;
     143
     144        if (_HasBitmap(bitmaps, bitmap) || !bitmaps.AddItem(bitmap))
     145            delete bitmap;
     146        else if (bitmaps.CountItems() >= 128) {
     147            // this is enough to choose from, stop eating memory...
     148            break;
     149        }
     150    }
     151
     152    // pick eight random bitmaps from the ones we got in the list
     153    srand((unsigned)time(0));
     154
     155    for (int i = 0; i < 8; i++) {
     156        int32 index = rand() % bitmaps.CountItems();
     157        fCard[i] = (BBitmap*)bitmaps.RemoveItem(index);
     158        if (fCard[i] == NULL) {
     159            BAlert* alert = new BAlert("fatal", "Pairs did not find enough "
     160                "vector icons in the system, it needs at least eight.",
     161                "OK", NULL, NULL, B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     162            alert->Go();
     163            exit(1);
     164        }
     165    }
     166
     167    // delete the remaining bitmaps from the list
     168    while (BBitmap* bitmap = (BBitmap*)bitmaps.RemoveItem(0L))
     169        delete bitmap;
     170}
     171
     172
     173void
     174PairsView::_SetPairsBoard()
     175{
     176    for (int i = 0; i < 16; i++) {
     177        fButtonMessage = new BMessage(kMsgCardButton);
     178        fButtonMessage->AddInt32("ButtonNum", i);
     179
     180        int x =  i % 4 * (kBitmapSize + 10) + 10;
     181        int y =  i / 4 * (kBitmapSize + 10) + 10;
     182
     183        fDeckCard[i] = new TopButton(x, y, fButtonMessage);
     184        AddChild(fDeckCard[i]);
     185    }
     186}
     187
     188
     189void
     190PairsView::_GenerateCardPos()
     191{
     192    _ReadRandomIcons();
     193
     194    srand((unsigned)time(0));
     195
     196    int positions[16];
     197    for (int i = 0; i < 16; i++)
     198        positions[i] = i;
     199
     200    for (int i = 16; i >= 1; i--) {
     201        int index = rand() % i;
     202
     203        fRandPos[16-i] = positions[index];
     204
     205        for (int j = index; j < i - 1; j++)
     206            positions[j] = positions[j + 1];
     207    }
     208
     209    for (int i = 0; i < 16; i++) {
     210        fPosX[i] = (fRandPos[i]) % 4 * (kBitmapSize + 10) + 10;
     211        fPosY[i] = (fRandPos[i]) / 4 * (kBitmapSize + 10) + 10;
     212    }
     213}
     214
     215
     216void
     217PairsView::Draw(BRect updateRect)
     218{
     219    SetDrawingMode(B_OP_ALPHA);
     220
     221    // draw rand pair 1 & 2
     222    for (int i = 0; i < 16; i++)
     223        DrawBitmap(fCard[i % 8], BPoint(fPosX[i], fPosY[i]));
     224}
     225
     226
     227int
     228PairsView::GetIconFromPos(int pos)
     229{
     230    return fRandPos[pos];
     231}
  • PairsView.h

     
    1 /*
    2  * Copyright 2008, Ralf Schülke, teammaui@web.de. All rights reserved.
    3  * Distributed under the terms of the MIT License.
    4  */
    5 #ifndef PAIRS_VIEW_H
    6 #define PAIRS_VIEW_H
    7 
    8 
    9 #include <View.h>
    10 
    11 
    12 class TopButton;
    13 
    14 class PairsView : public BView {
    15 public:
    16                                 PairsView(BRect frame, const char* name,
    17                                     uint32 resizingMode);
    18 
    19     virtual                     ~PairsView();
    20     virtual void                AttachedToWindow();
    21     virtual void                Draw(BRect updateRect);
    22     virtual void                CreateGameBoard();
    23 
    24             TopButton*          fDeckCard[16];
    25             int                 GetIconFromPos(int pos);
    26 
    27 private:
    28             void                _SetPairsBoard();
    29             void                _ReadRandomIcons();
    30             void                _GenerateCardPos();
    31             bool                _HasBitmap(BList& bitmaps, BBitmap* bitmap);
    32 
    33             BMessage*           fButtonMessage;
    34             BBitmap*            fCard[8];
    35             int                 fRandPos[16];
    36             int                 fPosX[16];
    37             int                 fPosY[16];
    38 };
    39 
    40 
    41 #endif  // PAIRS_VIEW_H
     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#ifndef PAIRS_VIEW_H
     7#define PAIRS_VIEW_H
     8
     9
     10#include <View.h>
     11
     12
     13class TopButton;
     14
     15class PairsView : public BView {
     16public:
     17                                PairsView(BRect frame, const char* name,
     18                                    uint32 resizingMode);
     19
     20    virtual                     ~PairsView();
     21    virtual void                AttachedToWindow();
     22    virtual void                Draw(BRect updateRect);
     23    virtual void                CreateGameBoard();
     24
     25            TopButton*          fDeckCard[16];
     26            int                 GetIconFromPos(int pos);
     27
     28private:
     29            void                _SetPairsBoard();
     30            void                _ReadRandomIcons();
     31            void                _GenerateCardPos();
     32            bool                _HasBitmap(BList& bitmaps, BBitmap* bitmap);
     33
     34            BMessage*           fButtonMessage;
     35            BBitmap*            fCard[8];
     36            int                 fRandPos[16];
     37            int                 fPosX[16];
     38            int                 fPosY[16];
     39};
     40
     41
     42#endif  // PAIRS_VIEW_H
  • 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                        << "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}
  • PairsWindow.h

     
    1 /*
    2  * Copyright 2007, Ralf Schülke, teammaui@web.de. All rights reserved.
    3  * Distributed under the terms of the MIT License.
    4  */
    5 #ifndef PAIRS_WINDOW_H
    6 #define PAIRS_WINDOW_H
    7 
    8 #include <Window.h>
    9 
    10 class PairsView;
    11 class BMessageRunner;
    12 
    13 
    14 class PairsWindow : public BWindow {
    15 public:
    16                                 PairsWindow();
    17         virtual                 ~PairsWindow();
    18 
    19         virtual void            MessageReceived(BMessage* message);
    20 
    21 private:
    22                 BView*          fBackgroundView;
    23                 PairsView*      fPairsView;
    24                 BMessageRunner* fPairComparing;
    25                 bool            fIsFirstClick;
    26                 bool            fIsPairsActive;
    27                 int             fPairCard;
    28                 int             fPairCardTmp;
    29                 int             fButtonTmp;
    30                 int             fButton;
    31                 int             fButtonClicks;
    32                 int             fFinishPairs;
    33 };
    34 
    35 #endif  // PAIRS_WINDOW_H
     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#ifndef PAIRS_WINDOW_H
     7#define PAIRS_WINDOW_H
     8
     9#include <Window.h>
     10
     11class PairsView;
     12class BMessageRunner;
     13
     14
     15class PairsWindow : public BWindow {
     16public:
     17                                PairsWindow();
     18        virtual                 ~PairsWindow();
     19
     20        virtual void            MessageReceived(BMessage* message);
     21
     22private:
     23                BView*          fBackgroundView;
     24                PairsView*      fPairsView;
     25                BMessageRunner* fPairComparing;
     26                bool            fIsFirstClick;
     27                bool            fIsPairsActive;
     28                int             fPairCard;
     29                int             fPairCardTmp;
     30                int             fButtonTmp;
     31                int             fButton;
     32                int             fButtonClicks;
     33                int             fFinishPairs;
     34};
     35
     36#endif  // PAIRS_WINDOW_H