{
     Apophysis Copyright (C) 2001-2004 Mark Townsend
     Apophysis Copyright (C) 2005-2006 Ronald Hordijk, Piotr Borys, Peter Sdobnov
     Apophysis Copyright (C) 2007-2008 Piotr Borys, Peter Sdobnov
     
     Apophysis "3D hack" Copyright (C) 2007-2008 Peter Sdobnov
     Apophysis "7X" Copyright (C) 2009-2010 Georg Kiehne
     Apophysis AV "Phoenix Edition" Copyright (C) 2021-2022 Alice V. Koryagina

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}
unit Preview;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, ControlPoint, RenderingInterface, Vcl.Menus, Vcl.Imaging.PNGimage;

type
  TPreviewForm = class(TForm)
    BackPanel: TPanel;
    Image: TImage;
    PreviewPopup: TPopupMenu;
    MakeScreenShot: TMenuItem;
    KeepFrame: TMenuItem;
    N1: TMenuItem;
    PreviewPause: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure MakeScreenShotClick(Sender: TObject);  // AV
    procedure PreviewPauseClick(Sender: TObject);  // AV
  private
  public
    cp: TControlPoint;
    Render: TRenderer;
    procedure DrawFlame;
  end;

var
  PreviewForm: TPreviewForm;

implementation

uses Main, Global, ScriptForm, Translation;

{$R *.DFM}

procedure TPreviewForm.DrawFlame;
begin
  Render.Stop;
  // AV: this is already done by cp.AdjustScale
  {
  cp.width := Image.width;
  cp.Height := Image.Height;
  }
  Render.SetCP(cp);
  Render.Render;
  Image.Picture.Bitmap.Assign(Render.GetImage);
  Application.ProcessMessages;
end;


procedure TPreviewForm.FormCreate(Sender: TObject);
begin
  self.Caption := TextByKey('preview-title');
  MakeScreenShot.Caption :=   TextByKey('main-menu-screenshot');
  KeepFrame.Caption := TextByKey('preview-keepframe');
  cp := TControlPoint.Create;
  Render := TRenderer.Create;
  PreviewPause.Caption := TextByKey('preview-pause');
end;

procedure TPreviewForm.FormDestroy(Sender: TObject);
begin
  Render.Free;
  cp.Free;
end;

procedure TPreviewForm.FormKeyPress(Sender: TObject; var Key: Char);
begin
   if ScriptEditor.btnPause.Down then ScriptEditor.btnPause.Click;  // AV
   ScriptEditor.Stopped := True;
end;

procedure TPreviewForm.MakeScreenShotClick(Sender: TObject);
var
  s, t: string;
  pic: TPNGImage;
begin
  if not DirectoryExists(ScreenShotPath) then   
  begin
	  CreateDir(AppPath + 'ScreenShots\');
	  ScreenShotPath := AppPath + 'ScreenShots\';
  end;
  t := FormatDateTime(' (MM-dd-yyyy hh-mm-ss)', Now);
  s := ScreenShotPath + 'Apophysis Animation Preview' + t + '.bmp';
  try
    if KeepFrame.Checked then
      GetFormScreenShot(s)
    else
    begin
      try
        pic := TPNGImage.Create;
        try
          pic.Assign(Image.Picture.Bitmap);
          if cp.name = '' then cp.name := RemoveExt(s) + t;
          pic.AddtEXt('ApoFlame', AnsiString(Trim(FlameToXML(cp))));
          s := ChangeFileExt(s, '.png');
          pic.SaveToFile(s);
        finally
          pic.Free;
        end;
      except
        Image.Picture.Bitmap.SaveToFile(s);
      end;
    end;
    Application.MessageBox(PChar(Format(TextByKey('common-screenshot-saved'),
      [ExtractFileName(s), ExtractFilePath(s)])), ApophysisSVN, MB_ICONINFORMATION);
  except
    Application.MessageBox(PChar(TextByKey('common-screenshot-error')), ApophysisSVN, MB_ICONERROR);
  end;
end;

procedure TPreviewForm.PreviewPauseClick(Sender: TObject);
begin
   ScriptEditor.btnPause.Click;
end;

procedure TPreviewForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if ScriptEditor.btnPause.Down then ScriptEditor.btnPause.Click;  // AV
  ScriptEditor.Stopped := True;
end;

end.

