GLS.CyborgManager.pas 3.1 KB

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