unit Vis2D_AxisSettings;

interface

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

type
  TVis2D_AxisSettingsFrm = class(TDrawableOptionsFrm)
    btnApply: TUxButton;
    ScrollBox1: TScrollBoxEx;
    Label1: TLabel;
    lblRangeFrom: TLabel;
    eRangeFrom: TTextEditor;
    lblRangeTo: TLabel;
    eRangeTo: TTextEditor;
    cbRangeUnlimited: TCheckBox;
    Label2: TLabel;
    cbVisible: TCheckBox;
    Label3: TLabel;
    cbTicksVisible: TCheckBox;
    lblTickDistance: TLabel;
    eTickDistance: TTextEditor;
    Label5: TLabel;
    eTickLength: TTextEditor;
    Label6: TLabel;
    eTickWidth: TTextEditor;
    Label7: TLabel;
    ccTicks: TColorContainer;
    Label8: TLabel;
    cbTickSide: TComboBox;
    Label9: TLabel;
    cbNumbersVisible: TCheckBox;
    lblNumberDistance: TLabel;
    eNumberDistance: TTextEditor;
    Label11: TLabel;
    eNumberFormat: TTextEditor;
    Label12: TLabel;
    pnNumberFont: TPanel;
    btnNumberFont: TUxButton;
    Label13: TLabel;
    ccAxis: TColorContainer;
    Label14: TLabel;
    Label15: TLabel;
    eText: TTextEditor;
    Label16: TLabel;
    eTextOffset: TTextEditor;
    Label17: TLabel;
    pnTextFont: TPanel;
    btnTextFont: TUxButton;
    cbAutomatic: TCheckBox;
    Label4: TLabel;
    eAxisWidth: TTextEditor;
    cbTextRotate: TCheckBox;
    lblAxisName: TLabel;
    Label10: TLabel;
    Label18: TLabel;
    ePosition: TTextEditor;
    procedure cbRangeUnlimitedClick(Sender: TObject);
    procedure btnApplyClick(Sender: TObject);
    procedure CallUpdateDrawable(Sender: TObject);
    procedure cbAutomaticClick(Sender: TObject);
    procedure btnNumberFontClick(Sender: TObject);
    procedure btnTextFontClick(Sender: TObject);
    procedure ScrollBox1Enter(Sender: TObject);
    procedure ScrollBox1Exit(Sender: TObject);
  protected
    procedure Initialize; override;
    procedure UpdateDrawable; override;
  public
  end;

  TVis2D_AxesSettingsFrm = class(TDrawableOptionsFrm)
  strict protected
    FX, FY: TVis2D_AxisSettingsFrm;
    procedure Initialize; override;
  public
    constructor Create(AOwner: TComponent; ADrawable: TDrawable); override;
  end;

implementation

{$R *.dfm}

procedure TVis2D_AxisSettingsFrm.btnApplyClick(Sender: TObject);
begin
  UpdateDrawable;
end;

procedure TVis2D_AxisSettingsFrm.cbAutomaticClick(Sender: TObject);
begin
  eTickDistance.Enabled := not cbAutomatic.Checked;
  lblTickDistance.Enabled := not cbAutomatic.Checked;
  eNumberDistance.Enabled := not cbAutomatic.Checked;
  lblNumberDistance.Enabled := not cbAutomatic.Checked;
  UpdateDrawable;
end;

procedure TVis2D_AxisSettingsFrm.cbRangeUnlimitedClick(Sender: TObject);
begin
  eRangeFrom.Enabled := not cbRangeUnlimited.Checked;
  eRangeTo.Enabled := not cbRangeUnlimited.Checked;
  lblRangeFrom.Enabled := not cbRangeUnlimited.Checked;
  lblRangeTo.Enabled := not cbRangeUnlimited.Checked;
  UpdateDrawable;
end;

procedure TVis2D_AxisSettingsFrm.Initialize;
var
  Axis: TAxis;
begin

  inherited;

  Axis := FDrawable as TAxis;

  lblAxisName.Caption := Axis.Name;

  cbVisible.Checked := Axis.Visible;
  ccAxis.Color := Axis.Color;
  eAxisWidth.PlainText := Axis.Width.ToString;
  cbRangeUnlimited.Checked := not Axis.Limited;
  eRangeFrom.PlainText := Axis.AxMin.ToString;
  eRangeTo.PlainText := Axis.AxMax.ToString;
  cbTicksVisible.Checked := Axis.TicksVisible;
  cbAutomatic.Checked := Axis.Automatic;
  eTickDistance.PlainText := Axis.TickDistance.ToString;
  eTickLength.PlainText := Axis.TickLength.ToString;
  eTickWidth.PlainText := Axis.TickWidth.ToString;
  ccTicks.Color := Axis.TickColor;
  cbTickSide.ItemIndex := Ord(Axis.TickSide);
  cbNumbersVisible.Checked := Axis.NumbersVisible;
  eNumberDistance.PlainText := Axis.NumberDistance.ToString;
  eNumberFormat.PlainText := Axis.NumberFormat;
  pnNumberFont.Font := Axis.NumberFont;
  pnNumberFont.Caption := Format('%s, %d pt', [pnNumberFont.Font.Name, pnNumberFont.Font.Size]);
  eText.PlainText := Axis.Text;
  eTextOffset.PlainText := Axis.TextOffset.ToString;
  pnTextFont.Font := Axis.TextFont;
  pnTextFont.Caption := Format('%s, %d pt', [pnTextFont.Font.Name, pnTextFont.Font.Size]);
  cbTextRotate.Enabled := Axis is TVerticalAxis;
  if Axis is TVerticalAxis then
    cbTextRotate.Checked := TVerticalAxis(Axis).RotateAxisText;
  ePosition.PlainText := Axis.Position.ToString;

end;

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

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

procedure TVis2D_AxisSettingsFrm.UpdateDrawable;
var
  Axis: TAxis;
begin

  if not Initialized then
    Exit;

  Axis := FDrawable as TAxis;

  Axis.Visible := cbVisible.Checked;
  Axis.Color := ccAxis.Color;
  Axis.Width := StrToInt(eAxisWidth.PlainText);
  if cbRangeUnlimited.Checked then
  begin
    Axis.AxMin := 0.0;
    Axis.AxMax := 0.0;
  end
  else
  begin
    Axis.AxMin := StrToFloat(eRangeFrom.PlainText);
    Axis.AxMax := StrToFloat(eRangeTo.PlainText);
  end;
  Axis.TicksVisible := cbTicksVisible.Checked;
  Axis.Automatic := cbAutomatic.Checked;
  Axis.TickDistance := StrToFloat(eTickDistance.PlainText);
  Axis.TickLength := StrToInt(eTickLength.PlainText);
  Axis.TickWidth := StrToInt(eTickWidth.PlainText);
  Axis.TickColor := ccTicks.Color;
  Axis.TickSide := TTickSide(cbTickSide.ItemIndex);
  Axis.NumbersVisible := cbNumbersVisible.Checked;
  Axis.NumberDistance := StrToFloat(eNumberDistance.PlainText);;
  Axis.NumberFormat := eNumberFormat.PlainText;
  Axis.NumberFont := pnNumberFont.Font;
  Axis.Text := eText.PlainText;
  Axis.TextOffset := StrToInt(eTextOffset.PlainText);
  Axis.TextFont := pnTextFont.Font;
  if Axis is TVerticalAxis then
    TVerticalAxis(Axis).RotateAxisText := cbTextRotate.Checked;
  Axis.Position := StrToFloat(ePosition.PlainText);

  inherited;

end;

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

procedure TVis2D_AxisSettingsFrm.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_AxisSettingsFrm.CallUpdateDrawable(Sender: TObject);
begin
  UpdateDrawable;
end;

{ TVis2D_AxesSettingsFrm }

constructor TVis2D_AxesSettingsFrm.Create(AOwner: TComponent;
  ADrawable: TDrawable);
var
  Axes: TAxes;
begin

  FDrawable := ADrawable;
  inherited CreateNew(AOwner);

  Axes := ADrawable as TAxes;

  FX := TVis2D_AxisSettingsFrm.Create(Self, Axes.X);
  FY := TVis2D_AxisSettingsFrm.Create(Self, Axes.Y);

  FX.BorderStyle := bsNone;
  FX.Parent := Self;

  FY.BorderStyle := bsNone;
  FY.Parent := Self;

  FX.Align := alLeft;
  FY.Align := alLeft;

  ClientWidth := FX.Width + FY.Width;
  ClientHeight := FX.ClientHeight;

  Constraints.MinWidth := Width;
  Constraints.MaxWidth := Width;

  FX.Visible := True;
  FY.Visible := True;

  Initialize;
  FInitialized := True;

end;

procedure TVis2D_AxesSettingsFrm.Initialize;
begin
  inherited;
  FX.Initialize;
  FY.Initialize;
end;

end.
