unit Vis2D_CategorySettings;

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_CategorySettingsFrm = class(TDrawableOptionsFrm)
    btnApply: TUxButton;
    ScrollBox1: TScrollBoxEx;
    Label2: TLabel;
    Label13: TLabel;
    Label4: TLabel;
    lblCategoryName: TLabel;
    ccArea: TColorContainer;
    eOpacity: TTextEditor;
    Label1: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    ccBorder: TColorContainer;
    eBorderWidth: TTextEditor;
    Label6: TLabel;
    Label7: TLabel;
    Label9: TLabel;
    eDisplacement: TTextEditor;
    Label8: TLabel;
    Label10: TLabel;
    eLabel: TTextEditor;
    Label11: TLabel;
    eValue: TTextEditor;
    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_CategorySettingsFrm.btnApplyClick(Sender: TObject);
begin
  UpdateDrawable;
end;

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

procedure TVis2D_CategorySettingsFrm.Initialize;
var
  Category: TCategoryElement;
begin

  inherited;

  Category := FDrawable as TCategoryElement;

  eLabel.PlainText := Category.&Label;
  eValue.PlainText := Category.Value.ToString;
  ccArea.Color := Category.Style.Color;
  eOpacity.PlainText := Round(100 * Category.Style.Opacity / Category.Style.Opacity.MaxValue).ToString;
  ccBorder.Color := Category.Style.BorderColor;
  eBorderWidth.PlainText := Category.Style.BorderWidth.ToString;
  eDisplacement.PlainText := Category.Style.Displacement.ToString;

end;

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

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

procedure TVis2D_CategorySettingsFrm.UpdateDrawable;
var
  Category: TCategoryElement;
begin

  if not Initialized then
    Exit;  

  Category := FDrawable as TCategoryElement;

  Category.&Label := eLabel.PlainText;
  Category.Value := StrToFloat(eValue.PlainText);
  Category.Style.Color := ccArea.Color;
  Category.Style.Opacity := EnsureRange(Round(Category.Style.Opacity.MaxValue * StrToFloat(eOpacity.PlainText) / 100), 0, Category.Style.Opacity.MaxValue);
  Category.Style.BorderColor := ccBorder.Color;
  Category.Style.BorderWidth := StrToInt(eBorderWidth.PlainText);
  Category.Style.Displacement := StrToFloat(eDisplacement.PlainText);

  inherited;  

end;

end.
