MyException - 我的异常网
当前位置:我的异常网» Delphi » 【Spine】Spine Runtime for Delphi移栽笔记(八)

【Spine】Spine Runtime for Delphi移栽笔记(八)

www.MyException.Cn  网友分享于:2013-08-22  浏览:0次
【Spine】Spine Runtime for Delphi移植笔记(八)
////////////////////////////////////////////////////////////////////////////////
//Generic delphi runtime v3.6 for Spine animation tool                        //
//Runtime port by cjk (hzi1980@163.com)                                       //
////////////////////////////////////////////////////////////////////////////////

unit spine.data;

interface

uses
  System.Classes, System.SysUtils, System.Generics.Collections,
  spine.types, spine.classes,
  spine.core.skin, spine.core.animation;

type
  TSkeletonData = class;
  TBoneData = class;
  TSlotData = class;
  TEventData = class;
  TAnimationStateData = class;
  TIkConstraintData = class;
  TTransformConstraintData = class;
  TPathConstraintData = class;

  TSkeletonData = class(TObject)
  private
    FPathConstraintDatas: TObjectList<TPathConstraintData>;
    FIkConstraintDatas: TObjectList<TIkConstraintData>;
    FTransformConstraintDatas: TObjectList<TTransformConstraintData>;
    FEventDatas: TObjectList<TEventData>;
    FBoneDatas: TObjectList<TBoneData>;
    FSkins: TObjectList<TSpineSkin>;
    FSlotDatas: TObjectList<TSlotData>;
    FAnimations: TObjectList<TSpineAnimation>;
  public
    Name: string;
    DefaultSkin: TSpineSkin;
    Width, Height: Single;
    Version: string;
    Hash: string;
    ImagesPath: string;
    FPS: Single;
    property BoneDatas: TObjectList<TBoneData> read FBoneDatas;
    property SlotDatas: TObjectList<TSlotData> read FSlotDatas;
    property Skins: TObjectList<TSpineSkin> read FSkins;
    property EventDatas: TObjectList<TEventData> read FEventDatas;
    property Animations: TObjectList<TSpineAnimation> read FAnimations;
    property IkConstraintDatas: TObjectList<TIkConstraintData> read FIkConstraintDatas;
    property TransformConstraintDatas: TObjectList<TTransformConstraintData> read FTransformConstraintDatas;
    property PathConstraintDatas: TObjectList<TPathConstraintData> read FPathConstraintDatas;
  public
    constructor Create;
    destructor Destroy; override;

    function FindBone(const ABoneDataName: string): TBoneData;
    function FindBoneIndex(const ABoneDataName: string): Integer;
    function FindSlot(const ASlotDataName: string): TSlotData;
    function FindSlotIndex(const ASlotDataName: string): Integer;
    function FindSkin(const ASkinName: string): TSpineSkin;
    function FintEvent(const AEventDataName: string): TEventData;
    function FindAnimation(const AAnimationName: string): TSpineAnimation;
    function FindIkConstraint(const AConstraintDataName: string): TIkConstraintData;
    function FindTransformConstraint(const AConstraintDataName: string): TTransformConstraintData;
    function FindPathConstraint(const AConstraintDataName: string): TPathConstraintData;
    function FindPathConstraintIndex(const AConstraintDataName: string): Integer;
  end;

  TBoneData = class(TObject)
  private
    FIndex: Integer;
    FName: string;
    FParent: TBoneData;
  public
    Length: Single;
    X, Y, Rotation, ScaleX, ScaleY, ShearX, ShearY: Single;
    TransformMode: TTransformMode;
    property Index: Integer read FIndex;
    property Name: string read FName;
    property Parent: TBoneData read FParent;
  public
    constructor Create(const AIndex: Integer; const AName: string; const AParent: TBoneData);
  end;

  TSlotData = class(TObject)
  private
    FIndex: Integer;
    FName: string;
    FBoneData: TBoneData;
  public
    R, G, B, A, R2, G2, B2: Single;
    HasSecondColor: Boolean;
    AttachmentName: string;
    BlendMode: TBlendMode;
    property Index: Integer read FIndex;
    property Name: string read FName;
    property BoneData: TBoneData read FBoneData;
  public
    constructor Create(const AIndex: Integer; const AName: string; const ABoneData: TBoneData);
  end;

  TEventData = class(TObject)
  private
    FName: string;
  public
    IntValue: Integer;
    FloatValue: Single;
    StringValue: string;
    property Name: string read FName;
  public
    constructor Create(const AName: string);
  end;

  TAnimationPair = record
    A1: TSpineAnimation;
    A2: TSpineAnimation;
  end;
  TAnimationStateData = class(TObject)
  private
    FSkeletonData: TSkeletonData;
    FAnimationToMixTime: TDictionary<TAnimationPair, Single>;
  public
    DefaultMix: Single;
    property SkeletonData: TSkeletonData read FSkeletonData;
  public
    constructor Create(const ASkeletonData: TSkeletonData);
    destructor Destroy; override;

    procedure SetMix(const AFromName, AToName: string; const ADuration: Single); overload;
    procedure SetMix(const AFrom, ATo: TSpineAnimation; const ADuration: Single); overload;
    function GetMix(const AFrom, ATo: TSpineAnimation): Single;
  end;

  TIkConstraintData = class(TObject)
  private
    FName: string;
    FBoneDatas: TList<TBoneData>;
  public
    Order: Integer;
    Mix: Single;
    BendDirection: Integer;
    Target: TBoneData;
    property Name: string read FName;
    property BoneDatas: TList<TBoneData> read FBoneDatas;
  public
    constructor Create(const AName: string);
    destructor Destroy; override;
  end;

  TTransformConstraintData = class(TObject)
  private
    FName: string;
    FBoneDatas: TList<TBoneData>;
  public
    Order: Integer;
    Target: TBoneData;
    RotateMix, TranslateMix, ScaleMix, ShearMix: Single;
    OffsetRotation, OffsetX, OffsetY, OffsetScaleX, OffsetScaleY, OffsetShearY: Single;
    Relative, Local: Boolean;
    property Name: string read FName;
    property BoneDatas: TList<TBoneData> read FBoneDatas;
  public
    constructor Create(const AName: string);
    destructor Destroy; override;
  end;

  TPathConstraintData = class(TObject)
  private
    FName: string;
    FBoneDatas: TList<TBoneData>;
  public
    Order: Integer;
    Relative: Boolean;
    Local: Boolean;
    Target: TSlotData;
    PositionMode: TPositionMode;
    SpacingMode: TSpacingMode;
    RotateMode: TRotateMode;
    OffsetRotation, Position, Spacing, RotateMix, TranslateMix: Single;
    property Name: string read FName;
    property BoneDatas: TList<TBoneData> read FBoneDatas;
  public
    constructor Create(const AName: string);
    destructor Destroy; override;
  end;

implementation

{ TSkeletonData }

constructor TSkeletonData.Create;
begin
  inherited Create;
  FPathConstraintDatas:= TObjectList<TPathConstraintData>.Create;
  FIkConstraintDatas:= TObjectList<TIkConstraintData>.Create;
  FTransformConstraintDatas:= TObjectList<TTransformConstraintData>.Create;
  FEventDatas:= TObjectList<TEventData>.Create;
  FBoneDatas:= TObjectList<TBoneData>.Create;
  FSkins:= TObjectList<TSpineSkin>.Create;
  FSlotDatas:= TObjectList<TSlotData>.Create;
  FAnimations:= TObjectList<TSpineAnimation>.Create;
end;

destructor TSkeletonData.Destroy;
begin
  FPathConstraintDatas.Free;
  FIkConstraintDatas.Free;
  FTransformConstraintDatas.Free;
  FEventDatas.Free;
  FBoneDatas.Free;
  FSkins.Free;
  FSlotDatas.Free;
  FAnimations.Free;
  inherited;
end;

function TSkeletonData.FindBone(const ABoneDataName: string): TBoneData;
var
  i: Integer;
begin
  result:= nil;
  if ABoneDataName.Trim.IsEmpty then raise Exception.Create('boneDataName cannot be null.');
  for i:= 0 to FBoneDatas.Count -1 do
  begin
    if FBoneDatas.Items[i].Name = ABoneDataName then
      exit(FBoneDatas.Items[i]);
  end;
end;

function TSkeletonData.FindBoneIndex(const ABoneDataName: string): Integer;
var
  i: Integer;
begin
  result:= -1;
  if ABoneDataName.Trim.IsEmpty then raise Exception.Create('boneDataName cannot be null.');
  for i:= 0 to FBoneDatas.Count -1 do
  begin
    if FBoneDatas.Items[i].Name = ABoneDataName then
      exit(i);
  end;
end;

function TSkeletonData.FindSkin(const ASkinName: string): TSpineSkin;
var
  lSkin: TSpineSkin;
begin
  result:= nil;
  if ASkinName.Trim.IsEmpty then raise Exception.Create('skinName cannot be null.');
  for lSkin in FSkins do
    if lSkin.Name = ASkinName then
      exit(lSkin);
end;

function TSkeletonData.FindSlot(const ASlotDataName: string): TSlotData;
var
  i: Integer;
begin
  result:= nil;
  if ASlotDataName.Trim.IsEmpty then raise Exception.Create('slotDataName cannot be null.');
  for i:= 0 to FSlotDatas.Count -1 do
  begin
    if FSlotDatas.Items[i].Name = ASlotDataName then
      exit(FSlotDatas.Items[i]);
  end;
end;

function TSkeletonData.FindSlotIndex(const ASlotDataName: string): Integer;
var
  i: Integer;
begin
  result:= -1;
  if ASlotDataName.Trim.IsEmpty then raise Exception.Create('slotDataName cannot be null.');
  for i:= 0 to FSlotDatas.Count -1 do
  begin
    if FSlotDatas.Items[i].Name = ASlotDataName then
      exit(i);
  end;
end;

function TSkeletonData.FintEvent(const AEventDataName: string): TEventData;
var
  lData: TEventData;
begin
  result:= nil;
  if AEventDataName.Trim.IsEmpty then raise Exception.Create('eventDataName cannot be null.');
  for lData in FEventDatas do
    if lData.Name = AEventDataName then
      exit(lData);
end;

function TSkeletonData.FindAnimation(const AAnimationName: string): TSpineAnimation;
var
  i: Integer;
begin
  result:= nil;
  if AAnimationName.Trim.IsEmpty then raise Exception.Create('animationName cannot be null.');
  for i:= 0 to FAnimations.Count -1 do
  begin
    if FAnimations.Items[i].Name = AAnimationName then
      exit(FAnimations.Items[i]);
  end;
end;

function TSkeletonData.FindIkConstraint(
  const AConstraintDataName: string): TIkConstraintData;
var
  i: Integer;
begin
  result:= nil;
  if AConstraintDataName.Trim.IsEmpty then raise Exception.Create('constraintDataName cannot be null.');
  for i:= 0 to FIkConstraintDatas.Count -1 do
  begin
    if FIkConstraintDatas.Items[i].Name = AConstraintDataName then
      exit(FIkConstraintDatas.Items[i]);
  end;
end;

function TSkeletonData.FindPathConstraint(
  const AConstraintDataName: string): TPathConstraintData;
var
  i: Integer;
begin
  result:= nil;
  if AConstraintDataName.Trim.IsEmpty then raise Exception.Create('constraintDataName cannot be null.');
  for i:= 0 to FPathConstraintDatas.Count -1 do
  begin
    if FPathConstraintDatas.Items[i].Name = AConstraintDataName then
      exit(FPathConstraintDatas.Items[i]);
  end;
end;

function TSkeletonData.FindPathConstraintIndex(
  const AConstraintDataName: string): Integer;
var
  i: Integer;
begin
  result:= -1;
  if AConstraintDataName.Trim.IsEmpty then raise Exception.Create('constraintDataName cannot be null.');
  for i:= 0 to FIkConstraintDatas.Count -1 do
  begin
    if FIkConstraintDatas.Items[i].Name = AConstraintDataName then
      exit(i);
  end;
end;

function TSkeletonData.FindTransformConstraint(
  const AConstraintDataName: string): TTransformConstraintData;
var
  i: Integer;
begin
  result:= nil;
  if AConstraintDataName.Trim.IsEmpty then raise Exception.Create('constraintDataName cannot be null.');
  for i:= 0 to FTransformConstraintDatas.Count -1 do
  begin
    if FTransformConstraintDatas.Items[i].Name = AConstraintDataName then
      exit(FTransformConstraintDatas.Items[i]);
  end;
end;

{ TBoneData }

constructor TBoneData.Create(const AIndex: Integer; const AName: string;
  const AParent: TBoneData);
begin
  inherited Create;
  if AIndex < 0 then raise Exception.Create('index must be >= 0.');
  if AName.Trim.IsEmpty then raise Exception.Create('name cannot be null.');
  FIndex:= AIndex;
  FName:= AName;
  FParent:= AParent;
  ScaleX:= 1;
  ScaleY:= 1;
  TransformMode:= TTransformMode.tmNormal;
end;

{ TSlotData }

constructor TSlotData.Create(const AIndex: Integer; const AName: string;
  const ABoneData: TBoneData);
begin
  inherited Create;
  if AIndex < 0 then raise Exception.Create('index must be >= 0.');
  if AName.Trim.IsEmpty then raise Exception.Create('name cannot be null.');
  if not Assigned(ABoneData) then raise Exception.Create('boneData cannot be null.');
  FIndex:= AIndex;
  FName:= AName;
  FBoneData:= ABoneData;
  R:= 1; G:= 1; B:= 1; A:= 1;
  R2:= 0; G2:= 0; B2:= 0;
  HasSecondColor:= False;
end;

{ TEventData }

constructor TEventData.Create(const AName: string);
begin
  inherited Create;
  if AName.Trim.IsEmpty then raise Exception.Create('name cannot be null.');
  FName:= AName;
end;

{ TAnimationStateData }

constructor TAnimationStateData.Create(const ASkeletonData: TSkeletonData);
begin
  inherited Create;
  if not Assigned(ASkeletonData) then raise Exception.Create('skeletonData cannot be null.');
  FAnimationToMixTime:= TDictionary<TAnimationPair, Single>.Create;
    FSkeletonData:= ASkeletonData;
end;

destructor TAnimationStateData.Destroy;
begin
  FAnimationToMixTime.Free;
  inherited;
end;

procedure TAnimationStateData.SetMix(const AFromName, AToName: string;
  const ADuration: Single);
var
  lFrom, lTo: TSpineAnimation;
begin
  lFrom:= FSkeletonData.FindAnimation(AFromName);
  if not Assigned(lFrom) then raise Exception.CreateFmt('Animation not found: %s',[AFromName]);
  lTo:= FSkeletonData.FindAnimation(AToName);
  if not Assigned(lTo) then raise Exception.CreateFmt('Animation not found: %s',[AToName]);
  SetMix(lFrom, lTo, ADuration);
end;

procedure TAnimationStateData.SetMix(const AFrom, ATo: TSpineAnimation;
  const ADuration: Single);
var
  lKey: TAnimationPair;
begin
  if not Assigned(AFrom) then raise Exception.Create('from animation cannot be null.');
  if not Assigned(ATo) then raise Exception.Create('to animation cannot be null.');
    lKey.A1:= AFrom;
  lKey.A2:= ATo;
  FAnimationToMixTime.AddOrSetValue(lKey, ADuration);
end;

function TAnimationStateData.GetMix(const AFrom, ATo: TSpineAnimation): Single;
var
  lKey: TAnimationPair;
begin
  if not Assigned(AFrom) then raise Exception.Create('from animation cannot be null.');
  if not Assigned(ATo) then raise Exception.Create('to animation cannot be null.');
    lKey.A1:= AFrom;
  lKey.A2:= ATo;
  if not FAnimationToMixTime.TryGetValue(lKey, result) then
    result:= DefaultMix;
end;

{ TIkConstraintData }

constructor TIkConstraintData.Create(const AName: string);
begin
  inherited Create;
  if AName.Trim.IsEmpty then raise Exception.Create('name cannot be null.');
  FName:= AName;
  FBoneDatas:= TList<TBoneData>.Create;
end;

destructor TIkConstraintData.Destroy;
begin
  FBoneDatas.Free;
  inherited;
end;

{ TTransformConstraintData }

constructor TTransformConstraintData.Create(const AName: string);
begin
  inherited Create;
  if AName.Trim.IsEmpty then raise Exception.Create('name cannot be null.');
  FName:= AName;
  FBoneDatas:= TList<TBoneData>.Create;
end;

destructor TTransformConstraintData.Destroy;
begin
  FBoneDatas.Free;
  inherited;
end;

{ TPathConstraintData }

constructor TPathConstraintData.Create(const AName: string);
begin
  inherited Create;
  if AName.Trim.IsEmpty then raise Exception.Create('name cannot be null.');
  FName:= AName;
  FBoneDatas:= TList<TBoneData>.Create;
end;

destructor TPathConstraintData.Destroy;
begin
  FBoneDatas.Free;
  inherited;
end;

end.

各种数据

1楼Tag
大侠,这是用DELPHI 做啥游戏呢?
Re: 水中盗影
@Tag,Delphi做游戏太难了,没必要浪费时间。这个纯属爱好,翻译着玩的!

文章评论

“懒”出效率是程序员的美德
“懒”出效率是程序员的美德
程序员和编码员之间的区别
程序员和编码员之间的区别
Java程序员必看电影
Java程序员必看电影
什么才是优秀的用户界面设计
什么才是优秀的用户界面设计
漫画:程序员的工作
漫画:程序员的工作
每天工作4小时的程序员
每天工作4小时的程序员
那些性感的让人尖叫的程序员
那些性感的让人尖叫的程序员
程序员必看的十大电影
程序员必看的十大电影
初级 vs 高级开发者 哪个性价比更高?
初级 vs 高级开发者 哪个性价比更高?
程序员眼里IE浏览器是什么样的
程序员眼里IE浏览器是什么样的
60个开发者不容错过的免费资源库
60个开发者不容错过的免费资源库
Web开发者需具备的8个好习惯
Web开发者需具备的8个好习惯
程序员的一天:一寸光阴一寸金
程序员的一天:一寸光阴一寸金
要嫁就嫁程序猿—钱多话少死的早
要嫁就嫁程序猿—钱多话少死的早
做程序猿的老婆应该注意的一些事情
做程序猿的老婆应该注意的一些事情
不懂技术不要对懂技术的人说这很容易实现
不懂技术不要对懂技术的人说这很容易实现
Java 与 .NET 的平台发展之争
Java 与 .NET 的平台发展之争
我是如何打败拖延症的
我是如何打败拖延症的
如何区分一个程序员是“老手“还是“新手“?
如何区分一个程序员是“老手“还是“新手“?
中美印日四国程序员比较
中美印日四国程序员比较
代码女神横空出世
代码女神横空出世
程序员最害怕的5件事 你中招了吗?
程序员最害怕的5件事 你中招了吗?
鲜为人知的编程真相
鲜为人知的编程真相
程序员周末都喜欢做什么?
程序员周末都喜欢做什么?
那些争议最大的编程观点
那些争议最大的编程观点
程序猿的崛起——Growth Hacker
程序猿的崛起——Growth Hacker
10个调试和排错的小建议
10个调试和排错的小建议
聊聊HTTPS和SSL/TLS协议
聊聊HTTPS和SSL/TLS协议
十大编程算法助程序员走上高手之路
十大编程算法助程序员走上高手之路
老程序员的下场
老程序员的下场
总结2014中国互联网十大段子
总结2014中国互联网十大段子
2013年美国开发者薪资调查报告
2013年美国开发者薪资调查报告
亲爱的项目经理,我恨你
亲爱的项目经理,我恨你
程序员的鄙视链
程序员的鄙视链
我跳槽是因为他们的显示器更大
我跳槽是因为他们的显示器更大
我的丈夫是个程序员
我的丈夫是个程序员
“肮脏的”IT工作排行榜
“肮脏的”IT工作排行榜
写给自己也写给你 自己到底该何去何从
写给自己也写给你 自己到底该何去何从
一个程序员的时间管理
一个程序员的时间管理
旅行,写作,编程
旅行,写作,编程
Google伦敦新总部 犹如星级庄园
Google伦敦新总部 犹如星级庄园
 程序员的样子
程序员的样子
当下全球最炙手可热的八位少年创业者
当下全球最炙手可热的八位少年创业者
为啥Android手机总会越用越慢?
为啥Android手机总会越用越慢?
10个帮程序员减压放松的网站
10个帮程序员减压放松的网站
如何成为一名黑客
如何成为一名黑客
为什么程序员都是夜猫子
为什么程序员都是夜猫子
程序员应该关注的一些事儿
程序员应该关注的一些事儿
5款最佳正则表达式编辑调试器
5款最佳正则表达式编辑调试器
软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有