Results 1 to 3 of 3

Thread: Directories to Treeview items...

  1. #1

    Directories to Treeview items...

    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
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

  2. #2

    Re: Directories to Treeview items...

    You can build the tree recursively.
    For example, you could have a procedure like:

    procedure BuildTree(item:TTreeItem; pathtring);

    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:
    [pascal]
    procedure BuildTree(item:TTreeItem; pathtring);
    var new:TTreeItem;
    begin
    new := (new tree item);
    new.parent := item;
    for each subdir of path:
    BuildTree(new, subdir);
    end;
    [/pascal]
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  3. #3

    Directories to Treeview items...

    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:
    [pascal]
    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;
    [/pascal]

    Thanx for your help
    M109uk
    <br />--------------------------------------------------------
    <br />www.pulse-soft.oneuk.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •