unit Vis3D_ImageRectSettings;

interface

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

type
  TVis3D_ImageRectSettingsFrm = class(TDrawableOptionsFrm3D)
    btnApply: TUxButton;
    ScrollBox1: TScrollBoxEx;
    lblTransparentColor: TLabel;
    lblStyleName: TLabel;
    Label1: TLabel;
    ccTransparentColor: TColorContainer;
    lblPosition: TLabel;
    ePosX: TTextEditor;
    ePosY: TTextEditor;
    ePosZ: TTextEditor;
    lblDirection: TLabel;
    eDirX: TTextEditor;
    eDirY: TTextEditor;
    eDirZ: TTextEditor;
    lblScale: TLabel;
    eScaleX: TTextEditor;
    eScaleY: TTextEditor;
    eScaleZ: TTextEditor;
    lblRotation: TLabel;
    eRot: TTextEditor;
    imPixmap: TImageViewer;
    lblPixmap: TLabel;
    lblOpaqueColor: TLabel;
    ccOpaqueColor: TColorContainer;
    lblTransparencymode: TLabel;
    cbTransparencyMode: TComboBox;
    procedure btnApplyClick(Sender: TObject);
    procedure ccTransparentColorChange(Sender: TObject);
    procedure cbTransparencyModeChange(Sender: TObject);
  public
    procedure Initialize; override;
    procedure UpdateDrawable; override;
  end;

implementation

{$R *.dfm}

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

procedure TVis3D_ImageRectSettingsFrm.cbTransparencyModeChange(Sender: TObject);
begin
  UpdateDrawable;
  ccTransparentColor.Enabled := cbTransparencyMode.ItemIndex >= Ord(TTransparentColorMode.tcmEqual);
  ccOpaqueColor.Enabled := cbTransparencyMode.ItemIndex >= Ord(TTransparentColorMode.tcmBipolar);
  lblTransparentColor.Enabled := ccTransparentColor.Enabled;
  lblOpaqueColor.Enabled := ccOpaqueColor.Enabled;
end;

procedure TVis3D_ImageRectSettingsFrm.ccTransparentColorChange(Sender: TObject);
begin
  UpdateDrawable;
end;

procedure TVis3D_ImageRectSettingsFrm.Initialize;
begin

  cbTransparencyMode.Items.BeginUpdate;
  try
    cbTransparencyMode.Clear;
    for var tm := Low(TTransparentColorMode) to High(TTransparentColorMode) do
      cbTransparencyMode.Items.AddObject(tm.ToString, TObject(tm));
  finally
    cbTransparencyMode.Items.EndUpdate;
  end;

  inherited;

  var Obj := FDrawable as TImageRect;

  imPixmap.Bitmap := Obj.Bitmap;
  cbTransparencyMode.ItemIndex :=
    cbTransparencyMode.Items.IndexOfObject(TObject(Ord(Obj.TransparentColorMode)));
  ccTransparentColor.Color := Obj.TransparentColor;
  ccOpaqueColor.Color := Obj.OpaqueColor;
  ccTransparentColor.Enabled := cbTransparencyMode.ItemIndex >= Ord(TTransparentColorMode.tcmEqual);
  ccOpaqueColor.Enabled := cbTransparencyMode.ItemIndex >= Ord(TTransparentColorMode.tcmBipolar);
  lblTransparentColor.Enabled := ccTransparentColor.Enabled;
  lblOpaqueColor.Enabled := ccOpaqueColor.Enabled;
  ePosX.PlainText := Obj.Position.x.ToString;
  ePosY.PlainText := Obj.Position.y.ToString;
  ePosZ.PlainText := Obj.Position.z.ToString;
  eDirX.PlainText := Obj.Direction.x.ToString;
  eDirY.PlainText := Obj.Direction.y.ToString;
  eDirZ.PlainText := Obj.Direction.z.ToString;
  eScaleX.PlainText := Obj.Scale.x.ToString;
  eScaleY.PlainText := Obj.Scale.y.ToString;
  eScaleZ.PlainText := Obj.Scale.z.ToString;
  eRot.PlainText := Obj.Rotation.ToString;

end;

procedure TVis3D_ImageRectSettingsFrm.UpdateDrawable;
begin
  if not FInitialized then
    Exit;
  var Obj := FDrawable as TImageRect;
  Obj.Bitmap := imPixmap.Bitmap;
  Obj.TransparentColorMode :=
    TTransparentColorMode(cbTransparencyMode.Items.Objects[cbTransparencyMode.ItemIndex]);
  Obj.TransparentColor := ccTransparentColor.Color;
  Obj.OpaqueColor := ccOpaqueColor.Color;
  Obj.Position := vec(
    StrToFloat(ePosX.PlainText),
    StrToFloat(ePosY.PlainText),
    StrToFloat(ePosZ.PlainText)
  );
  Obj.Direction := vec(
    StrToFloat(eDirX.PlainText),
    StrToFloat(eDirY.PlainText),
    StrToFloat(eDirZ.PlainText)
  );
  Obj.Scale := vec(
    StrToFloat(eScaleX.PlainText),
    StrToFloat(eScaleY.PlainText),
    StrToFloat(eScaleZ.PlainText)
  );
  Obj.Rotation := StrToFloat(eRot.PlainText);
  inherited;
end;

end.
