]>
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 wxWidgets 2.0
12 /////////////////////////////////////////////////////////////////////////////
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
32 BEGIN_EVENT_TABLE(FortyCanvas
, wxScrolledWindow
)
33 EVT_MOUSE_EVENTS(FortyCanvas::OnMouseEvent
)
36 FortyCanvas::FortyCanvas(wxWindow
* parent
, const wxPoint
& pos
, const wxSize
& size
) :
37 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
),
44 m_font
= wxTheFontList
->FindOrCreateFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
46 m_font
= wxTheFontList
->FindOrCreateFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
48 SetBackgroundColour(FortyApp::BackgroundColour());
50 m_handCursor
= new wxCursor(wxCURSOR_HAND
);
51 m_arrowCursor
= new wxCursor(wxCURSOR_ARROW
);
53 wxString name
= wxTheApp
->GetAppName();
54 if (name
.Length() <= 0) name
= _T("forty");
55 m_scoreFile
= new ScoreFile(name
);
56 m_game
= new Game(0, 0, 0);
61 FortyCanvas::~FortyCanvas()
72 Write the current player's score back to the score file
74 void FortyCanvas::UpdateScores()
76 if (m_player
.Length() > 0 && m_scoreFile
&& m_game
)
78 m_scoreFile
->WritePlayersScore(
81 m_game
->GetNumGames(),
88 void FortyCanvas::OnDraw(wxDC
& dc
)
93 // if player name not set (and selection dialog is not displayed)
94 // then ask the player for their name
95 if (m_player
.Length() == 0 && !m_playerDialog
)
97 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
98 m_playerDialog
->ShowModal();
99 m_player
= m_playerDialog
->GetPlayersName();
100 if (m_player
.Length() > 0)
102 // user entered a name - lookup their score
103 int wins
, games
, score
;
104 m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
);
105 m_game
->NewPlayer(wins
, games
, score
);
106 m_game
->DisplayScore(dc
);
107 m_playerDialog
->Destroy();
113 // user cancelled the dialog - exit the app
114 ((wxFrame
*)GetParent())->Close(true);
120 void FortyCanvas::ShowPlayerDialog()
122 // if player name not set (and selection dialog is not displayed)
123 // then ask the player for their name
124 if (m_player
.Length() == 0 && !m_playerDialog
)
126 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
127 m_playerDialog
->ShowModal();
128 m_player
= m_playerDialog
->GetPlayersName();
129 if (m_player
.Length() > 0)
131 // user entered a name - lookup their score
132 int wins
, games
, score
;
133 m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
);
134 m_game
->NewPlayer(wins
, games
, score
);
137 dc
.SetFont(* m_font
);
138 m_game
->DisplayScore(dc
);
139 m_playerDialog
->Destroy();
145 // user cancelled the dialog - exit the app
146 ((wxFrame
*)GetParent())->Close(true);
152 Called when the main frame is closed
154 bool FortyCanvas::OnCloseCanvas()
156 if (m_game
->InPlay() &&
157 wxMessageBox(_T("Are you sure you want to\nabandon the current game?"),
158 _T("Warning"), wxYES_NO
| wxICON_QUESTION
) == wxNO
)
165 void FortyCanvas::OnMouseEvent(wxMouseEvent
& event
)
167 int mouseX
= (int)event
.GetX();
168 int mouseY
= (int)event
.GetY();
172 dc
.SetFont(* m_font
);
174 if (event
.LeftDClick())
178 m_leftBtnDown
= false;
180 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
182 m_game
->LButtonDblClk(dc
, mouseX
, mouseY
);
184 else if (event
.LeftDown())
188 m_leftBtnDown
= true;
190 m_game
->LButtonDown(dc
, mouseX
, mouseY
);
193 else if (event
.LeftUp())
197 m_leftBtnDown
= false;
199 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
202 else if (event
.RightDown() && !event
.LeftIsDown())
204 // only allow right button undo if m_rightBtnUndo is true
207 if (event
.ControlDown() || event
.ShiftDown())
217 else if (event
.Dragging())
219 m_game
->MouseMove(dc
, mouseX
, mouseY
);
222 if (!event
.LeftIsDown())
224 SetCursorStyle(mouseX
, mouseY
);
228 void FortyCanvas::SetCursorStyle(int x
, int y
)
230 // Only set cursor to a hand if 'helping hand' is enabled and
231 // the card under the cursor can go somewhere
232 if (m_game
->CanYouGo(x
, y
) && m_helpingHand
)
234 SetCursor(* m_handCursor
);
238 SetCursor(* m_arrowCursor
);
243 void FortyCanvas::NewGame()
249 void FortyCanvas::Undo()
253 dc
.SetFont(* m_font
);
257 void FortyCanvas::Redo()
261 dc
.SetFont(* m_font
);
265 void FortyCanvas::LayoutGame()