var
  imgflowers,  //花的图像列表
  GrowList;    //花的成长列表
  tmrRain:TTimer;
  RainIndex:integer;

procedure Rain;
begin
  AutoAdjustMem:=0;

  if tmrRain=nil then
  begin
    tmrRain:=TTimer.create(Widget);
    tmrRain.Enabled:=false;
    tmrRain.OnTimer:='tmrRainOnTimer';
    tmrRain.Interval:=80;
  end;
  imgRain.visible:=true;
  imgRain.ImageStyle.SourceX:=0;
  imgRain.ImageStyle.SourceY:=260;
  tmrRain.Enabled:=true;
end;

//花的成长函数
procedure growing;
begin
  BeginUpdate;
  randomize;
  i:=round(random*8) mod 8;
  GrowList[i]:=GrowList[i]+1;
  if i>=6 then
  begin
    if GrowList[i]>3 then GrowList[i]:=3;
    if i=6 then
      imgflowers[i].FileName:=WidgetPath+'leaves1_'+inttostr(GrowList[i])+'.png'
    else
      imgflowers[i].FileName:=WidgetPath+'leaves2_'+inttostr(GrowList[i])+'.png';
  end
  else
  begin
    if GrowList[i]>5 then GrowList[i]:=5;
    if i<3 then
      s:=inttostr(i+1)
    else
      s:=inttostr(i);
    imgflowers[i].FileName:=WidgetPath+'Flower'+s+'_'+inttostr(GrowList[i])+'.png'
  end;
  EndUpdate;
  SetSettingInt('Growing'+inttostr(i),GrowList[i]);
end;

//读取当前Widget的INI设置
procedure InitIni;
begin
  BeginUpdate;
  for i:=0 to 7 do
  begin
    GrowList[i]:=GetSettingInt('Growing'+inttostr(i),1);
    if i>=6 then
    begin
      if i=6 then
        imgflowers[i].FileName:=WidgetPath+'leaves1_'+inttostr(GrowList[i])+'.png'
      else
        imgflowers[i].FileName:=WidgetPath+'leaves2_'+inttostr(GrowList[i])+'.png';
    end
    else
    begin
      if i<3 then
        s:=inttostr(i+1)
      else
        s:=inttostr(i);
      imgflowers[i].FileName:=WidgetPath+'Flower'+s+'_'+inttostr(GrowList[i])+'.png'
    end;
  end;
  EndUpdate;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  tmrRain:=nil;
  imgflowers:=[imgFlower1,imgFlower2,imgFlower3,imgFlower4,imgFlower5,imgFlower6,imgLeaves1,imgLeaves2];
  GrowList:=[1,1,1,1,1,1,1,1];
  InitIni;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  if tmrRain<>nil then tmrRain.free;
end;

procedure tmrRainOnTimer(sender);
begin
  BeginUpdate;
  imgRain.ImageStyle.SourceY:=imgRain.ImageStyle.SourceY-4;
  if imgRain.ImageStyle.SourceY<=-50 then
  begin
    imgRain.ImageStyle.SourceX:=0;
    tmrRain.Enabled:=false;
    imgRain.visible:=false;
    growing;
    AutoAdjustMem:=1;
  end;
  EndUpdate;
end;

procedure Reset;
begin
  for i:=0 to 7 do
  begin
    SetSettingInt('Growing'+inttostr(i),1);
  end;
  InitIni;
end;
