unit Unit1;

interface

uses
   System.SysUtils, System.Types, System.UITypes, System.Classes,
   System.Variants, System.ImageList, FMX.ImgList, FMX.StdCtrls, FMX.Controls,
   FMX.Controls.Presentation, System.IOUtils, FMX.MediaLibrary,
   FMX.Types, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Platform,
   FMX.Gestures, FMX.Objects, System.Generics.Collections, IdGlobal;

type
   TForm1 = class(TForm)
      btnAddArrow: TButton;
      Image1: TImage;
      btnHideControls: TButton;
      TrackBar1: TTrackBar;
      btnRemoveSelectedArrow: TButton;
      btnSavePicAs: TButton;
      ImageList1: TImageList;
      Switch1: TSwitch;
      Label1: TLabel;
      procedure FormCreate(Sender: TObject);
      procedure btnAddArrowClick(Sender: TObject);
      procedure TrackBar1Change(Sender: TObject);
      procedure btnHideControlsClick(Sender: TObject);
      procedure btnRemoveSelectedArrowClick(Sender: TObject);
      procedure btnSavePicAsClick(Sender: TObject);
   private
      { Private declarations }
      arrows: TList<TSelection>;
   public
      { Public declarations }
      pickedSelection: TSelection;

      procedure triggerSelections(Visible: Boolean);
      procedure onSelectionClicked(Sender: TObject);
   end;

var
   Form1: TForm1;

implementation

{$R *.fmx}
{$R *.iPhone4in.fmx IOS}

procedure TForm1.btnAddArrowClick(Sender: TObject);
var
   newSelection: TSelection;
   newGlyph: TGlyph;
begin
   newSelection := TSelection.Create(self);
   newSelection.GripSize := 10;
   newSelection.Width := 113;
   newSelection.Height := 113;

   newSelection.Position.X := (self.Width - 113) / 2;
   newSelection.Position.Y := (self.Height - 113) / 2;

   newGlyph := TGlyph.Create(self);
   newSelection.AddObject(newGlyph);
   newGlyph.Align := TAlignLayout.Client;
   newGlyph.Images := self.ImageList1;
   newGlyph.ImageIndex := 1;

   self.Image1.AddObject(newSelection);
   self.arrows.Add(newSelection);

   newSelection.OnClick := self.onSelectionClicked;
   self.onSelectionClicked(newSelection);
end;

procedure TForm1.btnHideControlsClick(Sender: TObject);
begin
   self.triggerSelections(False);
end;

procedure TForm1.btnRemoveSelectedArrowClick(Sender: TObject);
begin
   if Assigned(self.pickedSelection) then begin
      self.arrows.Remove(self.pickedSelection);
      self.Image1.RemoveObject(self.pickedSelection);

      if self.arrows.Count > 0 then begin
         self.onSelectionClicked(self.arrows.Items[self.arrows.Count - 1]);
      end
      else begin
         self.triggerSelections(False);
         self.btnRemoveSelectedArrow.Enabled := self.arrows.Count > 0;
      end;
   end;
end;

procedure TForm1.btnSavePicAsClick(Sender: TObject);
var
   visibles: array [0 .. 7] of Boolean;
   screenShot: TBitMap;
   filename, ext: string;
   saveParams: TBitmapCodecSaveParams;
   pSaveParam: PBitmapCodecSaveParams;
   Service: IFMXPhotoLibrary;
begin
{$REGION 'Hide all edit controls'}
   visibles[0] := self.btnAddArrow.Visible;
   visibles[1] := self.btnHideControls.Visible;
   visibles[2] := self.btnRemoveSelectedArrow.Visible;
   visibles[3] := self.btnSavePicAs.Visible;
   visibles[4] := self.TrackBar1.Visible;

   if Assigned(self.pickedSelection) then begin
      visibles[5] := self.pickedSelection.HideSelection;
      self.pickedSelection.HideSelection := True;
   end
   else
      visibles[5] := False;

   visibles[6] := self.Label1.Visible;
   visibles[7] := self.Switch1.Visible;

   self.btnAddArrow.Visible := False;
   self.btnHideControls.Visible := False;
   self.btnRemoveSelectedArrow.Visible := False;
   self.btnSavePicAs.Visible := False;
   self.TrackBar1.Visible := False;
{$ENDREGION}
   screenShot := self.Image1.MakeScreenshot.CreateThumbnail(1024, 768);

   if self.Switch1.IsChecked then begin
{$IFDEF iOS}
      if TPlatformServices.Current.SupportsPlatformService(IFMXPhotoLibrary,
          Service) then BEGIN
         Service.AddImageToSavedPhotosAlbum(screenShot);
         ShowMessage('圖片已儲存到相簿');
      END;
{$ENDIF}
   end
   else begin
      filename := System.IOUtils.TPath.GetTempFileName;

      filename := ExtractFileName(filename);
      ext := ExtractFileExt(filename);
      filename := Fetch(filename, ext);
      filename := filename + '.jpg';
      filename := System.IOUtils.TPath.Combine
          (System.IOUtils.TPath.GetDocumentsPath, filename);

      saveParams.Quality := 100;
      pSaveParam := @saveParams;
      screenShot.SaveToFile(filename, pSaveParam);
      ShowMessage('Pic save as ' + filename);
   end;

{$REGION 'Show Controls'}
   self.btnAddArrow.Visible := visibles[0];
   self.btnHideControls.Visible := visibles[1];
   self.btnRemoveSelectedArrow.Visible := visibles[2];
   self.btnSavePicAs.Visible := visibles[3];
   self.TrackBar1.Visible := visibles[4];

   if Assigned(self.pickedSelection) then
      self.pickedSelection.HideSelection := visibles[5];

   self.Label1.Visible := visibles[6];
   self.Switch1.Visible := visibles[7];
{$ENDREGION}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   arrows := TList<TSelection>.Create;
   pickedSelection := nil;

   self.triggerSelections(False);
   self.Image1.Visible := True;

{$IFDEF iOS}
   self.Label1.Visible := True;
   self.Switch1.Visible := True;
{$ENDIF}
end;

procedure TForm1.onSelectionClicked(Sender: TObject);
var
   enumedSelection: TSelection;
begin
   pickedSelection := Sender as TSelection;

   for enumedSelection in self.arrows do begin
      enumedSelection.HideSelection := True
   end;

   pickedSelection.HideSelection := False;
   self.TrackBar1.Value := pickedSelection.RotationAngle;

   self.btnRemoveSelectedArrow.Enabled := True;
   self.triggerSelections(True);
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
   if Assigned(self.pickedSelection) then begin
      self.pickedSelection.RotationAngle := self.TrackBar1.Value;
   end;
end;

procedure TForm1.triggerSelections(Visible: Boolean);
begin
   self.TrackBar1.Visible := Visible;
   self.btnRemoveSelectedArrow.Visible := Visible;
   self.btnHideControls.Visible := Visible;
   self.btnSavePicAs.Visible := Visible;

   self.Label1.Visible := Visible;
   self.Switch1.Visible := Visible;

   if Assigned(self.pickedSelection) then
      pickedSelection.HideSelection := not Visible;
end;

end.
