]>
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
, 0),
43 SetScrollbars(0, 0, 0, 0);
46 m_font
= wxTheFontList
->FindOrCreateFont(12, wxROMAN
, wxNORMAL
, wxNORMAL
);
48 m_font
= wxTheFontList
->FindOrCreateFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
);
50 SetBackgroundColour(FortyApp::BackgroundColour());
52 m_handCursor
= new wxCursor(wxCURSOR_HAND
);
53 m_arrowCursor
= new wxCursor(wxCURSOR_ARROW
);
55 wxString name
= wxTheApp
->GetAppName();
56 if (name
.Length() <= 0) name
= wxT("forty");
57 m_scoreFile
= new ScoreFile(name
);
58 m_game
= new Game(0, 0, 0);
63 FortyCanvas::~FortyCanvas()
74 Write the current player's score back to the score file
76 void FortyCanvas::UpdateScores()
78 if (m_player
.Length() > 0 && m_scoreFile
&& m_game
)
80 m_scoreFile
->WritePlayersScore(
83 m_game
->GetNumGames(),
90 void FortyCanvas::OnDraw(wxDC
& dc
)
95 // if player name not set (and selection dialog is not displayed)
96 // then ask the player for their name
97 if (m_player
.Length() == 0 && !m_playerDialog
)
99 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
100 m_playerDialog
->ShowModal();
101 m_player
= m_playerDialog
->GetPlayersName();
102 if (m_player
.Length() > 0)
104 // user entered a name - lookup their score
105 int wins
, games
, score
;
106 m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
);
107 m_game
->NewPlayer(wins
, games
, score
);
108 m_game
->DisplayScore(dc
);
109 m_playerDialog
->Destroy();
115 // user cancelled the dialog - exit the app
116 ((wxFrame
*)GetParent())->Close(true);
122 void FortyCanvas::ShowPlayerDialog()
124 // if player name not set (and selection dialog is not displayed)
125 // then ask the player for their name
126 if (m_player
.Length() == 0 && !m_playerDialog
)
128 m_playerDialog
= new PlayerSelectionDialog(this, m_scoreFile
);
129 m_playerDialog
->ShowModal();
130 m_player
= m_playerDialog
->GetPlayersName();
131 if (m_player
.Length() > 0)
133 // user entered a name - lookup their score
134 int wins
, games
, score
;
135 m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
);
136 m_game
->NewPlayer(wins
, games
, score
);
139 dc
.SetFont(* m_font
);
140 m_game
->DisplayScore(dc
);
141 m_playerDialog
->Destroy();
147 // user cancelled the dialog - exit the app
148 ((wxFrame
*)GetParent())->Close(true);
154 Called when the main frame is closed
156 bool FortyCanvas::OnCloseCanvas()
158 if (m_game
->InPlay() &&
159 wxMessageBox(wxT("Are you sure you want to\nabandon the current game?"),
160 wxT("Warning"), wxYES_NO
| wxICON_QUESTION
) == wxNO
)
167 void FortyCanvas::OnMouseEvent(wxMouseEvent
& event
)
169 int mouseX
= (int)event
.GetX();
170 int mouseY
= (int)event
.GetY();
174 dc
.SetFont(* m_font
);
176 if (event
.LeftDClick())
180 m_leftBtnDown
= false;
182 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
184 m_game
->LButtonDblClk(dc
, mouseX
, mouseY
);
186 else if (event
.LeftDown())
190 m_leftBtnDown
= true;
192 m_game
->LButtonDown(dc
, mouseX
, mouseY
);
195 else if (event
.LeftUp())
199 m_leftBtnDown
= false;
201 m_game
->LButtonUp(dc
, mouseX
, mouseY
);
204 else if (event
.RightDown() && !event
.LeftIsDown())
206 // only allow right button undo if m_rightBtnUndo is true
209 if (event
.ControlDown() || event
.ShiftDown())
219 else if (event
.Dragging())
221 m_game
->MouseMove(dc
, mouseX
, mouseY
);
224 if (!event
.LeftIsDown())
226 SetCursorStyle(mouseX
, mouseY
);
230 void FortyCanvas::SetCursorStyle(int x
, int y
)
232 // Only set cursor to a hand if 'helping hand' is enabled and
233 // the card under the cursor can go somewhere
234 if (m_game
->CanYouGo(x
, y
) && m_helpingHand
)
236 SetCursor(* m_handCursor
);
240 SetCursor(* m_arrowCursor
);
245 void FortyCanvas::NewGame()
251 void FortyCanvas::Undo()
255 dc
.SetFont(* m_font
);
259 void FortyCanvas::Redo()
263 dc
.SetFont(* m_font
);
267 void FortyCanvas::LayoutGame()