unit ViewForms;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ViewFrame, StdCtrls, Buttons, ComCtrls, initFrames,convImages,
  JPYInterfaced,cwCells,strUtils,TextImages, Menus,TempPosForms,Math,messageEntries;

type

  TCardDrawer= class(TObject)
   procedure drawCard(canvas:TCanvas);
  end;

  TViewForm = class(TForm,IAddingForm,IClipBrdDealer,ITextParent)
    ImageFrame1: TImageFrame;
    upBitBtn: TBitBtn;
    downBitBtn: TBitBtn;
    DelBitBtn: TBitBtn;
    PartsListBox: TListBox;
    posPanel: TPanel;
    WidthEdit: TEdit;
    WidthUpDown: TUpDown;
    HeightEdit: TEdit;
    HeightUpDown: TUpDown;
    PosXEdit: TEdit;
    XUpDown: TUpDown;
    PosYEdit: TEdit;
    YUpDown: TUpDown;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    transCheckBox: TCheckBox;
    PaintComboBox: TComboBox;
    backColorLabel: TLabel;
    ColorDialog1: TColorDialog;
    viewTabControl: TTabControl;
    partsListPanel: TPanel;
    initFrame1: TinitFrame;
    viewPanel: TPanel;
    backSizePanel: TPanel;
    backWidthEdit: TEdit;
    backHeightEdit: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    backWidthUpDown: TUpDown;
    backHeightUpDown: TUpDown;
    aEdit: TEdit;
    aUpDown: TUpDown;
    seekBitBtn: TBitBtn;
    animeBitBtn: TBitBtn;
    selectAllBitBtn: TBitBtn;
    PopupMenu1: TPopupMenu;
    resizePop: TMenuItem;
    sizePop: TMenuItem;
    arrangePop: TMenuItem;
    topPop: TMenuItem;
    bottomPop: TMenuItem;
    rightPop: TMenuItem;
    leftPop: TMenuItem;
    sameSizePop: TMenuItem;
    cardCB: TCheckBox;
    backBasicSizeBB: TBitBtn;
    initLabel: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure DelBitBtnClick(Sender: TObject);
    procedure downBitBtnClick(Sender: TObject);
    procedure upBitBtnClick(Sender: TObject);
    procedure PartsListBoxClick(Sender: TObject);
    procedure PosXEditChange(Sender: TObject);
    procedure backColorLabelClick(Sender: TObject);
    procedure viewTabControlChange(Sender: TObject);
    procedure backWidthEditChange(Sender: TObject);
    procedure CopyCellsBitBtnClick(Sender: TObject);
    procedure aEditChange(Sender: TObject);
    procedure seekBitBtnClick(Sender: TObject);
    procedure PosXEditKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure aEditKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure PartsListBoxKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure backWidthEditKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure copyBitBtnClick(Sender: TObject);
    procedure HeightUpDownClick(Sender: TObject; Button: TUDBtnType);
    procedure aUpDownClick(Sender: TObject; Button: TUDBtnType);
    procedure backWidthUpDownClick(Sender: TObject; Button: TUDBtnType);
    procedure FormDestroy(Sender: TObject);
    procedure copyContentBitBtnClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure animeBitBtnClick(Sender: TObject);
    procedure selectAllBitBtnClick(Sender: TObject);
    procedure resizePopClick(Sender: TObject);
    procedure sizePopClick(Sender: TObject);
    procedure arrangeLocation(Sender: TObject);
    procedure cardCBClick(Sender: TObject);
    procedure backBasicSizeBBClick(Sender: TObject);
  //  procedure setFlagItems(items:TStrings);
  private
    { Private 宣言 }
    sectionCount:Integer;
    initImageFileName:string;
    partsNameItems:TStringList;
    initDirType:shortInt;
    director:TEDDirector;
    FNotifySeekEvent:TNotifyArrangeEvent;
  //  procedure checkedAll(checked:boolean);
    procedure sendPartFromClipInfo(inf:TImageInfo);
    procedure loadPartsFromClipBoard();
    procedure loadContentFromClipBoard();
    procedure setListIndex(index:Integer);
    procedure checkSelect();
    function createParts(inf:TImageInfo;var bitmap:TBitmap):TImageParts;
    function createPartsName():string;
  public
    { Public 宣言 }
   lock:Boolean;
   pathctrl:TPathCtrl;
   animeForm:TForm;
   function IsExistText(text:string):boolean;
   procedure loadFromClipBoard();
   procedure setInitImage(dirType:shortInt;fileName:string;bitmap:TBitmap);overload;
   procedure setInitImage(inf:TImageInfo;bitmap:TBitmap);overload;
   function getBackColor():TColor;
   procedure setBackColor(newColor:TColor);
   function getInitInf():TImageInfo;
   procedure setSectionInf(inf:TImageInfo;bitmap:TBitmap);
   procedure reset();
   procedure setPosition();
   procedure saveToClipBoard(mode:TCWFormat);
   function getSectionInf(i:Integer):TImageInfo;
   procedure addSectionParts(inf:TImageInfo;bitmap:TBitmap);
   procedure receiveInf(inf:TImageInfo);
   procedure setInitInf(inf:TImageInfo);
   procedure receiveName(name:string);
   procedure receiveParts(index:Integer;parts:TImageParts);
   procedure notifyParts(index:Integer;parts:TImageParts);
   procedure setPosInfGUI(inf:TPosInf);
   procedure showMessageEntries();
   procedure saveImage(fileName:string;savetype:byte=1);
   procedure remaneParts(newName:string;index:Integer);
   property  NotifySeekEvent:TNotifyArrangeEvent
              read FNotifySeekEvent write FNotifySeekEvent;
   property backColor:TColor
    read getBackColor write setBackColor;
   end;

var
  viewForm: TviewForm;

implementation

{$R *.dfm}


(*リストに設定パーツを加える*)
procedure TViewForm.addSectionParts(inf:TImageInfo;bitmap:TBitmap);
var
 i:Integer;
 parts:TImageParts;
 pinf:TPosInf;
begin

 inc(inf.delCount);
 parts:=createParts(inf,bitmap);
 i:=PartsListBox.Items.IndexOf(inf.sectionName);
 bitmap.PixelFormat:=pf32bit;
 bitmap.TransparentColor:=bitmap.Canvas.Pixels[0,0];

 if(i<0)then
 begin
  inc(inf.delCount);
  pinf:=inf.posInf;
  self.lock:=true;
  setPosInfGUI(pinf);
  PartsListBox.Items.AddObject(inf.sectionName,inf);
  PartsListBox.ItemIndex:=PartsListBox.count-1;
  PartsListBox.Selected[PartsListBox.ItemIndex]:=true;
  self.lock:=false;
 end
 else
 begin
  pInf:=TImageInfo(PartsListBox.Items.Objects[i]).posInf;
  self.lock:=true;
  setPosInfGUI(pinf);
  PartsListBox.Items.Objects[i].Free();
  PartsListBox.Items.Objects[i]:=nil;
  inc(inf.delCount);
  PartsListBox.Items.Objects[i]:=inf;
  TImageInfo(PartsListBox.Items.Objects[i]).posInf:=pinf;
  PartsListBox.Items.Strings[i]:=inf.sectionName;
  PartsListBox.ItemIndex:=i;

  PartsListBox.Selected[PartsListBox.ItemIndex]:=true;
 // ImageFrame1.selectParts(i);
 // setListIndex(-1);
  self.lock:=false;

  if(AnsiStartsStr('パーツ',inf.sectionName))then
  begin
   partsNameItems.Add(inf.sectionName);
   inc(sectionCount);
  end;
 end;

 

  inc(inf.delCount);
  ImageFrame1.add(inf.sectionName,parts);
  setListIndex(-1);    //バグ防止用

  if(viewTabControl.TabIndex=1)then
   self.viewTabControlChange(self);

  inf.FreeEx();
end;


(*TImageInfoと画像からパーツ作成*)
function TViewForm.createParts(inf:TImageInfo;var bitmap:TBitmap):TImageParts;
begin
 result:=TImageParts.create();
 result.x:=inf.posInf.x;
 result.y:=inf.posInf.y;
 result.w:=inf.posInf.w;
 result.h:=inf.posInf.h;
 result.alpha:=inf.posInf.alpha;
 result.mode:=inf.posInf.mode;

 if(bitmap<>nil)then
 begin
  result.bitmap.Assign(bitmap);
  result.bitmap.Transparent:=inf.posInf.trans;
  result.bitmap.TransparentColor:=result.bitmap.Canvas.Pixels[0,0];
 end;

 inf.FreeEx();
end;

(*GUI等の初期化*)
procedure TViewForm.reset();
begin
 self.lock:=true;
 self.ImageFrame1.reset();
 self.PartsListBox.Clear();

 self.HeightUpDown.Position:=0;
 self.WidthUpDown.Position:=0;
 self.XUpDown.Position:=0;
 self.YUpDown.Position:=0;
 self.transCheckBox.Checked:=false;
 self.aUpDown.Position:=127;
 self.PaintComboBox.ItemIndex:=0;

 self.backColorLabel.Color:=clwhite;
 self.backColorLabel.Font.Color:=clblack;
 self.initFrame1.reset();
 self.initImageFileName:='';
 self.initDirType:=-1;
 sectionCount:=0;
 self.partsNameItems.Clear();
 self.cardCB.Checked:=false;
 self.lock:=false;
end;

{ViewFrameからパーツを更新}
procedure TviewForm.notifyParts(index:Integer;parts:TImageParts);
var
 inf:TPosInf;
begin
 if(PartsListBox.Count>index)and(index>-1)then
 begin
  inf:=parts.getPosInf();
  TImageInfo(PartsListBox.Items.Objects[index]).posInf:=inf;
 end; 
end;

{ViewFrameからパーツの位置を貰う}
procedure TviewForm.receiveParts(index:Integer;parts:TImageParts);
var
 inf:TPosInf;
// i:Integer;
begin

 lock:=true;
 try
  if(index>-1)then
  begin
  //何も選ばないので選択解除
  if(getKeyState( VK_SHIFT ) >= 0)and
   (not PartsListBox.Selected[index])then
  begin
   PartsListBox.ClearSelection;
   checkSelect();
  end;
   PartsListBox.Selected[index]:=true;
   inf:=parts.getPosInf();
   setPosInfGUI(inf);
   TImageInfo(PartsListBox.Items.Objects[index]).posInf:=inf;
  end
  else
  begin           
   PartsListBox.ClearSelection;
   checkSelect();
   XUpDown.Position:=0;
   YUpDown.Position:=0;
   transCheckBox.Checked:=false;
   PaintComboBox.ItemIndex:=0;
   HeightUpDown.Position:=0;
   WidthUpDown.Position:=0;
   PartsListBox.ItemIndex:=-1;
  end;
 finally
  self.lock:=False;
 end;

end;

(*何を選択しているのかをフレームに送る*)
procedure TViewForm.checkSelect();
var
 i:Integer;
begin

 for i:=0 to PartsListBox.Count-1 do
 begin
  ImageFrame1.selectList(i,(PartsListBox.Selected[i]));
 end;
     
end;
     {
procedure TViewForm.checkedAll(checked:boolean);
var
 i:Integer;
begin
 for i:=0 to PartsListBox.Items.Count-1 do
 begin
  self.ImageFrame1.selectList(i,true);
 end;
end;
      }
procedure TViewForm.FormCreate(Sender: TObject);
begin
 ImageFrame1.NotifyPartsAndSelectEvent:=self.receiveParts;
 ImageFrame1.NotifyPartsEvent:=self.notifyParts;
 ImageFrame1.NotifyNameEvent:=self.receiveName;

 //if(getKeyState( VK_SHIFT ) >= 0)then
 //PartsListBox.ClearSelection;

 partsNameItems:=TStringList.Create;
 sectionCount:=0;
 InitFrame1.NotifyArrangeEvent:=self.receiveInf;
 director:=TEDDirector.create();
end;

{ネームを受け取って本体に通知}
procedure TViewForm.receiveName(name:string);
var
 i:Integer;
 inf:TImageInfo;
begin
 i:=PartsListBox.Items.IndexOf(name);


  PartsListBox.ClearSelection;


 if(i>-1)then
 begin
  PartsListBox.Selected[i]:=true;
  inf:=TImageInfo(PartsListBox.Items.Objects[i]);
  inc(inf.delCount,2);

  if(assigned(FNotifyseekEvent))then
  begin
   inc(inf.delCount);
   NotifySeekEvent(inf);
   inf.FreeEx();
  end;
 inf.FreeEx();
 end;

end;

{パーツの閲覧}
procedure TViewForm.seekBitBtnClick(Sender: TObject);
var
 i:Integer;
 inf:TImageInfo;
begin
 i:=PartsListBox.ItemIndex;

 if(i>-1)and(PartsListBox.Count>0)then
 begin
  if(assigned(NotifySeekEvent))then
  begin
   inf:=TImageInfo(PartsListBox.Items.Objects[i]);
   
   if(inf<>nil)then
   begin
    inc(inf.delCount,2);
    NotifySeekEvent(inf);
    inf.FreeEx();
   end
   else
    NotifySeekEvent(inf);
   end;

 end;

end;


{パーツの削除}
procedure TViewForm.DelBitBtnClick(Sender: TObject);
var
 i,i2:Integer;
 del:boolean;
begin
 del:=false;
 i2:= PartsListBox.Count-1;

 for i:=i2 downto 0 do
 begin
  if(PartsListBox.Selected[i])then
  begin
   PartsListBox.Items.Objects[i].Free();
   PartsListBox.Items.Delete(i);
   ImageFrame1.delete(i);
   del:=true;
  end;
 end;

 if(del)then
  setListIndex(-1);
  
 viewTabControlChange(Sender);


end;

{パーツの入れ替え}
procedure TviewForm.downBitBtnClick(Sender: TObject);
var
 i,index:Integer;

begin
 i:=PartsListBox.ItemIndex;
 if(i>-1)and(i+1<PartsListBox.Count)then
 begin
  PartsListBox.Items.Exchange(i,i+1);
  ImageFrame1.exchange(i,i+1);

  if(i+1>PartsListBox.Count-1)then
   index:=PartsListBox.Count-1
  else
   index:=i+1;

  setListIndex(index);

 // PartsListBoxClick(Sender);
 end;
end;

{パーツの入れ替え}
procedure TviewForm.upBitBtnClick(Sender: TObject);
var
 i,index:Integer;
begin
 i:=PartsListBox.ItemIndex;
 if(i>0)then
 begin
  PartsListBox.Items.Exchange(i,i-1);
  ImageFrame1.exchange(i,i-1);

  if(i-1<0)then index:=0
   else index:=i-1;

   setListIndex(index);

 // PartsListBoxClick(Sender);
 end;
end;

procedure TviewForm.PartsListBoxClick(Sender: TObject);
var
 inf:TPosInf;
 info:TImageInfo;
begin
 self.lock:=true;
 inf:=ImageFrame1.getLocation(PartsListBox.ItemIndex);
 WidthUpDown.Position:=inf.w;
 HeightUpDown.Position:=inf.h;
 self.XUpDown.Position:=inf.x;
 self.YUpDown.Position:=inf.y;
 self.aUpDown.Position:=inf.alpha;
 self.PaintComboBox.ItemIndex:=inf.mode;
 self.transCheckBox.Checked:=inf.trans;

 if(PartsListBox.ItemIndex>-1)then
 begin
  info:=TImageInfo(PartsListBox.Items.Objects[PartsListBox.ItemIndex]);
  PartsListBox.Hint:=info.sectionName+'- '+info.fileName+' ['+info.flagName+']';
 end
 else
  PartsListBox.Hint:='';

 checkSelect();
 ImageFrame1.drawSelected();
 self.lock:=false;
end;

procedure TViewForm.setPosition();
var
 inf:TPosInf;
begin
 if not (self.lock)then
 begin
  inf.x:=XUpDown.Position;
  inf.y:=YUpDown.Position;
  inf.w:=WidthUpDown.Position;
  inf.h:=HeightUpDown.Position;
  inf.mode:=PaintComboBox.ItemIndex;
  inf.trans:=transCheckBox.Checked;
  inf.alpha:=aUpDown.Position;

  if(PartsListBox.Count>0)and(partsListBox.ItemIndex>-1)and
  (PartsListBox.Selected[PartsListBox.ItemIndex])then
  begin
   TImageInfo(PartsListBox.Items.Objects[PartsListBox.ItemIndex]).posInf:=inf;
   ImageFrame1.setLocation(inf);
  end;

 end;
end;

procedure TviewForm.PosXEditChange(Sender: TObject);
begin
 setPosition();
end;

procedure TviewForm.backColorLabelClick(Sender: TObject);
begin
 if(self.ColorDialog1.Execute)then
 begin
  self.setBackColor(ColorDialog1.Color);
  ImageFrame1.repaintParts();
 end;
end;

procedure TViewForm.viewTabControlChange(Sender: TObject);
var
 inf:TImageInfo;
 cd:TCardDrawer;
const
 CELL=0;
 INIT=1;
begin
 case viewTabControl.TabIndex of
  CELL:
  begin
   ImageFrame1.mode:=ifNormal;
   initFrame1.Visible:=false;
   posPanel.Visible:=true;
   partsListPanel.Visible:=true;
   animeBitBtn.Visible:=false;
   cardCB.Visible:=false;
   ImageFrame1.repaintParts();
   self.PopupMenu:=self.PopupMenu1;
   initLabel.Visible:=false;
  end;
  INIT:
  begin
   ImageFrame1.mode:=ifInit;
   partsListPanel.Visible:=false;
   posPanel.Visible:=false;
   initFrame1.Visible:=true;
   ImageFrame1.repaintParts();
   animeBitBtn.Visible:=true;
   self.PopupMenu:=nil;   
   cardCB.Visible:=true;
   inf:=initFrame1.getImageInfo();
   inc(inf.delCount);
   ImageFrame1.arrangeImage(inf);
   initLabel.Visible:=true;

   if(cardCB.Checked)then
   begin
    cd:=TCardDrawer.Create();
    cd.drawCard(ImageFrame1.ViewImage.Canvas);
    ImageFrame1.ViewImage.Repaint();
    cd.Free();
   end;

   inf.FreeEx();
  end;
 end;
end;

procedure TviewForm.backWidthEditChange(Sender: TObject);
begin
 ImageFrame1.resizeImage(backWidthUpDown.Position,backHeightUpDown.Position);
end;

(*iniFrameからイメージを受け取り、画像修飾して開放*)
procedure TViewForm.receiveInf(inf:TImageInfo);
begin
 ImageFrame1.arrangeImage(inf);
end;

(*読み込んだイメージをセット*)
procedure TViewForm.setInitInf(inf:TImageInfo);
begin
 ImageFrame1.drawlock:=true;
 ImageFrame1.resizeImage(inf.posInf.w,inf.posInf.h);

 inc(inf.delCount);
 initFrame1.setImageInfo(inf);
 ImageFrame1.drawlock:=false;
 self.lock:=true;
 self.backWidthUpDown.Position:=inf.posInf.w;
 self.backHeightUpDown.Position:=inf.posInf.h;
 self.setBackColor(inf.color);
 self.WidthUpDown.Position:=0;
 self.HeightUpDown.Position:=0;
 self.XUpDown.Position:=0;
 self.YUpDown.Position:=0;
 self.transCheckBox.Checked:=false;
 self.lock:=false;
 inf.FreeEx();
end;

(*読み込んだパーツをフレームにセット*)
procedure TViewForm.setSectionInf(inf:TImageInfo;bitmap:TBitmap);
begin
 ImageFrame1.drawlock:=true;
 inc(inf.delCount);
 PartsListBox.Items.AddObject(inf.sectionName,inf);
 self.lock:=true;
 inc(inf.delCount);
 ImageFrame1.add(inf.sectionName,createParts(inf,bitmap));
 self.lock:=false;
 ImageFrame1.drawlock:=false;
 inf.FreeEx();
end;

function TViewForm.getInitInf():TImageInfo;
begin
 result:=self.initFrame1.getImageInfo();
 result.posInf.w:=backWidthUpDown.Position;
 result.posInf.h:=backHeightUpDown.Position;
 result.color:=ImageFrame1.getBackColor;
 result.fileName:=self.initImageFileName;
 result.dirtype:=self.initDirType;
end;

function TViewForm.getSectionInf(i:Integer):TImageInfo;
begin
 if(i<PartsListBox.Items.count)then
 begin
  result:=TImageInfo(PartsListBox.Items.Objects[i]);
  inc(result.delCount);
  result.posInf:=ImageFrame1.getLocation(i);
 end
 else result:=nil;
end;

(*背景用の色*)
function TViewForm.getBackColor():TColor;
begin
 result:=ImageFrame1.getBackColor();
end;

procedure TViewForm.setBackColor(newColor:TColor);
begin
 backColorLabel.Color:=newColor;
 backColorLabel.Font.Color:=newColor xor $ffffff;
 ImageFrame1.setBackColor(newColor);
end;



procedure TViewForm.saveImage(fileName:string;savetype:byte=1);
begin
 ImageFrame1.saveImage(fileName,saveType);
end;

(*背景画像の変更*)
procedure TViewForm.setInitImage(dirType:shortInt;fileName:string;bitmap:TBitmap);
begin
 self.initDirType:=dirtype;
 self.initImageFileName:=fileName;
 self.ImageFrame1.setInitImage(bitmap);
 if not(lock)then self.ImageFrame1.repaintParts();
end;

procedure TViewForm.setInitImage(inf:TImageInfo;bitmap:TBitmap);
var
 oldLock:boolean;
begin
 oldLock:=lock;
 initDirType:=inf.dirtype;
 lock:=false;
 backWidthUpDown.Position:=inf.posInf.w;
 backHeightUpDown.Position:=inf.posInf.h;
 initImageFileName:=inf.fileName;
 ImageFrame1.setInitImage(bitmap);
 ImageFrame1.resizeImage
  (backWidthUpDown.Position,backHeightUpDown.Position);
 lock:=oldLock;
 
 if not(lock)then self.ImageFrame1.repaintParts();
end;

(*ポジション情報を上部のバーにセット*)
procedure TViewForm.setPosInfGUI(inf:TPosInf);
begin
 WidthUpDown.Position:=inf.w;
 HeightUpDown.Position:=inf.h;
 XUpDown.Position:=inf.x;
 YUpDown.Position:=inf.y;
 aUpDown.Position:=inf.alpha;
 PaintComboBox.ItemIndex:=inf.mode;
 transCheckBox.Checked:=inf.trans;
end;

procedure TViewForm.saveToClipBoard(mode:TCWFormat);
var
 cellDirector:TCWCellDirector;
 sList:TStringList;
 inf:TImageInfo;
 i:Integer;
begin

 if(mode=cfContent)or(mode=cfView)then
  begin
   cellDirector:=TCWCellDirector.create();
   sList:=TStringList.Create();
   sList.Assign(PartsListBox.Items);
   inf:=TImageInfo.create();
   inf.fileName:=initImageFileName;
   inf.posInf.x:=0;
   inf.posInf.y:=0;
   inf.posInf.w:=632;
   inf.posInf.h:=420;
   inf.posInf.trans:=false;
   sList.InsertObject(0,'init',inf);
  try
   if(mode=cfContent)then
    cellDirector.saveContent(sList)
   else
    cellDirector.saveCellView(sList);
  finally
   inf.Free();
   sList.Free();
   cellDirector.Free();
  end;
 end
 else if(mode=cfImage)then
 begin
  saveImage('',2);
 end
 else if(mode=cfCell)then
 begin
  sList:=TStringList.Create();

  for i:=0 to PartsListBox.Count-1 do
  begin
   if(PartsListBox.Selected[i])then
    sList.AddObject(PartsListBox.Items.Strings[i],PartsListBox.Items.Objects[i]);
  end;

  cellDirector:=TCWCellDirector.create();
  try
   cellDirector.saveCellParts(sList);
  finally

   sList.Free();
   cellDirector.Free();
  end;
 end;
 
end;

procedure TViewForm.loadFromClipBoard();
var
 mode:TCWFormat;
 selectWord:Word;
 //change:boolean;
begin
 mode:=TCWCellDirector.getClipFormat;


 if(mode=cfCell)then
 begin
  loadPartsFromClipBoard();
  {self.lock:=true;
  self.HeightUpDown.Position:=0;
  self.WidthUpDown.Position:=0;
  self.XUpDown.Position:=0;
  self.YUpDown.Position:=0;
  self.transCheckBox.Checked:=false;
  self.aUpDown.Position:=127;
  self.PaintComboBox.ItemIndex:=0;
  self.lock:=false;}
 end
 else if (mode=cfContent)then
 begin
  windows.MessageBeep(0);
  selectWord:=MessageDlg('編集中のデータをクリアしますか？',
   mtCustom,[mbYes,mbNo,mbCancel],0);
  case selectWord of
  mrYes:
   begin
    reset();
    loadContentFromClipBoard;
 //   PartsListBox.ItemIndex:=self.PartsListBox.Count-1;
   end;
  mrNo:
   begin
    loadContentFromClipBoard
   end;
  end;

 end;

end;

procedure TViewForm.loadPartsFromClipBoard();
var
 CellDirector:TCWCellDirector;
 list:TStrings;
 inf:TImageInfo;
 i:Integer;
begin
 cellDirector:=TCWCellDirector.create();

 list:=CellDirector.loadCellParts();
 for i:=list.Count-1 downto 0 do
 begin
  inf:=TImageInfo(list.Objects[i]);
  inc(inf.delCount);
  sendPartFromClipInfo(inf);
  inf.FreeEx();
 end;

 if(list.Count>0)then
  setListIndex(PartsListBox.Count-1);

 cellDirector.Free(); 
 list.Free();

end;

procedure TViewForm.loadContentFromClipBoard();
var
 CellDirector:TCWCellDirector;
 baseBitmap:TBitmap;
 list:TStrings;
 inf:TImageInfo;
 i:Integer;
begin

 cellDirector:=TCWCellDirector.create();
 list:=CellDirector.loadCellContent();
 if(list.Count>0)then
 begin

  baseBitmap:=TBitmap.Create();
  inf:=TImageInfo(list.Objects[0]);

  if(FileExists(pathctrl.scenarioName+'\'+inf.fileName))then
  begin
   director.basedir:=ExtractFileDir(pathctrl.scenarioName+'\'+inf.fileName);
   director.loadImage(pathctrl.scenarioName+'\'+inf.fileName,baseBitmap);
   inf.dirtype:=4;
  end
  else if(FileExists(pathctrl.baseName+'\data\table\'+inf.fileName))then
  begin
   director.basedir:=ExtractFileDir(pathctrl.baseName+'\data\table\'+inf.fileName);
   director.loadImage(pathctrl.baseName+'\data\table\'+inf.fileName,baseBitmap);
   inf.dirtype:=2
  end
  else
   baseBitmap.Canvas.FillRect(baseBitmap.Canvas.ClipRect);

  inf.sectionName:=createPartsName();
  setInitImage(inf.dirtype,inf.fileName,baseBitmap);
  baseBitmap.Free();
  inf.Free;

  for i:=1 to list.Count-1 do
  begin
   inf:=TImageInfo(list.Objects[i]);
   inc(inf.delCount);
   sendPartFromClipInfo(inf);
   inf.FreeEx();
  end;
 end;
 
  CellDirector.Free();
 // baseBitmap.ReleaseHandle;
 // baseBitmap.Free();

  list.Free();
end;



(*クリップボードでロードしたデータから画像を生成して送る*)
procedure TViewForm.sendPartFromClipInfo(inf:TImageInfo);
var
 baseBitmap:TBitmap;
begin
 baseBitmap:=TBitmap.Create();

 if(FileExists(pathctrl.scenarioName+'\'+inf.fileName))then
 begin
  director.basedir:=ExtractFileDir(pathctrl.scenarioName+'\'+inf.fileName);
  director.loadImage(pathctrl.scenarioName+'\'+inf.fileName,baseBitmap);
  inf.dirtype:=4;
 end
 else if(FileExists(pathctrl.baseName+'\data\table\'+inf.fileName))then
 begin
  director.basedir:=ExtractFileDir(pathctrl.baseName+'\data\table\'+inf.fileName);
  director.loadImage(pathctrl.baseName+'\data\table\'+inf.fileName,baseBitmap);
  inf.dirtype:=2
 end
 else
  baseBitmap.Canvas.FillRect(baseBitmap.Canvas.ClipRect);

 inf.sectionName:=createPartsName();
 inc(inf.delCount,2);
 addSectionParts(inf,baseBitmap);
 inf.FreeEx();
 baseBitmap.Free();
end;


(*コピペなどでセクション名が無いときに利用*)
function TViewForm.createPartsName():string;
var
 i:Integer;
begin
 if not(partsNameItems.Sorted)then
 partsNameItems.Sort;

 repeat
  result:='パーツ名('+IntToStr(sectionCount)+')';
  i:=partsNameItems.IndexOf(result);
  inc(sectionCount);
 until (i=-1);

end;

(*以下ＧＵＩイベント*)

procedure TViewForm.PosXEditKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if(key=VK_RETURN)and not(lock)then
  PosXEditChange(Sender);
end;

procedure TViewForm.aEditKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

 if(key=VK_RETURN)and not(lock)then
  aEditChange(Sender);
end;

procedure TViewForm.PartsListBoxKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin

  if not(lock)then
  case key of
   VK_DELETE:DelBitBtnClick(Sender);
   VK_RETURN:seekBitBtnClick(Sender);
   VK_SPACE:showMessageEntries();
  end;

end;

procedure TViewForm.backWidthEditKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if(key=VK_RETURN)and not(lock)then
  backWidthEditChange(Sender);
end;

procedure TViewForm.copyBitBtnClick(Sender: TObject);
begin
 saveToClipBoard(cfImage);
end;

procedure TViewForm.HeightUpDownClick(Sender: TObject; Button: TUDBtnType);
begin
 if not(lock)then
  setPosition();
end;

procedure TViewForm.aUpDownClick(Sender: TObject; Button: TUDBtnType);
begin
 if not(lock)then
  aEditChange(Sender);
end;

procedure TViewForm.backWidthUpDownClick(Sender: TObject;
  Button: TUDBtnType);
begin
  if not(lock)then
  backWidthEditChange(Sender);
end;

procedure TViewForm.FormDestroy(Sender: TObject);
var
 i:Integer;
begin
 director.Free;      

 for i:=0 to PartsNameItems.Count-1 do
 begin
  partsNameItems.Objects[i].Free();
 end;
 
 partsNameItems.Free();

 for i:=0 to PartsListBox.Count -1 do
 begin
  PartsListBox.Items.Objects[i].Free();
 end;
end;

procedure TViewForm.copyContentBitBtnClick(Sender: TObject);
begin
 saveToClipBoard(cfContent);
end;

procedure TViewForm.CopyCellsBitBtnClick(Sender: TObject);
begin
 self.lock:=true;
 PartsListBox.SelectAll();
 self.lock:=false;
 saveToClipBoard(cfCell);
end;

procedure TViewForm.aEditChange(Sender: TObject);
var
 old:boolean;
begin

  old:=self.lock;

 if(PaintComboBox.ItemIndex<>3)and not(lock)then
  self.lock:=true
 else
  self.lock:=false;

  self.setPosition();

  self.lock:=old;

end;


procedure TViewForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 if(key=VK_DELETE)and(viewTabControl.TabIndex=0)and not(lock)then
  DelBitBtnClick(Sender);
end;

procedure TViewForm.animeBitBtnClick(Sender: TObject);
var
 bitmap:TBitmap;
 i:Integer;
 dinf:TDirInf;
 inf:TImageInfo;
 //animeDC:hdc;
 loadFileName:string;
begin
 bitmap:=TBitmap.Create();
 self.animeBitBtn.Enabled:=false;
 try
  releaseDC(director.whandle,director.cwDC);
  director.whandle:=self.Handle;
  director.cwDC:=ImageFrame1.ViewImage.Canvas.Handle;
  director.Image:=ImageFrame1.ViewImage;
  director.animation:=true;
  director.searched:=true;
  director.isSound:=true;
  dinf.fileName:=self.initImageFileName;
  dinf.mode:=self.initDirType;
  dinf.currentFile:=pathctrl.scenarioName;
  dinf.firstDir:=pathctrl.scenarioName;
  dinf.exeDir:=pathctrl.baseName;

  loadFileName:=TTagText.serachFileName(dinf);
  director.basedir:=ExtractFileDir(loadFileName);
  director.loadImage(loadFileName,bitmap);
  director.reset();

  for i:=0 to PartsListBox.Count-1 do
  begin
   bitmap.Canvas.FillRect(self.Canvas.ClipRect);
   inf:=TImageInfo(PartsListBox.Items.Objects[i]);
   dinf.mode:=inf.dirtype;
   dinf.fileName:=inf.fileName;
   loadFileName:=TTagText.serachFileName(dinf);
   director.basedir:=ExtractFileDir(loadFileName);
   director.loadImage(loadFileName,bitmap);
   director.reset();
  end;

  director.animation:=false;
  director.whandle:=Handle;
  director.isSound:=false;
  sleep(500);
  viewTabControlChange(self);
// releaseDC(Handle,director.cwDC);
 finally
  bitmap.Free();
  self.animeBitBtn.Enabled:=true;
 end;

 
end;

procedure TViewForm.selectAllBitBtnClick(Sender: TObject);
//var
 //i:Integer;
begin
 PartsListBox.SelectAll;
 self.checkSelect();
 {for i:=0 to PartsListBox.Items.Count-1 do
 begin
  self.ImageFrame1.selectList(i,true);
 end;  }

end;

procedure TViewForm.setListIndex(index:Integer);
var
 i:Integer;
begin

 if(PartsListBox.Count>0)then
 begin

  for i:=0 to PartsListBox.Count-1 do
   PartsListBox.Selected[i]:=false;

  if(index>-1)and(index<=PartsListBox.Count-1)then
  begin
   PartsListBox.Selected[index]:=true;
   PartsListBox.ItemIndex:=index;
   PartsListBoxClick(self);
  end
  else
   PartsListBox.ItemIndex:=-1;

 end;
end;

procedure TViewForm.resizePopClick(Sender: TObject);
var
 bitmap:TBitmap;
begin
 if(PartsListBox.Count>0)and(PartsListBox.ItemIndex>-1)then
 begin
  self.lock:=true;
  bitmap:=ImageFrame1.getBitmap(PartsListBox.ItemIndex);
  self.WidthUpDown.Position:=bitmap.Width;
  self.HeightUpDown.Position:=bitmap.Height;
  self.lock:=false;
  self.setPosition;
 end;
end;

procedure TViewForm.sizePopClick(Sender: TObject);
var
 posForm:TTempPosForm;
 inf:TPosinf;
begin
 posForm:=TTempPosForm.Create(self);
 posForm.setFileName(pathctrl.baseName+'\CardWirthEditor.cfg');

 if(posForm.ShowModal=mrOK)then
 begin
  self.lock:=true;
  inf:=PosForm.getPosInf;
  WidthUpDown.Position:=inf.w;
  HeightUpDown.Position:=inf.h;
  XUpDown.Position:=inf.x;
  YUpDown.Position:=inf.y;
  transCheckBox.Checked:=inf.trans;
  self.lock:=false;
  setPosition();
 end;

 posForm.Free();
end;

procedure TViewForm.arrangeLocation(Sender: TObject);
var
 arrangeType:TArrangeType;
begin

 if(Sender=topPop)then
  arrangeType:=atTop
 else if(Sender=BottomPop)then
  arrangeType:=atBottom
 else if(Sender=LeftPop)then
  arrangeType:=atLeft
 else if(Sender=RightPop)then
  arrangeType:=atRight
 else if(Sender=sameSizePop)then
  arrangeType:=atSize
 else
  arrangeType:=atNone;

 ImageFrame1.arrangeLocation(arrangeType);

end;

procedure TCardDrawer.drawCard(canvas:TCanvas);
const
 h=130;w=95;t=285;
begin
 Canvas.Brush.Color:=clred;
 Canvas.Rectangle(Bounds(9,t,w,h));
 Canvas.Rectangle(Bounds(113,t,w,h));
 Canvas.Rectangle(Bounds(217,t,w,h));
 Canvas.Rectangle(Bounds(321,t,w,h));
 Canvas.Rectangle(Bounds(425,t,w,h));
 Canvas.Rectangle(Bounds(529,t,w,h));  
end;

procedure TViewForm.cardCBClick(Sender: TObject);
var
 cd:TCardDrawer;
 inf:TImageInfo;
begin
 if not(lock)then
 if(cardCB.Checked)then
 begin
  cd:=TCardDrawer.Create();
  cd.drawCard(ImageFrame1.ViewImage.Canvas);
  ImageFrame1.ViewImage.Repaint();
  cd.Free();
 end
 else
 begin
  inf:=initFrame1.getImageInfo();
  inc(inf.delCount);
  ImageFrame1.arrangeImage(inf);
 end;
end;

procedure TViewForm.backBasicSizeBBClick(Sender: TObject);
var
 oldLock:boolean;
begin
 oldLock:=lock;
 lock:=true;
 self.backWidthUpDown.Position:=632;
 lock:=false;
 self.backHeightUpDown.Position:=420;
// self.ImageFrame1.repaintParts();
 lock:=oldLock;
end;

procedure TViewForm.remaneParts(newName:string;index:Integer);
begin
 if(index<PartsListBox.Count)then
 begin
  PartsListBox.Items.Strings[index]:=newName;
  ImageFrame1.reNameListItme(newName,index);
 end;

end;

procedure TViewForm.showMessageEntries();
var
 dlg:TMessageEntryDlg;
begin
 if(PartsListBox.ItemIndex>-1)then
 begin

  dlg:=TMessageEntryDlg.Create(self);
  dlg.setEditText(PartsListBox.Items.Strings[PartsListBox.ItemIndex]);
  dlg.TextParent:=self;

  if dlg.ShowModal = mrOK then
  begin
   remaneParts(dlg.getNewName,PartsListBox.ItemIndex);
  end;

  dlg.Free();
 end;

end;

function TViewForm.IsExistText(text:string):boolean;
begin
 result:=PartsListBox.Items.IndexOf(text)>-1;
end;

end.
