unit dflash_interface;

//
// Это временный модуль. Когда флешплеер будет завёрнут в ресурс, потребность
// в нём отпадёт.
//

{$MODE FPC}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
{$MODESWITCH ADVANCEDRECORDS}

interface

uses
  dvector2f,
  dansiview,
  dstream,
  dstreamfile,
  dfilereader,
  deng,
  dflash_holder,
  dflash_player;

type
PFlash = ^TFlash;
TFlash = object
private
  FLoaded: Boolean;
  FHolder: PFlashHolder;
  FPlayer: TFlashPlayer;
  FSize: TVec2f;
public // resource part
  procedure Init;
  procedure Init(const FileName: AnsiString);
  procedure Done;
  procedure Unload;
  //function Load(const FileName: AnsiString): Boolean;
  function Load(Data: TData): Boolean;
  property Loaded: Boolean read FLoaded;

public // flash player part
  procedure Draw; inline;
  procedure ResetAnimation; inline;
  procedure UpdateAnimation(DeltaMs: PtrUInt); inline;

  procedure Play; inline;
  procedure Stop; inline;
  procedure SetStartFrame(Frame: LongInt); overload; inline;
  procedure SetStartLabel(Frame: TAnsiView); overload; inline;
  procedure SetFinishFrame(Frame: LongInt); overload; inline;
  procedure SetFinishLabel(Frame: TAnsiView); overload; inline;
  procedure SetLoop(Loop: Boolean); inline;
  function FrameCount: SizeUInt; inline;

  procedure FlipDiagonally; inline;
  procedure FlipHorizontally; inline;
  procedure FlipVertically; inline;

  function FrameLabelsCount: LongInt; inline;
  function FrameLabels: PFlashLabel; inline;

  function IsInside(P: TVec2f): Boolean;
  function CurrentFrame: LongInt;
  function BackgroundColor: Cardinal;
  function Size: TVec2f; inline;

  function Playing: Boolean; inline;
  function Loop: Boolean; inline;
end;

implementation

procedure TFlash.Init;
begin
  FLoaded := False;
  FSize := Vec2f(1, 1);
  New(FHolder);
end;

procedure TFlash.Init(const FileName: AnsiString);
begin
  Init;
  Load(FileName);
end;

procedure TFlash.Done;
begin
  Unload;
  Dispose(FHolder);
end;

procedure TFlash.Unload;
begin
  if FLoaded then begin
    FPlayer.Done;
    FHolder^.Done;
    FSize := Vec2f(1, 1);
  end;
  FLoaded := False;
end;

function TFlash.Load(Data: TData): Boolean;
begin
  Unload;
  FHolder^.Init;
  if not FHolder^.Load(Data) then begin
    FHolder^.Done;
    Log('FAILED load ' + Data.Name);
    Exit(False);
  end;
  FPlayer.Init(FHolder);
  FSize := FPlayer.Size;
  FLoaded := True;
  Exit(True);
end;

procedure TFlash.Draw;
begin
  if FLoaded then
    FPlayer.Draw;
end;

procedure TFlash.ResetAnimation;
begin
  FPlayer.ResetAnimation;
end;

procedure TFlash.UpdateAnimation(DeltaMs: PtrUInt);
begin
  FPlayer.UpdateAnimation(DeltaMs);
end;

procedure TFlash.Play;
begin
  FPlayer.Play;
end;

procedure TFlash.Stop;
begin
  FPlayer.Stop;
end;

procedure TFlash.SetStartFrame(Frame: LongInt);
begin
  FPlayer.SetStartFrame(Frame);
end;

procedure TFlash.SetStartLabel(Frame: TAnsiView);
begin
  FPlayer.SetStartLabel(Frame);
end;

procedure TFlash.SetFinishFrame(Frame: LongInt);
begin
  FPlayer.SetFinishFrame(Frame);
end;

procedure TFlash.SetFinishLabel(Frame: TAnsiView);
begin
  FPlayer.SetFinishLabel(Frame);
end;

procedure TFlash.SetLoop(Loop: Boolean);
begin
  FPlayer.SetLoop(Loop);
end;

function TFlash.FrameCount: SizeUInt;
begin
  Exit(FPlayer.FrameCount);
end;

procedure TFlash.FlipDiagonally;
begin
  FPlayer.FlipDiagonally;
end;

procedure TFlash.FlipHorizontally;
begin
  FPlayer.FlipHorizontally;
end;

procedure TFlash.FlipVertically;
begin
  FPlayer.FlipVertically;
end;

function TFlash.FrameLabelsCount: LongInt;
begin
  Exit(FPlayer.FrameLabelsCount);
end;

function TFlash.FrameLabels: PFlashLabel;
begin
  Exit(FPlayer.FrameLabels);
end;

function TFlash.IsInside(P: TVec2f): Boolean;
begin
  Exit(FPlayer.IsInside(P));
end;

function TFlash.CurrentFrame: LongInt;
begin
  Exit(FPlayer.CurrentFrame);
end;

function TFlash.BackgroundColor: Cardinal;
begin
  Exit(FPlayer.BackgroundColor);
end;

function TFlash.Size: TVec2f;
begin
  Exit(FSize);
end;

function TFlash.Playing: Boolean;
begin
  Exit(FPlayer.Playing);
end;

function TFlash.Loop: Boolean;
begin
  Exit(FPlayer.Loop);
end;

end.
