unit Vis2D_VectorFieldSettings;

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;

type
  TVis2D_VectorFieldSettingsFrm = class(TDrawableOptionsFrm)
    btnApply: TUxButton;
    ScrollBox1: TScrollBoxEx;
    Label2: TLabel;
    Label13: TLabel;
    Label4: TLabel;
    lblVectorFieldName: TLabel;
    ccArea: TColorContainer;
    eOpacity: TTextEditor;
    Label1: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    ccBorder: TColorContainer;
    eBorderWidth: TTextEditor;
    Label6: TLabel;
    Label8: TLabel;
    Label11: TLabel;
    eScale: TTextEditor;
    Label12: TLabel;
    cbUseMag: TCheckBox;
    procedure ScrollBox1Enter(Sender: TObject);
    procedure ScrollBox1Exit(Sender: TObject);
    procedure btnApplyClick(Sender: TObject);
    procedure CallUpdateDrawable(Sender: TObject);
  strict protected
    procedure Initialize; override;
    procedure UpdateDrawable; override;
  public

  end;

implementation

uses
  Math;

{$R *.dfm}

{ TVis2D_ElementSettingsFrm }

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

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

procedure TVis2D_VectorFieldSettingsFrm.Initialize;
var
  VectorField: TVectorField;
begin

  inherited;

  VectorField := FDrawable as TVectorField;

  cbUseMag.Checked := VectorField.UseMagnitude;
  eScale.PlainText := Round(100 * VectorField.ArrowScale).ToString;
  ccArea.Color := VectorField.Style.Color;
  eOpacity.PlainText := Round(100 * VectorField.Style.Opacity / VectorField.Style.Opacity.MaxValue).ToString;
  ccBorder.Color := VectorField.Style.BorderColor;
  eBorderWidth.PlainText := VectorField.Style.BorderWidth.ToString;

end;

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

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

procedure TVis2D_VectorFieldSettingsFrm.UpdateDrawable;
var
  VectorField: TVectorField;
begin

  if not Initialized then
    Exit;

  VectorField := FDrawable as TVectorField;

  if not VectorField.Name.IsEmpty then
    lblVectorFieldName.Caption := VectorField.Name;

  VectorField.UseMagnitude := cbUseMag.Checked;
  VectorField.ArrowScale := StrToFloat(eScale.PlainText) / 100;
  VectorField.Style.Color := ccArea.Color;
  VectorField.Style.Opacity := EnsureRange(Round(VectorField.Style.Opacity.MaxValue * StrToFloat(eOpacity.PlainText) / 100), 0, VectorField.Style.Opacity.MaxValue);
  VectorField.Style.BorderColor := ccBorder.Color;
  VectorField.Style.BorderWidth := StrToInt(eBorderWidth.PlainText);

  inherited;  

end;

end.
