PDA

View Full Version : Directories to Treeview items...



M109uk
24-01-2005, 06:33 PM
Hi all,

I have a list of directories in an array, for example an array of 10 directories:
[0] = 'data\'
[1] = 'data\textures\'
[2] = 'data\textures\walls\'
[3] = 'data\textures\floors\'
[4] = 'data\actors\'
[5] = 'data\actors\dragonino2\'
[6] = 'data\actors\dragonino2\skins\'
[7] = 'data\actors\dragonino2\animations\'
[8] = 'data\models\'
[9] = 'data\shaders\'

I want to create the above data in to a treeview list e.g.
-data\
---textures\
------walls\
------floors\
---actors\
------dragonino2\
---------skins\
---------animations\
---models\
---shaders\

What will the best way of doing this?

Thanx for any help

{MSX}
24-01-2005, 07:18 PM
You can build the tree recursively.
For example, you could have a procedure like:

procedure BuildTree(item:TTreeItem; path:String);

you start passing your root path and the root item, and then using "findfirst" and "findnext" you iterate throu the directory given in the path (first pass will be: textures, actors, models, etc).
For each step, you create a node, attach that node to the "item" you have as parameter and then call BuildTree on it again.

PseudoCode:

procedure BuildTree(item:TTreeItem; path:String);
var new:TTreeItem;
begin
new := (new tree item);
new.parent := item;
for each subdir of path:
BuildTree(new, subdir);
end;

M109uk
25-01-2005, 12:34 AM
Unfortunatly its not that easy,
The directories dont exists as part of the file system, they are directories stored in a pak file,

How ever i think i have managed to get it working, for any one who needs it:

function ExtractFirstDir(var Dir: String): String;
var
i,e: Integer;
begin
Result := '';
e := -1;
For i := 1 To Length(Dir) Do
If Dir[i] = '\' Then
Begin
e := i;
Break;
End;
If e = -1 Then Exit;
Result := Copy(Dir, 1, e);
If Dir[e] = '\' Then e := e+1;
Dir := Copy(Dir, e, Length(Dir));
end;

procedure PopulateDirectories;

function FindNode(const Caption: String; const Level: Integer): Integer;
var
i: Integer;
begin
Result := -1;
For i := 0 To TreeView1.Items.Count-1 Do
If (TreeView1.Items[i].Caption = Caption) And
(TreeView1.Items[i].Level = Level) Then
Begin
Result := i;
Exit;
End;
end;

var
Dirs: Array Of String;
Str,dStr: String;
i,j: Integer;
Node,aNode,pNode: TTreeNode;
begin
TreeView1.Items.Clear;
TreeView1.Items.AddChild(Nil, '<Root>'); // <---- This is used for empty dir

For i := 0 To Pak.nDirs Do // <---- Number of directories
Begin
SetLength(Dirs, 0);
Str := Pak.Dirs[i];
dStr := ExtractFirstDir(Str);
// <---- This formats the directory in to an array eg. ['Data\', 'Textures\', 'Floors\']

If Length(fDirs) = 0 Then Continue; // <---- Is empty dir?

If FindNode(Dirs[0], 0) = -1 Then
Begin
Node := TreeView1.Items.AddChild(Nil, Dirs[0]);
// Set nodes image index's
pNode := Node;
End Else
pNode := TreeView1.Items[FindNode(Dirs[0], 0)];

For j := 1 To Length(Dirs)-1 Do
Begin
aNode := TreeView1.Items.AddChild(pNode, Dirs[j]);
// Set nodes image index's
pNode := aNode;
End Else
pNode := TreeView1.Items[FindNode(Dirs[j], j)];
End;
end;


Thanx for your help :)