TextureBrowser.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "TextureBrowser.h"
  20. AssetEntry::AssetEntry(String assetPath, String assetName, String extension) : UIElement() {
  21. this->assetPath = assetPath;
  22. if(assetName.length() > 20)
  23. assetName = assetName.substr(0,20)+"...";
  24. selectShape = new ScreenShape(ScreenShape::SHAPE_RECT, 120, 100);
  25. selectShape->visible = false;
  26. selectShape->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  27. addChild(selectShape);
  28. selectShape->processInputEvents = true;
  29. selectShape->setColor(0.0, 0.0, 0.0, 0.5);
  30. imageShape = new ScreenShape(ScreenShape::SHAPE_RECT, 64,64);
  31. imageShape->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  32. addChild(imageShape);
  33. if(extension == "png") {
  34. imageShape->loadTexture(assetPath);
  35. }
  36. if(extension == "ogg" || extension == "wav") {
  37. imageShape->loadTexture("Images/sound_thumb.png");
  38. }
  39. if(extension == "ttf" || extension == "otf") {
  40. imageShape->loadTexture("Images/font_icon.png");
  41. }
  42. imageShape->setPosition(28, 10);
  43. nameLabel = new ScreenLabel(assetName, 10);
  44. addChild(nameLabel);
  45. nameLabel->color.a = 0.5;
  46. nameLabel->setPositionMode(ScreenEntity::POSITION_CENTER);
  47. nameLabel->setPosition(60, 90);
  48. }
  49. AssetEntry::~AssetEntry() {
  50. delete imageShape;
  51. delete nameLabel;
  52. delete selectShape;
  53. }
  54. AssetList::AssetList() : UIElement() {
  55. bgShape = new ScreenShape(ScreenShape::SHAPE_RECT, 100,100);
  56. bgShape->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  57. bgShape->setColor(0.0, 0.0, 0.0, 0.4);
  58. addChild(bgShape);
  59. }
  60. AssetList::~AssetList() {
  61. }
  62. void AssetList::setExtensions(std::vector<String> extensions) {
  63. this->extensions = extensions;
  64. if(currentFolderPath != "") {
  65. showFolder(currentFolderPath);
  66. }
  67. }
  68. bool AssetList::hasExtension(String extension) {
  69. for(int i=0; i < extensions.size(); i++) {
  70. if(extensions[i] == extension) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. void AssetList::showFolder(String folderPath) {
  77. currentFolderPath = folderPath;
  78. for(int i=0; i < assetEntries.size(); i++) {
  79. removeChild(assetEntries[i]);
  80. delete assetEntries[i];
  81. }
  82. assetEntries.clear();
  83. currentEntry = NULL;
  84. vector<OSFileEntry> assets = OSBasics::parseFolder(folderPath, false);
  85. Number xPos = 20;
  86. Number yPos = 20;
  87. for(int i=0; i < assets.size(); i++) {
  88. OSFileEntry entry = assets[i];
  89. if(entry.type != OSFileEntry::TYPE_FOLDER) {
  90. if(hasExtension(entry.extension)) {
  91. AssetEntry *newEntry = new AssetEntry(entry.fullPath, entry.name, entry.extension);
  92. newEntry->selectShape->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  93. assetEntries.push_back(newEntry);
  94. newEntry->setPosition(xPos, yPos);
  95. xPos += 120;
  96. if(xPos > 500) {
  97. xPos = 20;
  98. yPos += 100;
  99. }
  100. addChild(newEntry);
  101. }
  102. }
  103. }
  104. width = 640;
  105. if(xPos == 20) {
  106. height = yPos+20;
  107. } else {
  108. height = yPos + 120;
  109. }
  110. bgShape->setShapeSize(width, height);
  111. bgShape->rebuildTransformMatrix();
  112. rebuildTransformMatrix();
  113. }
  114. void AssetList::handleEvent(Event *event) {
  115. for(int i=0; i < assetEntries.size(); i++) {
  116. if(event->getDispatcher() == assetEntries[i]->selectShape && event->getEventCode() == InputEvent::EVENT_MOUSEDOWN) {
  117. assetEntries[i]->selectShape->visible = true;
  118. selectedPath = assetEntries[i]->assetPath;
  119. printf("%s\n", selectedPath.c_str());
  120. if(currentEntry) {
  121. currentEntry->selectShape->visible = false;
  122. }
  123. currentEntry = assetEntries[i];
  124. }
  125. }
  126. }
  127. AssetBrowser::AssetBrowser() : UIWindow(L"Asset Browser", 850, 500) {
  128. defaultTemplateTree = NULL;
  129. Config *conf = CoreServices::getInstance()->getConfig();
  130. String fontName = conf->getStringValue("Polycode", "uiDefaultFontName");
  131. int fontSize = conf->getNumericValue("Polycode", "uiDefaultFontSize");
  132. closeOnEscape = true;
  133. templateContainer = new UITreeContainer("boxIcon.png", L"Project", 200, 480-topPadding-padding-padding);
  134. FolderUserData *data = new FolderUserData();
  135. data->type = 0;
  136. templateContainer->getRootNode()->setUserData(data);
  137. addChild(templateContainer);
  138. templateContainer->setPosition(padding,topPadding+padding);
  139. templateContainer->getRootNode()->toggleCollapsed();
  140. templateContainer->getRootNode()->addEventListener(this, UITreeEvent::SELECTED_EVENT);
  141. assetList = new AssetList();
  142. listContainer = new UIScrollContainer(assetList, false, true, 640, 480-topPadding-padding-padding);
  143. listContainer->setPosition(220,topPadding+padding);
  144. addChild(listContainer);
  145. cancelButton = new UIButton(L"Cancel", 100);
  146. cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
  147. addChild(cancelButton);
  148. cancelButton->setPosition(850-80-padding-100-10, 485);
  149. okButton = new UIButton(L"OK", 100);
  150. okButton->addEventListener(this, UIEvent::CLICK_EVENT);
  151. addChild(okButton);
  152. okButton->setPosition(850-80-padding, 485);
  153. currentProject = NULL;
  154. }
  155. void AssetBrowser::setProject(PolycodeProject *project) {
  156. if(project == currentProject) {
  157. return;
  158. }
  159. templateContainer->getRootNode()->clearTree();
  160. vector<OSFileEntry> templates = OSBasics::parseFolder(project->getRootFolder(), false);
  161. templateContainer->getRootNode()->setLabelText(project->getProjectName());
  162. for(int i=0; i < templates.size(); i++) {
  163. OSFileEntry entry = templates[i];
  164. if(entry.type == OSFileEntry::TYPE_FOLDER) {
  165. UITree *newChild = templateContainer->getRootNode()->addTreeChild("folder.png", entry.name, NULL);
  166. FolderUserData *data = new FolderUserData();
  167. data->type = 0;
  168. data->folderPath = entry.fullPath;
  169. newChild->setUserData(data);
  170. newChild->toggleCollapsed();
  171. parseFolderIntoTree(newChild, entry);
  172. }
  173. }
  174. currentProject = project;
  175. }
  176. AssetBrowser::~AssetBrowser() {
  177. }
  178. String AssetBrowser::getFileName() {
  179. return "";
  180. }
  181. String AssetBrowser::getTemplatePath() {
  182. return templatePath;
  183. }
  184. String AssetBrowser::getSelectedAssetPath() {
  185. return assetList->selectedPath.replace(currentProject->getRootFolder()+"/", "");
  186. }
  187. void AssetBrowser::setExtensions(std::vector<String> extensions) {
  188. assetList->setExtensions(extensions);
  189. }
  190. void AssetBrowser::handleEvent(Event *event) {
  191. if(event->getEventType() == "UIEvent") {
  192. if(event->getEventCode() == UIEvent::CLICK_EVENT) {
  193. if(event->getDispatcher() == okButton) {
  194. dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
  195. }
  196. if(event->getDispatcher() == cancelButton) {
  197. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  198. }
  199. }
  200. }
  201. if(event->getEventType() == "UITreeEvent" && event->getEventCode() == UITreeEvent::SELECTED_EVENT) {
  202. if(event->getDispatcher() == templateContainer->getRootNode()) {
  203. UITreeEvent *treeEvent = (UITreeEvent*) event;
  204. FolderUserData *data = (FolderUserData *)treeEvent->selection->getUserData();
  205. assetList->showFolder(data->folderPath);
  206. listContainer->setContentSize(assetList->getWidth(), assetList->getHeight());
  207. listContainer->setScrollValue(0,0);
  208. }
  209. }
  210. UIWindow::handleEvent(event);
  211. }
  212. void AssetBrowser::parseFolderIntoTree(UITree *tree, OSFileEntry folder) {
  213. vector<OSFileEntry> templates = OSBasics::parseFolder(folder.fullPath, false);
  214. for(int i=0; i < templates.size(); i++) {
  215. OSFileEntry entry = templates[i];
  216. if(entry.type == OSFileEntry::TYPE_FOLDER) {
  217. UITree *newChild = tree->addTreeChild("folder.png", entry.nameWithoutExtension, NULL);
  218. FolderUserData *data = new FolderUserData();
  219. data->type = 1;
  220. data->folderPath = entry.fullPath;
  221. newChild->setUserData(data);
  222. parseFolderIntoTree(newChild, entry);
  223. }
  224. }
  225. }