]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/canvas.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Forty Thieves patience game
4 // Author: Chris Breeze
7 // Copyright: (c) 1993-1998 Chris Breeze
8 // Licence: wxWindows licence
9 //---------------------------------------------------------------------------
10 // Last modified: 22nd July 1998 - ported to wxWidgets 2.0
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
31 BEGIN_EVENT_TABLE(FortyCanvas
, wxScrolledWindow
)
32 EVT_MOUSE_EVENTS(FortyCanvas::OnMouseEvent
)
35 FortyCanvas::FortyCanvas(wxWindow
* parent
, const wxPoint
& pos
, const wxSize
& size
) :
36 wxScrolledWindow(parent
, wxID_ANY
, pos
, size
, 0),
42 SetScrollbars(0, 0, 0, 0);
45 m_font
= wxTheFontList
->FindOrCreateFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
47 m_font
= wxTheFontList
->FindOrCreateFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
49 SetBackgroundColour(FortyApp::BackgroundColour());
51 m_handCursor
= new wxCursor(wxCURSOR_HAND
);
52 m_arrowCursor
= new wxCursor(wxCURSOR_ARROW
);
54 wxString name
= wxTheApp
->GetAppName();
55 if ( name
.empty() ) name
= wxT("forty");
56 m_scoreFile
= new ScoreFile(name
);
57 m_game
= new Game(0, 0, 0);
62 FortyCanvas::~FortyCanvas()
73 Write the current player's score back to the score file
75 void FortyCanvas::UpdateScores()
77 if (!m_player
.empty() && m_scoreFile
&& m_game
)
79 m_scoreFile
->WritePlayersScore(
82 m_game
->GetNumGames(),
89 void FortyCanvas::OnDraw(wxDC
& dc
)
94 // if player name not set (and selection dialog is not displayed)
95 // then ask the player for their name
96 if (m_player
.empty() && !m_playerDialog
)
98 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
99 m_playerDialog
->ShowModal();
100 m_player
= m_playerDialog
->GetPlayersName();
101 if ( !m_player
.empty() )
103 // user entered a name - lookup their score
104 int wins
, games
, score
;
105 m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
);
106 m_game
->NewPlayer(wins
, games
, score
);
107 m_game
->DisplayScore(dc
);
108 m_playerDialog
->Destroy();
114 // user cancelled the dialog - exit the app
115 ((wxFrame
*)GetParent())->Close(true);
121 void FortyCanvas::ShowPlayerDialog()
123 // if player name not set (and selection dialog is not displayed)
124 // then ask the player for their name
125 if (m_player
.empty() && !m_playerDialog
)
127 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
128 m_playerDialog
->ShowModal();
129 m_player
= m_playerDialog
->GetPlayersName();
130 if ( !m_player
.empty() )
132 // user entered a name - lookup their score
133 int wins
, games
, score
;
134 m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
);
135 m_game
->NewPlayer(wins
, games
, score
);
138 dc
.SetFont(* m_font
);
139 m_game
->DisplayScore(dc
);
140 m_playerDialog
->Destroy();
146 // user cancelled the dialog - exit the app
147 ((wxFrame
*)GetParent())->Close(true);
153 Called when the main frame is closed
155 bool FortyCanvas::OnCloseCanvas()
157 if (m_game
->InPlay() &&
158 wxMessageBox(wxT("Are you sure you want to\nabandon the current game?"),
159 wxT("Warning"), wxYES_NO
| wxICON_QUESTION
) == wxNO
)
166 void FortyCanvas::OnMouseEvent(wxMouseEvent
& event
)
168 int mouseX
= (int)event
.GetX();
169 int mouseY
= (int)event
.GetY();
173 dc
.SetFont(* m_font
);
175 if (event
.LeftDClick())
179 m_leftBtnDown
= false;
181 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
183 m_game
->LButtonDblClk(dc
, mouseX
, mouseY
);
185 else if (event
.LeftDown())
189 m_leftBtnDown
= true;
191 m_game
->LButtonDown(dc
, mouseX
, mouseY
);
194 else if (event
.LeftUp())
198 m_leftBtnDown
= false;
200 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
203 else if (event
.RightDown() && !event
.LeftIsDown())
205 // only allow right button undo if m_rightBtnUndo is true
208 if (event
.ControlDown() || event
.ShiftDown())
218 else if (event
.Dragging())
220 m_game
->MouseMove(dc
, mouseX
, mouseY
);
223 if (!event
.LeftIsDown())
225 SetCursorStyle(mouseX
, mouseY
);
229 void FortyCanvas::SetCursorStyle(int x
, int y
)
231 // Only set cursor to a hand if 'helping hand' is enabled and
232 // the card under the cursor can go somewhere
233 if (m_game
->CanYouGo(x
, y
) && m_helpingHand
)
235 SetCursor(* m_handCursor
);
239 SetCursor(* m_arrowCursor
);
244 void FortyCanvas::NewGame()
250 void FortyCanvas::Undo()
254 dc
.SetFont(* m_font
);
258 void FortyCanvas::Redo()
262 dc
.SetFont(* m_font
);
266 void FortyCanvas::LayoutGame()