
Animation is integral to many parts of GUIs, including both the fancy effects when things change used in window managers, and of course games.   The core of any animation system is a scheme for periodically changing the display while still remaining responsive to the user.   This task demonstrates this.



Task

Create a window containing the string "Hello World! " (the trailing space is significant).

Make the text appear to be rotating right by periodically removing one letter from the end of the string and attaching it to the front.

When the user clicks on the (windowed) text, it should reverse its direction.




program HelloWorldAnimatedGUI;
 
uses {$IFDEF UNIX} {$IFDEF UseCThreads}
  cthreads, {$ENDIF} {$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms,
  Classes,
  Controls,
  StdCtrls,
  ExtCtrls;
 
type
  { TFrmHelloWorldAnim }
  TFrmHelloWorldAnim = class(TForm)
    constructor CreateNew(AOwner: TComponent; Num: integer = 0); override;
    procedure lblTextAnimateClick(Sender: TObject);
    procedure tmrAnimateTimer(Sender: TObject);
  private
    { private declarations }
    lblTextAnimate: TLabel;
    tmrAnimate: TTimer;
    FDirection: boolean;
  public
    { public declarations }
  end;
 
var
  FrmHelloWorldAnim: TFrmHelloWorldAnim;
 
  { TFrmHelloWorldAnim }
 
  constructor TFrmHelloWorldAnim.CreateNew(AOwner: TComponent; Num: integer);
  begin
    inherited CreateNew(AOwner, Num);
    Height := 50;
    lblTextAnimate := TLabel.Create(self);
    with lblTextAnimate do
    begin
      Caption := 'Hello World! ';
      Align := alClient;
      Alignment := taCenter;
      font.Name := 'Courier New';
      font.size := 20;
      OnClick := @lblTextAnimateClick;
      Parent := self;
    end;
    tmrAnimate := TTimer.Create(self);
    with tmrAnimate do
    begin
      Interval := 100;
      OnTimer := @tmrAnimateTimer;
    end;
  end;
 
  procedure TFrmHelloWorldAnim.lblTextAnimateClick(Sender: TObject);
  begin
    FDirection := not FDirection;
  end;
 
  procedure TFrmHelloWorldAnim.tmrAnimateTimer(Sender: TObject);
  begin
    if FDirection then
      lblTextAnimate.Caption :=
        copy(lblTextAnimate.Caption, length(lblTextAnimate.Caption), 1) +
        copy(lblTextAnimate.Caption, 1, length(lblTextAnimate.Caption) - 1)
    else
      lblTextAnimate.Caption :=
        copy(lblTextAnimate.Caption, 2, length(lblTextAnimate.Caption) - 1) +
        copy(lblTextAnimate.Caption, 1, 1);
  end;
 
begin
  RequireDerivedFormResource := False;
  Application.Initialize;
  Application.CreateForm(TFrmHelloWorldAnim, FrmHelloWorldAnim);
  Application.Run;
end.