]>
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_playerDialog
->ShowModal();
103 m_player
= m_playerDialog
->GetPlayersName();
104 if (m_player
.Length() > 0)
106 // user entered a name - lookup their score
107 int wins
, games
, score
;
108 m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
);
109 m_game
->NewPlayer(wins
, games
, score
);
110 m_game
->DisplayScore(dc
);
111 delete m_playerDialog
;
117 // user cancelled the dialog - exit the app
118 ((wxFrame
*)GetParent())->Close(TRUE
);
124 Called when the main frame is closed
126 bool FortyCanvas::OnCloseCanvas()
128 if (m_game
->InPlay() &&
129 wxMessageBox("Are you sure you want to\nabandon the current game?",
130 "Warning", wxYES_NO
| wxICON_QUESTION
) == wxNO
)
137 void FortyCanvas::OnMouseEvent(wxMouseEvent
& event
)
139 int mouseX
= (int)event
.GetX();
140 int mouseY
= (int)event
.GetY();
144 dc
.SetFont(* m_font
);
146 if (event
.LeftDClick())
150 m_leftBtnDown
= FALSE
;
152 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
154 m_game
->LButtonDblClk(dc
, mouseX
, mouseY
);
156 else if (event
.LeftDown())
160 m_leftBtnDown
= TRUE
;
162 m_game
->LButtonDown(dc
, mouseX
, mouseY
);
165 else if (event
.LeftUp())
169 m_leftBtnDown
= FALSE
;
171 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
174 else if (event
.RightDown() && !event
.LeftIsDown())
176 // only allow right button undo if m_rightBtnUndo is TRUE
179 if (event
.ControlDown() || event
.ShiftDown())
189 else if (event
.Dragging())
191 m_game
->MouseMove(dc
, mouseX
, mouseY
);
194 if (!event
.LeftIsDown())
196 SetCursorStyle(mouseX
, mouseY
);
200 void FortyCanvas::SetCursorStyle(int x
, int y
)
202 if (m_game
->HaveYouWon())
204 if (wxMessageBox("Do you wish to play again?",
205 "Well Done, You have won!", wxYES_NO
| wxICON_QUESTION
) == wxYES
)
211 dc
.SetFont(* m_font
);
216 // user cancelled the dialog - exit the app
217 ((wxFrame
*)GetParent())->Close(TRUE
);
221 // Only set cursor to a hand if 'helping hand' is enabled and
222 // the card under the cursor can go somewhere
223 if (m_game
->CanYouGo(x
, y
) && m_helpingHand
)
225 SetCursor(* m_handCursor
);
229 SetCursor(* m_arrowCursor
);
234 void FortyCanvas::NewGame()
240 void FortyCanvas::Undo()
244 dc
.SetFont(* m_font
);
248 void FortyCanvas::Redo()
252 dc
.SetFont(* m_font
);