const
  Count=11;

var
  Index:integer;
  FishName:string;
  lst:TStrings;
  tmr:TTimer;
  iStep,FishCount:integer;
  isReturn:boolean;
  

procedure RefreshPreview;
begin
  Beginupdate;
  if Index>lst.Count then 
    Index:=Index mod lst.Count
  else if Index<1 then
    Index:=Index+lst.Count;
  imgPreview.ImageStyle.Count:=FishCount;  
  lblName.caption:=FishName;
  imgPreview.FileName:=WidgetPath+FishName+'\left.png';
  lblIndex.caption:=inttostr(Index)+'/'+inttostr(lst.count);
  Endupdate;
end;

procedure NextPage(value:integer);
var
  ini:TIniFile;
begin
  if lst.count>0 then
    index:=(index+value) mod lst.count;
  FishName:=lst.strings[index];
  ini:=TIniFile.Create(WidgetPath+FishName+'\setting.ini');
  try
    FishCount:=ini.ReadInteger('setting','ImageCount',20);
    isReturn:=ini.ReadBool('setting','isReturn',false);
  finally
    ini.free;
  end;
  RefreshPreview;
end;

//--- 初始化设置 ---
procedure InitIni;
var
  ini:TIniFile;
begin
  FishName:=GetSetting('FishName','kuku');
  ini:=TIniFile.Create(WidgetPath+FishName+'\setting.ini');
  try
    FishCount:=ini.ReadInteger('setting','ImageCount',20);
    isReturn:=ini.ReadBool('setting','isReturn',false);
  finally
    ini.free;
  end;
  lst.text:=GetPathList(WidgetPath);
  Index:=lst.indexof(FishName);
  edtSpeed.text:=GetSetting('Speed','2');
  RefreshPreview;
end;

function CheckValid:boolean;
var
  i:integer;
begin
  try
    i:=StrToInt(edtSpeed.text);
    if (i<=0) or (i>30) then raise('');
    result:=true;
  except
    showmsg('无效的游动时间！');
    result:=false;
  end;
end;

//--- 保存设置 ---
procedure SaveIni;
begin
  SetSetting('FishName',FishName);
  SetSetting('Speed',edtSpeed.text);
end;

//--- 创建设置窗口时，触发此事件 ---
procedure OnSettingInit;
begin
  Index:=0;
  iStep:=1;
  lst:=TStringList.create;
  Beginupdate;
  InitIni;
  Endupdate;
  tmr:=TTimer.create(self);
  tmr.OnTimer:='OntmrTimer';
  tmr.Interval:=50;
end;

//--- 按下设置窗口的确定按钮时，触发此事件 ---
//通过设置返回值来改变是否允许确认,当result为False时不关闭设置窗口
function OnSettingClose(iResult);
begin
  result:=CheckValid;
  SaveIni;
  if lst<>nil then
  begin
    lst.free;
    lst:=nil;
  end;
end;

//--- 当按下“应用”按钮时，触发此事件。如果没有此事件，程序会自动触发OnSettingClose事件 ---
function OnSettingApply;
begin
  if CheckValid then
    SaveIni;
end;

procedure OntmrTimer(sender);
var
  FishIndex:integer;
begin
  FishIndex:=imgPreview.ImageStyle.ImageIndex;
  BeginUpdate;
  //设置动画Index
  if isReturn then
  begin//来回显示
    FishIndex:=FishIndex+iStep;
    if (FishIndex>=FishCount) and (iStep>0) then
    begin
      iStep:=-1;
      FishIndex:=FishCount-2;
    end
    else if (FishIndex<0) and (iStep<0) then
    begin
      iStep:=1;
      FishIndex:=1;
    end;
  end
  else//重复显示
  begin
    FishIndex:=FishIndex+1;
    if FishIndex>=FishCount then FishIndex:=0
    //FishIndex:=(FishIndex+1) mod FishCount;
  end;
  imgPreview.ImageStyle.ImageIndex:=FishIndex;
  EndUpdate;
end;