|
- unit uengShaderGeneratorEx;
-
- {$mode objfpc}{$H+}
-
- interface
-
- uses
- Classes, SysUtils,
- uengShaderFile, uengShaderGenerator, uengShaderFileTypes;
-
- type
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- { almost the same as TengShaderGenerator, but this is the extended version, that creates a copy of
- all properties. So you can keep more then one gerator configurations at once. It is a little bit slower
- than the normal generator. If you not need multiple configurations at once, use the simple one (TengShaderGenerator) }
- TengShaderGeneratorEx = class(TObject)
- private
- fName: String;
- fGeneratorPart: TengShaderGenerator;
-
- function GetPropertyByIndex(const aIndex: Integer): Variant;
- function GetPropertyByName(const aName: String): Variant;
- function GetPropertyCount: Integer;
- function GetPropertyNames(const aIndex: Integer): String;
-
- procedure SetPropertyByIndex(const aIndex: Integer; aValue: Variant);
- procedure SetPropertyByName(const aName: String; aValue: Variant);
-
- procedure GeneratorPartDestroy(aSender: TObject);
- protected
- fProperties: TStringVariantMap;
- public
- property Name: String read fName write fName;
- property PropertyByName [const aName: String]: Variant read GetPropertyByName write SetPropertyByName;
- property PropertyByIndex[const aIndex: Integer]: Variant read GetPropertyByIndex write SetPropertyByIndex;
- property PropertyNames [const aIndex: Integer]: String read GetPropertyNames;
- property PropertyCount: Integer read GetPropertyCount;
-
- function TryGetProperty(const aName: String; out aValue: Variant): Boolean;
- function TrySetProperty(const aName: String; const aValue: Variant): Boolean;
- procedure ListProperties(const aPropertyNames: TStrings);
-
- procedure GenerateCode(const aCode: TengShaderCode);
-
- constructor Create(const aShaderFile: TengShaderFile; const aName: String);
- destructor Destroy; override;
- end;
-
- implementation
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //TengShaderGeneratorEx///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function TengShaderGeneratorEx.GetPropertyByIndex(const aIndex: Integer): Variant;
- begin
- result := fProperties.ValueAt[aIndex];
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function TengShaderGeneratorEx.GetPropertyByName(const aName: String): Variant;
- var
- i: Integer;
- begin
- i := fProperties.IndexOf(aName);
- if (i < 0) then
- raise EengUnknownIdentifier.Create(aName, self);
- result := fProperties.ValueAt[i];
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function TengShaderGeneratorEx.GetPropertyCount: Integer;
- begin
- result := fProperties.Count;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function TengShaderGeneratorEx.GetPropertyNames(const aIndex: Integer): String;
- begin
- result := fProperties.Keys[aIndex];
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TengShaderGeneratorEx.SetPropertyByIndex(const aIndex: Integer; aValue: Variant);
- begin
- fProperties.ValueAt[aIndex] := aValue;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TengShaderGeneratorEx.SetPropertyByName(const aName: String; aValue: Variant);
- var
- i: Integer;
- begin
- i := fProperties.IndexOf(aName);
- if (i < 0) then
- raise EengUnknownIdentifier.Create(aName, self);
- fProperties.ValueAt[i] := aValue;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TengShaderGeneratorEx.GeneratorPartDestroy(aSender: TObject);
- begin
- fGeneratorPart := nil;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function TengShaderGeneratorEx.TryGetProperty(const aName: String; out aValue: Variant): Boolean;
- var
- i: Integer;
- begin
- i := fProperties.IndexOf(aName);
- result := (i >= 0);
- if result then
- aValue := fProperties.ValueAt[i];
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function TengShaderGeneratorEx.TrySetProperty(const aName: String; const aValue: Variant): Boolean;
- var
- i: Integer;
- begin
- i := fProperties.IndexOf(aName);
- result := (i >= 0);
- if result then
- fProperties.ValueAt[i] := aValue;
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TengShaderGeneratorEx.ListProperties(const aPropertyNames: TStrings);
- var
- s: String;
- begin
- for s in fProperties.Keys do
- aPropertyNames.Add(s);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- procedure TengShaderGeneratorEx.GenerateCode(const aCode: TengShaderCode);
- var
- kvp: TStringVariantMap.TKeyValuePair;
- begin
- if not Assigned(fGeneratorPart) then
- EengShaderPartInternal.Create('unable to generate code: generator is not available anymore');
- for kvp in fProperties.KeyValuePairs do
- fGeneratorPart.TrySetProperty(kvp.Key, kvp.Value);
- fGeneratorPart.GenerateCode(aCode);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- constructor TengShaderGeneratorEx.Create(const aShaderFile: TengShaderFile; const aName: String);
- begin
- inherited Create;
- fProperties := TStringVariantMap.Create;
- fGeneratorPart := aShaderFile.Generator[aName];
- fGeneratorPart.DuplicateProperties(fProperties);
- end;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- destructor TengShaderGeneratorEx.Destroy;
- begin
- FreeAndNil(fProperties);
- inherited Destroy;
- end;
-
- end.
|