const
  Count=11;
  FishCircleTimes=8;
var
  Index:integer;
  FishName:string;
  lst:TStrings;
  tmr:TTimer;
  iStep,FishCount:integer;
  isReturn:boolean;
  FishIndex:integer;
  
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;  
  lblName.caption:=Copy(FishName,0, Pos('.',FishName)-1) ;
  imgPreview.FileName:=WidgetPath+'Fishs\' + FishName;
  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];

  RefreshPreview;
end;

//--- 初始化设置 ---
procedure InitIni;
var
  ini:TIniFile;
  i:integer;
begin
  lst.text:=GetFileList(WidgetPath+'Fishs\','*.png',false,false,false);
  FishName:=GetSetting('FishName',''); 
  //随机取fishname
  if FishName='' then
  begin
    try
      ChkRndom.Checked:=1;
      if lst.count>0 then
      begin
        Randomize;
        i:=round(random*lst.count);
        if (i>=0) and (i< lst.count) then
          FishName:=lst.Strings[i]
        else
          FishName:='fishC2T3Regular2.png';
      end
      else
        FishName:='fishC2T3Regular2.png';
    finally
    
    end;
  end else
  begin
     ChkRndom.Checked:=0;
  end;    
  //showmessage(fishname);  
     

  FishCount:=5;
  isReturn:=false;

  Index:=lst.indexof(FishName);
  edtSpeed.text:=GetSetting('Speed','2');
  edtFlashSpeed.text:=GetSetting('FlashSpeed','250');

  RefreshPreview;
end;

function CheckValid:boolean;
var
  i:integer;
begin
   result:=true;
  try
    i:=StrToInt(edtSpeed.text);
    if (i<=0) or (i>30) then raise('');
  except
    showmsg('无效的游动时间！范围:1~30');
    result:=false;
  end;
  
  try
    i:=StrToInt(edtFlashSpeed.text);
    if (i<50)  then raise('');
  except
    showmsg('无效的动画速度,不允许小于50！');
    result:=false;
  end;
  
  
end;

//--- 保存设置 ---
procedure SaveIni;
begin
  if ChkRndom.Checked = 1 then SetSetting('FishName','') 
    else   SetSetting('FishName',FishName);
    
  SetSetting('Speed',edtSpeed.text);
  SetSetting('FlashSpeed',edtFlashSpeed.text);

end;

//--- 创建设置窗口时，触发此事件 ---
procedure OnSettingInit;
var
i:integer;
begin
  Index:=0;
  iStep:=1;
  FishIndex:=0;
  lst:=TStringList.create;
  
  Beginupdate;
  InitIni;
  Endupdate;
  
  tmr:=TTimer.create(Self);
  tmr.OnTimer:='OntmrTimer';
  try
     i:=StrToInt(edtFlashSpeed.text);  
  except
     i:=250;
  end;
  tmr.Interval:=i;
 
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
  i:integer;
begin
  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>=FishCircleTimes then FishIndex:=0;  

      case FishIndex of
        0,4: i:=0;
        1,2: i:=FishIndex;
        3:   i:=1;
        5,6: i:=FishIndex-2;
        7:   i:=3;
      end;      
      
  end;
  imgPreview.ImageStyle.ImageIndex:=i;
  
  EndUpdate;
end;