ArenaZ
146 строк · 3.9 Кб
1unit GLModelManager;
2
3(*
4* Author : Blaise Bernier
5* Date : 08/12/2003
6*
7******************************************************************************
8* Description :
9* The GLModelManager is a class that provides an easy way to manage
10* models and master objects of proxies at runtime. You just need to
11* have the model filename and it will load it into the list or return
12* the already existing model.
13*
14*
15*)
16
17interface
18
19uses GLScene, GLObjects, GLVectorFileObjects, SysUtils, Classes,
20GLFile3DS, GLFileOBJ;
21
22type
23
24(****************************************
25TGLModelManager - class definition
26****************************************)
27TGLModelManager = class
28private
29{ Private fields }
30FMasterObject : TGLDummyCube;
31FModelList : TStringList;
32FModelPath : string;
33
34{ Get/Set methods }
35procedure SetModelPath(const Value: String);
36public
37{ Constructor / Destructor }
38constructor Create(AMaster : TGLDummyCube; APath : string); virtual;
39destructor Destroy; override;
40
41{ Methods }
42function LoadModel(AFilename : string): TGLFreeForm;
43
44{ Properties }
45property MasterObject : TGLDummyCube read FMasterObject;
46property ModelList : TStringList read FModelList;
47property Path : string read FModelPath write SetModelPath;
48end;
49
50
51implementation
52
53(****************************************
54{ TGLModelManager }
55****************************************)
56
57(**********************************
58Name : Create
59Description :
60This is the constructor. It will create the list, assign the path to the
61models and the master object where the new models will be loaded.
62*)
63constructor TGLModelManager.Create(AMaster: TGLDummyCube; APath : string);
64begin
65// Set the master object
66FMasterObject := AMaster;
67// Create the model list
68FModelList := TStringList.Create;
69FModelList.CaseSensitive := false;
70FModelList.Sorted := true;
71// Set the path to the models
72SetModelPath(APath);
73end;
74
75(**********************************
76Name : Destroy
77Description :
78This is the destructor. It will free all the loaded models.
79*)
80destructor TGLModelManager.Destroy;
81var
82i : integer;
83begin
84// Destroy every models
85for i:=0 to Pred(FModelList.Count) do
86FModelList.Objects[i].Destroy;
87// Destroy the list
88FModelList.Destroy;
89inherited;
90end;
91
92(**********************************
93Name : LoadModel
94Description :
95It will load a new model if it's not in the list and then return the new
96freeform. If it,s already in the list, it will return the existing
97freeform.
98*)
99function TGLModelManager.LoadModel(AFilename: string): TGLFreeForm;
100var
101I : integer;
102NewFreeForm : TGLFreeForm;
103begin
104
105with FModelList do
106begin
107if Find(AFilename, I) then
108Result := TGLFreeForm(Objects[I])
109else
110begin
111NewFreeForm := TGLFreeForm(FMasterOBject.AddNewChild(TGLFreeForm));
112NewFreeForm.LoadFromFile(FModelPath + AFilename);
113FModelList.AddObject(AFilename,NewFreeForm);
114Result := NewFreeForm;
115end;
116end;
117
118end;
119
120(**********************************
121Name : SetModelPath
122Description :
123It will change the path to the models and refresh the
124already existing freeforms
125*)
126procedure TGLModelManager.SetModelPath(const Value: String);
127var
128Len : integer;
129i : integer;
130begin
131// Set the path
132FModelPath := Value;
133
134Len := Length(Value);
135
136// Correct it if there is no '\'
137if (Len > 0) then
138if (Value[Len - 1] <> '\') then
139FModelPath := Value + '\';
140
141// Reload the models
142for i := 0 to Pred(FModelList.Count) do
143TGLFreeForm(FModelList.Objects[i]).LoadFromFile(FModelPath + FModelList.Strings[i]);
144end;
145
146end.
147