]>
git.saurik.com Git - wxWidgets.git/blob - demos/forty/canvas.cpp
ef3ae68b825b72f07fe7f9e5da4975e5beeaa25a
   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() 
  75 Write the current player's score back to the score file 
  77 void FortyCanvas::UpdateScores() 
  79         if (m_player
.Length() > 0 && m_scoreFile 
&& m_game
) 
  81                 m_scoreFile
->WritePlayersScore( 
  84                                 m_game
->GetNumGames(), 
  91 void FortyCanvas::OnDraw(wxDC
& dc
) 
  96         // if player name not set (and selection dialog is not displayed) 
  97         // then ask the player for their name 
  98         if (m_player
.Length() == 0 && !m_playerDialog
) 
 100                 m_playerDialog 
= new PlayerSelectionDialog(this, m_scoreFile
); 
 101                 m_playerDialog
->ShowModal(); 
 102                 m_player 
= m_playerDialog
->GetPlayersName(); 
 103                 if (m_player
.Length() > 0) 
 105                         // user entered a name - lookup their score 
 106                         int wins
, games
, score
; 
 107                         m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
); 
 108                         m_game
->NewPlayer(wins
, games
, score
); 
 109                         m_game
->DisplayScore(dc
); 
 110                         m_playerDialog
->Destroy(); 
 116                         // user cancelled the dialog - exit the app 
 117                         ((wxFrame
*)GetParent())->Close(TRUE
); 
 123 void FortyCanvas::ShowPlayerDialog() 
 125         // if player name not set (and selection dialog is not displayed) 
 126         // then ask the player for their name 
 127         if (m_player
.Length() == 0 && !m_playerDialog
) 
 129                 m_playerDialog 
= new PlayerSelectionDialog(this, m_scoreFile
); 
 130                 m_playerDialog
->ShowModal(); 
 131                 m_player 
= m_playerDialog
->GetPlayersName(); 
 132                 if (m_player
.Length() > 0) 
 134                         // user entered a name - lookup their score 
 135                         int wins
, games
, score
; 
 136                         m_scoreFile
->ReadPlayersScore(m_player
, wins
, games
, score
); 
 137                         m_game
->NewPlayer(wins
, games
, score
); 
 140                         dc
.SetFont(* m_font
); 
 141                         m_game
->DisplayScore(dc
); 
 142                         m_playerDialog
->Destroy(); 
 148                         // user cancelled the dialog - exit the app 
 149                         ((wxFrame
*)GetParent())->Close(TRUE
); 
 155 Called when the main frame is closed 
 157 bool FortyCanvas::OnCloseCanvas() 
 159         if (m_game
->InPlay() && 
 160                 wxMessageBox("Are you sure you want to\nabandon the current game?", 
 161                         "Warning", wxYES_NO 
| wxICON_QUESTION
) == wxNO
) 
 168 void FortyCanvas::OnMouseEvent(wxMouseEvent
& event
) 
 170         int mouseX 
= (int)event
.GetX(); 
 171         int mouseY 
= (int)event
.GetY(); 
 175         dc
.SetFont(* m_font
); 
 177         if (event
.LeftDClick()) 
 181                         m_leftBtnDown 
= FALSE
; 
 183                         m_game
->LButtonUp(dc
, mouseX
, mouseY
); 
 185                 m_game
->LButtonDblClk(dc
, mouseX
, mouseY
); 
 187         else if (event
.LeftDown()) 
 191                         m_leftBtnDown 
= TRUE
; 
 193                         m_game
->LButtonDown(dc
, mouseX
, mouseY
); 
 196         else if (event
.LeftUp()) 
 200                         m_leftBtnDown 
= FALSE
; 
 202                         m_game
->LButtonUp(dc
, mouseX
, mouseY
); 
 205         else if (event
.RightDown() && !event
.LeftIsDown()) 
 207                 // only allow right button undo if m_rightBtnUndo is TRUE 
 210                         if (event
.ControlDown() || event
.ShiftDown()) 
 220         else if (event
.Dragging()) 
 222                 m_game
->MouseMove(dc
, mouseX
, mouseY
); 
 225         if (!event
.LeftIsDown()) 
 227                 SetCursorStyle(mouseX
, mouseY
); 
 231 void FortyCanvas::SetCursorStyle(int x
, int y
) 
 233         // Only set cursor to a hand if 'helping hand' is enabled and 
 234         // the card under the cursor can go somewhere  
 235         if (m_game
->CanYouGo(x
, y
) && m_helpingHand
) 
 237                 SetCursor(* m_handCursor
); 
 241                 SetCursor(* m_arrowCursor
); 
 246 void FortyCanvas::NewGame() 
 252 void FortyCanvas::Undo() 
 256         dc
.SetFont(* m_font
); 
 260 void FortyCanvas::Redo() 
 264         dc
.SetFont(* m_font
); 
 268 void FortyCanvas::LayoutGame()