]>
Commit | Line | Data |
---|---|---|
63cafd27 JS |
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 | |
16553659 | 53 | SetBackgroundColour(FortyApp::BackgroundColour()); |
63cafd27 JS |
54 | |
55 | m_handCursor = new wxCursor(wxCURSOR_HAND); | |
56 | m_arrowCursor = new wxCursor(wxCURSOR_ARROW); | |
57 | ||
58 | wxString name = wxTheApp->GetAppName(); | |
f37c24e0 | 59 | if (name.Length() <= 0) name = _T("forty"); |
63cafd27 JS |
60 | m_scoreFile = new ScoreFile(name); |
61 | m_game = new Game(0, 0, 0); | |
62 | m_game->Deal(); | |
63 | } | |
64 | ||
65 | ||
66 | FortyCanvas::~FortyCanvas() | |
67 | { | |
68 | UpdateScores(); | |
69 | delete m_game; | |
70 | delete m_scoreFile; | |
15bee36f JS |
71 | delete m_handCursor; |
72 | delete m_arrowCursor; | |
63cafd27 JS |
73 | } |
74 | ||
75 | ||
76 | /* | |
77 | Write the current player's score back to the score file | |
78 | */ | |
79 | void FortyCanvas::UpdateScores() | |
80 | { | |
81 | if (m_player.Length() > 0 && m_scoreFile && m_game) | |
82 | { | |
83 | m_scoreFile->WritePlayersScore( | |
84 | m_player, | |
85 | m_game->GetNumWins(), | |
86 | m_game->GetNumGames(), | |
87 | m_game->GetScore() | |
88 | ); | |
89 | } | |
90 | } | |
91 | ||
92 | ||
93 | void FortyCanvas::OnDraw(wxDC& dc) | |
94 | { | |
16553659 | 95 | dc.SetFont(* m_font); |
63cafd27 | 96 | m_game->Redraw(dc); |
868741e9 | 97 | #if 0 |
63cafd27 JS |
98 | // if player name not set (and selection dialog is not displayed) |
99 | // then ask the player for their name | |
100 | if (m_player.Length() == 0 && !m_playerDialog) | |
101 | { | |
cba2db0c | 102 | m_playerDialog = new PlayerSelectionDialog(this, m_scoreFile); |
7b5408ea | 103 | m_playerDialog->ShowModal(); |
63cafd27 JS |
104 | m_player = m_playerDialog->GetPlayersName(); |
105 | if (m_player.Length() > 0) | |
106 | { | |
107 | // user entered a name - lookup their score | |
108 | int wins, games, score; | |
109 | m_scoreFile->ReadPlayersScore(m_player, wins, games, score); | |
110 | m_game->NewPlayer(wins, games, score); | |
111 | m_game->DisplayScore(dc); | |
cba2db0c | 112 | m_playerDialog->Destroy(); |
63cafd27 | 113 | m_playerDialog = 0; |
fc799548 | 114 | Refresh(false); |
63cafd27 JS |
115 | } |
116 | else | |
117 | { | |
118 | // user cancelled the dialog - exit the app | |
119 | ((wxFrame*)GetParent())->Close(TRUE); | |
120 | } | |
121 | } | |
868741e9 | 122 | #endif |
63cafd27 JS |
123 | } |
124 | ||
868741e9 JS |
125 | void FortyCanvas::ShowPlayerDialog() |
126 | { | |
127 | // if player name not set (and selection dialog is not displayed) | |
128 | // then ask the player for their name | |
129 | if (m_player.Length() == 0 && !m_playerDialog) | |
130 | { | |
131 | m_playerDialog = new PlayerSelectionDialog(this, m_scoreFile); | |
132 | m_playerDialog->ShowModal(); | |
133 | m_player = m_playerDialog->GetPlayersName(); | |
134 | if (m_player.Length() > 0) | |
135 | { | |
136 | // user entered a name - lookup their score | |
137 | int wins, games, score; | |
138 | m_scoreFile->ReadPlayersScore(m_player, wins, games, score); | |
139 | m_game->NewPlayer(wins, games, score); | |
140 | ||
141 | wxClientDC dc(this); | |
142 | dc.SetFont(* m_font); | |
143 | m_game->DisplayScore(dc); | |
144 | m_playerDialog->Destroy(); | |
145 | m_playerDialog = 0; | |
146 | Refresh(false); | |
147 | } | |
148 | else | |
149 | { | |
150 | // user cancelled the dialog - exit the app | |
151 | ((wxFrame*)GetParent())->Close(TRUE); | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
63cafd27 JS |
156 | /* |
157 | Called when the main frame is closed | |
158 | */ | |
e3065973 | 159 | bool FortyCanvas::OnCloseCanvas() |
63cafd27 JS |
160 | { |
161 | if (m_game->InPlay() && | |
f37c24e0 MB |
162 | wxMessageBox(_T("Are you sure you want to\nabandon the current game?"), |
163 | _T("Warning"), wxYES_NO | wxICON_QUESTION) == wxNO) | |
63cafd27 | 164 | { |
e3065973 | 165 | return FALSE; |
63cafd27 JS |
166 | } |
167 | return TRUE; | |
168 | } | |
169 | ||
170 | void FortyCanvas::OnMouseEvent(wxMouseEvent& event) | |
171 | { | |
172 | int mouseX = (int)event.GetX(); | |
173 | int mouseY = (int)event.GetY(); | |
174 | ||
175 | wxClientDC dc(this); | |
176 | PrepareDC(dc); | |
16553659 | 177 | dc.SetFont(* m_font); |
63cafd27 JS |
178 | |
179 | if (event.LeftDClick()) | |
180 | { | |
181 | if (m_leftBtnDown) | |
182 | { | |
183 | m_leftBtnDown = FALSE; | |
184 | ReleaseMouse(); | |
185 | m_game->LButtonUp(dc, mouseX, mouseY); | |
186 | } | |
187 | m_game->LButtonDblClk(dc, mouseX, mouseY); | |
188 | } | |
189 | else if (event.LeftDown()) | |
190 | { | |
191 | if (!m_leftBtnDown) | |
192 | { | |
193 | m_leftBtnDown = TRUE; | |
194 | CaptureMouse(); | |
195 | m_game->LButtonDown(dc, mouseX, mouseY); | |
196 | } | |
197 | } | |
198 | else if (event.LeftUp()) | |
199 | { | |
200 | if (m_leftBtnDown) | |
201 | { | |
202 | m_leftBtnDown = FALSE; | |
203 | ReleaseMouse(); | |
204 | m_game->LButtonUp(dc, mouseX, mouseY); | |
205 | } | |
206 | } | |
207 | else if (event.RightDown() && !event.LeftIsDown()) | |
208 | { | |
209 | // only allow right button undo if m_rightBtnUndo is TRUE | |
210 | if (m_rightBtnUndo) | |
211 | { | |
212 | if (event.ControlDown() || event.ShiftDown()) | |
213 | { | |
214 | m_game->Redo(dc); | |
215 | } | |
216 | else | |
217 | { | |
218 | m_game->Undo(dc); | |
219 | } | |
220 | } | |
221 | } | |
222 | else if (event.Dragging()) | |
223 | { | |
224 | m_game->MouseMove(dc, mouseX, mouseY); | |
225 | } | |
226 | ||
227 | if (!event.LeftIsDown()) | |
228 | { | |
229 | SetCursorStyle(mouseX, mouseY); | |
230 | } | |
231 | } | |
232 | ||
233 | void FortyCanvas::SetCursorStyle(int x, int y) | |
234 | { | |
63cafd27 JS |
235 | // Only set cursor to a hand if 'helping hand' is enabled and |
236 | // the card under the cursor can go somewhere | |
237 | if (m_game->CanYouGo(x, y) && m_helpingHand) | |
238 | { | |
16553659 | 239 | SetCursor(* m_handCursor); |
63cafd27 JS |
240 | } |
241 | else | |
242 | { | |
16553659 | 243 | SetCursor(* m_arrowCursor); |
63cafd27 JS |
244 | } |
245 | ||
246 | } | |
247 | ||
248 | void FortyCanvas::NewGame() | |
249 | { | |
250 | m_game->Deal(); | |
251 | Refresh(); | |
252 | } | |
253 | ||
254 | void FortyCanvas::Undo() | |
255 | { | |
256 | wxClientDC dc(this); | |
257 | PrepareDC(dc); | |
16553659 | 258 | dc.SetFont(* m_font); |
63cafd27 JS |
259 | m_game->Undo(dc); |
260 | } | |
261 | ||
262 | void FortyCanvas::Redo() | |
263 | { | |
264 | wxClientDC dc(this); | |
265 | PrepareDC(dc); | |
16553659 | 266 | dc.SetFont(* m_font); |
63cafd27 JS |
267 | m_game->Redo(dc); |
268 | } | |
fc799548 JS |
269 | |
270 | void FortyCanvas::LayoutGame() | |
271 | { | |
272 | m_game->Layout(); | |
273 | } |