Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit tank.animate;
- interface
- uses System.classes, System.SysUtils, Vcl.ExtCtrls, Vcl.Controls, Vcl.Graphics;
- type
- TAnimateDirection = (adBack, adNext);
- TAnimated = class(Timage)
- private
- FanimateList: TImageList;
- findex: integer;
- FmaskColor: Tcolor;
- FonFinshed: TNotifyEvent;
- fonanimate:TNotifyEvent;
- Ftimer: TTimer;
- Finterval: integer;
- fanimateDirection: TAnimateDirection;
- function getCountAnimate: integer;
- procedure setInterval(const Value: integer);
- procedure setAnimateDirection(const Value: TAnimateDirection);
- function getCenterX: integer;
- function getCenterY: integer;
- procedure setCenterX(const Value: integer);
- procedure setCenterY(const Value: integer);
- protected
- procedure doFinshed; virtual;
- procedure doAnimate(sender: Tobject); virtual;
- public
- constructor Create(AOwner: TComponent); override;
- procedure loadAnimateFromFile(const filename: TFileName;
- const maskColor: Tcolor);
- procedure resizeImage(const Awidth, Aheight: integer);
- destructor Destroy; override;
- procedure next;
- procedure back;
- procedure startAnimate;
- procedure StopAnimate;
- property Interval: integer read Finterval write setInterval;
- property animateList: TImageList read FanimateList;
- property count: integer read getCountAnimate;
- property animateDirection: TAnimateDirection read fanimateDirection
- write setAnimateDirection;
- property OnFinched:TNotifyEvent read FonFinshed write FonFinshed;
- property onAnimate:TNotifyEvent read fonanimate write fonanimate;
- property centerX:integer read getCenterX write setCenterX;
- property centerY:integer read getCenterY write setCenterY;
- end;
- implementation
- { TAnimated }
- procedure TAnimated.back;
- var
- bmp: TIcon;
- begin
- bmp := TIcon.Create;
- try
- animateList.GetIcon(findex, bmp);
- Picture.Assign(bmp);
- Invalidate;
- Dec(findex); // -1
- if findex < 0 then
- begin
- doFinshed;
- findex := animateList.count - 1;
- end;
- finally
- FreeAndNil(bmp);
- end;
- end;
- constructor TAnimated.Create(AOwner: TComponent);
- begin
- inherited;
- //Transparent := true;
- Stretch := true;
- Center := true;
- Ftimer := TTimer.Create(self);
- Ftimer.Enabled := false;
- Interval := 100; // Ftimer.Interval:=100;
- Ftimer.OnTimer:=doAnimate;
- FanimateList := TImageList.Create(self);
- FanimateList.Clear;
- FanimateList.Width := 32;
- FanimateList.Height := 32;
- findex := 0;
- FmaskColor := clFuchsia;
- animateDirection:=adNext;
- end;
- destructor TAnimated.Destroy;
- begin
- if Assigned(FanimateList) then
- begin
- FanimateList.Clear;
- FreeAndNil(FanimateList);
- end;
- if Assigned(Ftimer) then
- begin
- StopAnimate;
- FreeAndNil(Ftimer);
- end;
- inherited;
- end;
- procedure TAnimated.doFinshed;
- begin
- if Assigned(FonFinshed) then
- FonFinshed(self);
- end;
- function TAnimated.getCenterX: integer;
- begin
- result:=Left+Width div 2;
- end;
- function TAnimated.getCenterY: integer;
- begin
- result:=top+Height div 2;
- end;
- function TAnimated.getCountAnimate: integer;
- begin
- Result := animateList.count;
- end;
- procedure TAnimated.loadAnimateFromFile(const filename: TFileName;
- const maskColor: Tcolor);
- var
- bmp: TBitmap;
- begin
- bmp := TBitmap.Create;
- try
- bmp.LoadFromFile(filename);
- FmaskColor := maskColor;
- animateList.Clear;
- animateList.Masked := true;
- animateList.AddMasked(bmp, FmaskColor);
- finally
- FreeAndNil(bmp);
- end;
- end;
- procedure TAnimated.next;
- var
- bmp: TIcon;
- begin
- bmp := TIcon.Create;
- try
- animateList.GetIcon(findex, bmp);
- Picture.Assign(bmp);
- Invalidate;
- Inc(findex); // +1
- if findex >= animateList.count then
- doFinshed;
- findex := findex mod animateList.count;
- finally
- FreeAndNil(bmp);
- end;
- end;
- procedure TAnimated.doAnimate(sender: Tobject);
- begin
- case animateDirection of
- adBack:
- back;
- adNext:
- next;
- end;
- if Assigned(fonanimate) then
- fonanimate(self);
- end;
- procedure TAnimated.resizeImage(const Awidth, Aheight: integer);
- begin
- self.Width := Awidth;
- self.Height := Aheight;
- end;
- procedure TAnimated.setAnimateDirection(const Value: TAnimateDirection);
- begin
- fanimateDirection := Value;
- end;
- procedure TAnimated.setCenterX(const Value: integer);
- begin
- left:=value-Width div 2;
- end;
- procedure TAnimated.setCenterY(const Value: integer);
- begin
- top:=Value - Height div 2;
- end;
- procedure TAnimated.setInterval(const Value: integer);
- begin
- Finterval := Value;
- Ftimer.Interval := Finterval;
- end;
- procedure TAnimated.startAnimate;
- begin
- Ftimer.Enabled := true;
- end;
- procedure TAnimated.StopAnimate;
- begin
- Ftimer.Enabled := false;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement