unit Frame.MapPreview;

interface
uses
  System.Classes,
  Vcl.Controls,
  Vcl.Dialogs,
  Vcl.ExtCtrls,
  Vcl.Forms,
  Vcl.Imaging.jpeg,

  X2CLGraphicList;


type
  TMapPreviewFrame = class(TFrame)
    shpBorder: TShape;
    imgMapPreview: TImage;
    gcMapPreview: TX2GraphicContainer;
    gcMapPreviewUnknownMap: TX2GraphicContainerItem;
  public
    constructor Create(AOwner: TComponent); override;

    procedure Clear;
    procedure Load(const AMapName: string);
  end;


implementation
uses
  System.SysUtils,
  Vcl.Graphics,

  Resources;


{$R *.dfm}


{ TMapPreviewFrame }
constructor TMapPreviewFrame.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  { Having the components not aligned at design-time makes it easier to drag the frame around }
  shpBorder.Align := alClient;
  imgMapPreview.Align := alClient;

  Clear;
end;


procedure TMapPreviewFrame.Clear;
begin
  imgMapPreview.Picture.Assign(gcMapPreviewUnknownMap.Picture);
end;


procedure TMapPreviewFrame.Load(const AMapName: string);
var
  preview: TPicture;
  fileName: string;
  previewItem: TX2GraphicContainerItem;

begin
  preview := nil;

  if Length(AMapName) > 0 then
  begin
    preview := gcMapPreview.PictureByName(AMapName);
    if not Assigned(preview) then
    begin
      previewItem := TX2GraphicContainerItem.Create(Self);
      previewItem.Container := gcMapPreview;
      previewItem.PictureName := AMapName;
      preview := previewItem.Picture;

      fileName := GetAssetPath(AssetMapPreviewPath + AMapName + AssetMapPreviewExtension);
      if FileExists(fileName) then
      begin
        try
          preview.LoadFromFile(fileName);
        except
          preview.Assign(nil);
        end;
      end;
    end;
  end;

  if Assigned(preview) and Assigned(preview.Graphic) then
    imgMapPreview.Picture.Assign(preview)
  else
    Clear;
end;

end.
