unit PartsForms;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,shellAPI,
  StdCtrls, Buttons, ComCtrls, ExtCtrls, initFrames,convImages,textImages,
  JPYInterfaced, ImgList,cwCells,Clipbrd, PartsFrames,ExtAllowedForms;

type

  

  TPartsForm = class(TForm,IClipBrdDealer)
    partsInitFrame: TinitFrame;
    Panel1: TPanel;
    PartsEdit: TEdit;
    entryBitBtn: TBitBtn;
    Label1: TLabel;
    ClearBitBtn: TBitBtn;
    backBitBtn: TBitBtn;
    cellViewImage: TImage;
    DirListFrame1: TDirListFrame;
    Label2: TLabel;
    reEditButton: TButton;
    flagCB: TComboBox;
    srcBitBtn: TBitBtn;
    extBB: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure entryBitBtnClick(Sender: TObject);
    procedure cellListViewChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure backBitBtnClick(Sender: TObject);
    procedure ClearBitBtnClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure reEditButtonClick(Sender: TObject);
    procedure flagCBExit(Sender: TObject);
    procedure srcBitBtnClick(Sender: TObject);
    procedure extBBClick(Sender: TObject);
  private
    { Private 宣言 }
    FNotifyNameEvent:TCellCreateEvent;
    procedure receiveFileName(fileName:string);
    function IsSendPartsAllowed():boolean;
    function getDirtype():shortInt;
    function getImageInfo():TImageInfo;
  public
    { Public 宣言 }
    pathctrl:TPathCtrl;
    addingForm:IaddingForm;
    director:TEDDirector;
    baseBitmap,arrangedBitmap:TBitmap;
    selectFileName:string;
    selectDirtype:Integer;
    selectPosInf:TPosInf;
    hasInfo,lock:boolean;
    procedure openFolder(dirName:string);
    procedure saveToClipBoard(mode:TCWFormat);
    procedure loadFromClipBoard();
    procedure reset();
    procedure setFlagItems(items:TStrings);
    procedure receiveInf(inf:TImageInfo);
    procedure receiveParts(inf:TImageInfo);
    property  cellCreateEvent:TcellCreateEvent
             read FNotifyNameEvent write FNotifyNameEvent;

  end;



implementation

uses Dialogs;

{$R *.dfm}

(*viewFormの選択を受け取る*)
procedure TPartsForm.receiveParts(inf:TImageInfo);
var
 dinf:TDirInf;
 fileName:string;
 oldColor:TColor;
 flagIndex:Integer;
begin
 
 dinf.fileName:=inf.fileName;
 dinf.mode:=inf.dirtype;
 dinf.currentFile:=pathctrl.scenarioName;
 dinf.firstDir:=pathctrl.scenarioName;
 dinf.exeDir:=pathctrl.baseName;
 fileName:=TTagText.serachFileName(dinf);


 if(FileExists(fileName))then
 begin
  director.basedir:=extractFileDir(fileName);
  director.loadImage(fileName,baseBitmap)
 end
 else
 begin
  oldColor:=baseBitmap.Canvas.Brush.Color;
  baseBitmap.Canvas.Brush.Color:=inf.color;
  baseBitmap.Canvas.FillRect(baseBitmap.Canvas.ClipRect);
  baseBitmap.Canvas.Brush.Color:=oldColor;
 end;

 if(inf.posInf.w=-1)then
  if(inf.needClip)then
   inf.posInf.w:=inf.clip.Right-inf.clip.Left
  else
   inf.posInf.w:=baseBitmap.Width;

 if(inf.posInf.h=-1)then
  if(inf.needClip)then
   inf.posInf.w:=inf.clip.Bottom-inf.clip.Top
  else
   inf.posInf.h:=baseBitmap.Height;

 arrangedBitmap.Assign(baseBitmap);
 partsInitFrame.lock:=true;
 inc(inf.delCount);
 partsInitFrame.setImageInfo(inf);
 partsInitFrame.lock:=false;
 inc(inf.delCount);
 customizeBitmap(inf,arrangedBitmap);

 self.selectFileName:=inf.fileName;
 self.selectDirtype:=inf.dirtype;
 self.selectPosInf:=inf.posInf;
 flagIndex:=flagCB.Items.IndexOf(inf.flagName);
 if(flagIndex>-1)then
  flagCB.ItemIndex:=flagIndex
 else
  flagCB.ItemIndex:=0;
  
 PartsEdit.Text:=inf.sectionName;
 cellViewImage.Canvas.CopyRect(cellViewImage.ClientRect,
  arrangedBitmap.Canvas,arrangedBitmap.Canvas.ClipRect);
 hasInfo:=true;
 inf.FreeEx();
 self.Show();

end;

procedure TPartsForm.receiveInf(inf:TImageInfo);
begin
 arrangedBitmap.Assign(baseBitmap);
 inc(inf.delCount);
 convImages.customizeBitmap(inf,arrangedBitmap);
 self.cellViewImage.Canvas.FillRect(cellViewImage.ClientRect);
 stretchBlt(cellViewImage.Canvas.Handle,
  0,0,cellViewImage.Width,cellViewImage.Height,
  arrangedBitmap.Canvas.Handle,0,0,arrangedBitmap.Width,
  arrangedBitmap.Height,srcCopy);
 self.cellViewImage.Repaint();


 inf.FreeEx();
end;

(*パーツ位置の初期化*)
procedure TPartsForm.reset();
begin
 partsInitFrame.reset();
 self.DirListFrame1.reset();
// self.flagCB.Clear();
 cellViewImage.Canvas.FillRect(cellViewImage.ClientRect);
 arrangedBitmap.Canvas.FillRect(arrangedBitmap.Canvas.ClipRect);
 baseBitmap.Canvas.FillRect(baseBitmap.Canvas.ClipRect);
end;

procedure TPartsForm.FormCreate(Sender: TObject);
begin

  baseBitmap:=TBitmap.Create();
  baseBitmap.PixelFormat:=pf32bit;
  arrangedBitmap:=TBitmap.Create();
  arrangedBitmap.Assign(baseBitmap);
  DirListFrame1.NotifyNameEvent:=receiveFileName;



  partsInitFrame.NotifyArrangeEvent:=receiveInf;
  director:=TEDDirector.create();

  hasInfo:=False;

end;

procedure TPartsForm.backBitBtnClick(Sender: TObject);
var
 bitmap:TBitmap;
 fileName:string;
 dirtype:shortInt;
begin

// subfileName:=DirListFrame1.getFileName();
 fileName:=DirListFrame1.getFullFileName();
// pathctrl.pathName+'\'+subFileName;

 if(FileExists(fileName))then
 begin

   dirtype:=getDirtype();

  bitmap:=TBitmap.Create();
  director.basedir:=extractFileDir(fileName);
  director.loadImage(fileName,bitmap);
  addingForm.setInitImage(dirtype,DirListFrame1.getFileName(),bitmap);
  bitmap.Free();
 end;

end;


procedure TPartsForm.openFolder(dirName:string);
begin
 lock:=true;
 reset();
 DirListFrame1.reset();
 DirListFrame1.setPathctrl(pathctrl);
 DirListFrame1.setImageFileList(pathctrl.scenarioName);
 entryBitBtn.Enabled:=true;
 backBitBtn.Enabled:=true;
 clearBitBtn.Enabled:=true;
 lock:=false;
end;

procedure TPartsForm.entryBitBtnClick(Sender: TObject);
var
 inf:TImageInfo;
begin

 if(IsSendPartsAllowed)and(PartsEdit.Text<>'init')then
 begin
  inf:=self.getImageInfo();
  inc(inf.delCount);        
  inf.color:=arrangedBitmap.Canvas.Brush.Color;
  addingForm.addSectionParts(inf,arrangedBitmap);

  inf.FreeEx();
  
 end
 else windows.MessageBeep(0);  
end;

function TPartsForm.IsSendPartsAllowed():boolean;
begin
 result:=(hasInfo)
  or((PartsEdit.Text<>'')and(DirListFrame1.isDir)
  and(arrangedBitmap.Width*arrangedBitmap.Height<>0));
end;

function TPartsForm.getDirtype():shortInt;
begin
  if(hasInfo)then result:=selectDirtype
  else result:=DirListFrame1.getDirtype();
end;

procedure TPartsForm.cellListViewChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
 receiveFileName(pathctrl.pathName+'\'+item.Caption);
end;

procedure TPartsForm.receiveFileName(fileName:string);
var
 inf:TImageInfo;
begin
 if not(lock)and FileExists(fileName)then
 begin
  //ext:=AnsiLowercase(ExtractFileExt(fileName));
 { if(ext='.bmp')or(ext='.jpg')or(ext='.jpeg')or(ext='.jptx')
   or(ext='.jpy1')or(ext='.jpmp')then
   begin            }
  director.basedir:=ExtractFileDir(fileName);

  if(not director.loadImage(fileName,baseBitmap))
   and(ExtractFileExt(fileName)='.wav')then
  begin
   baseBitmap.Width:=12;
   baseBitmap.Height:=12;
   baseBitmap.Canvas.Font.Size:=9;
   baseBitmap.Canvas.FillRect(baseBitmap.Canvas.ClipRect);
   baseBitmap.Canvas.TextOut(1,1,'♪');
  end;

  if(ExtractFileExt(fileName)='.jpy1')then
   DirListFrame1.cellHint:=director.getIniComment(fileName)
  else
   DirListFrame1.cellHint:='';

  baseBitmap.PixelFormat:=pf32bit;
  inf:=PartsInitFrame.getImageInfo();
  inc(inf.delCount);
  receiveInf(inf);

  inf.FreeEx();
  hasInfo:=false;
  // end;
 end
 else
   cellViewImage.Canvas.FillRect(cellViewImage.Canvas.ClipRect);
   
end;


procedure TPartsForm.ClearBitBtnClick(Sender: TObject);
begin
 addingForm.setInitImage(-1,'',nil);
end;

procedure TPartsForm.FormDestroy(Sender: TObject);
begin
 lock:=true;
 self.DirListFrame1.eventlock:=true;
 self.DirListFrame1.NotifyNameEvent:=nil;
 director.Free();
 baseBitmap.Free();
 arrangedBitmap.Free();
end;

procedure TPartsForm.BitBtn1Click(Sender: TObject);
begin
 saveToClipBoard(cfCell);
end;

procedure TPartsForm.saveToClipBoard(mode:TCWFormat);
var
 cellDirector:TCWCellDirector;
 sList:TStringList;
 inf:TImageInfo;
begin
 if(mode=cfCell)then
 begin
  cellDirector:=TCWCellDirector.create();
  try
   cellDirector.saveCellPart(self.getImageInfo);
  finally
   cellDirector.Free();
  end;
 end
 else if(mode=cfContent)or(mode=cfView)then
 begin
  cellDirector:=TCWCellDirector.create();
  sList:=TStringList.Create();
  try
   inf:=getImageInfo();
   sList.AddObject('',inf);
   if(mode=cfContent)then
    cellDirector.saveContent(sList)
   else
    cellDirector.saveCellView(sList);

  finally
   cellDirector.Free();
   sList.Free();
   inf.Free();
  end;
 end
 else if(mode=cfImage)then
 begin
  ClipBoard.Assign(arrangedBitmap);
 end;
end;

procedure TPartsForm.loadFromClipBoard();
var
 cellDirector:TCWCellDirector;
 inf:TImageInfo;
 mode:TCWFormat;
 sList:TStrings;
 fileName:string;
begin

 mode:=TCWCellDirector.getClipFormat;

 inf:=nil;
 if(mode=cfCell)or(mode=cfContent)then
 try
  cellDirector:=TCWCellDirector.create();

  if(mode=cfCell)then
   inf:=cellDirector.loadCellPart()
  else if(mode=cfContent)then
  begin
   sList:=cellDirector.loadCellContent();
   if(sList.Count>0)then
    inf:=TImageInfo(sList.Objects[0]);
   sList.Free();
  end;

  if(inf<>nil)then
  begin
   self.selectFileName:=inf.fileName;
   

   if(FileExists(pathctrl.scenarioName+'\'+inf.fileName))then
   begin
    self.selectDirtype:=4;
    fileName:=pathctrl.scenarioName+'\'+inf.fileName;
    director.basedir:=extractFileDir(fileName);
    director.loadImage(fileName,baseBitmap);
   end
   else if(FileExists(pathctrl.baseName+'\data\table\'+inf.fileName))then
   begin
    self.selectDirtype:=2;
    fileName:=pathctrl.baseName+'\data\table\'+inf.fileName;
    director.basedir:=extractFileDir(fileName);
    director.loadImage(fileName,baseBitmap);
   end
   else
   begin
    baseBitmap.Width:=inf.posInf.w;
    baseBitmap.Height:=inf.posInf.h;
    baseBitmap.Canvas.FillRect(baseBitmap.Canvas.ClipRect);
   end;



   self.hasInfo:=true;
   partsInitFrame.reset();
   arrangedBitmap.Assign(baseBitmap);
   selectPosInf:=inf.posInf;
   cellViewImage.Canvas.CopyRect(cellViewImage.ClientRect,
   baseBitmap.Canvas,baseBitmap.Canvas.ClipRect);
  end;
  
  finally
   cellDirector.Free();
   inf.Free();
  end;

end;

(*パーツの情報を作り出す*)
function TPartsForm.getImageInfo():TImageInfo;
begin
  result:=PartsInitFrame.getImageInfo();
  inc(result.delCount);
  result.sectionName:=PartsEdit.Text;
  result.flagName:=flagCB.Items.Strings[flagCB.itemIndex];

  if(hasInfo)then
  begin
   result.fileName:=self.selectFileName;
   result.posInf:=self.selectPosInf;
   result.dirtype:=self.selectDirtype;
  end
  else
  begin
   result.dirtype:=getDirtype();
   result.fileName:=DirListFrame1.getFileName();
   result.posInf.w:=arrangedBitmap.Width;
   result.posInf.h:=arrangedBitmap.Height;
  end;
end;


procedure TPartsForm.BitBtn2Click(Sender: TObject);
begin
 saveToClipBoard(cfImage);
end;

procedure TPartsForm.BitBtn3Click(Sender: TObject);
begin
 loadFromClipBoard();
end;

procedure TPartsForm.reEditButtonClick(Sender: TObject);
var
 fileName:string;
 selectWord:Word;
begin
 fileName:=DirListFrame1.getFullFileName();
 if(Assigned(cellCreateEvent))and(fileExists(fileName))and
 (AnsiLowerCase(extractFileExt(fileName))='.jpy1')then
 begin
  windows.MessageBeep(0);
  selectWord:=MessageDlg('編集中のデータをクリアしますか？',
   mtCustom,[mbYes,mbNo,mbCancel],0);
  case selectWord of
  mrYes:
   begin
    pathctrl.nowFileName:=fileName;
    cellCreateEvent(fileName,true);
   end;
  mrNo:
   begin
    cellCreateEvent(fileName,false);
   end;
  end;

   
  end
 else windows.MessageBeep(0);
end;

procedure TPartsForm.setFlagItems(items:TStrings);
begin
 flagCB.Items.Clear;
 flagCB.Items.Assign(items);
 flagCB.Items.Insert(0,'');
end;

procedure TPartsForm.flagCBExit(Sender: TObject);
begin                                        
 self.flagCB.Text:=self.flagCB.Items.Strings[self.flagCB.ItemIndex];
end;

procedure TPartsForm.srcBitBtnClick(Sender: TObject);
var
 fileName:string;
begin
 fileName:=DirListFrame1.getFullFileName();
 shellExecute(self.Handle,'open',PChar(fileName),'','',SW_SHOWNORMAL);
end;

procedure TPartsForm.extBBClick(Sender: TObject);
begin
 DirListFrame1.exeExtAllowedDlg();
end;

end.
