Applied patch [ 914126 ] Cleaning of the forty demo

Also made configurable between use of grid and canvas


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26310 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2004-03-23 21:23:17 +00:00
parent d8e1aa86f1
commit e0b5519ad9
11 changed files with 148 additions and 156 deletions

View File

@@ -38,12 +38,12 @@ BEGIN_EVENT_TABLE(FortyCanvas, wxScrolledWindow)
EVT_MOUSE_EVENTS(FortyCanvas::OnMouseEvent)
END_EVENT_TABLE()
FortyCanvas::FortyCanvas(wxWindow* parent, int x, int y, int w, int h) :
wxScrolledWindow(parent, -1, wxPoint(x, y), wxSize(w, h)),
m_helpingHand(TRUE),
m_rightBtnUndo(TRUE),
FortyCanvas::FortyCanvas(wxWindow* parent, const wxPoint& pos, const wxSize& size) :
wxScrolledWindow(parent, wxID_ANY, pos, size),
m_helpingHand(true),
m_rightBtnUndo(true),
m_playerDialog(0),
m_leftBtnDown(FALSE)
m_leftBtnDown(false)
{
#ifdef __WXGTK__
m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
@@ -116,7 +116,7 @@ void FortyCanvas::OnDraw(wxDC& dc)
else
{
// user cancelled the dialog - exit the app
((wxFrame*)GetParent())->Close(TRUE);
((wxFrame*)GetParent())->Close(true);
}
}
#endif
@@ -148,7 +148,7 @@ void FortyCanvas::ShowPlayerDialog()
else
{
// user cancelled the dialog - exit the app
((wxFrame*)GetParent())->Close(TRUE);
((wxFrame*)GetParent())->Close(true);
}
}
}
@@ -162,9 +162,9 @@ bool FortyCanvas::OnCloseCanvas()
wxMessageBox(_T("Are you sure you want to\nabandon the current game?"),
_T("Warning"), wxYES_NO | wxICON_QUESTION) == wxNO)
{
return FALSE;
return false;
}
return TRUE;
return true;
}
void FortyCanvas::OnMouseEvent(wxMouseEvent& event)
@@ -180,7 +180,7 @@ void FortyCanvas::OnMouseEvent(wxMouseEvent& event)
{
if (m_leftBtnDown)
{
m_leftBtnDown = FALSE;
m_leftBtnDown = false;
ReleaseMouse();
m_game->LButtonUp(dc, mouseX, mouseY);
}
@@ -190,7 +190,7 @@ void FortyCanvas::OnMouseEvent(wxMouseEvent& event)
{
if (!m_leftBtnDown)
{
m_leftBtnDown = TRUE;
m_leftBtnDown = true;
CaptureMouse();
m_game->LButtonDown(dc, mouseX, mouseY);
}
@@ -199,14 +199,14 @@ void FortyCanvas::OnMouseEvent(wxMouseEvent& event)
{
if (m_leftBtnDown)
{
m_leftBtnDown = FALSE;
m_leftBtnDown = false;
ReleaseMouse();
m_game->LButtonUp(dc, mouseX, mouseY);
}
}
else if (event.RightDown() && !event.LeftIsDown())
{
// only allow right button undo if m_rightBtnUndo is TRUE
// only allow right button undo if m_rightBtnUndo is true
if (m_rightBtnUndo)
{
if (event.ControlDown() || event.ShiftDown())

View File

@@ -21,7 +21,7 @@ class PlayerSelectionDialog;
class FortyCanvas: public wxScrolledWindow
{
public:
FortyCanvas(wxWindow* parent, int x, int y, int w, int h);
FortyCanvas(wxWindow* parent, const wxPoint& pos, const wxSize& size);
virtual ~FortyCanvas();
virtual void OnDraw(wxDC& dc);

View File

@@ -111,11 +111,11 @@ Card::Card(int value, WayUp way_up) :
break;
}
m_pipValue = 1 + (value - 1) % 13;
m_status = TRUE;
m_status = true;
}
else
{
m_status = FALSE;
m_status = false;
}
} // Card::Card()

View File

@@ -15,6 +15,7 @@
scorefil.cpp
</sources>
<wx-lib>html</wx-lib>
<wx-lib>adv</wx-lib>
<wx-lib>core</wx-lib>
<wx-lib>base</wx-lib>
<win32-res>forty.rc</win32-res>

View File

@@ -74,27 +74,30 @@ FortyApp::~FortyApp()
bool FortyApp::OnInit()
{
bool largecards = FALSE;
bool largecards = false;
wxSize size(668,510);
if ((argc > 1) && (!wxStrcmp(argv[1],_T("-L"))))
{
largecards = TRUE;
largecards = true;
size = wxSize(1000,750);
}
FortyFrame* frame = new FortyFrame(
0,
_T("Forty Thieves"),
-1, -1, size.x, size.y,largecards
wxDefaultPosition,
size,
largecards
);
// Show the frame
frame->Show(TRUE);
frame->Show(true);
frame->GetCanvas()->ShowPlayerDialog();
return TRUE;
return true;
}
const wxColour& FortyApp::BackgroundColour()
@@ -128,8 +131,8 @@ const wxColour& FortyApp::TextColour()
}
// My frame constructor
FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, int x, int y, int w, int h,bool largecards):
wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size, bool largecards):
wxFrame(frame, wxID_ANY, title, pos, size)
{
#ifdef __WXMAC__
wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
@@ -157,21 +160,21 @@ FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, int x, int y, int
optionsMenu->Append(RIGHT_BUTTON_UNDO,
_T("&Right button undo"),
_T("Enables/disables right mouse button undo and redo"),
TRUE
true
);
optionsMenu->Append(HELPING_HAND,
_T("&Helping hand"),
_T("Enables/disables hand cursor when a card can be moved"),
TRUE
true
);
optionsMenu->Append(LARGE_CARDS,
_T("&Large cards"),
_T("Enables/disables large cards for high resolution displays"),
TRUE
true
);
optionsMenu->Check(HELPING_HAND, TRUE);
optionsMenu->Check(RIGHT_BUTTON_UNDO, TRUE);
optionsMenu->Check(LARGE_CARDS, largecards ? TRUE : FALSE);
optionsMenu->Check(HELPING_HAND, true);
optionsMenu->Check(RIGHT_BUTTON_UNDO, true);
optionsMenu->Check(LARGE_CARDS, largecards ? true : false);
wxMenu* helpMenu = new wxMenu;
helpMenu->Append(wxID_HELP_CONTENTS, _T("&Help Contents"), _T("Displays information about playing the game"));
@@ -188,13 +191,12 @@ FortyFrame::FortyFrame(wxFrame* frame, const wxString& title, int x, int y, int
if (largecards)
Card::SetScale(1.3);
m_canvas = new FortyCanvas(this, 0, 0, 400, 400);
wxLayoutConstraints* constr = new wxLayoutConstraints;
constr->left.SameAs(this, wxLeft);
constr->top.SameAs(this, wxTop);
constr->right.SameAs(this, wxRight);
constr->height.SameAs(this, wxHeight);
m_canvas->SetConstraints(constr);
m_canvas = new FortyCanvas(this, wxDefaultPosition, size);
wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
topsizer->Add( m_canvas, 1, wxEXPAND | wxALL, 0);
SetSizer( topsizer );
topsizer->SetSizeHints( this );
CreateStatusBar();
}
@@ -222,7 +224,7 @@ FortyFrame::NewGame(wxCommandEvent&)
void
FortyFrame::Exit(wxCommandEvent&)
{
Close(TRUE);
Close(true);
}
void
@@ -231,7 +233,7 @@ FortyFrame::Help(wxCommandEvent& event)
#if wxUSE_HTML
if (wxFileExists(wxT("about.htm")))
{
FortyAboutDialog dialog(this, -1, wxT("Forty Thieves Instructions"));
FortyAboutDialog dialog(this, wxID_ANY, wxT("Forty Thieves Instructions"));
if (dialog.ShowModal() == wxID_OK)
{
}
@@ -380,13 +382,12 @@ bool FortyAboutDialog::AddControls(wxWindow* parent)
item0->Add( item2, 0, wxALIGN_RIGHT|wxALL, 5 );
parent->SetAutoLayout( TRUE );
parent->SetSizer( item0 );
parent->Layout();
item0->Fit( parent );
item0->SetSizeHints( parent );
#endif
return TRUE;
return true;
}

View File

@@ -34,7 +34,7 @@ class FortyCanvas;
class FortyFrame: public wxFrame
{
public:
FortyFrame(wxFrame* frame, const wxString& title, int x, int y, int w, int h,bool largecards);
FortyFrame(wxFrame* frame, const wxString& title, const wxPoint& pos, const wxSize& size, bool largecards);
virtual ~FortyFrame();
void OnCloseWindow(wxCloseEvent& event);

View File

@@ -35,7 +35,7 @@
#include "game.h"
Game::Game(int wins, int games, int score) :
m_inPlay(FALSE),
m_inPlay(false),
m_moveIndex(0),
m_redoIndex(0),
m_bmap(0),
@@ -181,7 +181,7 @@ void Game::DoMove(wxDC& dc, Pile* src, Pile* dest)
if (!m_inPlay)
{
m_inPlay = TRUE;
m_inPlay = true;
m_numGames++;
}
DisplayScore(dc);
@@ -198,7 +198,7 @@ void Game::DoMove(wxDC& dc, Pile* src, Pile* dest)
}
// This game is over
m_inPlay = FALSE;
m_inPlay = false;
// Redraw the score box to update games won
DisplayScore(dc);
@@ -212,7 +212,7 @@ void Game::DoMove(wxDC& dc, Pile* src, Pile* dest)
else
{
// user cancelled the dialog - exit the app
((wxFrame*)canvas->GetParent())->Close(TRUE);
((wxFrame*)canvas->GetParent())->Close(true);
}
}
}
@@ -315,7 +315,7 @@ void Game::Deal()
m_totalScore += m_currentScore;
}
m_currentScore = 0;
m_inPlay = FALSE;
m_inPlay = false;
}
@@ -524,21 +524,21 @@ void Game::LButtonDblClk(wxDC& dc, int x, int y)
// i.e. m_pack, discard and bases are empty
bool Game::HaveYouWon()
{
if (m_pack->GetTopCard()) return FALSE;
if (m_discard->GetTopCard()) return FALSE;
if (m_pack->GetTopCard()) return false;
if (m_discard->GetTopCard()) return false;
for(int i = 0; i < 10; i++)
{
if (m_bases[i]->GetTopCard()) return FALSE;
if (m_bases[i]->GetTopCard()) return false;
}
m_numWins++;
m_totalScore += m_currentScore;
m_currentScore = 0;
return TRUE;
return true;
}
// See whether the card under the cursor can be moved somewhere else
// Returns TRUE if it can be moved, FALSE otherwise
// Returns 'true' if it can be moved, 'false' otherwise
bool Game::CanYouGo(int x, int y)
{
Pile* pile = WhichPile(x, y);
@@ -553,7 +553,7 @@ bool Game::CanYouGo(int x, int y)
{
if (m_foundations[i]->AcceptCard(card) && m_foundations[i] != pile)
{
return TRUE;
return true;
}
}
for(i = 0; i < 10; i++)
@@ -562,12 +562,12 @@ bool Game::CanYouGo(int x, int y)
m_bases[i]->AcceptCard(card) &&
m_bases[i] != pile)
{
return TRUE;
return true;
}
}
}
}
return FALSE;
return false;
}
@@ -642,12 +642,12 @@ void Game::LButtonUp(wxDC& dc, int x, int y)
bool Game::DropCard(int x, int y, Pile* pile, Card* card)
{
bool retval = FALSE;
bool retval = false;
if (pile->Overlap(x, y))
{
if (pile->AcceptCard(card))
{
retval = TRUE;
retval = true;
}
}
return retval;
@@ -843,20 +843,20 @@ Base::Base(int x, int y) : Pile(x, y, 0, 12)
bool Base::AcceptCard(Card* card)
{
bool retval = FALSE;
bool retval = false;
if (m_topCard >= 0)
{
if (m_cards[m_topCard]->GetSuit() == card->GetSuit() &&
m_cards[m_topCard]->GetPipValue() - 1 == card->GetPipValue())
{
retval = TRUE;
retval = true;
}
}
else
{
// pile is empty - ACCEPT
retval = TRUE;
retval = true;
}
return retval;
}
@@ -877,20 +877,20 @@ Foundation::Foundation(int x, int y) : Pile(x, y, 0, 0)
bool Foundation::AcceptCard(Card* card)
{
bool retval = FALSE;
bool retval = false;
if (m_topCard >= 0)
{
if (m_cards[m_topCard]->GetSuit() == card->GetSuit() &&
m_cards[m_topCard]->GetPipValue() + 1 == card->GetPipValue())
{
retval = TRUE;
retval = true;
}
}
else if (card->GetPipValue() == 1)
{
// It's an ace and the pile is empty - ACCEPT
retval = TRUE;
retval = true;
}
return retval;
}

View File

@@ -224,9 +224,9 @@ bool Pile::CanCardLeave(Card* card)
{
for (int i = 0; i <= m_topCard; i++)
{
if (card == m_cards[i]) return TRUE;
if (card == m_cards[i]) return true;
}
return FALSE;
return false;
}
// Calculate how far x, y is from top card in the pile
@@ -294,9 +294,9 @@ bool Pile::Overlap(int x, int y)
if (x >= cardX - Card::GetWidth() && x <= cardX + Card::GetWidth() &&
y >= cardY - Card::GetHeight() && y <= cardY + Card::GetHeight())
{
return TRUE;
return true;
}
return FALSE;
return false;
}

View File

@@ -66,7 +66,7 @@ public:
virtual Card* RemoveTopCard(wxDC& pDC, int xOffset = 0, int yOffset = 0);
// Functions to add a card to the top of a pile
virtual bool AcceptCard(Card*) { return FALSE; }
virtual bool AcceptCard(Card*) { return false; }
virtual void AddCard(Card* card); // Add card to top of pile
virtual void AddCard(wxDC& pDC, Card* card); // Add card + redraw it
void SetPos(int x,int y) {m_x = x;m_y = y;};

View File

@@ -44,15 +44,12 @@ PlayerSelectionDialog::PlayerSelectionDialog(
wxWindow* parent,
ScoreFile* file
) :
wxDialog(parent, -1, _T("Player Selection"),
wxDefaultPosition, wxSize(320, 200),
wxDialog(parent, wxID_ANY, _T("Player Selection"),
wxDefaultPosition, wxDefaultSize,
wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
m_scoreFile(file)
{
// enable constraints
SetAutoLayout (TRUE);
wxStaticText* msg = new wxStaticText(this, -1, _T("Please select a name or type a new one:"));
wxStaticText* msg = new wxStaticText(this, wxID_ANY, _T("Please select a name or type a new one:"));
wxListBox* list = new wxListBox(
this, ID_LISTBOX,
@@ -68,63 +65,24 @@ PlayerSelectionDialog::PlayerSelectionDialog(
list->Append(players[i]);
}
m_textField = new wxTextCtrl(this, -1, _T(""), wxDefaultPosition, wxDefaultSize, 0);
m_textField = new wxTextCtrl(this, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize, 0);
m_OK = new wxButton(this, wxID_OK, _T("OK"));
m_cancel = new wxButton(this, wxID_CANCEL, _T("Cancel"));
wxLayoutConstraints* layout;
wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
button_sizer->Add( m_OK, 0, wxALL, 10 );
button_sizer->Add( m_cancel, 0, wxALL, 10 );
// Constrain the msg at the top of the window
layout = new wxLayoutConstraints;
layout->left.SameAs (this, wxLeft, 10);
layout->top.SameAs (this, wxTop, 10);
layout->height.AsIs();
layout->width.AsIs();
msg->SetConstraints(layout);
wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
topsizer->Add( msg, 0, wxALL , 10 );
topsizer->Add( list, 1, wxEXPAND | wxLEFT | wxRIGHT, 10 );
topsizer->Add( m_textField, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10 );
topsizer->Add( button_sizer, 0, wxALIGN_LEFT );
// Constrain the OK button
layout = new wxLayoutConstraints;
layout->left.SameAs (this, wxLeft, 10);
layout->bottom.SameAs (this, wxBottom, 10);
layout->height.AsIs();
layout->width.AsIs();
m_OK->SetConstraints(layout);
SetSizer( topsizer );
// Constrain the OK button
layout = new wxLayoutConstraints;
layout->left.RightOf (m_OK, 10);
layout->bottom.SameAs (this, wxBottom, 10);
layout->height.AsIs();
layout->width.AsIs();
m_cancel->SetConstraints(layout);
// Constrain the Name text entry field
layout = new wxLayoutConstraints;
layout->left.SameAs (this, wxLeft, 10);
layout->right.SameAs (this, wxRight, 10);
layout->bottom.SameAs (m_OK, wxTop, 10);
layout->height.AsIs();
m_textField->SetConstraints(layout);
// Constrain the list of players
layout = new wxLayoutConstraints;
layout->left.SameAs (this, wxLeft, 10);
layout->right.SameAs (this, wxRight, 10);
layout->top.Below (msg, 10);
layout->bottom.SameAs (m_textField, wxTop, 10);
list->SetConstraints(layout);
wxString prevPlayer = m_scoreFile->GetPreviousPlayer();
if ((prevPlayer.Length() > 0) && (list->FindString(prevPlayer) != -1))
{
list->SetStringSelection(prevPlayer);
m_textField->SetValue(prevPlayer);
}
m_textField->SetFocus();
Layout();
topsizer->SetSizeHints( this );
CentreOnParent();
}
@@ -142,7 +100,7 @@ const wxString& PlayerSelectionDialog::GetPlayersName()
{
/*
m_player = "";
Show(TRUE);
Show(true);
*/
return m_player;
}

View File

@@ -30,10 +30,15 @@
#include "scorefil.h"
#include "scoredg.h"
#define USE_GRID_FOR_SCORE 0
#if USE_GRID_FOR_SCORE
#include "wx/grid.h"
#else
class ScoreCanvas : public wxScrolledWindow
{
public:
ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile);
ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size);
virtual ~ScoreCanvas();
void OnDraw(wxDC& dc);
@@ -43,17 +48,17 @@ private:
wxString m_text;
};
ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile) :
wxScrolledWindow(parent)
ScoreCanvas::ScoreCanvas(wxWindow* parent, ScoreFile* scoreFile, const wxPoint& pos, wxSize& size) :
wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
{
SetBackgroundColour(*wxWHITE);
#ifdef __WXGTK__
m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
#else
m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
#endif
wxArrayString players;
wxArrayString players;
scoreFile->GetPlayerList( players);
wxString os;
@@ -124,45 +129,72 @@ void ScoreCanvas::OnDraw(wxDC& dc)
if (*str) str++;
}
}
#endif
BEGIN_EVENT_TABLE(ScoreDialog, wxDialog)
EVT_CLOSE(ScoreDialog::OnCloseWindow)
END_EVENT_TABLE()
ScoreDialog::ScoreDialog(
wxWindow* parent,
ScoreFile* file
) :
wxDialog(parent, -1, _("Scores"),
wxDefaultPosition, wxSize(310, 200),
ScoreDialog::ScoreDialog(wxWindow* parent, ScoreFile* file) :
wxDialog(parent, wxID_ANY, _("Scores"),
wxDefaultPosition, wxSize(400, 300),
wxDIALOG_MODAL | wxDEFAULT_DIALOG_STYLE),
m_scoreFile(file)
{
// enable constraints
SetAutoLayout (TRUE);
// create grid with players
wxArrayString players;
file->GetPlayerList(players);
ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile);
m_OK = new wxButton(this, wxID_OK, _("OK"));
wxSize sz = wxSize(400, 300);
wxLayoutConstraints* layout;
#if USE_GRID_FOR_SCORE
wxGrid* list = new wxGrid(this, wxID_ANY, wxDefaultPosition, sz, 0);
list->CreateGrid(players.Count(), 4);
for (unsigned int i = 0; i < players.Count(); i++)
{
int wins, games, score;
wxString string_value;
// Constrain the OK button
layout = new wxLayoutConstraints;
layout->left.SameAs (this, wxLeft, 10);
layout->bottom.SameAs (this, wxBottom, 10);
layout->height.AsIs();
layout->width.AsIs();
m_OK->SetConstraints(layout);
file->ReadPlayersScore(players[i], wins, games, score);
int average = 0;
if (games > 0)
{
average = (2 * score + games) / (2 * games);
}
list->SetCellValue(i,0,players[i]);
string_value.Printf( _T("%u"), wins );
list->SetCellValue(i,1,string_value);
string_value.Printf( _T("%u"), games );
list->SetCellValue(i,2,string_value);
string_value.Printf( _T("%u"), average );
list->SetCellValue(i,3,string_value);
}
list->SetColLabelValue(0, _T("Players"));
list->SetColLabelValue(1, _T("Wins"));
list->SetColLabelValue(2, _T("Games"));
list->SetColLabelValue(3, _T("Score"));
list->SetEditable(false);
list->AutoSizeColumns();
list->AutoSizeRows();
list->SetRowLabelSize(0);
list->EnableDragRowSize(false);
list->EnableDragColSize(false);
list->EnableDragGridSize(false);
#else
ScoreCanvas* list = new ScoreCanvas(this, m_scoreFile, wxDefaultPosition, sz);
#endif
// Constrain the list of players
layout = new wxLayoutConstraints;
layout->left.SameAs (this, wxLeft, 10);
layout->right.SameAs (this, wxRight, 10);
layout->top.SameAs (this, wxTop, 10);
layout->bottom.SameAs (m_OK, wxTop, 10);
list->SetConstraints(layout);
// locate and resize with sizers
wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
topsizer->Add( list, 1, wxALL|wxGROW, 10 );
topsizer->Add( new wxButton(this, wxID_OK, _("OK")), 0, wxALIGN_CENTER_HORIZONTAL|wxALL , 10 );
Layout();
SetSizer( topsizer );
GetSizer()->Fit(this);
GetSizer()->SetSizeHints(this);
CentreOnParent();
}
ScoreDialog::~ScoreDialog()
@@ -171,7 +203,7 @@ ScoreDialog::~ScoreDialog()
void ScoreDialog::Display()
{
Show(TRUE);
Show(true);
}
void ScoreDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))