const
 CMovePixelX=300;    //横向坐标移动基数
 CMovePixelY=100;    //纵向移动范围
 Pi=3.1415926;
 FishCircleTimes=8;
 
var
  DesktopBottom,DesktopRight:integer;
  tmr,tmrRandom:TTimer;
  FishCount:integer;
  FishIndex:integer;
  FishName:string;
  isReturn:boolean;
  iStep, iRunCount, iSpeed, iFlashSpeed,RefreshCount,
  iMoveStep, iMoveVStep:integer;
  isMoveToRight:boolean;
  EndPointX,EndPointY,StartPointX,StartPointY:integer;
  XYscale:Double;   //横纵向比例
  FishIsMoved:boolean;
  FlashSpeedScale:integer;//动画速度与游动速度的比例

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

 // ----------------计算图片要旋转的角度-------------------
procedure SetWidgetBgAngel;
begin 
  angle:=Round(ArcTan(XYscale)*180/Pi);
  if   isMoveToRight  then
  begin
    if  XYscale<0 then
    begin
      angle:= 360+angle ;     //原来 360+angle;
    end else
    begin
      angle:= angle;     //原来  angle
    end;
  end else
  begin
    if  XYscale<0 then
    begin
      angle:=angle*(-1);   //原来 360+angle*(-1)
    end else
    begin
      angle:=360-angle;     //原来 360-angle
    end;
  end;
  imgbg.ImageStyle.angle:=angle;
end;

//------------设置水平方向下一点坐标---------
procedure SetWidgetLeft;
begin
  //设置EndPointX
  if isMoveToRight then//向右
  begin
    EndPointX:=Widget.left+iMoveStep;
    if (Widget.left>DesktopRight) then
    begin
      isMoveToRight:=false;
      imgbg.RotateFlipType:=4;
      //imgbg.FileName:=WidgetPath+FishName+'\left.png';
      //重设当前坐标 与图片旋转角度
      StartPointX:=widget.left;
      StartPointY:=widget.top;
      SetWidgetBgAngel;
    end;
  end
  else//向左
  begin
    EndPointX:=Widget.left-iMoveStep;
    if (Widget.left+Widget.width<0) then
    begin
      isMoveToRight:=true;
      imgbg.RotateFlipType:=0;
      //imgbg.FileName:=WidgetPath+FishName+'\right.png';
      //重设当前坐标 与图片旋转角度
      StartPointX:=widget.left;
      StartPointY:=widget.top;
      SetWidgetBgAngel;
    end;
  end;
end;

//--------------设置横纵比例、横坐标移动步长、获取起点坐标---------------
procedure SetScaleAndMovestep;
var
  i:integer;
  MovePixelX,MovePixelY:integer;  //横纵坐标的比例基数
begin
    //记录当前坐标
   StartPointX:=widget.left;
   StartPointY:=widget.top;
    //设置横坐标移动步长
   Randomize;
   iMoveStep:=round(random*2)+iSpeed;
     //设置横纵比例
   MovePixelX:=CMovePixelX;
   Randomize;
   MovePixelY:= random*CMovePixelY;
   XYscale:= MovePixelY/MovePixelX;
   Randomize;
   i:=round(random*10) mod 2;
   if i =0 then XYscale:=XYscale*(-1); //随机得到Y轴移动方向
   
   //终点坐标
   SetWidgetLeft;
   EndPointY:=StartPointY + Abs(EndPointX -StartPointX)*xyscale;
   if (EndPointY<=0) then
   begin
     if XYscale<0  then  XYscale:=XYscale*(-1);
   end else if Widget.Top+Widget.Height>DesktopBottom then
   begin
     if XYscale>0  then  XYscale:=XYscale*(-1);
   end;

end;

//------------设置纵向方向下一点坐标---------
procedure SetWidgetTop;
begin
   EndPointY:=StartPointY + Abs(EndPointX -StartPointX)*xyscale;
   if (EndPointY<=0) then   //重设
   begin
     if XYscale<0  then
     begin
      XYscale:=XYscale*(-1);
      StartPointX:=widget.left;
      StartPointY:=widget.top;
      SetWidgetBgAngel;
      SetWidgetLeft;
      EndPointY:=StartPointY + Abs(EndPointX -StartPointX)*xyscale;
     end;
   end else if Widget.Top+Widget.Height>DesktopBottom then
   begin
     if XYscale>0  then
     begin
       XYscale:=XYscale*(-1);
       StartPointX:=widget.left;
       StartPointY:=widget.top;
       SetWidgetBgAngel;
       SetWidgetLeft;
       EndPointY:=StartPointY + Abs(EndPointX -StartPointX)*xyscale;
     end;
   end;
end;

//---------------------读取当前Widget的INI设置 ---------------------------------
procedure InitSetting;
var
  ini:TIniFile;
  w,h:integer;
  i:Integer;
  lst:TStrings;
begin
  FishCount:=5;
  isReturn:=false;
  FishName:=GetSetting('FishName','');
  //随机取fishname
  if FishName='' then
  begin
    lst:=TStringList.create;
    try
      lst.text:=GetFileList(WidgetPath+'Fishs\','*.png',false,false,false);
      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:='fishC2T5Regular1';
      end
      else
        FishName:='fishC2T5Regular1';
    finally
      lst.free;
    end;
  end;
  
  Randomize;
  iSpeed:=GetSetting('Speed',2);//取游动速度 
  iFlashSpeed:=GetSetting('FlashSpeed',250); //取动画速度   毫秒
  RefreshCount:=0; 
  FlashSpeedScale:=iFlashSpeed/50; //动画速度与游动速度的比例,即改变图片index的频率
  
  isMoveToRight:=GetSettingInt('isMoveToRight',round(random*10) mod 2)=1;
  BeginUpdate;
  imgbg.ImageStyle.Count:=FishCount;
  if FileExists(WidgetPath+'Fishs\'+FishName) then
  	imgbg.FileName:=WidgetPath+'Fishs\'+FishName
  else
    imgbg.FileName:=WidgetPath+'Fishs\fishC2T5Regular1.png';
	if not isMoveToRight then
    imgbg.RotateFlipType:=4;
	  
  //---设置图片角度与横纵比例、横坐标移动步长
  SetScaleAndMovestep;
  SetWidgetBgAngel;
  
  if imgbg.actualwidth>imgbg.actualheight then
  begin
    w:=round(imgbg.actualwidth/FishCount);
    h:=imgbg.actualheight;
  end
  else
  begin
    w:=imgbg.actualwidth;
    h:=imgbg.actualheight/FishCount;
  end;
  w:=round(sqrt(w*w + h*h))
  h:=w;
  imgbg.width:=w;
  imgbg.height:=h;
  Widget.Width:=imgbg.width;
  Widget.Height:=imgbg.height
  EndUpdate;
end;


//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  FishIndex:=0;
  iRunCount:=0;
  iStep:=1;
  FishIsMoved:=false;
  ShowToolbar:=false;
  AllowDock:=false;
  isMoveToRight:=false;
  AutoScreenSnap:=false;
  imgbg.OnClick:='AddMoveSpeed';
  DesktopBottom:=screen.DesktopRect.bottom;
  DesktopRight:=screen.DesktopRect.right;

  BeginUpdate;
  InitSetting;
  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;

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


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



//--- 改变图片角度的Timer事件 ---
procedure OntmrRandomTimer(sender);
begin
  SetScaleAndMovestep;   //获取横坐标移动步长，横纵坐标移动比例  
  SetWidgetBgAngel ;   // 计算图片要旋转的角度
end;



//--- -----------图片动画、移动的Timer事件 --- ------------
procedure OntmrTimer(sender);
var
  i:integer;
begin 
  beginUpdate;
  RefreshCount:=RefreshCount +1;
  //--- 更换图片 ---
  if  RefreshCount=FlashSpeedScale then   
  begin
    RefreshCount:=0;     
     //设置动画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;
      if not isMoveToRight then    i:=FishCount-i-1;      
      imgbg.ImageStyle.ImageIndex:=i;
      //---
      //FishIndex:=(FishIndex+1) mod FishCount;
    end;
  end;

   if LMousePressed then
    begin
      if not FishIsMoved then FishIsMoved:=true;
      EndUpdateAll;
      exit;
    end;
    if FishIsMoved then
    begin
       SetScaleAndMovestep;
       SetWidgetBgAngel ;
       FishIsMoved:=false;
    end;

  //--- 点击后往前快跑 ---
  if (iRunCount>0) and (iRunCount<=5) then inc(iRunCount)
    else if iRunCount>5 then
    begin
      Randomize;
      iMoveStep:=round(random*2)+iSpeed;
      iRunCount:=0;
    end;  
      
   //----设置鱼的游动 ------ 
    SetWidgetLeft;   //获取水平方向下一点坐标  
    SetWidgetTop;   //获取垂直方向下一点坐标 判断是否越界
  
   // 设置当前游鱼移动到的位置
   Widget.left:=EndPointX;
   Widget.Top:=EndPointY;

  EndUpdate;
  //SetWindowPos(Widget.Handle, 0, EndPointX, EndPointY, Widget.width, Widget.height, 4 + $10);

end; 