Input from a Touch Move

George Wright makes excellent use of touch in the UMouseInputs unit of TowerOfArcher. This simple program positions an image with its centre at the position of the touch as the move is made.

You need to add this image koala named koala.png to Resources so that it can be accessed by the code below. The demo following this code responds to mouse and touch moves and beneath the demo is the code that achieves the move from either form of input.

unit Unit1;

interface

uses
  System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application,
  SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics, SmartCL.Controls.Image,
  SmartCL.Touch;

type
  TCanvasProject = class(TW3CustomGameApplication)
  private
    FImage: TW3Image;
    KoalaX, KoalaY: integer;
  protected
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
  end;

implementation

procedure TCanvasProject.ApplicationStarting;
begin
  inherited;
  FImage := TW3Image.Create(nil);
  FImage.LoadFromURL('res/koala.png');
  GameView.OnTouchMove := procedure(sender: TObject; td: TW3TouchData)
    begin
      KoalaX := td.Touches.Touches[0].PageX - FImage.Width div 2;
      KoalaY := td.Touches.Touches[0].PageY - FImage.Height div 2;
    end;
  GameView.Delay := 1;
  GameView.StartSession(False);
end;

procedure TCanvasProject.ApplicationClosing;
begin
  GameView.EndSession;
  inherited;
end;

procedure TCanvasProject.PaintView(Canvas: TW3Canvas);
begin
  // Clear background
  Canvas.FillStyle := 'white';
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);
  // Draw koala centred at point of touch
  Canvas.DrawImageF(FImage.Handle, KoalaX, KoalaY);
end;

end.
    

Demonstration of Dragging by Mouse or Touch

MouseTouchMove.html

If the program does not work, try another browser such as Chrome. If you see no display at school, the security system might have blocked it. You can try instead this direct link to the program running on its own page.

Code for Dragging by Mouse or Touch

unit Unit1;

interface

uses
  System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application,
  SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics, SmartCL.Controls.Image,
  SmartCL.Touch;

type
  TCanvasProject = class(TW3CustomGameApplication)
  private
    FImage: TW3Image;
    KoalaX, KoalaY: integer;
  protected
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
  end;

implementation

procedure TCanvasProject.ApplicationStarting;
begin
  inherited;
  FImage := TW3Image.Create(nil);
  FImage.LoadFromURL('res/koala.png');
  GameView.OnMouseMove := procedure(o: TObject; ss: TShiftState; x, y : integer)
    begin
      KoalaX := x - FImage.Width div 2;
      KoalaY := y - FImage.Height div 2;
    end;
  GameView.OnTouchMove := procedure(sender: TObject; td: TW3TouchData)
    begin
      KoalaX := td.Touches.Touches[0].PageX - FImage.Width div 2;
      KoalaY := td.Touches.Touches[0].PageY - FImage.Height div 2;
    end;
  GameView.Delay := 1;
  GameView.StartSession(False);
end;

procedure TCanvasProject.ApplicationClosing;
begin
  GameView.EndSession;
  inherited;
end;

procedure TCanvasProject.PaintView(Canvas: TW3Canvas);
begin
  // Clear background
  Canvas.FillStyle := 'white';
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);
  // Draw koala centred at point of touch or position of pointer
  Canvas.DrawImageF(FImage.Handle, KoalaX, KoalaY);
end;

end.    
Programming - a skill for life!

Input from the keyboard, mouse and touch using Smart Mobile Studio