From: <pha...@us...> - 2007-08-20 07:02:42
|
Revision: 52 http://chessdrills.svn.sourceforge.net/chessdrills/?rev=52&view=rev Author: phantomfive Date: 2007-08-20 00:02:31 -0700 (Mon, 20 Aug 2007) Log Message: ----------- The following functions of ChessDrill.h are now functional: ShouldAllowPiecePickup(), PieceWasPickedUp(), ShouldAllowPiecePlacement(), PieceWasPlaced(), SquareWasSelected() in addition to the ones that were already working Modified Paths: -------------- base/ChessDrill.h base/Main.cpp drills/ExampleDrill.cpp drills/ExampleDrill.h gui/GuiControl.cpp gui/GuiControl.h gui/MainWindow.cpp gui/MainWindow.h Modified: base/ChessDrill.h =================================================================== --- base/ChessDrill.h 2007-08-16 06:54:31 UTC (rev 51) +++ base/ChessDrill.h 2007-08-20 07:02:31 UTC (rev 52) @@ -153,10 +153,11 @@ // void SetTimer( (***), unsigned int milliseconds, void *data); //Enables or disables the board. The default state is enabled. - //PARAM enable - if false, all user input is ignored + //PARAM enable - if false, all user input is ignored so you will have to set + // a timer if you want to do anything else void Enable(bool enable); -//Pay o attention to the lines below this comment +//Pay no attention to the lines below this comment void SetGuiControl( GuiControl *gui ); private: GuiControl *gui; Modified: base/Main.cpp =================================================================== --- base/Main.cpp 2007-08-16 06:54:31 UTC (rev 51) +++ base/Main.cpp 2007-08-20 07:02:31 UTC (rev 52) @@ -14,14 +14,9 @@ bool Main::OnInit(){ //create the main window - printf("about to create a new window\n"); MainWindow *win = new MainWindow( "Chess Drills", wxPoint(50,50), wxSize(640,640) ); - printf("about to show window\n"); - win->Show(TRUE); - printf("about to set top\n"); SetTopWindow(win); - printf("returning true\n"); return TRUE; } Modified: drills/ExampleDrill.cpp =================================================================== --- drills/ExampleDrill.cpp 2007-08-16 06:54:31 UTC (rev 51) +++ drills/ExampleDrill.cpp 2007-08-20 07:02:31 UTC (rev 52) @@ -23,16 +23,16 @@ //first clear the board (not necessary, but just in case) ClearBoard(); - //put my pawn on E5 + //put my pawn on E3 myPawn.row = ROW_FIVE; - myPawn.col = COL_A; + myPawn.col = COL_E; tempSquare.piece.color = CLR_BLACK; tempSquare.piece.type = CP_PAWN; SetSquare(myPawn.col, myPawn.row, tempSquare); - //put their pawn on A3 - theirPawn.row = ROW_THREE; + //put their pawn on A5 + theirPawn.row = ROW_FIVE; theirPawn.col = COL_A; tempSquare.piece.color = CLR_WHITE; tempSquare.piece.type = CP_PAWN; @@ -100,6 +100,13 @@ location.row, piece.type ); } +bool ExampleDrill::ShouldAllowPiecePickup( const ChessPiece &piece, + const ChessSquare &from ){ + if( piece.color != CLR_WHITE ){ + return false; + } + return true; +} //------------------------------------------------------------------------- // Private methods //------------------------------------------------------------------------- @@ -110,7 +117,7 @@ if( from.col != to.col ) return false; - if( from.col != to.col - 1 ) + if( from.row != to.row - 1 ) return false; return true; Modified: drills/ExampleDrill.h =================================================================== --- drills/ExampleDrill.h 2007-08-16 06:54:31 UTC (rev 51) +++ drills/ExampleDrill.h 2007-08-20 07:02:31 UTC (rev 52) @@ -28,6 +28,10 @@ void ExampleDrill::SquareWasSelected( const ChessSquare &location, const ChessPiece &piece ); + +bool ExampleDrill::ShouldAllowPiecePickup( const ChessPiece &piece, + const ChessSquare &from ); + private: ChessSquare myPawn; //location of computer's pawn Modified: gui/GuiControl.cpp =================================================================== --- gui/GuiControl.cpp 2007-08-16 06:54:31 UTC (rev 51) +++ gui/GuiControl.cpp 2007-08-20 07:02:31 UTC (rev 52) @@ -75,6 +75,39 @@ //--------------------------------------------------------------- // Methods called by the GUI //--------------------------------------------------------------- +bool GuiControl::ShouldAllowPiecePlacement(const ChessPiece &piece, + const ChessSquare &from, + const ChessSquare &to, + const ChessPiece &captured){ + if( currentDrill != NULL ){ + return currentDrill->ShouldAllowPiecePlacement( piece, from, to, captured ); + } + return false; +} + +void GuiControl::PieceWasPlaced(const ChessPiece &piece, + const ChessSquare &from, + const ChessSquare &to, + const ChessPiece &captured){ + if( currentDrill != NULL ){ + currentDrill->PieceWasPlaced(piece, from, to, captured ); + } +} +bool GuiControl::ShouldAllowPiecePickup(const ChessPiece &piece, + const ChessSquare &from){ + if( currentDrill != NULL ){ + return currentDrill->ShouldAllowPiecePickup( piece, from ); + } + return false; +} + void GuiControl::PieceWasPickedUp(const ChessPiece &piece, + const ChessSquare &from){ + if(currentDrill != NULL ){ + currentDrill->PieceWasPickedUp( piece, from ); + } + } + + void GuiControl::SquareWasSelected( int &row, int &col ){ const ChessSquare *sqr = board.GetSquare((ChessColumn)col, (ChessRow)row); const ChessPiece *piece = &(sqr->piece); Modified: gui/GuiControl.h =================================================================== --- gui/GuiControl.h 2007-08-16 06:54:31 UTC (rev 51) +++ gui/GuiControl.h 2007-08-20 07:02:31 UTC (rev 52) @@ -52,11 +52,29 @@ void FinishDrill(); + void SetUserColor(ChessColor color); //-------------------------------------------------------------- //Methods called by the GUI //-------------------------------------------------------------- + bool ShouldAllowPiecePlacement(const ChessPiece &piece, + const ChessSquare &from, + const ChessSquare &to, + const ChessPiece &captured); + + void PieceWasPlaced(const ChessPiece &piece, + const ChessSquare &from, + const ChessSquare &to, + const ChessPiece &captured); + + bool ShouldAllowPiecePickup(const ChessPiece &piece, + const ChessSquare &from); + + void PieceWasPickedUp(const ChessPiece &piece, + const ChessSquare &from); + + //Indicate when a square is selected void SquareWasSelected( int &row, int &col ); //Returns a list of the drills that are available. These names Modified: gui/MainWindow.cpp =================================================================== --- gui/MainWindow.cpp 2007-08-16 06:54:31 UTC (rev 51) +++ gui/MainWindow.cpp 2007-08-20 07:02:31 UTC (rev 52) @@ -42,7 +42,7 @@ const wxSize &size): wxFrame((wxFrame*)NULL, -1, title, pos, size){ wxImage::AddHandler(new wxGIFHandler); - printf("starting to load file\n"); + //load the images if( !blackSquare.LoadFile("images/blackSquare.gif",wxBITMAP_TYPE_GIF) || !whiteSquare.LoadFile("images/whiteSquare.gif",wxBITMAP_TYPE_GIF) || @@ -62,9 +62,7 @@ printf("error couldn't load images!!!\n"); exit(-1); } - printf("loaded images\n"); CreateStatusBar(); - SetStatusText( "Here is our board!" ); boardIsFlipped = false; dragging = false; @@ -134,21 +132,12 @@ if( NULL != board ){ for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ + if( dragging && j==(int)dragStartCol && i==(int)dragStartRow ){ + continue; + } const ChessPiece *piece = board->GetPiece((ChessColumn)j,(ChessRow)i); - ChessColor c = piece->color; - //find which piece and color - switch (piece->type){ - case CP_PAWN: img = (c==CLR_WHITE)?&whitePawn: &blackPawn;break; - case CP_ROOK: img = (c==CLR_WHITE)?&whiteRook: &blackRook;break; - case CP_KNIGHT:img = (c==CLR_WHITE)?&whiteKnight:&blackKnight;break; - case CP_BISHOP:img = (c==CLR_WHITE)?&whiteBishop:&blackBishop;break; - case CP_QUEEN: img = (c==CLR_WHITE)?&whiteQueen: &blackQueen;break; - case CP_KING: img = (c==CLR_WHITE)?&whiteKing: &blackKing;break; - - case CP_NOPIECE: - default: - img = NULL; - } + img = GetImageForPiece( piece ); + //draw that piece if( img!= NULL ){ if(boardIsFlipped) @@ -160,12 +149,35 @@ } } - //dragging stuff + //draw the piece being dragged, if necessary if( dragging ){ - dc.DrawBitmap( blackQueen, mouseX-(SQUARE_HEIGHT/2), mouseY-(SQUARE_WIDTH/2), true ); + img = GetImageForPiece( dragPiece ); + dc.DrawBitmap( *img, mouseX-(SQUARE_HEIGHT/2), + mouseY-(SQUARE_WIDTH/2), true ); } } +wxBitmap *MainWindow::GetImageForPiece( const ChessPiece *piece ){ + wxBitmap *img; + ChessColor c = piece->color; + switch (piece->type){ + case CP_PAWN: img = (c==CLR_WHITE)?&whitePawn: &blackPawn;break; + case CP_ROOK: img = (c==CLR_WHITE)?&whiteRook: &blackRook;break; + case CP_KNIGHT:img = (c==CLR_WHITE)?&whiteKnight:&blackKnight;break; + case CP_BISHOP:img = (c==CLR_WHITE)?&whiteBishop:&blackBishop;break; + case CP_QUEEN: img = (c==CLR_WHITE)?&whiteQueen: &blackQueen;break; + case CP_KING: img = (c==CLR_WHITE)?&whiteKing: &blackKing;break; + + case CP_NOPIECE: + default: + img = NULL; + } + return img; +} + +//------------------------------------------------------------------------ +// Menu Functions +//------------------------------------------------------------------------ void MainWindow::OnQuit(wxCommandEvent& WXUNUSED(event)) { Close(TRUE); @@ -185,38 +197,83 @@ //----------------------------------------------------------------------- void MainWindow::OnMouseDown( wxMouseEvent& event ){ - bool rv; int row, col; - printf("Got a mouse down event!!\n"); - printf("located at (%d,%d)\n",event.m_x, event.m_y); - rv = GetSquareFromPoint(event.m_x,event.m_y,row,col); - printf("square is (%d,%d) it is %s the board\n",col,row, - (rv)?"on":"not on"); - dragging = true; - mouseX = event.m_x; - mouseY = event.m_y; - Refresh( false ); - Update(); + ChessBoard *board = ctl.GetCurrentBoard(); + const ChessPiece *piece; + bool onBoard; + bool shouldAllow; + const ChessSquare *from; + + onBoard = GetSquareFromPoint(event.m_x,event.m_y,row,col); + dragStartCol = col; + dragStartRow = row; + if( onBoard ){ + piece = board->GetPiece((ChessColumn)col, (ChessRow)row); + if(piece->type != CP_NOPIECE){ + //check to see if we should let it drag + from = board->GetSquare((ChessColumn)col, (ChessRow)row); + shouldAllow = ctl.ShouldAllowPiecePickup( *piece, *from); + if(shouldAllow){ + dragging = true; + mouseX = event.m_x; + mouseY = event.m_y; + dragPiece = piece; + dragStartX = event.m_x; + dragStartY = event.m_y; + Refresh( false ); + Update(); + ctl.PieceWasPickedUp( *piece, *from ); + } + } + } } void MainWindow::OnMouseUp( wxMouseEvent& event ){ - /*int row, col; - bool rv; - rv = GetSquareFromPoint(event.m_x, event.m_y, row, col ); - if( rv ){ - ctl.SquareWasSelected( row, col ); - } - */ - bool rv; - int row, col; - printf("Got a mouse up event!!\n"); - printf("located at (%d,%d)\n",event.m_x, event.m_y); - rv = GetSquareFromPoint(event.m_x,event.m_y,row,col); - printf("square is (%d,%d) it is %s the board\n",col,row, - (rv)?"on":"not on"); - dragging = false; - Refresh( false ); - Update(); + + bool onBoard; + bool shouldAllow; + int row, col; + const ChessPiece *captured; + ChessBoard *board = ctl.GetCurrentBoard(); + const ChessSquare *endSquare; + const ChessSquare *startSquare; + + onBoard = GetSquareFromPoint(event.m_x,event.m_y,row,col); + + startSquare = board->GetSquare((ChessColumn)dragStartCol, + (ChessRow)dragStartRow ); + + //see if it was a click + if( onBoard && col==dragStartCol && row==dragStartRow ){ + ctl.SquareWasSelected( row, col ); + } + + if(dragging){ + //if they released on a different square than they started + if( onBoard && (col!=dragStartCol || row!=dragStartRow)){ + endSquare = board->GetSquare((ChessColumn)col,(ChessRow)row); + captured = &endSquare->piece; + shouldAllow = ctl.ShouldAllowPiecePlacement(*dragPiece, *startSquare, + *endSquare, *captured); + + //check if we should move it + if( shouldAllow ){ + board->MovePiece(*startSquare, *endSquare); + } + //redraw + dragging = false; + Refresh( false ); + Update(); + + //inform that it was moved + if(shouldAllow){ + ctl.PieceWasPlaced(*dragPiece, *startSquare, *endSquare, *captured ); + } + } + dragging = false; + Refresh( false ); + Update(); + } } void MainWindow::OnMouseMove( wxMouseEvent& event ){ Modified: gui/MainWindow.h =================================================================== --- gui/MainWindow.h 2007-08-16 06:54:31 UTC (rev 51) +++ gui/MainWindow.h 2007-08-20 07:02:31 UTC (rev 52) @@ -25,8 +25,9 @@ void OnQuit(wxCommandEvent &event); void Redraw(wxPaintEvent &event); void OnSelectDrill(wxCommandEvent& event); - void OnMouseClick( wxMouseEvent& event ); + void OnMouseDown( wxMouseEvent& event ); void OnMouseUp( wxMouseEvent& event ); + void OnMouseMove( wxMouseEvent& event ); DECLARE_EVENT_TABLE() //PUBLIC METHODS @@ -68,7 +69,18 @@ blackBishop, whiteBishop, blackKing, whiteKing, blackQueen, whiteQueen; + //returns NULL if there is no image + wxBitmap *GetImageForPiece( const ChessPiece *piece ); + //Mouse stuff + bool dragging; + int mouseX; + int mouseY; + const ChessPiece *dragPiece; + int dragStartX; + int dragStartY; + int dragStartCol; + int dragStartRow; }; enum{ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |