]> git.saurik.com Git - wxWidgets.git/blob - samples/forty/canvas.cpp
Corrected some problems
[wxWidgets.git] / samples / forty / canvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: canvas.cpp
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
5 // Modified by:
6 // Created: 21/07/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1993-1998 Chris Breeze
9 // Licence: wxWindows licence
10 //---------------------------------------------------------------------------
11 // Last modified: 22nd July 1998 - ported to wxWindows 2.0
12 /////////////////////////////////////////////////////////////////////////////
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #pragma interface
17 #endif
18
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/wx.h"
28 #endif
29
30 #include "forty.h"
31 #include "card.h"
32 #include "game.h"
33 #include "scorefil.h"
34 #include "playerdg.h"
35 #include "canvas.h"
36
37 BEGIN_EVENT_TABLE(FortyCanvas, wxScrolledWindow)
38 EVT_MOUSE_EVENTS(FortyCanvas::OnMouseEvent)
39 END_EVENT_TABLE()
40
41 FortyCanvas::FortyCanvas(wxWindow* parent, int x, int y, int w, int h) :
42 wxScrolledWindow(parent, -1, wxPoint(x, y), wxSize(w, h)),
43 m_helpingHand(TRUE),
44 m_rightBtnUndo(TRUE),
45 m_playerDialog(0),
46 m_leftBtnDown(FALSE)
47 {
48 #ifdef __WXGTK__
49 m_font = wxTheFontList->FindOrCreateFont(12, wxROMAN, wxNORMAL, wxNORMAL);
50 #else
51 m_font = wxTheFontList->FindOrCreateFont(10, wxSWISS, wxNORMAL, wxNORMAL);
52 #endif
53 SetBackgroundColour(*FortyApp::BackgroundColour());
54 AllowDoubleClick(true);
55
56 m_handCursor = new wxCursor(wxCURSOR_HAND);
57 m_arrowCursor = new wxCursor(wxCURSOR_ARROW);
58
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);
63 m_game->Deal();
64 }
65
66
67 FortyCanvas::~FortyCanvas()
68 {
69 UpdateScores();
70 delete m_game;
71 delete m_scoreFile;
72 }
73
74
75 /*
76 Write the current player's score back to the score file
77 */
78 void FortyCanvas::UpdateScores()
79 {
80 if (m_player.Length() > 0 && m_scoreFile && m_game)
81 {
82 m_scoreFile->WritePlayersScore(
83 m_player,
84 m_game->GetNumWins(),
85 m_game->GetNumGames(),
86 m_game->GetScore()
87 );
88 }
89 }
90
91
92 void FortyCanvas::OnDraw(wxDC& dc)
93 {
94 dc.SetFont(m_font);
95 m_game->Redraw(dc);
96
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)
100 {
101 m_playerDialog = new PlayerSelectionDialog(this, m_scoreFile);
102 m_player = m_playerDialog->GetPlayersName();
103 if (m_player.Length() > 0)
104 {
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;
111 m_playerDialog = 0;
112 }
113 else
114 {
115 // user cancelled the dialog - exit the app
116 ((wxFrame*)GetParent())->Close(TRUE);
117 }
118 }
119 }
120
121 /*
122 Called when the main frame is closed
123 */
124 bool FortyCanvas::OnClose()
125 {
126 if (m_game->InPlay() &&
127 wxMessageBox("Are you sure you want to\nabandon the current game?",
128 "Warning", wxYES_NO | wxICON_QUESTION) == wxNO)
129 {
130 return FALSE;
131 }
132 return TRUE;
133 }
134
135 void FortyCanvas::OnMouseEvent(wxMouseEvent& event)
136 {
137 int mouseX = (int)event.GetX();
138 int mouseY = (int)event.GetY();
139
140 wxClientDC dc(this);
141 PrepareDC(dc);
142 dc.SetFont(m_font);
143
144 if (event.LeftDClick())
145 {
146 if (m_leftBtnDown)
147 {
148 m_leftBtnDown = FALSE;
149 ReleaseMouse();
150 m_game->LButtonUp(dc, mouseX, mouseY);
151 }
152 m_game->LButtonDblClk(dc, mouseX, mouseY);
153 }
154 else if (event.LeftDown())
155 {
156 if (!m_leftBtnDown)
157 {
158 m_leftBtnDown = TRUE;
159 CaptureMouse();
160 m_game->LButtonDown(dc, mouseX, mouseY);
161 }
162 }
163 else if (event.LeftUp())
164 {
165 if (m_leftBtnDown)
166 {
167 m_leftBtnDown = FALSE;
168 ReleaseMouse();
169 m_game->LButtonUp(dc, mouseX, mouseY);
170 }
171 }
172 else if (event.RightDown() && !event.LeftIsDown())
173 {
174 // only allow right button undo if m_rightBtnUndo is TRUE
175 if (m_rightBtnUndo)
176 {
177 if (event.ControlDown() || event.ShiftDown())
178 {
179 m_game->Redo(dc);
180 }
181 else
182 {
183 m_game->Undo(dc);
184 }
185 }
186 }
187 else if (event.Dragging())
188 {
189 m_game->MouseMove(dc, mouseX, mouseY);
190 }
191
192 if (!event.LeftIsDown())
193 {
194 SetCursorStyle(mouseX, mouseY);
195 }
196 }
197
198 void FortyCanvas::SetCursorStyle(int x, int y)
199 {
200 if (m_game->HaveYouWon())
201 {
202 if (wxMessageBox("Do you wish to play again?",
203 "Well Done, You have won!", wxYES_NO | wxICON_QUESTION) == wxYES)
204 {
205 m_game->Deal();
206
207 wxClientDC dc(this);
208 PrepareDC(dc);
209 dc.SetFont(m_font);
210 m_game->Redraw(dc);
211 }
212 else
213 {
214 // user cancelled the dialog - exit the app
215 ((wxFrame*)GetParent())->Close(TRUE);
216 }
217 }
218
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)
222 {
223 SetCursor(m_handCursor);
224 }
225 else
226 {
227 SetCursor(m_arrowCursor);
228 }
229
230 }
231
232 void FortyCanvas::NewGame()
233 {
234 m_game->Deal();
235 Refresh();
236 }
237
238 void FortyCanvas::Undo()
239 {
240 wxClientDC dc(this);
241 PrepareDC(dc);
242 dc.SetFont(m_font);
243 m_game->Undo(dc);
244 }
245
246 void FortyCanvas::Redo()
247 {
248 wxClientDC dc(this);
249 PrepareDC(dc);
250 dc.SetFont(m_font);
251 m_game->Redo(dc);
252 }