]>
git.saurik.com Git - wxWidgets.git/blob - samples/forty/canvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
8 // Copyright: (c) 1993-1998 Chris Breeze
9 // Licence: wxWindows licence
10 //---------------------------------------------------------------------------
11 // Last modified: 22nd July 1998 - ported to wxWindows 2.0
12 /////////////////////////////////////////////////////////////////////////////
15 #pragma implementation
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
37 BEGIN_EVENT_TABLE(FortyCanvas
, wxScrolledWindow
)
38 EVT_MOUSE_EVENTS(FortyCanvas::OnMouseEvent
)
41 FortyCanvas::FortyCanvas(wxWindow
* parent
, int x
, int y
, int w
, int h
) :
42 wxScrolledWindow(parent
, -1, wxPoint(x
, y
), wxSize(w
, h
)),
49 m_font
= wxTheFontList
->FindOrCreateFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
51 m_font
= wxTheFontList
->FindOrCreateFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
53 SetBackgroundColour(*FortyApp::BackgroundColour());
54 AllowDoubleClick(true);
56 m_handCursor
= new wxCursor(wxCURSOR_HAND
);
57 m_arrowCursor
= new wxCursor(wxCURSOR_ARROW
);
59 wxString name
= wxTheApp
->GetAppName();
60 if (name
.Length() <= 0) name
= "forty";
61 m_scoreFile
= new ScoreFile(name
);
62 m_game
= new Game(0, 0, 0);
67 FortyCanvas::~FortyCanvas()
76 Write the current player's score back to the score file
78 void FortyCanvas::UpdateScores()
80 if (m_player
.Length() > 0 && m_scoreFile
&& m_game
)
82 m_scoreFile
->WritePlayersScore(
85 m_game
->GetNumGames(),
92 void FortyCanvas::OnDraw(wxDC
& dc
)
97 // if player name not set (and selection dialog is not displayed)
98 // then ask the player for their name
99 if (m_player
.Length() == 0 && !m_playerDialog
)
101 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
102 m_player
= m_playerDialog
->GetPlayersName();
103 if (m_player
.Length() > 0)
105 // user entered a name - lookup their score
106 int wins
, games
, score
;
107 m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
);
108 m_game
->NewPlayer(wins
, games
, score
);
109 m_game
->DisplayScore(dc
);
110 delete m_playerDialog
;
115 // user cancelled the dialog - exit the app
116 ((wxFrame
*)GetParent())->Close(TRUE
);
122 Called when the main frame is closed
124 bool FortyCanvas::OnClose()
126 if (m_game
->InPlay() &&
127 wxMessageBox("Are you sure you want to\nabandon the current game?",
128 "Warning", wxYES_NO
| wxICON_QUESTION
) == wxNO
)
135 void FortyCanvas::OnMouseEvent(wxMouseEvent
& event
)
137 int mouseX
= (int)event
.GetX();
138 int mouseY
= (int)event
.GetY();
144 if (event
.LeftDClick())
148 m_leftBtnDown
= FALSE
;
150 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
152 m_game
->LButtonDblClk(dc
, mouseX
, mouseY
);
154 else if (event
.LeftDown())
158 m_leftBtnDown
= TRUE
;
160 m_game
->LButtonDown(dc
, mouseX
, mouseY
);
163 else if (event
.LeftUp())
167 m_leftBtnDown
= FALSE
;
169 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
172 else if (event
.RightDown() && !event
.LeftIsDown())
174 // only allow right button undo if m_rightBtnUndo is TRUE
177 if (event
.ControlDown() || event
.ShiftDown())
187 else if (event
.Dragging())
189 m_game
->MouseMove(dc
, mouseX
, mouseY
);
192 if (!event
.LeftIsDown())
194 SetCursorStyle(mouseX
, mouseY
);
198 void FortyCanvas::SetCursorStyle(int x
, int y
)
200 if (m_game
->HaveYouWon())
202 if (wxMessageBox("Do you wish to play again?",
203 "Well Done, You have won!", wxYES_NO
| wxICON_QUESTION
) == wxYES
)
214 // user cancelled the dialog - exit the app
215 ((wxFrame
*)GetParent())->Close(TRUE
);
219 // Only set cursor to a hand if 'helping hand' is enabled and
220 // the card under the cursor can go somewhere
221 if (m_game
->CanYouGo(x
, y
) && m_helpingHand
)
223 SetCursor(m_handCursor
);
227 SetCursor(m_arrowCursor
);
232 void FortyCanvas::NewGame()
238 void FortyCanvas::Undo()
246 void FortyCanvas::Redo()