PolycodeProjectBrowser.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolycodeProjectBrowser.h"
  20. extern UIGlobalMenu *globalMenu;
  21. PolycodeProjectBrowser::PolycodeProjectBrowser(PolycodeProject *project) : UIElement() {
  22. this->project = project;
  23. headerBg = new UIRect(10,10);
  24. addChild(headerBg);
  25. headerBg->setAnchorPoint(-1.0, -1.0, 0.0);
  26. headerBg->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderBgColor"));
  27. UILabel *label = new UILabel("PROJECT BROWSER", 18, "section", Label::ANTIALIAS_FULL);
  28. label->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderFontColor"));
  29. addChild(label);
  30. label->setPosition(10, 3);
  31. treeContainer = new UITreeContainer("boxIcon.png", project->getProjectName(), 200, 555);
  32. treeContainer->getRootNode()->toggleCollapsed();
  33. treeContainer->getRootNode()->addEventListener(this, UITreeEvent::SELECTED_EVENT);
  34. treeContainer->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  35. treeContainer->setPosition(0, 30);
  36. BrowserUserData *data = new BrowserUserData();
  37. data->type = 0;
  38. data->parentProject = project;
  39. treeContainer->getRootNode()->setUserData((void*) data) ;
  40. addChild(treeContainer);
  41. selectedData = NULL;
  42. parseFolderIntoNode(treeContainer->getRootNode(), project->getRootFolder());
  43. }
  44. PolycodeProjectBrowser::~PolycodeProjectBrowser() {
  45. }
  46. void PolycodeProjectBrowser::Refresh() {
  47. parseFolderIntoNode(treeContainer->getRootNode(), project->getRootFolder());
  48. }
  49. void PolycodeProjectBrowser::handleEvent(Event *event) {
  50. if(event->getDispatcher() == contextMenu) {
  51. UIMenuItem *item = contextMenu->getSelectedItem();
  52. PolycodeProjectBrowserEvent *bEvent = new PolycodeProjectBrowserEvent();
  53. bEvent->command = item->_id;
  54. dispatchEvent(bEvent, PolycodeProjectBrowserEvent::HANDLE_MENU_COMMAND);
  55. }
  56. if(event->getDispatcher() == treeContainer) {
  57. if(event->getEventCode() == InputEvent::EVENT_MOUSEDOWN) {
  58. InputEvent *inputEvent = (InputEvent*) event;
  59. if(inputEvent->mouseButton == CoreInput::MOUSE_BUTTON2) {
  60. contextMenu = globalMenu->showMenuAtMouse(130);
  61. contextMenu->addOption("New File", "add_new_file");
  62. contextMenu->addOption("New Project", "add_new_project");
  63. contextMenu->addOption("New Folder", "add_new_folder");
  64. contextMenu->addDivider();
  65. contextMenu->addOption("Add external files", "add_files");
  66. contextMenu->addOption("Import 3D assets", "import_assets");
  67. contextMenu->addDivider();
  68. contextMenu->addOption("Refresh", "refresh");
  69. contextMenu->addOption("Rename", "rename");
  70. contextMenu->addDivider();
  71. contextMenu->addOption("Remove", "remove");
  72. contextMenu->fitToScreenVertical();
  73. contextMenu->addEventListener(this, UIEvent::OK_EVENT);
  74. }
  75. }
  76. }
  77. if(event->getDispatcher() == treeContainer->getRootNode()) {
  78. if(event->getEventCode() == UITreeEvent::SELECTED_EVENT){
  79. BrowserUserData *data = (BrowserUserData *)treeContainer->getRootNode()->getSelectedNode()->getUserData();
  80. selectedData = data;
  81. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  82. }
  83. }
  84. Entity::handleEvent(event);
  85. }
  86. UITree *PolycodeProjectBrowser::nodeHasName(UITree *node, String name) {
  87. for(int i=0; i < node->getNumTreeChildren(); i++) {
  88. UITree *projectChild = node->getTreeChild(i);
  89. if(projectChild->getLabelText() == name) {
  90. return projectChild;
  91. }
  92. }
  93. return NULL;
  94. }
  95. bool PolycodeProjectBrowser::listHasFileEntry(vector<OSFileEntry> files, OSFileEntry fileEntry) {
  96. for(int i=0; i < files.size(); i++) {
  97. if(files[i].fullPath == fileEntry.fullPath && files[i].type == fileEntry.type) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. void parseOpenNodesIntoEntry(ObjectEntry *entry, UITree *node, bool addNewNode) {
  104. bool hasOpenNodes = false;
  105. for(int i=0; i < node->getNumTreeChildren(); i++) {
  106. UITree *child = node->getTreeChild(i);
  107. if(!child->isCollapsed()) {
  108. hasOpenNodes = true;
  109. }
  110. }
  111. if(!hasOpenNodes) {
  112. return;
  113. }
  114. ObjectEntry *childNodes = entry;
  115. if(addNewNode) {
  116. childNodes = entry->addChild("child_nodes");
  117. }
  118. for(int i=0; i < node->getNumTreeChildren(); i++) {
  119. UITree *child = node->getTreeChild(i);
  120. if(!child->isCollapsed()) {
  121. ObjectEntry *newEntry = childNodes->addChild("open_node");
  122. newEntry->addChild("name", child->getLabelText());
  123. parseOpenNodesIntoEntry(newEntry, child, true);
  124. }
  125. }
  126. }
  127. ObjectEntry *PolycodeProjectBrowser::getBrowserConfig() {
  128. ObjectEntry *configEntry = new ObjectEntry();
  129. configEntry->name = "project_browser";
  130. configEntry->addChild("width", getWidth());
  131. ObjectEntry *openNodes = configEntry->addChild("open_nodes");
  132. parseOpenNodesIntoEntry(openNodes, treeContainer->getRootNode(), false);
  133. return configEntry;
  134. }
  135. void PolycodeProjectBrowser::applyOpenNodeToTree(UITree* treeNode, ObjectEntry *nodeEntry) {
  136. for(int i=0; i < treeNode->getNumTreeChildren(); i++) {
  137. if(treeNode->getTreeChild(i)->getLabelText() == (*nodeEntry)["name"]->stringVal ){
  138. if(treeNode->getTreeChild(i)->isCollapsed()) {
  139. treeNode->getTreeChild(i)->toggleCollapsed();
  140. ObjectEntry *childNodes = (*nodeEntry)["child_nodes"];
  141. if(childNodes) {
  142. for(int j=0; j < childNodes->length; j++) {
  143. applyOpenNodeToTree(treeNode->getTreeChild(i), (*childNodes)[j]);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. void PolycodeProjectBrowser::applyBrowserConfig(ObjectEntry *entry) {
  151. ObjectEntry *openNodes = (*entry)["open_nodes"];
  152. if(openNodes) {
  153. for(int i=0; i < openNodes->length; i++) {
  154. ObjectEntry *openNode = (*openNodes)[i];
  155. if(openNode) {
  156. applyOpenNodeToTree(treeContainer->getRootNode(), openNode);
  157. }
  158. }
  159. }
  160. }
  161. void PolycodeProjectBrowser::parseFolderIntoNode(UITree *node, String spath) {
  162. vector<OSFileEntry> files = OSBasics::parseFolder(spath, false);
  163. // check if files got deleted
  164. for(int i=0; i < node->getNumTreeChildren(); i++) {
  165. UITree *projectChild = node->getTreeChild(i);
  166. if(!listHasFileEntry(files, ((BrowserUserData*)projectChild->getUserData())->fileEntry)) {
  167. node->removeTreeChild(projectChild);
  168. }
  169. }
  170. for(int i=0; i < files.size(); i++) {
  171. OSFileEntry entry = files[i];
  172. if(entry.type == OSFileEntry::TYPE_FOLDER) {
  173. UITree *existing = nodeHasName(node, entry.name);
  174. if(!existing) {
  175. BrowserUserData *data = new BrowserUserData();
  176. data->fileEntry = entry;
  177. UITree *newChild = node->addTreeChild("folder.png", entry.name, (void*) data);
  178. data->type = 2;
  179. data->parentProject = project;
  180. parseFolderIntoNode(newChild, entry.fullPath);
  181. } else {
  182. parseFolderIntoNode(existing, entry.fullPath);
  183. }
  184. } else {
  185. if(!nodeHasName(node, entry.name)) {
  186. BrowserUserData *data = new BrowserUserData();
  187. data->fileEntry = entry;
  188. data->type = 1;
  189. data->parentProject = project;
  190. node->addTreeChild("file.png", entry.name, (void*) data);
  191. }
  192. }
  193. }
  194. }
  195. void PolycodeProjectBrowser::Resize(Number width, Number height) {
  196. headerBg->Resize(width, 30);
  197. treeContainer->Resize(width, height-30);
  198. UIElement::Resize(width, height);
  199. }