Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.82 KB | None | 0 0
  1. unit tank.animate;
  2.  
  3. interface
  4.  
  5. uses System.classes, System.SysUtils, Vcl.ExtCtrls, Vcl.Controls, Vcl.Graphics;
  6.  
  7. type
  8.   TAnimateDirection = (adBack, adNext);
  9.  
  10.   TAnimated = class(Timage)
  11.   private
  12.     FanimateList: TImageList;
  13.     findex: integer;
  14.     FmaskColor: Tcolor;
  15.     FonFinshed: TNotifyEvent;
  16.     fonanimate:TNotifyEvent;
  17.     Ftimer: TTimer;
  18.     Finterval: integer;
  19.     fanimateDirection: TAnimateDirection;
  20.     function getCountAnimate: integer;
  21.     procedure setInterval(const Value: integer);
  22.     procedure setAnimateDirection(const Value: TAnimateDirection);
  23.     function getCenterX: integer;
  24.     function getCenterY: integer;
  25.     procedure setCenterX(const Value: integer);
  26.     procedure setCenterY(const Value: integer);
  27.   protected
  28.     procedure doFinshed; virtual;
  29.     procedure doAnimate(sender: Tobject); virtual;
  30.   public
  31.     constructor Create(AOwner: TComponent); override;
  32.     procedure loadAnimateFromFile(const filename: TFileName;
  33.       const maskColor: Tcolor);
  34.     procedure resizeImage(const Awidth, Aheight: integer);
  35.     destructor Destroy; override;
  36.     procedure next;
  37.     procedure back;
  38.     procedure startAnimate;
  39.     procedure StopAnimate;
  40.     property Interval: integer read Finterval write setInterval;
  41.     property animateList: TImageList read FanimateList;
  42.     property count: integer read getCountAnimate;
  43.     property animateDirection: TAnimateDirection read fanimateDirection
  44.       write setAnimateDirection;
  45.     property OnFinched:TNotifyEvent read FonFinshed write FonFinshed;
  46.     property onAnimate:TNotifyEvent read fonanimate write fonanimate;
  47.     property centerX:integer read getCenterX write setCenterX;
  48.     property centerY:integer read getCenterY write setCenterY;
  49.   end;
  50.  
  51. implementation
  52.  
  53. { TAnimated }
  54.  
  55. procedure TAnimated.back;
  56. var
  57.   bmp: TIcon;
  58. begin
  59.   bmp := TIcon.Create;
  60.   try
  61.     animateList.GetIcon(findex, bmp);
  62.     Picture.Assign(bmp);
  63.     Invalidate;
  64.     Dec(findex); // -1
  65.     if findex < 0 then
  66.     begin
  67.       doFinshed;
  68.       findex := animateList.count - 1;
  69.     end;
  70.   finally
  71.     FreeAndNil(bmp);
  72.   end;
  73.  
  74. end;
  75.  
  76. constructor TAnimated.Create(AOwner: TComponent);
  77. begin
  78.   inherited;
  79.   //Transparent := true;
  80.   Stretch := true;
  81.   Center := true;
  82.   Ftimer := TTimer.Create(self);
  83.   Ftimer.Enabled := false;
  84.   Interval := 100; // Ftimer.Interval:=100;
  85.   Ftimer.OnTimer:=doAnimate;
  86.   FanimateList := TImageList.Create(self);
  87.   FanimateList.Clear;
  88.   FanimateList.Width := 32;
  89.   FanimateList.Height := 32;
  90.   findex := 0;
  91.   FmaskColor := clFuchsia;
  92.   animateDirection:=adNext;
  93. end;
  94.  
  95. destructor TAnimated.Destroy;
  96. begin
  97.   if Assigned(FanimateList) then
  98.   begin
  99.     FanimateList.Clear;
  100.     FreeAndNil(FanimateList);
  101.   end;
  102.   if Assigned(Ftimer) then
  103.   begin
  104.     StopAnimate;
  105.     FreeAndNil(Ftimer);
  106.   end;
  107.   inherited;
  108. end;
  109.  
  110. procedure TAnimated.doFinshed;
  111. begin
  112.   if Assigned(FonFinshed) then
  113.     FonFinshed(self);
  114. end;
  115.  
  116. function TAnimated.getCenterX: integer;
  117. begin
  118.   result:=Left+Width div 2;
  119. end;
  120.  
  121. function TAnimated.getCenterY: integer;
  122. begin
  123.  result:=top+Height div 2;
  124. end;
  125.  
  126. function TAnimated.getCountAnimate: integer;
  127. begin
  128.   Result := animateList.count;
  129. end;
  130.  
  131. procedure TAnimated.loadAnimateFromFile(const filename: TFileName;
  132.   const maskColor: Tcolor);
  133. var
  134.   bmp: TBitmap;
  135. begin
  136.   bmp := TBitmap.Create;
  137.   try
  138.     bmp.LoadFromFile(filename);
  139.     FmaskColor := maskColor;
  140.     animateList.Clear;
  141.     animateList.Masked := true;
  142.     animateList.AddMasked(bmp, FmaskColor);
  143.   finally
  144.     FreeAndNil(bmp);
  145.   end;
  146. end;
  147.  
  148. procedure TAnimated.next;
  149. var
  150.   bmp: TIcon;
  151. begin
  152.   bmp := TIcon.Create;
  153.   try
  154.     animateList.GetIcon(findex, bmp);
  155.     Picture.Assign(bmp);
  156.  
  157.     Invalidate;
  158.     Inc(findex); // +1
  159.     if findex >= animateList.count then
  160.       doFinshed;
  161.  
  162.     findex := findex mod animateList.count;
  163.  
  164.   finally
  165.     FreeAndNil(bmp);
  166.   end;
  167.  
  168. end;
  169.  
  170. procedure TAnimated.doAnimate(sender: Tobject);
  171. begin
  172.  
  173.   case animateDirection of
  174.  
  175.     adBack:
  176.           back;
  177.     adNext:
  178.           next;
  179.  
  180.   end;
  181.    if Assigned(fonanimate) then
  182.     fonanimate(self);
  183. end;
  184.  
  185. procedure TAnimated.resizeImage(const Awidth, Aheight: integer);
  186. begin
  187.   self.Width := Awidth;
  188.   self.Height := Aheight;
  189. end;
  190.  
  191. procedure TAnimated.setAnimateDirection(const Value: TAnimateDirection);
  192. begin
  193.   fanimateDirection := Value;
  194. end;
  195.  
  196. procedure TAnimated.setCenterX(const Value: integer);
  197. begin
  198.  left:=value-Width div 2;
  199. end;
  200.  
  201. procedure TAnimated.setCenterY(const Value: integer);
  202. begin
  203.  top:=Value - Height div 2;
  204. end;
  205.  
  206. procedure TAnimated.setInterval(const Value: integer);
  207. begin
  208.   Finterval := Value;
  209.   Ftimer.Interval := Finterval;
  210. end;
  211.  
  212. procedure TAnimated.startAnimate;
  213. begin
  214.   Ftimer.Enabled := true;
  215. end;
  216.  
  217. procedure TAnimated.StopAnimate;
  218. begin
  219.   Ftimer.Enabled := false;
  220. end;
  221.  
  222. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement