Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Chess pieces snap to new squares, how?

Author: Bo Persson

Date: 13:11:39 11/30/01

Go up one level in this thread


On November 30, 2001 at 12:46:40, Eran wrote:

>
>I develop a chess interface based on MFC 6. The draggable chess pieces, from
>bitmap sources, are not based on Ole Drag and Drop at all but based on
>OnLButtonDown, OnMouseMove, and OnLButtonUp functions. I successfully drag them
>smoothly every where on the chess board window, but I do not know how to write
>code that make a dragged chess piece snapped into a new square.

It's not supposed to land on the new square. :-)
You have to check the validity of the move first. Use the From and To squares of
the mouse drag and find a move that matches those squares.

Here's my code for the mouse-up event:

void CBoardView::OnLButtonUp(UINT /*nFlags*/, CPoint point)
{
   if (GetDocument()->InputIsActive() && Chess::IsOnBoard(DragFromSquare))
   {
      const Chess::SQUARE FromSquare = DragFromSquare;
      const Chess::SQUARE ToSquare   = PointToSquare(point);

      // Button released -> stop further dragging

      ResetDragDrop();

      // Remove the dragged image

      PieceImages.EndDrag();
      PieceImages.DragLeave(this);

      // Release the mouse

      ReleaseCapture();

      // Initiate repainting of the original square

      InvalidateRect(DisplayedBoard[FromSquare]);

      // Process move, if piece really dragged to a new square

      if (ToSquare != FromSquare && Chess::IsOnBoard(ToSquare))
         GetDocument()->ProcessHumanMove(FromSquare,ToSquare);

   }
}


The move is processed just like if it had been entered through the keyboard, or
anything else. The dragged image is just a fancy interface!

>In addition, I
>do not know how to write another code that when a chess player cancels dragging
>a chess piece, it should automatically snap back to an old square. I do not know
>whether the CRectTracker::AdjustRect function works out for it.

You might try to redraw the piece by something like:

    InvalidateRect(DisplayedBoard[DragFromSquare]);
    UpdateWindow();


>If you know how to do it or if you have a nice code as a sample, please let me
>know.
>
>I would appreciate your help a lot.
>
>Thanks in advance,
>Eran
>Owner of MS Visual C++ 6 program
>My new email address: erk@012.net.il



Bo Persson
bop2@telia.com



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.