unit ViewFrame;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Clipbrd,Dialogs,
  ExtCtrls,light, ImgList, StdCtrls, Spin, ComCtrls,TextImages,Math,types,convImages,
  Menus,ViewPartsDrawers;

type
                                                                  //        ,ViewPartsDrawers;

  TNotifyPartsEvent = procedure (index:Integer;parts:TImageParts) of object;

  TArrangeType =(atTop,atBottom,atLeft,atRight,atSize,atNone);

  TPartsArranger = class(TObject)
  public
   minX,minY,maxX,maxY,width,height:Integer;
   procedure setLocation(List:TStrings);
   procedure changeLocation(List:TStrings;Arrangetype:TArrangeType);
   procedure changeSize(List:TStrings;width,height:Integer);
  end;

  TImageFrame = class(TFrame)
    ViewImage: TImage;
    positionLabel: TLabel;
   procedure ViewImageMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
   procedure ViewImageMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
   procedure ViewImageMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FrameDblClick(Sender: TObject);
    procedure popDeleteClick(Sender: TObject);

  private
    { Private 宣言 }
    ImageWidth,ImageHeight,moveX,moveY:Integer;
    FNotifyPartsEvent,FNotifyPartsAndSelectEvent:TNotifyPartsEvent;
    FNotifyNameEvent:TNotifyNameEvent;
    List:TStringList;
    selectedParts:TImageParts;
    mypoint:TPoint;
    selectID:Integer;
    selectRect:TRect;
    ADCanvas:TADCanvas;
    isInitImage:boolean;
    backBitmap,arrangedBitmap,initBitmap:TBitmap;
  public
    { Public 宣言 }
    partsDrawer:TViewPartsDrawer;
    mode:ifMode;
    drawlock:boolean;
    procedure arrangeLocation(Arrangetype:TArrangeType);
    procedure selectList(index:Integer;checked:boolean);
    constructor create(Owner:TComponent);override;
    procedure arrangeImage(inf:TImageInfo);
    procedure add(name:string;newParts:TImageParts);
    procedure delete(index:Integer);
    procedure exchange(index1,index2:Integer);
    procedure repaintParts(drawAll:boolean=true);
    function getBitmap(index:Integer):TBitmap;
    procedure setBackColor(const color:TColor);
    function getBackColor():TColor;
    procedure selectParts(index:Integer);
    procedure setLocation(inf:TPosInf);
    procedure resizeImage(w,h:Integer);
    procedure saveImage(fileName:string;saveType:byte=1);
    procedure drawSelected();
    procedure drawSelects();
    procedure drawPartsEdge(parts:TImageParts);
    function IsOnRect(pt:TPoint;rec:TRect):Integer;
    function checkAllParts(pt:TPoint):Integer;
    procedure reset;
    procedure setInitImage(bitmap:TBitmap);
    destructor destroy();override;
    function getLocation(index:Integer):TPosInf;
    procedure reNameListItme(newName:string;index:Integer);
  published
    property NotifyPartsEvent:TNotifyPartsEvent
     read FNotifyPartsEvent write FNotifyPartsEvent;

    property NotifyPartsAndSelectEvent:TNotifyPartsEvent
     read FNotifyPartsAndSelectEvent write FNotifyPartsAndSelectEvent;

    property NotifyNameEvent:TNotifyNameEvent
     read FNotifyNameEvent write FNotifyNameEvent;
  end;


implementation

{$R *.dfm}


{frame}

constructor TImageFrame.create(Owner:TComponent);
begin
 inherited;
 self.drawlock:=false;
 self.ADCanvas:=TADCanvas.create();
 backBitmap:=TBitmap.Create();
 initBitmap:=TBitmap.Create();
 self.backBitmap.PixelFormat:=pf32bit;
 self.backBitmap.Width:=632;
 self.backBitmap.Height:=420;

 self.arrangedBitmap:=TBitmap.Create();
 self.arrangedBitmap.PixelFormat:=pf32bit;

 self.Color:=self.backBitmap.Canvas.Brush.Color;

 self.List:=TStringList.Create();
 self.List.Sorted:=false;
 self.selectID:=-1;
 self.selectRect:=Rect(-1,-1,-1,-1);

 ViewImage.Canvas.Pen.Mode:=pmNot;
 ViewImage.Canvas.Pen.Color:=clred;
 ViewImage.Canvas.Brush.Color:=clgray;
                            
 ImageWidth:=632;
 ImageHeight:=420;
 mode:=ifNormal;

 partsDrawer:=TViewPartsDrawer.Create();
 partsDrawer.List:=self.List;

end;

destructor TImageFrame.destroy();
var
 i:Integer;
begin

 partsDrawer.Free;
 arrangedBitmap.Free();
 backBitmap.Free();
 initBitmap.Free();
 adCanvas.Free;

 for i:=0 to List.Count-1 do
 begin
  if(List.Objects[i]<>nil)then
   List.Objects[i].Free();
 end;

 List.Free();

 inherited;
end;

(*パーツを初期化*)
procedure TImageFrame.reset();
begin
 self.ViewImage.Canvas.Brush.Color:=$ffffff;
 self.ViewImage.Width:=632;
 self.ViewImage.Height:=420;
 self.moveX:=0;
 self.moveY:=0;
 self.selectID:=-1;
 //ViewImage.Canvas.FillRect(self.ViewImage.ClientRect);
 light.fillBMP(ViewImage.Picture.Bitmap,ViewImage.Canvas.Brush.Color,true);
 ViewImage.Repaint();
 light.FillBMP(initBitmap,initBitmap.TransparentColor,false);
 self.backBitmap.Assign(ViewImage.Picture.Bitmap);
 self.arrangedBitmap.Assign(ViewImage.Picture.Bitmap);
 self.List.Clear();
end;

(*パーツを追加*)
procedure TImageFrame.add(name:string;newParts:TImageParts);
var
 i:Integer;
 oldParts:TImageParts;
 pInf:TPosInf;
begin
 i:=List.IndexOf(name);
 pInf:=newParts.getPosInf();

 if(i<0)then
 begin
  List.Add(name);
  List.Objects[List.count-1]:=newParts;
  selectParts(List.count-1);
 end
 else
 begin
  oldparts:=TImageParts(List.Objects[i]);
  newParts.bitmap.Transparent:=oldParts.bitmap.Transparent;
  oldparts.bitmap.Handle:=newParts.bitmap.ReleaseHandle;
  oldParts.bitmap.TransparentColor:=oldParts.bitmap.Canvas.Pixels[0,0];
  newParts.Free();
 end;

  if not(self.drawlock)then
   repaintParts();


end;

(*任意のパーツの淵を描画*)
procedure TImageFrame.drawPartsEdge(parts:TImageParts);
begin
 with parts do
  ViewImage.Canvas.Polyline([point(x,y),point(x+w-1,y),
   point(x+w-1,y+h-1),point(x,y+h-1),point(x,y)]);
end;

(*選択中のパーツに枠を描画*)
procedure TImageFrame.drawSelected();
var
 oldMode:TPenMode;
 parts:TImageParts;
// iRect:TRect;
 i:Integer;
begin

(*はみ出た部分を補正*)

 {
 //if(IntersectRect(iRect,Bounds(0,0,ImageWidth,ImageHeight),selectRect))then
 // ViewImage.Canvas.CopyRect(iRect,backBitmap.Canvas,iRect);
    

 if(viewImage.Width<>ImageWidth)then
  viewImage.Canvas.FillRect(Rect(ImageWidth,0,viewImage.Width,viewImage.Height));

 if(viewImage.Height<>ImageHeight)then
  viewImage.Canvas.FillRect(Rect(0,ImageHeight,viewImage.Width,viewImage.Height));
}

 ViewImage.Canvas.CopyRect(Bounds(0,0,ImageWidth,ImageHeight),backBitmap.Canvas,Bounds(0,0,ImageWidth,ImageHeight));

 if(viewImage.Width<>ImageWidth)then
  viewImage.Canvas.FillRect(Rect(ImageWidth,0,viewImage.Width,viewImage.Height));

 if(viewImage.Height<>ImageHeight)then
  viewImage.Canvas.FillRect(Rect(0,ImageHeight,viewImage.Width,viewImage.Height));

 oldMode:=ViewImage.Canvas.Pen.Mode;

 if(selectID>-1)then
 begin

  ViewImage.Canvas.Pen.Mode:=pmCopy;
  drawPartsEdge(selectedParts);
  ViewImage.Canvas.Pen.Mode:=oldMode;

  with selectedParts do
  begin
   selectRect:=Bounds(x,y,w+1,h+1);
  end;

 end
 else selectRect:=Rect(-1,-1,-1,-1);

 for i:=0 to self.List.Count-1 do
 begin
  if(i<>selectID)then
  begin
   parts:=nil;
   parts:=TImageParts(List.Objects[i]);
   if(parts.selected)then drawPartsEdge(parts);
  end 
 end;

end;

(*パーツを削除*)
procedure TImageFrame.delete(Index:Integer);
begin
 if(index>-1)and(index<List.Count)then
 begin

  if(List.Objects[index]=selectedParts)then
  begin
   self.mode:=ifNormal;
   self.selectID:=-1;
   selectedParts:=nil;
  end;

  self.List.Objects[index].Free();
  self.List.Delete(index);
  self.repaintParts();
 end;
end;


(*パーツの順番を交換*)
procedure TImageFrame.exchange(Index1,index2:Integer);
begin
  List.Exchange(index1,index2);
  repaintParts();
end;


(*パーツを選択*)
procedure TImageFrame.selectParts(index:Integer);
var
 newParts:TImageParts;
begin
 selectID:=index;
 selectedParts:=nil;

 if(index>-1)then
 begin
  newParts:=TImageParts(self.List.Objects[index]);
  selectedParts:=newParts;
 end;
  
end;

function TImageFrame.getLocation(index:Integer):TPosInf;
var
 newParts:TImageParts;
begin
 if(index>-1)then
 begin
  selectID:=index;
  newParts:=TImageParts(self.List.Objects[index]);
  selectedParts:=nil;
  selectedParts:=newParts;
  result:=selectedParts.getPosInf();
 end;

end;

(*全パーツをイメージに描画*)
procedure TImageFrame.repaintParts(drawAll:boolean=true);
var
 i:Integer;
 newParts:TImageParts;
 inf:TPaintInf;
begin

 inf.isClip:=false;

 if not(self.drawlock)then
 begin
  light.FillBMP(viewImage.Picture.Bitmap,viewImage.Canvas.Brush.Color,false);

  light.FillBMP(backBitmap,backBitmap.Canvas.Brush.Color,false);


 if(isInitImage)then
  backBitmap.Canvas.CopyRect
   (bounds(0,0,ImageWidth,ImageHeight),
    initBitmap.Canvas,initBitmap.Canvas.ClipRect);

  ADCanvas.setBitmap(backBitmap);

  for i:=0 to List.Count-1 do
  begin
   newparts:=TImageParts(List.Objects[i]);
   if (newParts<>nil)then
   begin
    inf.srcX:=newparts.X;
    inf.srcY:=newparts.Y;
    inf.srcW:=newParts.w;
    inf.srcH:=newParts.h;
    inf.mode:=newParts.mode;
    inf.alpha:=newParts.alpha;
    
    if(newParts.visible)or(drawAll)then
     ADCanvas.StretchDrawEx(newparts.bitmap,inf);
   end
   else
    ADCanvas.StretchDrawEx(newparts.bitmap,inf);


  end;


  ViewImage.Canvas.CopyRect
   (Bounds(0,0,ImageWidth,ImageHeight),backbitmap.Canvas,
    Bounds(0,0,ImageWidth,ImageHeight));

 end; 

end;

procedure TImageFrame.setInitImage(bitmap:TBitmap);
begin
 if(bitmap<>nil)then
 begin
  initBitmap.Assign(bitmap);
  initBitmap.PixelFormat:=pf32bit;
  isInitImage:=true;
 end
 else
  isInitImage:=false;

end;
      
function TImageFrame.checkAllParts(pt:TPoint):Integer;
var
 i:Integer;
 newParts:TImageParts;
begin
 result:=-1;

 (*シフトを押していなければ選択中パーツの選択解除*)

 with pt do
 begin
   for i:=List.Count-1 downto 0 do
   begin
    selectedParts:=nil;
    newParts:=TImageParts(List.Objects[i]);
    selectedParts:=newParts;
    if(x>newparts.x)and(y>newparts.y)and
       (x<newparts.x+newparts.w)and(y<newparts.y+newparts.h)then
    begin
     selectedParts.selected:=true;
     mypoint.X:=x;
     mypoint.Y:=y;
     result:=i;
     mode:=ifMove;
     break;
    end;

   end;
 end;



end;

procedure TImageFrame.ViewImageMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin

 if(mode=ifNormal)then
 begin

  if(selectID>-1)then
   case IsOnRect(Point(x,y),selectRect) of
    crSizeAll:
    begin
     mypoint.X:=x;
     mypoint.Y:=y;
     mode:=ifMove;
    end;
    crSizeNWSE:
    begin
     mypoint.X:=x;
     mypoint.Y:=y;
     mode:=ifReSizeAll;
    end;
    crSizeNS:
    begin
     mypoint.Y:=y;
     mypoint.X:=x;
     mode:=ifReSizeV;
    end;
    crSizeWE:
    begin
 //    
     mypoint.x:=x;
     mypoint.Y:=y;
     mode:=ifReSizeH;
    end;
    else
    begin
     selectID:=checkAllParts(Point(x,y));
    end
   end
  else
  begin
   selectID:=checkAllParts(Point(x,y));
 //  if(select
  end;

   if(assigned(FNotifyPartsAndSelectEvent))then
       NotifyPartsAndSelectEvent(selectID,selectedParts);

   drawSelected();

 end;

end;

procedure TImageFrame.ViewImageMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var
 i,mx,my:Integer;
 parts:TImageParts;
begin

  self.positionLabel.Caption:='X:'+IntToStr(x)+' , Y:'+IntToStr(y);
  if(selectID<0)then exit;

  case mode of
   ifMove:
   begin

    self.drawPartsEdge(selectedparts);

    mx:=x-mypoint.x;
    my:=y-mypoint.Y;

    selectedParts.x:=selectedParts.x+mx;//-mypoint.X+x;
    selectedParts.y:=selectedParts.y+my;//-mypoint.Y+y;

    (*コントロール値の変更*)
    mypoint.X:=x;
    mypoint.Y:=y;
    drawSelected();
    
     (*テスト中*)
    for i:=0 to self.List.Count-1 do
    begin
     if(i<>self.selectID)then
     begin
      parts:=TImageParts(List.Objects[i]);
      if(parts.selected)then
      begin

       Parts.x:=Parts.x+mx;
       Parts.y:=Parts.y+my;
       drawPartsEdge(parts);
      if(assigned(FNotifyPartsEvent))then
        NotifyPartsEvent(i,parts);
      end;

      parts:=nil;
     end;
    end;



    if(assigned(FNotifyPartsAndSelectEvent))then
      NotifyPartsAndSelectEvent(selectID,selectedParts);

   end;
  ifResizeAll:
  begin
    self.drawPartsEdge(selectedparts);

    selectedParts.w:=max(3,selectedParts.w-mypoint.X+x);
    selectedParts.h:=max(3,selectedParts.h-mypoint.Y+y);

    (*コントロール値の変更*)
    mypoint.X:=x;
    mypoint.Y:=y;

    drawPartsEdge(selectedparts);


    if(assigned(FNotifyPartsAndSelectEvent))then
      NotifyPartsAndSelectEvent(selectID,selectedParts);
  end;
  ifResizeV:
  begin
    self.drawPartsEdge(selectedparts);
    selectedParts.h:=max(3,selectedParts.h-mypoint.Y+y);
    mypoint.Y:=y;
    self.drawPartsEdge(selectedparts);

    if(assigned(FNotifyPartsAndSelectEvent))then
      NotifyPartsAndSelectEvent(selectID,selectedParts);
  end;
  ifResizeH:
  begin
    drawPartsEdge(selectedparts);
    selectedParts.w:=max(3,selectedParts.w-mypoint.X+x);
    mypoint.X:=x;
    self.drawPartsEdge(selectedparts);

    if(assigned(FNotifyPartsAndSelectEvent))then
      NotifyPartsAndSelectEvent(selectID,selectedParts);
  end;
  ifNormal:
   self.Cursor:=IsOnRect(Point(x,y),selectRect);
  end;

end;


procedure TImageFrame.ViewImageMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);

begin

  case mode of
   ifMove:
   begin

    if(selectedParts.X+selectedParts.w<1)then
     selectedParts.x:=1
    else if(selectedParts.x>ViewImage.Width)then
     selectedParts.x:=ViewImage.Width-selectedParts.w;

    if(selectedParts.y+selectedParts.h<1)then
     selectedParts.y:=1
    else if(selectedParts.y>ViewImage.Height)then
     selectedParts.y:=ViewImage.Height-selectedParts.h;

    if(assigned(FNotifyPartsAndSelectEvent))then
      NotifyPartsAndSelectEvent(selectID,selectedParts);

  //  selectedParts.selected:=false;
    self.mode:=ifNormal;
    self.repaintParts;
    self.drawSelected();
    
  //  self.selectRect:=Rect(-1,-1,-1,-1);
   end;
  ifResizeAll,ifResizeV,ifResizeH:
  begin
   if(assigned(FNotifyPartsAndSelectEvent))then
    NotifyPartsAndSelectEvent(selectID,selectedParts);
    
   self.mode:=ifNormal;
   self.repaintParts;
   self.drawSelected();
 //  self.selectRect:=Rect(-1,-1,-1,-1);
  end;
 end;


end;

(*パーツに動かした情報を与える*)
procedure TImageFrame.setLocation(inf:TPosInf);
var
 newParts:TImageParts;
begin
 if(self.selectID>-1)then
 begin
  newParts:=TImageParts(self.List.Objects[selectID]);
  newParts.mode:=inf.mode;
  newParts.x:=inf.x;
  newParts.y:=inf.y;
  newParts.w:=inf.w;
  newParts.h:=inf.h;
  newParts.alpha:=inf.alpha;
  newParts.bitmap.Transparent:=inf.trans;
  self.repaintParts;
  drawSelected();
 // if(assigned(FNotifyPartsEvent))then
 //  NotifyPartsEvent(selectID,selectedParts);

 end;
end;

procedure TImageFrame.setBackColor(const color:TColor);
begin
 backBitmap.Canvas.Brush.Color:=color;
end;
             
(*画像サイズの変更〜見かけだけ*)
procedure TImageFrame.resizeImage(w,h:Integer);
begin
 ImageWidth:=w;
 ImageHeight:=h;
 repaintParts();
end;

(*イメージのみを取得*)
function TImageFrame.getBitmap(index:Integer):TBitmap;
begin        
 result:=selectedParts.bitmap;
end;

(*イメージ情報で元の画像を加工*)
procedure TImageFrame.arrangeImage(inf:TImageInfo);
begin
 arrangedBitmap.Width:=ImageWidth;
 arrangedBitmap.Height:=ImageHeight;
 bitBlt(arrangedBitmap.Canvas.Handle,0,0,arrangedBitmap.Width,
  arrangedBitmap.Height,backBitmap.Canvas.Handle,0,0,SrcCopy);

 if(inf<>nil)then
 begin
  inc(inf.delCount);
  convImages.customizeBitmap(inf,arrangedBitmap);
  inf.FreeEx();
 end;

 if not(drawlock)then
 begin
  if(inf<>nil)and(inf.turn>0)then
   fillBMP(viewImage.Picture.Bitmap,viewImage.Canvas.Brush.Color,false);
   ViewImage.Canvas.Draw(0,0,arrangedBitmap);
 end;

end;

procedure TImageFrame.FrameDblClick(Sender: TObject);
begin
 if(selectID<List.Count)and(selectID>-1)and(self.mode<>ifinit)then
 begin
  mode:=ifNormal;
  if(assigned(FNotifyNameEvent))then
   NotifyNameEvent(List.Strings[selectID]);
 end;

end;

(*initの背景色を取得*)
function TImageFrame.getBackColor():TColor;
begin
 result:=backBitmap.Canvas.Brush.Color;
end;

(*ベースイメージを保存*)
procedure TImageFrame.saveImage(fileName:string;saveType:byte=1);
var
 saveBitmap:TBitmap;
begin

 saveBitmap:=TBitmap.Create();
 saveBitmap.Width:=ImageWidth;
 saveBitmap.Height:=ImageHeight;

 bitBlt(saveBitmap.Canvas.Handle,0,0,ImageWidth,ImageHeight,
  ViewImage.canvas.handle,0,0,srcCopy);

 if(saveType=2)then
   ClipBoard.Assign(saveBitmap)
 else
   saveBitmap.SaveToFile(fileName);

 saveBitmap.Free();

end;

(*ポイントが特定のパーツの中に存在するか*)
function TImageFrame.IsOnRect(pt:TPoint;rec:TRect):Integer;
var
 subX,subY:Integer;
begin
 subY:=abs(pt.Y-rec.Bottom);
 subX:=abs(pt.X-rec.Right);

 if(subX<3)then
 begin
  if(subY<3)then
   result:=crSizeNWSE
  else
   result:=crSizeWE;
 end
 else if(subY<3)then
  result:=crSizeNS
 else if(pt.X>rec.Left)and(pt.X<rec.Right)and(pt.Y>rec.Top)and(pt.Y<rec.Bottom)then
  result:=crSizeAll
 else
  result:=crArrow;

end;

(*パーツの選択の有無をセット*)
procedure TImageFrame.selectList(index:Integer;checked:boolean);
var
 sParts:TImageParts;
begin
 if(index>-1)and(List.Count>index)then
 begin
  sParts:=TImageParts(List.Objects[index]);
  sParts.selected:=checked;
 end;
end;

(*パーツ名の変更*)
procedure TImageFrame.reNameListItme(newName:string;index:Integer);
begin
 if(index<List.Count)then
 begin
  List.Strings[index]:=newName;
 end;

end;

procedure TImageFrame.popDeleteClick(Sender: TObject);
begin
  delete(selectID);
end;

procedure TImageFrame.drawSelects();
var
 i:Integer;
 parts:TImageParts;
begin

 for i:=0 to self.List.Count-1 do
 begin
  parts:=nil;
  parts:=TImageParts(List.Objects[i]);
  if(parts.selected)then drawPartsEdge(parts);
 end;
          
end;

procedure TImageFrame.arrangeLocation(Arrangetype:TArrangeType);
var
 arranger:TPartsArranger;
 parts:TImageParts;
 i:Integer;
begin
 arranger:=TPartsArranger.Create();
 arranger.setLocation(List);

 if(arrangeType=atSize)then
 begin
  if(selectedParts<>nil)then
  begin
   arranger.changeSize(List,selectedParts.w,selectedParts.h);
  end;
 end
 else
  arranger.changeLocation(List,arrangeType);

 for i:=0 to list.Count-1 do
 begin
  parts:=TImageParts(List.Objects[i]);
  if(parts.selected)then
  begin
   if(i=self.selectID)then
   begin
    if(assigned(FNotifyPartsAndSelectEvent))then
      NotifyPartsAndSelectEvent(i,Parts);
   end
   else
   begin
    if(assigned(FNotifyPartsEvent))then
      NotifyPartsEvent(i,Parts);
   end;
  end;
 end;

 self.repaintParts();
 self.drawSelected();

end;


procedure TPartsArranger.changeLocation(List:TStrings;Arrangetype:TArrangeType);
var
 i:Integer;
 parts:TImageParts;
begin

 case Arrangetype of
  atTop:
   for i:=0 to List.Count-1 do
   begin
    parts:=TImageParts(List.Objects[i]);
    if(parts.selected)then
     parts.y:=self.minY;
    parts:=nil;
   end;
  atBottom:
   for i:=0 to List.Count-1 do
   begin
    parts:=TImageParts(List.Objects[i]);
    if(parts.selected)then
     parts.y:=self.maxY;
    parts:=nil;
   end;
  atLeft:
   for i:=0 to List.Count-1 do
   begin
    parts:=TImageParts(List.Objects[i]);
    if(parts.selected)then
     parts.x:=self.minX;
    parts:=nil;
   end;
  atRight:
   for i:=0 to List.Count-1 do
   begin
    parts:=TImageParts(List.Objects[i]);
    if(parts.selected)then
     parts.x:=self.maxX;
    parts:=nil;
   end;


 end;

end;

procedure TPartsArranger.setLocation(List:TStrings);
var
 i:Integer;
 parts:TImageParts;
begin

 maxX:=0; minX:=999; maxy:=0; miny:=999;

 for i:=0 to List.Count-1 do
 begin
  parts:=TImageParts(List.Objects[i]);

  if(parts.selected)then
  begin
   maxY:=Math.Max(parts.y,maxY);
   minY:=Math.Min(parts.y,minY);
   maxX:=Math.Max(parts.x,maxX);
   minX:=Math.Min(parts.x,minX);
  end;

  parts:=nil;
 end;

end;

procedure TPartsArranger.changeSize(List:TStrings;width,height:Integer);
var
 i:Integer;
 parts:TImageParts;
begin

 maxX:=0; minX:=999; maxy:=0; miny:=999;

 for i:=0 to List.Count-1 do
 begin
  parts:=TImageParts(List.Objects[i]);

  if(parts.selected)then
  begin
   parts.w:=width;
   parts.h:=height;
  end;

  parts:=nil;

 end;

end;

end.
