unit SceneUnit;

{$mode delphi}
interface

uses
  Classes, SysUtils,
  {ZenGL Modules}
   zglHeader
  {ZenGL Modules},
    GTimer;

type
  TScene = class
    {public declarations}
  public
    ID: string;
    constructor Create(InputID: string);
    procedure Init(); virtual; //abstract;
    procedure Draw(); virtual; //abstract;
    procedure Proc(dt: double); virtual;// abstract;
    procedure Exit(); virtual;// abstract;

    procedure Unfreeze();
    procedure Freeze();
    procedure Show();
    procedure Hide();
    function GetFreezed(): boolean;
    function GetVisible(): boolean;

    function GetID(): string;
    procedure AddTimer(inputID: string; _proc: pointer; interval: integer;
      userPointer: pointer = nil);
    procedure DelTimer(inputID: string);
    procedure StopTimer(inputID: string);
    procedure StartTimer(inputID: string);
    procedure StartTimers;
    procedure StopTimers;
    procedure ClearTimers;
    {protected declarations}
  protected
    freezed, Visible: boolean;
    timers: array of TTimer;
  end;

implementation

constructor TScene.Create(InputID: string);
begin
  ID := LowerCase(InputID);
  freezed := False;
  Visible := False;
end;

procedure TScene.Unfreeze();
begin
  freezed := False;
  StartTimers;
end;

procedure TScene.Freeze();
begin
  StopTimers;
  freezed := True;
end;

procedure TScene.Show();
begin
  Visible := True;
end;

procedure TScene.Hide();
begin
  Visible := False;
end;

function TScene.GetFreezed(): boolean;
begin
  Result := Freezed;
end;

function TScene.GetVisible(): boolean;
begin
  Result := Visible;
end;

function TScene.GetID(): string;
begin
  Result := ID;
end;

procedure TScene.AddTimer(inputID: string; _proc: pointer; interval: integer;
  userPointer: pointer = nil);
var
  a: integer;
begin
  a := length(timers);
  setLength(timers, length(timers) + 1);
  timers[a].ID := lowercase(inputID);
  if userPointer = nil then
    timers[a].ZTimer := timer_add(_proc, interval)
  else
    timers[a].ZTimer := timer_add(_proc, interval, True, userPointer);
  timers[a].ZTimer^.Active := False;
end;

procedure TScene.DelTimer(inputID: string);
var
  i: integer;
  tempnum: integer;
begin
  tempnum := -1;
  for i := 0 to length(timers) - 1 do
    if timers[i].ID = lowercase(inputID) then
      tempnum := i;
  if tempnum >= 0 then
  begin
    timer_del(timers[tempnum].ZTimer);
    for i := tempnum to length(timers) - 2 do
      timers[i] := timers[i + 1];
    setlength(timers, length(timers) - 1);
  end;
end;

procedure TScene.ClearTimers;
var
  i: integer;
begin
  if length(timers) >= 1 then
    for i := 0 to length(timers) - 1 do
      timer_del(timers[i].ZTimer);
  setlength(timers, 0);
end;

procedure TScene.StopTimer(inputID: string);
var
  i: integer;
  tempnum: integer;
begin
  tempnum := -1;
  for i := 0 to length(timers) - 1 do
    if timers[i].ID = lowercase(inputID) then
      tempnum := i;
  if tempnum >= 0 then
  begin
    timers[tempnum].ZTimer^.Active := False;
  end;
end;

procedure TScene.StartTimer(inputID: string);
var
  i: integer;
  tempnum: integer;
begin
  tempnum := -1;
  for i := 0 to length(timers) - 1 do
    if timers[i].ID = lowercase(inputID) then
      tempnum := i;
  if tempnum >= 0 then
  begin
    timers[tempnum].ZTimer^.Active := True;
  end;
end;

procedure TScene.StopTimers();
var
  i: integer;
begin
  if length(timers) >= 1 then
    for i := 0 to length(timers) - 1 do
      timers[i].ZTimer^.Active := False;
end;

procedure TScene.StartTimers();
var
  i: integer;
begin
  if length(timers) >= 1 then
    for i := 0 to length(timers) - 1 do
      timers[i].ZTimer^.Active := True;
end;
//Next functions may (must be!) overrided!
procedure TScene.Init();
begin

end;

procedure TScene.Draw();
begin

end;

procedure TScene.Proc(dt: double);
begin

end;

procedure TScene.Exit();
begin

end;

end.
