unit Vis2D_TextSettings;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, VisCtl2D,
  TextEditor, ColorContainer, Vcl.StdCtrls, ScrollBoxEx, UxPanel, Vcl.ExtCtrls,
  DoublePoint;

type
  TVis2D_TextSettingsFrm = class(TDrawableOptionsFrm)
    pnToolbar: TUxClient;
    btnText: TUxButton;
    btnPosition: TUxButton;
    btnStyle: TUxButton;
    pnStyle: TPanel;
    pnText: TPanel;
    pnPosition: TPanel;
    btnApply: TUxButton;
    ScrollBox1: TScrollBoxEx;
    eText: TTextEditor;
    Label8: TLabel;
    eAnchorX: TTextEditor;
    eAnchorY: TTextEditor;
    Label17: TLabel;
    pnTextFont: TPanel;
    btnTextFont: TUxButton;
    Label1: TLabel;
    Label2: TLabel;
    btnATopLeft: TUxButton;
    btnATop: TUxButton;
    btnATopRight: TUxButton;
    btnALeft: TUxButton;
    btnACenter: TUxButton;
    btnARight: TUxButton;
    btnABottomLeft: TUxButton;
    btnABottom: TUxButton;
    btnABottomRight: TUxButton;
    rbAutoSize: TRadioButton;
    lblPadding: TLabel;
    ePadding: TTextEditor;
    rbFixedSize: TRadioButton;
    lblWidth: TLabel;
    eWidth: TTextEditor;
    lblHeight: TLabel;
    eHeight: TTextEditor;
    btnTCenter: TUxButton;
    btnTBottomRight: TUxButton;
    btnTBottom: TUxButton;
    btnTBottomLeft: TUxButton;
    btnTRight: TUxButton;
    btnTLeft: TUxButton;
    btnTTopRight: TUxButton;
    btnTTop: TUxButton;
    btnTTopLeft: TUxButton;
    Label6: TLabel;
    lblTextAnchor: TLabel;
    Panel1: TPanel;
    rbPixels: TRadioButton;
    rbSpaceUnits: TRadioButton;
    cbZoomable: TCheckBox;
    cbFramed: TCheckBox;
    procedure CallUpdateDrawable(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SetPage(Sender: TObject);
    procedure ScrollBox1Enter(Sender: TObject);
    procedure ScrollBox1Exit(Sender: TObject);
    procedure btnTextFontClick(Sender: TObject);
    procedure SetAnchor(Sender: TObject);
    procedure SetTextAnchor(Sender: TObject);
    procedure SizeRadioButtonClicked(Sender: TObject);
  strict private
    FPageButtons: TArray<TUxButton>;
    FStyle: TDrawableOptionsFrm;
  strict protected
    procedure Initialize; override;
    procedure UpdateDrawable; override;
  public

  end;

implementation

uses
  Math, Vis2D_ElementSettings, Generics.Collections;

{$R *.dfm}

{ TVis2D_TextSettingsFrm }

procedure TVis2D_TextSettingsFrm.btnTextFontClick(Sender: TObject);
begin
  with TFontDialog.Create(nil) do
    try
      Font := pnTextFont.Font;
      if Execute then
      begin
        pnTextFont.Font := Font;
        pnTextFont.Caption := Format('%s, %d pt', [Font.Name, Font.Size]);
        UpdateDrawable;
      end;
    finally
      Free;
    end;
end;

procedure TVis2D_TextSettingsFrm.CallUpdateDrawable(Sender: TObject);
begin
  UpdateDrawable;
end;

procedure TVis2D_TextSettingsFrm.FormCreate(Sender: TObject);
var
  Text: TText;
begin

  Text := FDrawable as TText;

  btnText.Tag := NativeInt(pnText);
  btnPosition.Tag := NativeInt(pnPosition);
  btnStyle.Tag := NativeInt(pnStyle);
  FPageButtons := [btnText, btnPosition, btnStyle];

  SetPage(btnText);

  FStyle := TVis2D_ElementSettingsFrm.Create(Self, Text.Style);
  FStyle.Parent := pnStyle;
  FStyle.BorderStyle := bsNone;
  FStyle.Align := alClient;
  FStyle.Show;

end;

procedure TVis2D_TextSettingsFrm.Initialize;
var
  Text: TText;
  i: Integer;
begin

  inherited;

  Text := FDrawable as TText;

  eText.PlainText := Text.Text;
  pnTextFont.Font := Text.Font;
  pnTextFont.Caption := Format('%s, %d pt', [pnTextFont.Font.Name, pnTextFont.Font.Size]);

  cbZoomable.Checked := Text.ZoomText;
  cbFramed.Checked := Text.Boxed;

  eAnchorX.PlainText := Text.Position.X.ToString;
  eAnchorY.PlainText := Text.Position.Y.ToString;

  for i := 0 to ScrollBox1.ControlCount - 1 do
    if ScrollBox1.Controls[i] is TUxButton then
      if ScrollBox1.Controls[i].Tag >= 10 then
        TUxButton(ScrollBox1.Controls[i]).Down := ScrollBox1.Controls[i].Tag = 10 + Ord(Text.TextAnchorPoint)
      else
        TUxButton(ScrollBox1.Controls[i]).Down := ScrollBox1.Controls[i].Tag = Ord(Text.AnchorPoint);

  rbAutoSize.Checked := not Text.UseRect;
  rbFixedSize.Checked := Text.UseRect;
  eWidth.PlainText := Text.Width.ToString;
  eHeight.PlainText := Text.Height.ToString;
  ePadding.PlainText := Text.Padding.ToString;
  rbPixels.Checked := not Text.ZoomRect;
  rbSpaceUnits.Checked := Text.ZoomRect;

end;

procedure TVis2D_TextSettingsFrm.ScrollBox1Enter(Sender: TObject);
begin
  btnApply.Default := True;
end;

procedure TVis2D_TextSettingsFrm.ScrollBox1Exit(Sender: TObject);
begin
  btnApply.Default := False;
end;

procedure TVis2D_TextSettingsFrm.SetAnchor(Sender: TObject);
var
  i: Integer;
begin
  if Sender is TUxButton then
  begin
    for i := 0 to ScrollBox1.ControlCount - 1 do
      if (ScrollBox1.Controls[i] is TUxButton) and (ScrollBox1.Controls[i].Tag < 10) then
        TUxButton(ScrollBox1.Controls[i]).Down := ScrollBox1.Controls[i] = Sender;
    UpdateDrawable;
  end;
end;

procedure TVis2D_TextSettingsFrm.SetPage(Sender: TObject);
var
  B: TUxButton;
  P: TPanel;
begin
  for B in FPageButtons do
  begin
    if B.Tag = 0 then
      Continue;
    P := TObject(B.Tag) as TPanel;
    P.Align := alClient;
    P.Visible := B = Sender;
    B.Down := B = Sender;
    if (B = Sender) and Visible and P.Visible and P.CanFocus then
      P.SetFocus;
  end;
end;

procedure TVis2D_TextSettingsFrm.SetTextAnchor(Sender: TObject);
var
  i: Integer;
begin
  if Sender is TUxButton then
  begin
    for i := 0 to ScrollBox1.ControlCount - 1 do
      if (ScrollBox1.Controls[i] is TUxButton) and (ScrollBox1.Controls[i].Tag >= 10) then
        TUxButton(ScrollBox1.Controls[i]).Down := ScrollBox1.Controls[i] = Sender;
    UpdateDrawable;
  end;
end;

procedure TVis2D_TextSettingsFrm.SizeRadioButtonClicked(Sender: TObject);
var
  AutoSize, FixedSize: Boolean;
begin

  AutoSize := rbAutoSize.Checked;
  FixedSize := rbFixedSize.Checked;

  lblPadding.Enabled := AutoSize;
  ePadding.Enabled := AutoSize;

  lblTextAnchor.Enabled := FixedSize;
  btnTTopLeft.Enabled := FixedSize;
  btnTTop.Enabled := FixedSize;
  btnTTopRight.Enabled := FixedSize;
  btnTLeft.Enabled := FixedSize;
  btnTCenter.Enabled := FixedSize;
  btnTRight.Enabled := FixedSize;
  btnTBottomLeft.Enabled := FixedSize;
  btnTBottom.Enabled := FixedSize;
  btnTBottomRight.Enabled := FixedSize;
  lblWidth.Enabled := FixedSize;
  eWidth.Enabled := FixedSize;
  lblHeight.Enabled := FixedSize;
  eHeight.Enabled := FixedSize;
  rbPixels.Enabled := FixedSize;
  rbSpaceUnits.Enabled := FixedSize;

  UpdateDrawable;

end;

procedure TVis2D_TextSettingsFrm.UpdateDrawable;
var
  Text: TText;
begin

  if not Initialized then
    Exit;

  Text := FDrawable as TText;

  Text.Text := eText.PlainText;
  Text.Font := pnTextFont.Font;
  Text.ZoomText := cbZoomable.Checked;
  Text.Boxed := cbFramed.Checked;

  Text.Position := TPointD.Create(
    StrToFloat(eAnchorX.PlainText),
    StrToFloat(eAnchorY.PlainText)
  );

  if btnATopLeft.Down then
    Text.AnchorPoint := apTopLeft
  else if btnATop.Down then
    Text.AnchorPoint := apTop
  else if btnATopRight.Down then
    Text.AnchorPoint := apTopRight
  else if btnALeft.Down then
    Text.AnchorPoint := apLeft
  else if btnACenter.Down then
    Text.AnchorPoint := apCenter
  else if btnARight.Down then
    Text.AnchorPoint := apRight
  else if btnABottomLeft.Down then
    Text.AnchorPoint := apBottomLeft
  else if btnABottom.Down then
    Text.AnchorPoint := apBottom
  else if btnABottomRight.Down then
    Text.AnchorPoint := apBottomRight
  else
    Text.AnchorPoint := apTopLeft;

  if btnTTopLeft.Down then
    Text.TextAnchorPoint := apTopLeft
  else if btnTTop.Down then
    Text.TextAnchorPoint := apTop
  else if btnTTopRight.Down then
    Text.TextAnchorPoint := apTopRight
  else if btnTLeft.Down then
    Text.TextAnchorPoint := apLeft
  else if btnTCenter.Down then
    Text.TextAnchorPoint := apCenter
  else if btnTRight.Down then
    Text.TextAnchorPoint := apRight
  else if btnTBottomLeft.Down then
    Text.TextAnchorPoint := apBottomLeft
  else if btnTBottom.Down then
    Text.TextAnchorPoint := apBottom
  else if btnTBottomRight.Down then
    Text.TextAnchorPoint := apBottomRight
  else
    Text.TextAnchorPoint := apTopLeft;

  Text.UseRect := rbFixedSize.Checked;

  Text.Width := StrToFloat(eWidth.PlainText);
  Text.Height := StrToFloat(eHeight.PlainText);
  Text.Padding := StrToInt(ePadding.PlainText);

  Text.ZoomRect := rbSpaceUnits.Checked;

  inherited;

end;

end.
