unit BitmapTile;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls;

type
  TBitmapTile = class(TGraphicControl)
  protected
    FBitmapPreveiw : TBitmap;
    procedure Resize; override;
    procedure Paint; override;
  private
    FBitmap: TBitmap;
    procedure SetBitmap(const Value: TBitmap);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Align;
    property Enabled;
    property ShowHint;
    property Visible;

    property Bitmap : TBitmap read FBitmap write SetBitmap;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('RyuLib', [TBitmapTile]);
end;

{ TBitmapTile }

constructor TBitmapTile.Create(AOwner: TComponent);
begin
  inherited;

  ControlStyle := ControlStyle + [csOpaque];

  FBitmap := TBitmap.Create;
  FBitmapPreveiw := TBitmap.Create;
end;

destructor TBitmapTile.Destroy;
begin
  FreeAndNil(FBitmap);
  FreeAndNil(FBitmapPreveiw);

  inherited;
end;

procedure TBitmapTile.Paint;
begin
  if csDesigning in ComponentState then
    with inherited Canvas do begin
      Pen.Style := psDash;
      Brush.Style := bsClear;
    end;

  Canvas.Draw(0, 0, FBitmapPreveiw);
end;

procedure TBitmapTile.Resize;
var
  LoopX, LoopY: Integer;
begin
  if (Width * Height) = 0 then Exit;
  if (FBitmap.Width * FBitmap.Height) = 0 then Exit;

  FBitmapPreveiw.Width := Width;
  FBitmapPreveiw.Height := Height;

  for LoopY := 0 to Height div FBitmap.Height do
  for LoopX := 0 to Width div FBitmap.Width do
    FBitmapPreveiw.Canvas.Draw(LoopX * FBitmap.Width, LoopY * FBitmap.Height, FBitmap);
end;

procedure TBitmapTile.SetBitmap(const Value: TBitmap);
begin
  FBitmap.Assign(Value);
  Invalidate;
end;

end.
