home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_PAS / ANIDIAL.ZIP / ANIMATED.PAS < prev    next >
Pascal/Delphi Source File  |  1994-01-15  |  5KB  |  195 lines

  1. unit AnimateDialog;
  2.  
  3. interface
  4.  
  5.  uses Objects,Dialogs,Views,Drivers,Crt,DOS;
  6.  
  7.  const cmUpDateDialog=7863;
  8.        AniNone=0;
  9.        AniBounceBack=1;
  10.        AniBounceAround=2;
  11.  
  12.  type
  13.       PAniItem=^TAniItem;
  14.       TAniItem=object(TObject)
  15.                  P:PView;
  16.                  Bounds,Original:TRect;
  17.                  Param:Word;
  18.                  DelayTime:Word;
  19.                  LastUpdated:LongInt;
  20.                  XStep,YStep:Integer;
  21.                  constructor Init(TheView:PView; R:TRect; AParam,ADelay:Word;
  22.                                   AXStep,AYStep:Integer);
  23.                 end; {object}
  24.  
  25.       PAnimateDialog=^TAnimateDialog;
  26.       TAnimateDialog=object(TDialog)
  27.                       AniList:PCollection;
  28.                       UpDateOn,MouseDown:Boolean;
  29.                       SpecialKey:Word;
  30.                       SpecialView:PView;
  31.                       constructor Init(var Bounds: TRect; ATitle: TTitleStr);
  32.                       procedure UpDate;
  33.                       procedure AdjustRect(var Bounds,R:TRect; Param:Word;
  34.                                            var XStep,YStep:Integer);
  35.                       procedure HandleEvent(var Event:TEvent); virtual;
  36.                       procedure RestoreViews;
  37.                       function TimeToHunds:LongInt;
  38.                       destructor Done; virtual;
  39.                      end; {object}
  40.  
  41. implementation
  42.  
  43. {******}
  44.  
  45.  constructor TAniItem.Init(TheView:PView; R:TRect; AParam,ADelay:Word;
  46.                            AXStep,AYStep:Integer);
  47.  
  48.   var H,M,S,HS:Word;
  49.  
  50.  begin
  51.   inherited Init;
  52.   P:=TheView;
  53.   Bounds:=R;
  54.   Original.A.X:=P^.Origin.X; Original.A.Y:=P^.Origin.Y;
  55.   Original.B.X:=P^.Origin.X+P^.Size.X; Original.B.Y:=P^.Origin.Y+P^.Size.Y;
  56.   Param:=AParam;
  57.   DelayTime:=ADelay;
  58.   XStep:=AXStep;
  59.   YStep:=AYStep;
  60.   GetTime(H,M,S,HS); LastUpDated:=100*(60*(60*H+M)+S)+HS;
  61.  end;
  62.  
  63. {******}
  64.  
  65.  constructor TAnimateDialog.Init(var Bounds:TRect; ATitle:TTitleStr);
  66.  
  67.  begin
  68.   inherited Init(Bounds,ATitle);
  69.   AniList:=New(PCollection,Init(10,1));
  70.   UpDateOn:=True;
  71.   SpecialKey:=kbNoKey;
  72.   SpecialView:=nil;
  73.  end;
  74.  
  75. {******}
  76.  
  77.  procedure TAnimateDialog.AdjustRect(var Bounds,R:TRect; Param:Word;
  78.                                      var XStep,YStep:Integer);
  79.  
  80.   var HitTop,HitBottom,HitRight,HitLeft:Boolean;
  81.       Temp:TRect;
  82.  
  83.  begin
  84.   Temp:=R;
  85.   HitTop:=R.A.Y<=Bounds.A.Y+abs(YStep)-1;
  86.   HitBottom:=R.B.Y>=Bounds.B.Y-abs(YStep)+1;
  87.   HitRight:=R.B.X>=Bounds.B.X-abs(XStep)+1;
  88.   HitLeft:=R.A.X<=Bounds.A.X+abs(XStep)-1;
  89.  
  90.   case Param of
  91.    AniNone:
  92.     begin
  93.      if HitTop and (YStep<0) then R.Move(0,Bounds.B.Y-Bounds.A.Y-R.B.Y+R.A.Y);
  94.      if HitBottom and (YStep>0) then R.Move(0,Bounds.A.Y-Bounds.B.Y+R.B.Y-R.A.Y);
  95.      if HitRight and (XStep>0) then R.Move(Bounds.A.X-Bounds.B.X+R.B.X-R.A.X,0);
  96.      if HitLeft and (XStep<0) then R.Move(Bounds.B.X-Bounds.A.X+R.A.X-R.B.X,0);
  97.     end; {AniNone}
  98.    AniBounceBack:
  99.     begin
  100.      if (HitTop and (YStep<0)) or
  101.         (HitBottom and (YStep>0)) or
  102.         (HitRight and (XStep>0)) or
  103.         (HitLeft and (XStep<0))
  104.         then begin XStep:=-XStep; YStep:=-YStep; end;
  105.     end; {AniNone}
  106.    AniBounceAround:
  107.     begin
  108.      if (HitTop and (YStep<0)) or (HitBottom and (YStep>0))
  109.         then YStep:=-YStep;
  110.      if (HitRight and (XStep>0)) or (HitLeft and (XStep<0))
  111.         then XStep:=-XStep;
  112.     end; {AniNone}
  113.   end; {case}
  114.  
  115.   if R.Equals(Temp) then R.Move(XStep,YStep);
  116.  end;
  117.  
  118. {******}
  119.  
  120.  procedure TAnimateDialog.UpDate;
  121.  
  122.   var Counter:Integer;
  123.       R:TRect;
  124.       CurrentTime:LongInt;
  125.  
  126.  begin
  127.   if not UpdateOn or MouseDown then Exit;
  128.   CurrentTime:=TimeToHunds;
  129.   for Counter:=1 to AniList^.Count do
  130.    with PAniItem(AniList^.At(Counter-1))^ do   {After Midnight!}
  131.     if (CurrentTime>=LastUpDated+DelayTime) or (CurrentTime<LastUpDated) then
  132.      begin
  133.       P^.GetBounds(R);
  134.       AdjustRect(Bounds,R,Param,XStep,YStep);
  135.       P^.Locate(R);
  136.       LastUpDated:=CurrentTime;
  137.      end; {if}
  138.  end;
  139.  
  140. {******}
  141.  
  142.  procedure TAnimateDialog.RestoreViews;
  143.  
  144.    var Counter:Integer;
  145.  
  146.  begin
  147.   UpDateOn:=False;
  148.   for Counter:=1 to AniList^.Count do
  149.    with PAniItem(AniList^.At(Counter-1))^ do
  150.     P^.Locate(Original);
  151.  end;
  152.  
  153. {******}
  154.  
  155.  procedure TAnimateDialog.HandleEvent(var Event:TEvent);
  156.  
  157.  begin
  158.   MouseDown:=Event.What=evMouseDown;
  159.  
  160.   if (Event.What=evKeyDown) and (Event.KeyCode=SpecialKey)
  161.      then begin UpDateOn:=not UpDateOn;
  162.                 if not UpDateOn then RestoreViews; end;
  163.  
  164.   if (SpecialView<>nil) and (Event.What=evMouseDown) and
  165.      (Event.Buttons=mbLeftButton) and (Event.Double) and
  166.      SpecialView^.MouseInView(MouseWhere) then
  167.      begin UpDateOn:=not UpDateOn;
  168.                 if not UpDateOn then RestoreViews; end;
  169.  
  170.   inherited HandleEvent(Event);
  171.  end;
  172.  
  173. {******}
  174.  
  175.  function TAnimateDialog.TimeToHunds:LongInt;
  176.  
  177.   var H,M,S,HS:Word;
  178.  
  179.  begin
  180.   GetTime(H,M,S,HS);
  181.   TimeToHunds:=100*(60*(60*H+M)+S)+HS;
  182.  end;
  183.  
  184. {******}
  185.  
  186.  destructor TAnimateDialog.Done;
  187.  
  188.  begin
  189.   Dispose(AniList,Done);
  190.   inherited Done;
  191.  end;
  192.  
  193. {******}
  194.  
  195. end.