Search code examples
delphigraphics32

Delphi graphics32 delete selected layer


graphics32 is a very nice library, but I am having trouble figuring out how to use it properly. For instance LAYERS, they are an awesome feature.

  • I can add layers to an ImageView, select them, move them around, resize them, but, if I want to delete them I have no idea how to do it.

  • Also the layer is selected but I cannot capture any key events on them. I mean I would like to move the layer by a pixel using the arrows on the keyboard, but I cant.

Does anybody know how to solve these problems?

Please help Thank you


Solution

  • The key to capturing the arrow keys is to allow this. For this you need to adjust a public (but not published) property of the underlying TCustomPaintBox32 class.

    Something like

    ImgView.Options := ImgView.Options + [pboWantArrowKeys];
    

    should allow the TImgView32 class (named ImgView here) to capture arrow keys.

    Once enabled you can write a keyboard handler like:

    procedure TMainForm.ImgViewKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var
      Location: TFloatRect;
    begin
      if Assigned(FSelection) then
        case Key of
          VK_LEFT:
            FSelection.Location := OffsetRect(FSelection.Location, -1, 0);
    ...
    

    where OffsetRect adjusts the TFloatRect by adding the deltas (2nd and 3rd argument) to Left/Right and Top/Bottom.

    In the above example FSelection is the currently selected layer. It has been stored after selecting the layer (with a mouse click). In addition you may also need to adjust the rubberband location as well, in case you are using a TRubberBandLayer as selector.

    Addendum:

    Implementation of OffsetRect:

    function OffsetRect(const Rct: TFloatRect; const DeltaX, DeltaY: TFloat): TFloatRect;
    begin
      Result.TopLeft := OffsetPoint(Rct.TopLeft, DeltaX, DeltaY);
      Result.BottomRight := OffsetPoint(Rct.BottomRight, DeltaX, DeltaY);
    end;
    

    alternatively you can directly use code like this:

    procedure TMainForm.ImgViewKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var
      Location: TFloatRect;
    begin
      if Assigned(FSelection) then
        case Key of
          VK_LEFT:
            FSelection.Location := FloatRect(FSelection.Location.Left - 1, FSelection.Location.Top, FSelection.Location.Right - 1, FSelection.Location.Bottom);
    ...
    

    but that looks a bit ugly.

    Addendum 2:

    For older versions of the library (e.g. 1.9.x) the OffsetPoint function might be missing as well. This is implemented as:

    function OffsetPoint(const Pt: TFloatPoint; DeltaX, DeltaY: TFloat): TFloatPoint;
    begin
      Result.X := Pt.X + DeltaX;
      Result.Y := Pt.Y + DeltaY;
    end;