home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 January / Pcwk0198.iso / Dcomplib / ANIMATED.LZH / INSTSTUF.ZIP / ANIMATE.PAS < prev   
Pascal/Delphi Source File  |  1995-04-02  |  5KB  |  214 lines

  1. unit Animate;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TAnimated = class(TGraphicControl)
  11.   private
  12.     FBitMap : TBitmap;
  13.     FFrameCount : integer;
  14.     FFrame : Integer;
  15.     Timer : TTimer;
  16.     FInterval : integer;
  17.     FLoop : boolean;
  18.     FReverse : boolean;
  19.     FPlay : boolean;
  20.     FTransparentColor : TColor;
  21.     FOnChangeFrame : TNotifyEvent;
  22.     procedure SetFrame(Value : Integer);
  23.     procedure SetInterval(Value : integer);
  24.     procedure SetBitMap(Value : TBitMap);
  25.     procedure SetPlay(Onn : boolean);
  26.     procedure SetTransparentColor(Value : TColor);
  27.   protected
  28.     procedure Paint; override;
  29.     procedure TimeHit(Sender : TObject);
  30.   public
  31.     constructor Create(AOwner: TComponent); override;
  32.     destructor Destroy; override;
  33.   published
  34.     property Interval : integer read FInterval write SetInterval;
  35.     {Note: FrameCount must precede Frame in order for initialization to be correct}
  36.     property FrameCount : integer read FFrameCount write FFrameCount default 1;
  37.     property Frame : Integer read FFrame write SetFrame;
  38.     property BitMap : TBitMap read FBitMap write SetBitMap;
  39.     property Play : boolean read FPlay write SetPlay;
  40.     property Reverse: boolean read FReverse write FReverse;
  41.     property Loop: boolean read FLoop write FLoop default True;
  42.     property TransparentColor : TColor read FTransparentColor
  43.              write SetTransparentColor default -1;
  44.     property Height default 30;
  45.     property Width default 30;
  46.     property OnChangeFrame: TNotifyEvent read FOnChangeFrame
  47.                             write FOnChangeFrame;
  48.     property OnDragDrop;
  49.     property OnDragOver;
  50.     property OnEndDrag;
  51.     property OnMouseDown;
  52.     property OnMouseMove;
  53.     property OnMouseUp;
  54.     property Visible;
  55.   end;
  56.  
  57. procedure Register;
  58.  
  59. implementation
  60.  
  61. constructor TAnimated.Create(AOwner: TComponent);
  62. begin
  63. inherited Create(AOwner);
  64. Width := 30;
  65. Height := 30;
  66. FBitMap := TBitMap.Create;
  67. FrameCount := 1;
  68. ControlStyle := ControlStyle +[csOpaque];
  69. FLoop := True;
  70. FTransparentColor := -1;
  71. end;
  72.  
  73. destructor TAnimated.Destroy;
  74. begin
  75. Timer.Free;
  76. FBitMap.Free;
  77. inherited Destroy;
  78. end;
  79.  
  80. procedure TAnimated.SetBitMap(Value : TBitMap);
  81. begin
  82. FBitMap.Assign(Value);
  83. Height := FBitMap.Height;
  84. if Height = 0 then Height := 30;  {so something will display}
  85. end;
  86.  
  87. procedure TAnimated.SetInterval(Value : Integer);
  88. begin
  89. if Value <> FInterval then
  90.   begin
  91.   Timer.Free;
  92.   Timer := Nil;
  93.   if FPlay and (Value > 0) then
  94.     begin
  95.     Timer := TTimer.Create(Self);
  96.     Timer.Interval := Value;
  97.     Timer.OnTimer := TimeHit;
  98.     end;
  99.   FInterval := Value;
  100.   end;
  101. end;
  102.  
  103. procedure TAnimated.SetPlay(Onn : boolean);
  104. begin
  105. if Onn <> FPlay then
  106.   begin
  107.   FPlay := Onn;
  108.   if not Onn then
  109.     begin
  110.     Timer.Free;
  111.     Timer := Nil;
  112.     end
  113.   else if FInterval > 0 then
  114.     begin
  115.     Timer := TTimer.Create(Self);
  116.     Timer.Interval := FInterval;
  117.     Timer.OnTimer := TimeHit;
  118.     end;
  119.   end;
  120. end;
  121.  
  122. procedure TAnimated.SetFrame(Value : Integer);
  123. var
  124.   Temp : Integer;
  125. begin
  126. if Value < 0 then
  127.   Temp := FFrameCount - 1
  128. else
  129.   Temp := Value Mod FFrameCount;
  130. if Temp <> FFrame then
  131.   begin
  132.   FFrame := Temp;
  133.   if Assigned(FOnChangeFrame) then FOnChangeFrame(Self);
  134.   Invalidate;
  135.   end;
  136. end;
  137.  
  138. procedure TAnimated.SetTransparentColor(Value : TColor);
  139. begin
  140. if Value <> FTransparentColor then
  141.   begin
  142.   FTransparentColor := Value;
  143.   Invalidate;
  144.   end;
  145. end;
  146.  
  147. procedure TAnimated.TimeHit(Sender : TObject);
  148.   procedure ChkStop;
  149.   begin
  150.   if not FLoop then
  151.     begin
  152.     FPlay := False;
  153.     Timer.Free;
  154.     Timer := Nil;
  155.     end;
  156.   end;
  157.  
  158. begin
  159. if FReverse then
  160.   begin
  161.   Frame := Frame-1;
  162.   if FFrame = 0 then ChkStop;
  163.   end
  164. else
  165.   begin
  166.   Frame := Frame+1;
  167.   if FFrame = FrameCount-1 then ChkStop;
  168.   end;
  169. end;
  170.  
  171. procedure TAnimated.Paint;
  172. var
  173.   ARect, BRect : TRect;
  174.   X : Integer;
  175.   Tmp : TBitMap;
  176. begin
  177. ARect := Rect(0,0,Width,Height);
  178. if FBitMap.Height > 0 then
  179.   begin
  180.   X := Width*FFrame;
  181.   BRect := Rect(X,0, X+Width, Height);
  182.   if (FTransparentColor >= 0) and (FTransparentColor <= $7FFFFFFF) then
  183.     begin    {draw on Tmp bitmap to eliminate flicker}
  184.     Tmp := TBitmap.Create;
  185.     Tmp.Height := FBitMap.Height;
  186.     Tmp.Width := FBitMap.Width;
  187.     Tmp.Canvas.Brush.Color := Color;
  188.     Tmp.Canvas.BrushCopy(ARect, FBitmap, BRect, FTransparentColor);
  189.     Canvas.CopyRect(ARect, Tmp.Canvas, ARect);
  190.     Tmp.Free;
  191.     end
  192.   else  {can draw direct}
  193.     Canvas.CopyRect(ARect, FBitmap.Canvas, BRect);
  194.   end
  195. else
  196.   begin   {fill with something}
  197.   Canvas.Brush.Color := clWhite;
  198.   Canvas.FillRect(BoundsRect);
  199.   end;
  200. if csDesigning in ComponentState then
  201.   begin    {to add visibility when designing}
  202.   Canvas.Pen.Style := psDash;
  203.   Canvas.Brush.Style := bsClear;
  204.   Canvas.Rectangle(0, 0, Width, Height);
  205.   end;
  206. end;
  207.  
  208. procedure Register;
  209. begin
  210.   RegisterComponents('MyStuff', [TAnimated]);
  211. end;
  212.  
  213. end.
  214.