]> git.saurik.com Git - wxWidgets.git/blob - demos/forty/canvas.cpp
removed GSocket_[Un]Streamed
[wxWidgets.git] / demos / 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 wxWidgets 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, const wxPoint& pos, const wxSize& size) :
42 wxScrolledWindow(parent, wxID_ANY, pos, size),
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
55 m_handCursor = new wxCursor(wxCURSOR_HAND);
56 m_arrowCursor = new wxCursor(wxCURSOR_ARROW);
57
58 wxString name = wxTheApp->GetAppName();
59 if (name.Length() <= 0) name = _T("forty");
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;
71 delete m_handCursor;
72 delete m_arrowCursor;
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 {
95 dc.SetFont(* m_font);
96 m_game->Redraw(dc);
97 #if 0
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 {
102 m_playerDialog = new PlayerSelectionDialog(this, m_scoreFile);
103 m_playerDialog->ShowModal();
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);
112 m_playerDialog->Destroy();
113 m_playerDialog = 0;
114 Refresh(false);
115 }
116 else
117 {
118 // user cancelled the dialog - exit the app
119 ((wxFrame*)GetParent())->Close(true);
120 }
121 }
122 #endif
123 }
124
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
156 /*
157 Called when the main frame is closed
158 */
159 bool FortyCanvas::OnCloseCanvas()
160 {
161 if (m_game->InPlay() &&
162 wxMessageBox(_T("Are you sure you want to\nabandon the current game?"),
163 _T("Warning"), wxYES_NO | wxICON_QUESTION) == wxNO)
164 {
165 return false;
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);
177 dc.SetFont(* m_font);
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 {
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 {
239 SetCursor(* m_handCursor);
240 }
241 else
242 {
243 SetCursor(* m_arrowCursor);
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);
258 dc.SetFont(* m_font);
259 m_game->Undo(dc);
260 }
261
262 void FortyCanvas::Redo()
263 {
264 wxClientDC dc(this);
265 PrepareDC(dc);
266 dc.SetFont(* m_font);
267 m_game->Redo(dc);
268 }
269
270 void FortyCanvas::LayoutGame()
271 {
272 m_game->Layout();
273 }