-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileTree.cpp
117 lines (107 loc) · 4.12 KB
/
FileTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "FileTree.h"
//finds the parent and add the item to the parent.
// If the parent isn't found, a new parent is created
void FileTree::push(FileNode* &root, string parent, string item, Image::image P, Image::image C){
// FileItem item(Image::FOLDER, item, (0.1f,0.1f),initPosition);
sf::Vector2f size(0.1f,0.1f);
// FileItem item = new FileItem;
Image::image iconC = C;
// cout << "PUSH START" << endl;
// cout << item << endl;
// cout << item.at(1) << endl;
FileNode *n = new FileNode(iconC, item, size,initPosition);
// n->set;
// FileNode node(Image::FOLDER, item, size,initPosition);
if(root == nullptr){
Image::image iconP = P;
root = new FileNode(iconP, parent, size,initPosition);
// FileNode h(Image::FOLDER, parent, size,initPosition);
// FileNode *p = new FileNode;
// p->set(Image::FOLDER, parent, size,initPosition);
// root->setData(p->getData());
// cout << "Parent: " << parent << " Children: " << item << endl;
root->children.insert(pair<string, FileNode*> (item, n));
return;
}
if(root->label == parent){
root->children.insert(pair<string, FileNode*> (item, n));
// cout << "Parent: " << root->label << " Children: " << item << endl;
return;
}
for(auto iter = root->children.begin(); iter != root->children.end(); ++iter){
// root->children.insert(pair<string, FileNode*>(item, node&));
push(iter->second, parent, item, P, C);
}
return;
}
// //traverses the tree ad adds the evenhandlers for each of the nodes
void FileTree::traverse(FileNode* &root, sf::RenderWindow& window, sf::Event event){
if(root == nullptr){
return;
}
for(auto itr = root->children.begin(); itr != root->children.end(); itr++){
root = itr->second;
if(root != nullptr){
traverse(root, window,event);
}
}
return;
}
FileTree::FileTree(){
// // push("File", "NULL");
// sf::Vector2f size(0.1f,0.1f);
// // FileNode *n = new FileNode;
// // n->set(Image::FOLDER, "item", size,initPosition);
// FileNode n(Image::FOLDER, "item", size,initPosition);
// root->setData(n.getData());
// root = new FileNode(Image::FOLDER, "item", size,initPosition);
}
// //this is the public version of push
void FileTree::push(std::string parent, std::string item, Image::image P, Image::image C){
push(root, parent, item, P, C);
}
void FileTree::traverse(sf::RenderWindow& window, sf::Event event){
traverse(root, window, event);
}
//draws the root node of the tree
void FileTree::draw(sf::RenderTarget& window, sf::RenderStates states) const{
window.draw(*root);
// for(auto itr = root->children.begin(); itr != root->children.end(); itr++){
// window.draw(root->getData());
// }
}
//calls the traverse function to add the event handlers to each node
void FileTree::addEventHandler(sf::RenderWindow& window, sf::Event event){
root->addEventHandler(window,event);
}
void FileTree::update(){
// root->update();
if((*root).state){
FindFile(root);
}
}
void FileTree::Clean(FileNode* &root){
for(auto itr = root->children.begin(); itr != root->children.end(); itr++){
root = itr->second;
if(root->children.empty()){
delete root;
return;
}else{
Clean(root);
return;
}
}
return;
}
void FileTree::Clean(){
Clean(root);
}
void FileTree::FindFile(FileNode* &root){
if((*root).state && (*root).children.empty()){
click = (*root).click;
}
for(auto itr = root->children.begin(); itr != root->children.end(); itr++){
FindFile(itr->second);
}
return;
}