//DJXMaterials v1.1
unit DJXMaterials;

interface

uses
  Classes,
  DJXClasses,
  Direct3D9;

type
  TDJXMaterialList = class;
  TDJXMaterial = class;

  TDJXMaterial = class
  private
    m_DanJetX: Pointer;
    m_Material: TD3DMaterial9;
    m_Name: string;
  public
    property DanJetX: pointer read m_DanJetX write m_DanJetX;
    property name: string read m_name write m_name;
    procedure SetToDevice;
    procedure SetMaterial(
      DiffuseR,DiffuseG,DiffuseB,DiffuseA: single;
      AmbientR,AmbientG,AmbientB,AmbientA: single;
      SpecularR,SpecularG,SpecularB,SpecularA: single;
      EmissiveR,EmissiveG,EmissiveB,EmissiveA: single;
      Power: Single = 0
    );
    procedure Assign(Material: TDJXMaterial); 
    property DiffuseR: single read m_Material.Diffuse.r write m_Material.Diffuse.r;
    property DiffuseG: single read m_Material.Diffuse.g write m_Material.Diffuse.g;
    property DiffuseB: single read m_Material.Diffuse.b write m_Material.Diffuse.b;
    property DiffuseA: single read m_Material.Diffuse.a write m_Material.Diffuse.a;
    property AmbientR: single read m_Material.Ambient.r write m_Material.Ambient.r;
    property AmbientG: single read m_Material.Ambient.g write m_Material.Ambient.g;
    property AmbientB: single read m_Material.Ambient.b write m_Material.Ambient.b;
    property AmbientA: single read m_Material.Ambient.a write m_Material.Ambient.a;
    property SpecularR: single read m_Material.Specular.r write m_Material.Specular.r;
    property SpecularG: single read m_Material.Specular.g write m_Material.Specular.g;
    property SpecularB: single read m_Material.Specular.b write m_Material.Specular.b;
    property SpecularA: single read m_Material.Specular.a write m_Material.Specular.a;
    property EmissiveR: single read m_Material.Emissive.r write m_Material.Emissive.r;
    property EmissiveG: single read m_Material.Emissive.g write m_Material.Emissive.g;
    property EmissiveB: single read m_Material.Emissive.b write m_Material.Emissive.b;
    property EmissiveA: single read m_Material.Emissive.a write m_Material.Emissive.a;
    property Power: Single read m_Material.Power write m_Material.Power;
    property Material: TD3DMaterial9 read m_Material write m_Material;
  end;
  
  TDJXMaterialList = class(TDJXComponent)
  protected
    m_Materials: Tlist;
    function getItem(Index: integer): TDJXMaterial;
    procedure setItem(Index: integer; Value: TDJXMAterial);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property DanJetX: Pointer read m_DanJetX write m_DanJetX;
    property Materials[Index: integer]:TDJXMaterial read getItem write setItem;
    function Find(name: string): TDJXMaterial;
    procedure Add(Material: TDJXMaterial);
    procedure Init(DanJetX: Pointer); override;
    procedure OnLostDevice; override;
    procedure OnResetDevice; override;
    function CreateMaterial(name: string): TDJXMaterial; overload;
    function CreateMaterial(
      name: string;
      DiffuseR, DiffuseG, DiffuseB, DiffuseA: single;
      AmbientR, AmbientG, AmbientB, AmbientA: single;
      SpecularR, SpecularG, SpecularB, SpecularA: single;
      EmissiveR, EmissiveG, EmissiveB, EmissiveA: single;
      Power: Single = 0
    ): TDJXMaterial; overload;
    function SetMaterial(
      DiffuseR, DiffuseG, DiffuseB, DiffuseA: single;
      AmbientR, AmbientG, AmbientB, AmbientA: single;
      SpecularR, SpecularG, SpecularB, SpecularA: single;
      EmissiveR, EmissiveG, EmissiveB, EmissiveA: single;
      Power: Single = 0
    ): TDJXMaterial;
  published
  end;

  

implementation

uses
  DJX;

//DJXMaterialList BEGIN
constructor TDJXMaterialList.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  m_Materials := Tlist.Create;
end;
destructor TDJXMaterialList.Destroy; 
begin
  if m_DanJetX <> nil then
  m_DanJetX := nil;
  if m_Materials <> nil then
  begin
    while m_Materials.Count > 0 do
    begin
      TDJXMaterial(m_Materials.Items[0]).Free;
      m_Materials.Delete(0);
    end;
    m_Materials.Free;
  end;
  inherited Destroy;
end;

function TDJXMaterialList.getItem(Index: integer): TDJXMaterial;
begin
  result := TDJXMaterial(m_Materials.items[Index]);
end;
procedure TDJXMaterialList.setItem(Index: integer; Value: TDJXMaterial);
begin
  m_Materials.items[Index] := Value;
end;
function TDJXMaterialList.Find(name: string): TDJXMaterial;
var
  i: integer;
begin
  for i := 0 to m_Materials.Count - 1 do
  if TDJXMaterial(m_Materials.Items[i]).Name = name then
  begin
    result := TDJXMaterial(m_Materials.Items[i]);
    exit;
  end;
  result := nil;
end;
procedure TDJXMaterialList.Add(Material: TDJXMaterial);
begin
  m_Materials.Add(Material);
end;
procedure TDJXMaterialList.Init(DanJetX: Pointer);
begin
  m_Materials := Tlist.Create;
  inherited Init(DanJetX);
end;
procedure TDJXMaterialList.OnLostDevice;
begin

end;
procedure TDJXMaterialList.OnResetDevice;
begin

end;
function TDJXMaterialList.CreateMaterial(name: string): TDJXMaterial;
var
  Material: TDJXMaterial;
begin
  Material := TDJXMaterial.Create;
  Material.name := name;
  Material.DanJetX := m_DanJetX;
  self.add(Material);
  Result := Material;
end;
function TDJXMaterialList.CreateMaterial(
      name: string;
      DiffuseR, DiffuseG, DiffuseB, DiffuseA: single;
      AmbientR, AmbientG, AmbientB, AmbientA: single;
      SpecularR, SpecularG, SpecularB, SpecularA: single;
      EmissiveR, EmissiveG, EmissiveB, EmissiveA: single;
      Power: Single = 0
    ): TDJXMaterial;
var
  Material: TDJXMaterial;
begin
  Material := TDJXMaterial.Create;
  Material.name := name;
  Material.DanJetX := m_DanJetX;
  Material.m_Material.Diffuse.r := DiffuseR;
  Material.m_Material.Diffuse.g := DiffuseG;
  Material.m_Material.Diffuse.b := DiffuseB;
  Material.m_Material.Diffuse.a := DiffuseA;
  Material.m_Material.Ambient.r := AmbientR;
  Material.m_Material.Ambient.g := AmbientG;
  Material.m_Material.Ambient.b := AmbientB;
  Material.m_Material.Ambient.a := AmbientA;
  Material.m_Material.Specular.r := SpecularR;
  Material.m_Material.Specular.g := SpecularG;
  Material.m_Material.Specular.b := SpecularB;
  Material.m_Material.Specular.a := SpecularA;
  Material.m_Material.Emissive.r := EmissiveR;
  Material.m_Material.Emissive.g := EmissiveG;
  Material.m_Material.Emissive.b := EmissiveB;
  Material.m_Material.Emissive.a := EmissiveA;
  Material.Power := Power;
  self.add(Material);
  Result := Material;
end;
function TDJXMaterialList.SetMaterial(
      DiffuseR, DiffuseG, DiffuseB, DiffuseA: single;
      AmbientR, AmbientG, AmbientB, AmbientA: single;
      SpecularR, SpecularG, SpecularB, SpecularA: single;
      EmissiveR, EmissiveG, EmissiveB, EmissiveA: single;
      Power: Single = 0
    ): TDJXMaterial;
var
  Material: TDJXMaterial;
begin
  Material := TDJXMaterial.Create;
  Material.name := '';
  Material.DanJetX := m_DanJetX;
  Material.m_Material.Diffuse.r := DiffuseR;
  Material.m_Material.Diffuse.g := DiffuseG;
  Material.m_Material.Diffuse.b := DiffuseB;
  Material.m_Material.Diffuse.a := DiffuseA;
  Material.m_Material.Ambient.r := AmbientR;
  Material.m_Material.Ambient.g := AmbientG;
  Material.m_Material.Ambient.b := AmbientB;
  Material.m_Material.Ambient.a := AmbientA;
  Material.m_Material.Specular.r := SpecularR;
  Material.m_Material.Specular.g := SpecularG;
  Material.m_Material.Specular.b := SpecularB;
  Material.m_Material.Specular.a := SpecularA;
  Material.m_Material.Emissive.r := EmissiveR;
  Material.m_Material.Emissive.g := EmissiveG;
  Material.m_Material.Emissive.b := EmissiveB;
  Material.m_Material.Emissive.a := EmissiveA;
  Material.Power := Power;
  Material.SetToDevice;
  Result := Material;
end;
//DJXMaterialList END

//DJXMaterial BEGIN
procedure TDJXMaterial.SetToDevice;
begin
  TDanJetX(m_DanJetX).Device.SetMaterial(m_Material);
end;
procedure TDJXMaterial.SetMaterial(
  DiffuseR, DiffuseG, DiffuseB, DiffuseA: single;
  AmbientR, AmbientG, AmbientB, AmbientA: single;
  SpecularR, SpecularG, SpecularB, SpecularA: single;
  EmissiveR, EmissiveG, EmissiveB, EmissiveA: single;
  Power: Single = 0
);
begin
  m_Material.Diffuse.r := DiffuseR;
  m_Material.Diffuse.g := DiffuseG;
  m_Material.Diffuse.b := DiffuseB;
  m_Material.Diffuse.a := DiffuseA;
  m_Material.Ambient.r := AmbientR;
  m_Material.Ambient.g := AmbientG;
  m_Material.Ambient.b := AmbientB;
  m_Material.Ambient.a := AmbientA;
  m_Material.Specular.r := SpecularR;
  m_Material.Specular.g := SpecularG;
  m_Material.Specular.b := SpecularB;
  m_Material.Specular.a := SpecularA;
  m_Material.Emissive.r := EmissiveR;
  m_Material.Emissive.g := EmissiveG;
  m_Material.Emissive.b := EmissiveB;
  m_Material.Emissive.a := EmissiveA;
  m_Material.Power := Power;
end;
procedure TDJXMaterial.Assign(Material: TDJXMaterial);
begin
  m_Material.Diffuse.r := Material.DiffuseR;
  m_Material.Diffuse.g := Material.DiffuseG;
  m_Material.Diffuse.b := Material.DiffuseB;
  m_Material.Diffuse.a := Material.DiffuseA;
  m_Material.Ambient.r := Material.AmbientR;
  m_Material.Ambient.g := Material.AmbientG;
  m_Material.Ambient.b := Material.AmbientB;
  m_Material.Ambient.a := Material.AmbientA;
  m_Material.Specular.r := Material.SpecularR;
  m_Material.Specular.g := Material.SpecularG;
  m_Material.Specular.b := Material.SpecularB;
  m_Material.Specular.a := Material.SpecularA;
  m_Material.Emissive.r := Material.EmissiveR;
  m_Material.Emissive.g := Material.EmissiveG;
  m_Material.Emissive.b := Material.EmissiveB;
  m_Material.Emissive.a := Material.EmissiveA;
  m_Material.Power := Material.Power;
end;
//DJXMaterial END

end.
