Image32_Layers

This unit contains the TLayeredImage32 class that provides a relatively easy way to layer multiple images that will at some stage be merged into a single image.

Each TLayer32 in a TLayeredImage32 object represents a single image. The index of each layer determines the order that each layered image is merged into the final image, with lower indexed layers being below or behind layers with higher indexes.

Layers can be positioned anywhere, but only those portions of layers that are within their owner TLayeredImage32 bounds will be merged with other layers (see TLayeredImage32.GetMergedImage)

There are 3 principle layer classes: the generic (unspecialised) TLayer32 class; the TDesignerLayer32 class; and theTButtonDesignerLayer32 class.

  1. TLayer32 - this generic class will typically be included in the final layered (merged) image, and it's usually there when designing too;
  2. TDesignerLayer32 - are easily filtered (excluded) from the final merged image. This layered class is intended for images that aid in design rather than be a part of the final image. Examples include: hatched backgrounds (commonly used to indicate background transparency); grids; and design guide lines. TDesignerLayer32 are also 'unclickable' in that they won't be returned by TLayeredImage32.GetLayerAt method calls.
  3. TButtonDesignerLayer32 - is a hybrid of TLayer32 and TDesignerLayer32. Like TDesignerLayer32, it's easily stripped from merged images; yet it's still 'clickable' (ie can be returned by TLayeredImage32.GetLayerAt method calls). As its name implies, this class is intended for button controls that dynamically move, size, rotate or reshape images.



From Examples/LayersAndTransform

uses Image32, Image32_Vectors, 
  Image32_BMP, Image32_PNG, Image32_JPG, 
  Image32_Extra, Image32_Layers, 
  Image32_Transform, BitmapPanels;


type
  TForm1 = class(TForm)  
  private
...
    layeredImage32: TLayeredImage32;
    buttonMovingLayer: TLayer32;
    btnLayerSize: integer;
    clickPoint: TPoint;
    ImageIsMoving: Boolean;
    splinePts: TArrayOfPointD;
...    
  end;
  
procedure TForm1.FormCreate(Sender: TObject);
begin
  //SETUP THE DISPLAY PANEL

  Panel1.BorderWidth := DPI(16);
  Panel1.BevelInner := bvLowered;
  //set Panel1.Tabstop := true to enable keyboard controls
  Panel1.TabStop := true;
  Panel1.FocusedColor := clGradientInactiveCaption;
  Panel1.BitmapProperties.Scale := 1;

  //SETUP THE LAYERED IMAGE
  DefaultButtonSize := DPI(8);

  layeredImage32 := TLayeredImage32.Create(100, 100);
  //layeredImage32.BackgroundColor := clWhite32;

  //add a 'hatched' layer (0) to highlight transparency
  layeredImage32.AddNewLayer(TDesignerLayer32, 'background - hatched');              

  //add a hidden master layer (1) containing the starting image
  with layeredImage32.AddNewLayer(TDesignerLayer32, 'master - hidden') do            
  begin
    image.LoadFromResource('UNION_JACK', 'BMP');
    Visible := false;
  end;

  //add another layer (2) for the transformed image
  layeredImage32.AddNewLayer('transformed');                       

  //add a design layer (3) for design lines etc
  layeredImage32.AddNewLayer(TDesignerLayer32, 'design');          

  //and reset for a very basic starting transformation
  //adding layers for each tangible spline control button
  btnLayerSize := Ceil(DefaultButtonSize * 1.25);
  if Odd(btnLayerSize) then inc(btnLayerSize);
  ResetSplinePts;                                              

  //finally update and display the layered image
  UpdateLayeredImage;
end;

procedure TForm1.ResetSplinePts;
begin
  //remove the group of buttons
  //and buttons will only be in group 1
  with layeredImage32 do
    if GroupCount(1) > 0 then DeleteGroup(1);

  with layeredImage32[1].Image do //with the master image
    splinePts := MakePathI([0,margin, Width div 2,0, Width, margin]);
  splinePts := OffsetPath(splinePts, margin, margin);

  CreateButtonGroup(layeredImage32,
    splinePts, clGreen32, DefaultButtonSize, []);
end;

procedure TForm1.UpdateLayeredImage(transform: Boolean);
var
  i,w,h: integer;
  pt: TPoint;
  path: TArrayOfPointD;
begin
  if transform then
  begin
    with layeredImage32[2] do 
    begin
      //using splinePts, update the 'transformed' layer
      image.Assign(layeredImage32[1].Image); //copy the master image
      if not SplineTransformVert(image, splinePts,
        stQuadratic, clRed32, false, pt) then Exit;
      PositionAt(pt); //nb: 'pt' returned by SplineTransformVert above

      //resize layeredImage32 so there's room for further transforms
      w := image.Width + margin  + pt.X;
      h := image.Height + margin + pt.Y;
      layeredImage32.SetSize(w, h);
      layeredImage32[0].SetSize(w, h);
      HatchBackground(layeredImage32[0].Image, clWhite32, $FFE0E0E0);
    end;
  end else
  begin
    w := layeredImage32.Width;
    h := layeredImage32.Height;
  end;

  //draw spline's design lines and implicit buttons on the design layer
  if not mnuShowImage.Checked then
    with TDesignerLayer32(layeredImage32[3]) do
    begin
      SetSize(w, h); //also clears layer
      if mnuShowControls.Checked then
      begin
        path := QSpline(splinePts);
        DrawLine(image, path, 1.5, clBlack32, esSquare);
      end;
      DrawQSplineDesign(splinePts);
    end;

  //move the 'button' layers to the splinePts coordinates (layers 4+)
  for i := 0 to high(splinePts) do
    with layeredImage32[4 +i] do
      PositionCenteredAt(Point(splinePts[i]));

  layeredImage32[2].Visible := not mnuShowControls.Checked;
  for i := 3 to layeredImage32.Count -1 do
    layeredImage32[i].Visible := not mnuShowImage.Checked;

  //copy the merged layeredImage32 to Panel1
  Panel1.Bitmap.SetSize(layeredImage32.Width, layeredImage32.Height);
  dc := Panel1.Bitmap.Canvas.Handle;
  layeredImage32.GetMergedImage(false).CopyToDc(dc);
  Panel1.Refresh;
end;
      

Contents

Types Classes Routines Variables Constants
TLayer32Class TButtonDesignerLayer32 AddToButtonGroup Image32_Layers Image32_Layers
TSizingStyle TCustomDesignerLayer32 CreateButtonGroup
TDesignerLayer32 CreateSizingBtnsGroup
TLayer32 StartButtonGroup
TLayeredImage32 UpdateSizingGroup

See Also

TButtonDesignerLayer32, TDesignerLayer32, TLayer32, TLayeredImage32, TLayeredImage32.GetLayerAt, TLayeredImage32.GetMergedImage