home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 10.ddi / CHESS.ZIP / OWPIECES.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  4.8 KB  |  177 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Chess Demo                     }
  4. {   Game Pieces                                  }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit OWPieces;
  10.  
  11. interface
  12.  
  13. uses WinProcs, WinTypes, Objects, OWindows, Chessdll;
  14.  
  15. type
  16.   PGamePiece = ^TGamePiece;
  17.   TGamePiece = object(TObject)
  18.     Parent: PWindowsObject;      { the game board }
  19.     Square: TLocation;
  20.     Rect: TRect;
  21.     Bitmap: HBitmap;
  22.     Bitmask: HBitmap;
  23.     BitOfsX: shortint;     { offset to center bitmap}
  24.     BitOfsY: shortint;
  25.     BitSize: TPoint;
  26.     Dragging: Boolean;
  27.     DragHidden: Boolean;
  28.     NeedRedraw: Boolean;
  29.     constructor Init(AParent: PWindowsObject; BitName, MaskName: PChar;
  30.       ASquare: TLocation);
  31.     destructor Done; virtual;
  32.     function  HitTest(P: TPoint): Boolean; virtual;
  33.     procedure Paint(DestDC: HDC);
  34.     procedure RequestRedraw;
  35.     function  GetCursor: HCursor; virtual;
  36.     procedure SetRect(const R: TRect); virtual;
  37.     function  CanDrag: Boolean; virtual;
  38.     procedure DragBegin(DC: HDC; Mouse: TPoint); virtual;
  39.     procedure DragContinue(DC: HDC; Mouse: TPoint; Sq: TLocation); virtual;
  40.     procedure DragHide;  virtual;
  41.     function  DragEnd(DC: HDC; Mouse: TPoint; Sq: TLocation;
  42.       var Move): Boolean; virtual;
  43.   end;
  44.  
  45. implementation
  46.  
  47. constructor TGamePiece.Init(AParent: PWindowsObject; BitName, MaskName: PChar;
  48.   ASquare: TLocation);
  49. var
  50.   BI: TBitmap;
  51. begin
  52.   inherited Init;
  53.   Parent := AParent;
  54.   Bitmap  := LoadBitmap(HInstance, BitName);
  55.   Bitmask := LoadBitmap(HInstance, MaskName);
  56.   FillChar(Rect, SizeOf(Rect), 0);
  57.   GetObject(Bitmap, SizeOf(BI), @BI);
  58.   BitOfsX := 0;
  59.   BitOfsY := 0;
  60.   BitSize.X := BI.bmWidth;
  61.   BitSize.Y := BI.bmHeight;
  62.   Square := ASquare;
  63.   Dragging := False;
  64.   DragHidden := False;
  65.   NeedRedraw := False;
  66. end;
  67.  
  68. destructor TGamePiece.Done;
  69. begin
  70.   DeleteObject(Bitmap);
  71.   DeleteObject(Bitmask);
  72.   inherited Done;
  73. end;
  74.  
  75. function TGamePiece.HitTest(P: TPoint): Boolean;
  76. begin
  77.   HitTest := PtInRect(Rect, P);
  78. end;
  79.  
  80. procedure TGamePiece.Paint(DestDC: HDC);
  81. var
  82.   OldBits : HBitmap;
  83.   MemDC : HDC;
  84. begin
  85.   if not DragHidden then
  86.   begin
  87.     MemDC := CreateCompatibleDC(DestDC);
  88.     OldBits := SelectObject(MemDC, BitMask);
  89.     BitBlt(DestDC, Rect.Left + BitOfsX,
  90.                Rect.Top + BitOfsY,
  91.                BitSize.X, BitSize.Y,
  92.            MemDC, 0, 0, SrcAnd);
  93.     SelectObject(MemDC, Bitmap);
  94.     BitBlt(DestDC, Rect.Left + BitOfsX,
  95.                Rect.Top + BitOfsY,
  96.                BitSize.X, BitSize.Y,
  97.            MemDC, 0, 0, SrcPaint);
  98.     SelectObject(MemDC, OldBits);
  99.     DeleteDC(MemDC);
  100.   end;
  101.   NeedRedraw := False;
  102. end;
  103.  
  104. procedure TGamePiece.RequestRedraw;
  105. begin
  106.   NeedRedraw := True;
  107.   InvalidateRect(Parent^.HWindow, @Rect, False);
  108. end;
  109.  
  110. function TGamePiece.GetCursor: HCursor;
  111. begin
  112.   GetCursor := LoadCursor(0, idc_Arrow);
  113. end;
  114.  
  115. procedure TGamePiece.SetRect(const R: TRect);
  116. begin
  117.   Rect := R;
  118.   BitOfsX := ((Rect.Right - Rect.Left) div 2) - (BitSize.X div 2);
  119.   BitOfsY := ((Rect.Bottom - Rect.Top) div 2) - (BitSize.Y div 2);
  120. end;
  121.  
  122. function TGamePiece.CanDrag : Boolean;
  123. begin
  124.   CanDrag := True;
  125. end;
  126.  
  127. procedure TGamePiece.DragBegin(DC: HDC; Mouse: TPoint);
  128. begin
  129.   Dragging := True;
  130.   with Rect do
  131.   begin
  132.     Left := Mouse.X - BitSize.X div 2;
  133.     Top := Mouse.Y - BitSize.Y div 2;
  134.     Right := Left + BitSize.X;
  135.     Bottom := Top + BitSize.Y;
  136.   end;
  137.   BitOfsX := 0;
  138.   BitOfsY := 0;
  139.   Paint(DC);
  140.   SetCursor(GetCursor);
  141. end;
  142.  
  143. procedure TGamePiece.DragContinue(DC: HDC; Mouse: TPoint; Sq: TLocation);
  144. begin
  145.   DragHidden := False;
  146.   OffSetRect(Rect, (Mouse.X - BitSize.X div 2) - Rect.Left,
  147.                    (Mouse.Y - BitSize.Y div 2) - Rect.Top);
  148.   Paint(DC);
  149.   SetCursor(GetCursor);
  150. end;
  151.  
  152. { DragEnd
  153.   Result = True means that the destination square is acceptable to this piece.
  154.   Result = False means the destination is unacceptable and the piece wants
  155.   to return to its original location.
  156.   Do not modify the Square field during a drag.
  157.   You may modify Rect during a drag - SetRect will be called after the drag
  158.   is completed to reset the Rect to its true board pixel coordinates.
  159.   Descendents should fill in the Move parameter with relavent info
  160.   (Piecetype, from, to, etc) if the destination is acceptable.
  161.   If a repaint of part of the board is needed, use InvalidateRect. }
  162.  
  163. function  TGamePiece.DragEnd(DC: HDC; Mouse: TPoint;
  164.                              Sq: TLocation; var Move): Boolean;
  165. begin
  166.   Dragging := False;
  167.   DragEnd := not DragHidden;
  168.   DragHidden := False;
  169.   SetCursor(GetCursor);
  170. end;
  171.  
  172. procedure TGamePiece.DragHide;
  173. begin
  174.   DragHidden := True;
  175. end;
  176.  
  177. end.