home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / CHESSOWL.ZIP / PIECES.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  4.6 KB  |  171 lines

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