var
  tmr,tmrRandom:TTimer;
  FishCount:integer;
  FishIndex:integer;
  FishName:string;
  isReturn:boolean;
  iStep, iRunCount, iSpeed, 
  iMoveStep, iMoveVStep:integer;
  isMoveToRight:boolean;

procedure AddMoveSpeed(sender);
begin
  Randomize;
  iMoveStep:=iMoveStep+3+round(random*2);
  iRunCount:=1;
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  EndUpdate;
end;

//读取当前Widget的INI设置
procedure InitSetting;
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;
  Randomize;
  iSpeed:=GetSetting('Speed',2);
  Randomize;
  iMoveStep:=round(random*2)+iSpeed;
  Randomize;
  iMoveVStep:=round(random*4)-2;
  isMoveToRight:=GetSettingInt('isMoveToRight',round(random*10) mod 2)=1;
  imgbg.ImageStyle.Count:=FishCount;
  if isMoveToRight then
    imgbg.FileName:=WidgetPath+FishName+'\right.png'
  else
    imgbg.FileName:=WidgetPath+FishName+'\left.png';
  Widget.Width:=imgbg.Width;
  Widget.Height:=imgbg.Height;
end;

procedure SaveSetting;
begin
  if isMoveToRight then
    SetSetting('isMoveToRight',1)
  else
    SetSetting('isMoveToRight',0);
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  FishIndex:=0;
  iRunCount:=0;
  iStep:=1;
  ShowToolbar:=false;
  AllowDock:=false;
//  isMoveToRight:=false;
  AutoScreenSnap:=false;
  imgbg.OnClick:='AddMoveSpeed';
  BeginUpdate;
  InitSetting;
  if Docking then 
  	OnWidgetDock;
  EndUpdate;
  tmr:=TTimer.create(Widget);
  tmr.OnTimer:='OntmrTimer';
  tmr.Interval:=50;
  AutoAdjustMem:=0;
  tmrRandom:=TTimer.create(Widget);
  Randomize;
  tmrRandom.Interval:=(round(random*10)+20)*1000;
  tmrRandom.OnTimer:='OntmrRandomTimer';
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  SaveSetting;
end;

procedure OntmrRandomTimer(sender);
var
  i:integer;
begin
  Randomize;
  iMoveStep:=round(random*2)+iSpeed;
  Randomize;
  
  i:=round(random*4)-2;
  if (iMoveVStep=i) and (i=0) then
    iMoveVStep:=-1
  else iMoveVStep:=i;
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>=FishCount then FishIndex:=0
    //FishIndex:=(FishIndex+1) mod FishCount;
  end;
  imgbg.ImageStyle.ImageIndex:=FishIndex;
  
  //点击后往前快跑
  if (iRunCount>0) and (iRunCount<=20) then inc(iRunCount)
    else if iRunCount>20 then
    begin
      Randomize;
      iMoveStep:=round(random*2)+iSpeed;
      iRunCount:=0;
    end;
    
  //设置鱼的游动
  if isMoveToRight then//向右
  begin
    Widget.left:=Widget.left+iMoveStep;
    if (Widget.left>screen.DesktopRect.right) then
    begin
      isMoveToRight:=false;
      imgbg.FileName:=WidgetPath+FishName+'\left.png';
    end;
  end
  else//向左
  begin
    Widget.left:=Widget.left-iMoveStep;
    if (Widget.left+Widget.width<0) then
    begin
      isMoveToRight:=true;
      imgbg.FileName:=WidgetPath+FishName+'\right.png';
    end;
  end;
  i:=Widget.Top+iMoveVStep;
  if i<0 then iMoveVStep:=1
  else if Widget.Top+Widget.Height>screen.DesktopRect.bottom then iMoveVStep:=-1;
  Widget.Top:=i;
  EndUpdate;
end;