]>
git.saurik.com Git - wxWidgets.git/blob - demos/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());
55 m_handCursor
= new wxCursor(wxCURSOR_HAND
);
56 m_arrowCursor
= new wxCursor(wxCURSOR_ARROW
);
58 wxString name
= wxTheApp
->GetAppName();
59 if (name
.Length() <= 0) name
= "forty";
60 m_scoreFile
= new ScoreFile(name
);
61 m_game
= new Game(0, 0, 0);
66 FortyCanvas::~FortyCanvas()
77 Write the current player's score back to the score file
79 void FortyCanvas::UpdateScores()
81 if (m_player
.Length() > 0 && m_scoreFile
&& m_game
)
83 m_scoreFile
->WritePlayersScore(
86 m_game
->GetNumGames(),
93 void FortyCanvas::OnDraw(wxDC
& dc
)
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
)
102 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
103 m_playerDialog
->ShowModal();
104 m_player
= m_playerDialog
->GetPlayersName();
105 if (m_player
.Length() > 0)
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();
118 // user cancelled the dialog - exit the app
119 ((wxFrame
*)GetParent())->Close(TRUE
);
125 void FortyCanvas::ShowPlayerDialog()
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
)
131 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
132 m_playerDialog
->ShowModal();
133 m_player
= m_playerDialog
->GetPlayersName();
134 if (m_player
.Length() > 0)
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
);
142 dc
.SetFont(* m_font
);
143 m_game
->DisplayScore(dc
);
144 m_playerDialog
->Destroy();
150 // user cancelled the dialog - exit the app
151 ((wxFrame
*)GetParent())->Close(TRUE
);
157 Called when the main frame is closed
159 bool FortyCanvas::OnCloseCanvas()
161 if (m_game
->InPlay() &&
162 wxMessageBox("Are you sure you want to\nabandon the current game?",
163 "Warning", wxYES_NO
| wxICON_QUESTION
) == wxNO
)
170 void FortyCanvas::OnMouseEvent(wxMouseEvent
& event
)
172 int mouseX
= (int)event
.GetX();
173 int mouseY
= (int)event
.GetY();
177 dc
.SetFont(* m_font
);
179 if (event
.LeftDClick())
183 m_leftBtnDown
= FALSE
;
185 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
187 m_game
->LButtonDblClk(dc
, mouseX
, mouseY
);
189 else if (event
.LeftDown())
193 m_leftBtnDown
= TRUE
;
195 m_game
->LButtonDown(dc
, mouseX
, mouseY
);
198 else if (event
.LeftUp())
202 m_leftBtnDown
= FALSE
;
204 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
207 else if (event
.RightDown() && !event
.LeftIsDown())
209 // only allow right button undo if m_rightBtnUndo is TRUE
212 if (event
.ControlDown() || event
.ShiftDown())
222 else if (event
.Dragging())
224 m_game
->MouseMove(dc
, mouseX
, mouseY
);
227 if (!event
.LeftIsDown())
229 SetCursorStyle(mouseX
, mouseY
);
233 void FortyCanvas::SetCursorStyle(int x
, int y
)
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
)
239 SetCursor(* m_handCursor
);
243 SetCursor(* m_arrowCursor
);
248 void FortyCanvas::NewGame()
254 void FortyCanvas::Undo()
258 dc
.SetFont(* m_font
);
262 void FortyCanvas::Redo()
266 dc
.SetFont(* m_font
);
270 void FortyCanvas::LayoutGame()