unit SprAnim;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,DesignIntf, Dialogs,dxsprite,dxdraws,dxInput,
  DesignEditors;

type
  TSprAnim = class(TPersistent)
  private
    { Déclarations privées}
    Acc1,Acc2:integer;
    FItm1,FItm2:integer;
    FHIndexDebut:integer;
    FHindexCount:integer;
    FBIndexDebut:integer;
    FBindexCount:integer;
    FDIndexDebut:integer;
    FDindexCount:integer;
    FGIndexDebut:integer;
    FGindexCount:integer;
    FHVitesse:integer;
    FBVitesse:integer;
    FDVitesse:integer;
    FGVitesse:integer;
    FHVitesseDiv:integer;
    FBVitesseDiv:integer;
    FDVitesseDiv:integer;
    FGVitesseDiv:integer;
    FAnimBoucle:boolean;
    FHDeplace:integer;
    FBDeplace:integer;
    FDDeplace:integer;
    FGDeplace:integer;
    FDefileEcran:Boolean;
    FLimitH:integer;
    FLimitB:integer;
    FLimitD:integer;
    FlimitG:integer;
    FLimitActive:boolean;
    FAcceleration:integer;
    FRalenti:integer;

  protected
    { Déclarations protégées}
  public
    { Déclarations publiques}
  published
    { Déclarations publiées}
    property HIndexDebut:integer read FHIndexDebut write FHIndexDebut;
    property HindexCount:integer read FHindexCount write FHindexCount;
    property BIndexDebut:integer read FbIndexDebut write FbIndexDebut;
    property BindexCount:integer read FbindexCount write FbindexCount;
    property DIndexDebut:integer read FdIndexDebut write FdIndexDebut;
    property DindexCount:integer read FdindexCount write FdindexCount;
    property GIndexDebut:integer read FgIndexDebut write FgIndexDebut;
    property GindexCount:integer read FgindexCount write FgindexCount;
    property HVitesse:integer read FHVitesse write FHVitesse;
    property BVitesse:integer read FBVitesse write FBVitesse;
    property DVitesse:integer read FDVitesse write FDVitesse;
    property GVitesse:integer read FGVitesse write FGVitesse;
    property HVitesseDiv:integer read FHVitesseDiv write FHVitesseDiv;
    property BVitesseDiv:integer read FBVitesseDiv write FBVitesseDiv;
    property DVitesseDiv:integer read FDVitesseDiv write FDVitesseDiv;
    property GVitesseDiv:integer read FGVitesseDiv write FGVitesseDiv;
    property HDeplace:integer read FHDeplace write fHDeplace;
    property BDeplace:integer read FBDeplace write FBDeplace;
    property DDeplace:integer read FDDeplace write FDDeplace;
    property GDeplace:integer read FGDeplace write FGDeplace;
    property AnimBoucle:Boolean read FAnimBoucle write FAnimBoucle;
    property DefileEcran:Boolean read FDefileEcran write FDefileEcran;
    property LimitH:integer read FLimitH write FLimitH ;
    property LimitB:integer read FLimitB write FLimitB ;
    property LimitD:integer read FLimitD write FLimitD ;
    property LimitG:integer read FLimitG write FLimitG ;
    property LimitActive:boolean read FLimitActive write FLimitActive;
    property Acceleration:integer read FAcceleration write FAcceleration ;
    property Ralenti:integer read FRalenti write FRalenti;
    property Itm1:integer read FitM1 write FItm1;
    property Itm2:integer read FitM2 write FItm2;
  end;
    TSprProperty = class(TClassProperty)
  public
    procedure Edit; override;
    function GetAttributes : TPropertyAttributes; override;
  end;

  TSprEditor = class(TDefaultEditor)
  public
    procedure EditProperty(PropertyEditor : TPropertyEditor;
                           var continue, FreeEditor : boolean); virtual;
  end;

  TSprDirection = class(TComponent)
  private
    { Déclarations private }
    FDirection : TSprAnim;
    FDxInput:TDxInput;

  protected
    { Déclarations protected }
  public
    { Déclarations public }
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
    Procedure Animation(Sprite:TImageSprite;MoveCount:integer);
  published
    { Déclarations published }
    property Direction : TSprAnim read FDirection write FDirection;
     property DxInput:TDxInput read FDxInput write fDxInput;

  end;

procedure Register;

implementation
uses
  dlgspr;
 constructor TSprDirection.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);

  FDirection:=TSprAnim.create;

end;

destructor TSprDirection.Destroy;
begin
  FDirection.Destroy;

  inherited Destroy;
end;
procedure TSprProperty.Edit;
var
  Direction : TSprAnim;
  dlg      : TSprDlg;
begin
  Direction:=TSprAnim(GetOrdValue);
  dlg:=TSprDlg.create(application);
  dlg.Edit1.text:=Inttostr(Direction.HIndexDebut);
  dlg.Edit2.text:=Inttostr(Direction.HindexCount);
  dlg.Edit6.text:=Inttostr(Direction.BIndexDebut);
  dlg.Edit5.text:=Inttostr(Direction.BindexCount);
  dlg.Edit9.text:=Inttostr(Direction.DIndexDebut);
  dlg.Edit8.text:=Inttostr(Direction.DindexCount);
  dlg.Edit12.text:=Inttostr(Direction.GIndexDebut);
  dlg.Edit11.text:=Inttostr(Direction.GindexCount);
  dlg.Edit3.text:=Inttostr(Direction.HVitesse);
  dlg.Edit4.text:=Inttostr(Direction.BVitesse);
  dlg.Edit7.text:=Inttostr(Direction.DVitesse);
  dlg.Edit10.text:=Inttostr(Direction.GVitesse);
  dlg.Edit13.text:=Inttostr(Direction.HVitesseDiv);
  dlg.Edit14.text:=Inttostr(Direction.BVitesseDiv);
  dlg.Edit15.text:=Inttostr(Direction.DVitesseDiv);
  dlg.Edit16.text:=Inttostr(Direction.GVitesseDiv);
  dlg.Edit17.text:=Inttostr(Direction.HDeplace);
  dlg.Edit18.text:=Inttostr(Direction.BDeplace);
  dlg.Edit19.text:=Inttostr(Direction.DDeplace);
  dlg.Edit20.text:=Inttostr(Direction.GDeplace);
  dlg.CheckBox1.checked:=Direction.AnimBoucle;
  dlg.CheckBox2.checked:=Direction.DefileEcran;
  dlg.Edit21.text:=Inttostr(Direction.LimitH);
  dlg.Edit22.text:=Inttostr(Direction.LimitB);
  dlg.Edit23.text:=Inttostr(Direction.LimitD);
  dlg.Edit24.text:=Inttostr(Direction.LimitG);
  dlg.CheckBox3.checked:=Direction.LimitActive;
  dlg.Edit25.text:=IntToStr(Direction.Acceleration);
  if(Direction.acc1=0)then Direction.acc1:=35;
  dlg.Edit25.top:=Direction.Acc1;
  dlg.Edit26.text:=IntToStr(Direction.Ralenti);
  if(Direction.acc2=0)then Direction.acc2:=59;
  dlg.Edit26.top:=Direction.Acc2;
  case Direction.itm1 of
  1:dlg.combobox1.ItemIndex:=1;
  2:dlg.combobox2.ItemIndex:=1;
  3:dlg.combobox3.ItemIndex:=1;
  4:dlg.combobox4.ItemIndex:=1;
  5:dlg.combobox5.ItemIndex:=1;
  6:dlg.combobox6.ItemIndex:=1;
  7:dlg.combobox7.ItemIndex:=1;
  8:dlg.combobox8.ItemIndex:=1;
  end;
  case Direction.itm2 of
  1:dlg.combobox1.ItemIndex:=2;
  2:dlg.combobox2.ItemIndex:=2;
  3:dlg.combobox3.ItemIndex:=2;
  4:dlg.combobox4.ItemIndex:=2;
  5:dlg.combobox5.ItemIndex:=2;
  6:dlg.combobox6.ItemIndex:=2;
  7:dlg.combobox7.ItemIndex:=2;
  8:dlg.combobox8.ItemIndex:=2;
  end;
  if dlg.showModal=mrOK then
  begin
    Direction.HIndexDebut:=strtoint(dlg.Edit1.text);
    Direction.HindexCount:=strtoint(dlg.Edit2.text);
    Direction.BIndexDebut:=strtoint(dlg.Edit6.text);
    Direction.BindexCount:=strtoint(dlg.Edit5.text);
    Direction.DIndexDebut:=strtoint(dlg.Edit9.text);
    Direction.DindexCount:=strtoint(dlg.Edit8.text);
    Direction.GIndexDebut:=strtoint(dlg.Edit12.text);
    Direction.GindexCount:=strtoint(dlg.Edit11.text);
    Direction.HVitesse:=strtoint(dlg.Edit3.text);
    Direction.BVitesse:=strtoint(dlg.Edit4.text);
    Direction.DVitesse:=strtoint(dlg.Edit7.text);
    Direction.GVitesse:=strtoint(dlg.Edit10.text);
    Direction.HVitesseDiv:=strtoint(dlg.Edit13.text);
    Direction.BVitesseDiv:=strtoint(dlg.Edit14.text);
    Direction.DVitesseDiv:=strtoint(dlg.Edit15.text);
    Direction.GVitesseDiv:=strtoint(dlg.Edit16.text);
    Direction.HDeplace:=strtoint(dlg.Edit17.text);
    Direction.BDeplace:=strtoint(dlg.Edit18.text);
    Direction.DDeplace:=strtoint(dlg.Edit19.text);
    Direction.GDeplace:=strtoint(dlg.Edit20.text);
    Direction.AnimBoucle:=dlg.CheckBox1.checked;
    Direction.DefileEcran:=dlg.CheckBox2.checked;
    Direction.LimitH:=strtoint(dlg.Edit21.text);
    Direction.LimitB:=strtoint(dlg.Edit22.text);
    Direction.LimitD:=strtoint(dlg.Edit23.text);
    Direction.LimitG:=strtoint(dlg.Edit24.text);
    Direction.LimitActive:=dlg.CheckBox3.checked;
    Direction.Acceleration:=StrToInt(dlg.edit25.text);
    Direction.acc1:=Dlg.Edit25.Top;
    Direction.Ralenti:=StrToInt(dlg.edit26.text);
    Direction.acc2:=Dlg.Edit26.Top;
    if(dlg.ComboBox1.itemindex=1)then Direction.itm1:=1;
    if(dlg.ComboBox1.itemindex=2)then Direction.itm2:=1;
    if(dlg.ComboBox2.itemindex=1)then Direction.itm1:=2;
    if(dlg.ComboBox2.itemindex=2)then Direction.itm2:=2;
    if(dlg.ComboBox3.itemindex=1)then Direction.itm1:=3;
    if(dlg.ComboBox3.itemindex=2)then Direction.itm2:=3;
    if(dlg.ComboBox4.itemindex=1)then Direction.itm1:=4;
    if(dlg.ComboBox4.itemindex=2)then Direction.itm2:=4;
    if(dlg.ComboBox5.itemindex=1)then Direction.itm1:=5;
    if(dlg.ComboBox5.itemindex=2)then Direction.itm2:=5;
    if(dlg.ComboBox6.itemindex=1)then Direction.itm1:=6;
    if(dlg.ComboBox6.itemindex=2)then Direction.itm2:=6;
    if(dlg.ComboBox7.itemindex=1)then Direction.itm1:=7;
    if(dlg.ComboBox7.itemindex=2)then Direction.itm2:=7;
    if(dlg.ComboBox8.itemindex=1)then Direction.itm1:=8;
    if(dlg.ComboBox8.itemindex=2)then Direction.itm2:=8;

     end;
  dlg.free;
end;
Procedure TSprDirection.Animation(Sprite:TImageSprite;MoveCount:integer);
Var H,B,D,G,Accel,Ralent:integer;
begin
Ralent:=0;
Accel:=0;
//Ralenti Acceleration
if (isbutton1 in Dxinput.States)and(direction.itm1=1) then Accel:=direction.Acceleration;
if (isbutton2 in Dxinput.States)and(direction.itm1=2) then Accel:=direction.Acceleration;
if (isbutton3 in Dxinput.States)and(direction.itm1=3) then Accel:=direction.Acceleration;
if (isbutton4 in Dxinput.States)and(direction.itm1=4) then Accel:=direction.Acceleration;
if (isbutton5 in Dxinput.States)and(direction.itm1=5) then Accel:=direction.Acceleration;
if (isbutton6 in Dxinput.States)and(direction.itm1=6) then Accel:=direction.Acceleration;
if (isbutton7 in Dxinput.States)and(direction.itm1=7) then Accel:=direction.Acceleration;
if (isbutton8 in Dxinput.States)and(direction.itm1=8) then Accel:=direction.Acceleration;


if (isbutton1 in Dxinput.States)and(direction.Itm2=1) then Ralent:=direction.Ralenti;
if (isbutton2 in Dxinput.States)and(direction.Itm2=2) then Ralent:=direction.Ralenti;
if (isbutton3 in Dxinput.States)and(direction.Itm2=3) then Ralent:=direction.Ralenti;
if (isbutton4 in Dxinput.States)and(direction.Itm2=4) then Ralent:=direction.Ralenti;
if (isbutton5 in Dxinput.States)and(direction.Itm2=5) then Ralent:=direction.Ralenti;
if (isbutton6 in Dxinput.States)and(direction.Itm2=6) then Ralent:=direction.Ralenti;
if (isbutton7 in Dxinput.States)and(direction.Itm2=7) then Ralent:=direction.Ralenti;
if (isbutton8 in Dxinput.States)and(direction.Itm2=8) then Ralent:=direction.Ralenti;
//fin de sralenti acceleration
H:=direction.HDeplace+accel-ralent;
B:=Direction.BDeplace+accel-ralent;
D:=Direction.DDeplace+accel-ralent;
G:=Direction.GDeplace+accel-ralent;
if(h<=0)then h:=1;
if(b<=0)then b:=1;
if(d<=0)then b:=1;
if(g<=0)then g:=1;
sprite.AnimLooped:=Direction.AnimBoucle;
if isUp in DXInput.States then begin
 sprite.Y := Sprite.Y - (H/1000)*Movecount;
if(Direction.LimitActive=true)and(sprite.y<Direction.LimitH)then
 sprite.Y:=Direction.LimitH;
sprite.AnimSpeed := direction.HVitesse/direction.HVitesseDiv;
sprite.AnimStart := Direction.HIndexDebut; //position de départ
sprite.AnimCount := Direction.HIndexCount;//nombre d'image de l'animation
end;
if isdown in DXInput.States then begin
 sprite.Y := Sprite.Y +(B/1000)*Movecount;
 if(Direction.LimitActive=true)and(sprite.y>Direction.LimitB)then
 sprite.Y:=Direction.LimitB;
sprite.AnimSpeed := direction.BVitesse/direction.BVitesseDiv;
sprite.AnimStart := Direction.BIndexDEbut; //position de départ
sprite.AnimCount := Direction.BIndexCount;//nombre d'image de l'animation
end;
if isLeft in DXInput.States then begin
sprite.x := Sprite.x - (G/1000)*Movecount;
if(Direction.LimitActive=true)and(sprite.x<Direction.LimitG)then
 sprite.X:=Direction.LimitG;
sprite.AnimSpeed := direction.GVitesse/direction.GVitesseDiv;
sprite.AnimStart := Direction.GIndexDEbut; //position de départ
sprite.AnimCount := Direction.GIndexCount;//nombre d'image de l'animation
end;
if isRight in DXInput.States then begin
sprite.X := Sprite.X + (D/1000)*Movecount;
if(Direction.LimitActive=true)and(sprite.x>Direction.LimitD)then
sprite.X:=Direction.LimitD;
sprite.AnimSpeed := direction.DVitesse/direction.DVitesseDiv;
sprite.AnimStart := Direction.DIndexDEbut; //position de départ
sprite.AnimCount := Direction.DIndexCount;//nombre d'image de l'animation
end;
if Direction.DefileEcran=true then begin
sprite.Engine.X := -sprite.X+sprite.engine.Width div 2-sprite.Width div 2;
sprite.engine.Y := -sprite.Y+sprite.engine.Height div 2-sprite.Height div 2;
end;
end;
function TSprProperty.GetAttributes  : TPropertyAttributes;
begin
  result:=[paDialog, paSubProperties];
end;

procedure TSprEditor.EditProperty(PropertyEditor : TPropertyEditor;
                                   var continue, FreeEditor : boolean);
var
  PropName : string;
begin
  PropName:=PropertyEditor.GetName;
  if CompareText(PropName, 'SprImg')=0 then
  begin
    PropertyEditor.edit;
    continue:=false;
  end;
end;

{$r spranim.res}
procedure Register;
begin
  RegisterComponents('DelphiXplus', [TSprDirection]);
  RegisterPropertyEditor(TypeInfo(TSprAnim), nil, '', TSprProperty);

end;

end.
