GXS.CyborgManager.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. unit GXS.CyborgManager;
  2. (*
  3. The CyborgManager is a class that provides a way to manage
  4. AI smart objects and thier swarms or proxies at runtime.
  5. You need to have the model filename and it will load it
  6. into the list or return the already existing model
  7. *)
  8. interface
  9. uses
  10. System.SysUtils,
  11. System.Classes,
  12. GXS.SmartObjects,
  13. GXS.Scene,
  14. GXS.Objects,
  15. GXS.FileGLTF,
  16. GXS.File3DS,
  17. GXS.FilePly,
  18. GXS.FileOBJ;
  19. type
  20. TgxCyborgManager = class
  21. private
  22. FMasterObject: TgxDummyCube;
  23. FModelList: TStringList;
  24. FModelPath: string;
  25. (* Changing the path to the models and refresh the
  26. already existing freeforms *)
  27. procedure SetPathToModel(const Value: String);
  28. public
  29. (* It will create the list, assign the path to the
  30. models and the master object where the new models will be loaded *)
  31. constructor Create(AMaster: TgxDummyCube; APath: string); virtual;
  32. (* It will free all the loaded models *)
  33. destructor Destroy; override;
  34. (* It will load a new model if it's not in the list and then return the new
  35. freeform. If it,s already in the list, it will return the existing freeform *)
  36. function LoadModel(AFilename: string): TgxCyborg;
  37. property MasterObject: TgxDummyCube read FMasterObject;
  38. property ModelList: TStringList read FModelList;
  39. property Path: string read FModelPath write SetPathToModel;
  40. end;
  41. implementation //-------------------------------------------------------------
  42. constructor TgxCyborgManager.Create(AMaster: TgxDummyCube; APath: string);
  43. begin
  44. // Set the master object
  45. FMasterObject := AMaster;
  46. // Create the model list
  47. FModelList := TStringList.Create;
  48. FModelList.CaseSensitive := false;
  49. FModelList.Sorted := true;
  50. // Set the path to the models
  51. SetPathToModel(APath);
  52. end;
  53. // --------------------------------
  54. destructor TgxCyborgManager.Destroy;
  55. var
  56. i: Integer;
  57. begin
  58. // Destroy every models
  59. for i := 0 to Pred(FModelList.Count) do
  60. FModelList.Objects[i].Destroy;
  61. // Destroy the list
  62. FModelList.Destroy;
  63. inherited;
  64. end;
  65. // --------------------------------------------------------------
  66. function TgxCyborgManager.LoadModel(AFilename: string): TgxCyborg;
  67. var
  68. i: Integer;
  69. NewFreeForm: TgxCyborg;
  70. begin
  71. with FModelList do
  72. begin
  73. if Find(AFilename, i) then
  74. Result := TgxCyborg(Objects[i])
  75. else
  76. begin
  77. NewFreeForm := TgxCyborg(FMasterObject.AddNewChild(TgxCyborg));
  78. NewFreeForm.LoadFromFile(FModelPath + AFilename);
  79. FModelList.AddObject(AFilename, NewFreeForm);
  80. Result := NewFreeForm;
  81. end;
  82. end;
  83. end;
  84. // ----------------------------------------------------------
  85. procedure TgxCyborgManager.SetPathToModel(const Value: String);
  86. var
  87. Len: Integer;
  88. i: Integer;
  89. begin
  90. // Set the path
  91. FModelPath := Value;
  92. Len := Length(Value);
  93. // Correct it if there is no '\'
  94. if (Len > 0) then
  95. if (Value[Len - 1] <> '\') then
  96. FModelPath := Value + '\';
  97. // Reload the models
  98. for i := 0 to Pred(FModelList.Count) do
  99. TgxCyborg(FModelList.Objects[i])
  100. .LoadFromFile(FModelPath + FModelList.Strings[i]);
  101. end;
  102. initialization //-------------------------------------------------------------
  103. // RegisterClasses([TgxCyborgManager]);
  104. end.