ToolWindows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 "ToolWindows.h"
  20. #include "PolycodeToolLauncher.h"
  21. TextInputPopup::TextInputPopup() : UIWindow(L"", 300, 80) {
  22. textInput = new UITextInput(false, 290, 12);
  23. addChild(textInput);
  24. textInput->setPosition(padding, 35);
  25. cancelButton = new UIButton(L"Cancel", 100);
  26. cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
  27. addChild(cancelButton);
  28. cancelButton->setPosition(padding+300-100-100-10, 64);
  29. okButton = new UIButton(L"OK", 100);
  30. okButton->addEventListener(this, UIEvent::CLICK_EVENT);
  31. addChild(okButton);
  32. okButton->setPosition(padding+300-100, 64);
  33. closeOnEscape = true;
  34. }
  35. void TextInputPopup::setCaption(String caption) {
  36. setWindowCaption(caption);
  37. }
  38. String TextInputPopup::getValue() {
  39. return textInput->getText();
  40. }
  41. void TextInputPopup::setValue(String value) {
  42. textInput->setText(value);
  43. focusChild(textInput);
  44. }
  45. void TextInputPopup::handleEvent(Event *event) {
  46. if(event->getEventType() == "UIEvent") {
  47. if(event->getEventCode() == UIEvent::CLICK_EVENT) {
  48. if(event->getDispatcher() == okButton) {
  49. dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
  50. }
  51. if(event->getDispatcher() == cancelButton) {
  52. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  53. }
  54. }
  55. }
  56. UIWindow::handleEvent(event);
  57. }
  58. TextInputPopup::~TextInputPopup() {
  59. }
  60. YesNoPopup::YesNoPopup() : UIWindow(L"", 300, 80) {
  61. captionLabel = new UILabel("This is a caption", 12);
  62. addChild(captionLabel);
  63. captionLabel->setPosition(padding, 35);
  64. buttonAnchor = new Entity();
  65. buttonAnchor->processInputEvents = true;
  66. addChild(buttonAnchor);
  67. cancelButton = new UIButton(L"No", 100);
  68. cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
  69. buttonAnchor->addChild(cancelButton);
  70. cancelButton->setPosition(0, 60);
  71. okButton = new UIButton(L"Yes", 100);
  72. okButton->addEventListener(this, UIEvent::CLICK_EVENT);
  73. buttonAnchor->addChild(okButton);
  74. okButton->setPosition(120, 60);
  75. closeOnEscape = true;
  76. }
  77. void YesNoPopup::setCaption(String caption) {
  78. captionLabel->setText(caption);
  79. Number windowSize = captionLabel->getWidth() + 50;
  80. if(windowSize < 400)
  81. windowSize = 400;
  82. setWindowSize(windowSize, 80);
  83. captionLabel->setPosition(padding + (windowSize - captionLabel->getWidth()) / 2.0, 35);
  84. buttonAnchor->setPosition(padding + ((windowSize - 220) / 2.0), 0);
  85. }
  86. void YesNoPopup::handleEvent(Event *event) {
  87. if(event->getEventType() == "UIEvent") {
  88. if(event->getEventCode() == UIEvent::CLICK_EVENT) {
  89. if(event->getDispatcher() == okButton) {
  90. dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
  91. }
  92. if(event->getDispatcher() == cancelButton) {
  93. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  94. dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);
  95. }
  96. }
  97. }
  98. UIWindow::handleEvent(event);
  99. }
  100. YesNoPopup::~YesNoPopup() {
  101. }
  102. YesNoCancelPopup::YesNoCancelPopup() : UIWindow(L"", 300, 80) {
  103. captionLabel = new UILabel("This is a caption", 12);
  104. addChild(captionLabel);
  105. captionLabel->setPosition(padding, 35);
  106. buttonAnchor = new Entity();
  107. buttonAnchor->processInputEvents = true;
  108. addChild(buttonAnchor);
  109. noButton = new UIButton(L"No", 100);
  110. noButton->addEventListener(this, UIEvent::CLICK_EVENT);
  111. buttonAnchor->addChild(noButton);
  112. noButton->setPosition(0, 60);
  113. okButton = new UIButton(L"Yes", 100);
  114. okButton->addEventListener(this, UIEvent::CLICK_EVENT);
  115. buttonAnchor->addChild(okButton);
  116. okButton->setPosition(120, 60);
  117. cancelButton = new UIButton(L"Cancel", 100);
  118. cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
  119. buttonAnchor->addChild(cancelButton);
  120. cancelButton->setPosition(240, 60);
  121. closeOnEscape = true;
  122. }
  123. void YesNoCancelPopup::setCaption(String caption) {
  124. captionLabel->setText(caption);
  125. Number windowSize = captionLabel->getWidth() + 50;
  126. if(windowSize < 400)
  127. windowSize = 400;
  128. setWindowSize(windowSize, 80);
  129. captionLabel->setPosition(padding + (windowSize - captionLabel->getWidth()) / 2.0, 35);
  130. buttonAnchor->setPosition(padding + ((windowSize - 360) / 2.0), 0);
  131. }
  132. void YesNoCancelPopup::handleEvent(Event *event) {
  133. if(event->getEventType() == "UIEvent") {
  134. if(event->getEventCode() == UIEvent::CLICK_EVENT) {
  135. if(event->getDispatcher() == okButton) {
  136. dispatchEvent(new UIEvent(), UIEvent::YES_EVENT);
  137. }
  138. if(event->getDispatcher() == noButton) {
  139. dispatchEvent(new UIEvent(), UIEvent::NO_EVENT);
  140. }
  141. if(event->getDispatcher() == cancelButton) {
  142. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  143. dispatchEvent(new UIEvent(), UIEvent::CANCEL_EVENT);
  144. }
  145. }
  146. }
  147. UIWindow::handleEvent(event);
  148. }
  149. YesNoCancelPopup::~YesNoCancelPopup() {
  150. }
  151. AssetImporterWindow::AssetImporterWindow() : UIWindow("3D Asset Importer", 650, 250) {
  152. filesToImportLabel = new UILabel("Files that will be imported:", 12);
  153. addChild(filesToImportLabel);
  154. filesToImportLabel->setPosition(padding, 35);
  155. filesAnchor = new Entity();
  156. filesScroller = new UIScrollContainer(filesAnchor, true, true, 270, 250);
  157. addChild(filesScroller);
  158. filesScroller->setPosition(padding, 60);
  159. cancelButton = new UIButton(L"Cancel", 100);
  160. cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
  161. addChild(cancelButton);
  162. cancelButton->setPosition(padding+650-100-100-10-10, 235);
  163. okButton = new UIButton(L"OK", 100);
  164. okButton->addEventListener(this, UIEvent::CLICK_EVENT);
  165. addChild(okButton);
  166. okButton->setPosition(padding+650-100-10, 235);
  167. closeOnEscape = true;
  168. prefixInput = new UITextInput(false, 200, 16);
  169. prefixInput->setPosition(290, 30);
  170. addChild(prefixInput);
  171. prefixInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  172. usePrefixCheckbox = new UICheckBox("Custom filename prefix", false);
  173. usePrefixCheckbox->setPosition(290, 60);
  174. addChild(usePrefixCheckbox);
  175. usePrefixCheckbox->addEventListener(this, UIEvent::CHANGE_EVENT);
  176. addMeshesCheckbox = new UICheckBox("Add all meshes to a single mesh", false);
  177. addMeshesCheckbox->setPosition(290, 90);
  178. addChild(addMeshesCheckbox);
  179. addMeshesCheckbox->addEventListener(this, UIEvent::CHANGE_EVENT);
  180. generateTangensCheckbox = new UICheckBox("Generate tangents", true);
  181. generateTangensCheckbox->setPosition(290, 120);
  182. addChild(generateTangensCheckbox);
  183. swapZYAxisCheckbox = new UICheckBox("Swap Z/Y axis (e.g. for Blender)", false);
  184. swapZYAxisCheckbox->setPosition(290, 150);
  185. addChild(swapZYAxisCheckbox);
  186. exportNormals = new UICheckBox("Vertex normals", true);
  187. exportNormals->setPosition(520, 30);
  188. addChild(exportNormals);
  189. exportTangents = new UICheckBox("Vertex tangents", true);
  190. exportTangents->setPosition(520, 60);
  191. addChild(exportTangents);
  192. exportColors = new UICheckBox("Vertex colors", false);
  193. exportColors->setPosition(520, 90);
  194. addChild(exportColors);
  195. exportBoneWeights = new UICheckBox("Bone weights", false);
  196. exportBoneWeights->setPosition(520, 120);
  197. addChild(exportBoneWeights);
  198. exportUVs = new UICheckBox("UV coordinates", true);
  199. exportUVs->setPosition(520, 150);
  200. addChild(exportUVs);
  201. exportSecondaryUVs = new UICheckBox("Secondary UVs", true);
  202. exportSecondaryUVs->setPosition(520, 180);
  203. addChild(exportSecondaryUVs);
  204. }
  205. void AssetImporterWindow::handleEvent(Event *event) {
  206. if(!enabled) {
  207. return;
  208. }
  209. if(event->getDispatcher() == okButton) {
  210. String prefixString;
  211. if(usePrefixCheckbox->isChecked() && prefixInput->getText() != "") {
  212. prefixString = prefixInput->getText().replace(" ", "_");
  213. }
  214. PolycodeToolLauncher::importAssets(file, folder, addMeshesCheckbox->isChecked(), prefixString, swapZYAxisCheckbox->isChecked(), generateTangensCheckbox->isChecked(), false, exportNormals->isChecked(), exportTangents->isChecked(), exportColors->isChecked(), exportBoneWeights->isChecked(), exportUVs->isChecked(), exportSecondaryUVs->isChecked());
  215. dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
  216. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  217. } else if(event->getDispatcher() == cancelButton) {
  218. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  219. } else {
  220. refreshPreview();
  221. }
  222. UIWindow::handleEvent(event);
  223. }
  224. void AssetImporterWindow::clearFiles() {
  225. for(int i=0; i < fileLabels.size(); i++) {
  226. filesAnchor->removeChild(fileLabels[i]);
  227. delete fileLabels[i];
  228. }
  229. fileLabels.clear();
  230. filesScroller->setContentSize(0,0);
  231. }
  232. void AssetImporterWindow::addFile(String fileName) {
  233. UILabel *fileLabel = new UILabel(fileName, 12);
  234. filesAnchor->addChild(fileLabel);
  235. fileLabel->setPosition(0.0, 14 * fileLabels.size());
  236. if(fileLabel->getWidth() > filesScroller->getContentSize().x) {
  237. filesScroller->setContentSize(fileLabel->getWidth(), (fileLabels.size()+1) * 14);
  238. } else {
  239. filesScroller->setContentSize(filesScroller->getContentSize().x, (fileLabels.size()+1) * 14);
  240. }
  241. filesScroller->setScrollValue(0, 0);
  242. fileLabels.push_back(fileLabel);
  243. }
  244. void AssetImporterWindow::setSourceFileAndTargetFolder(String file, String folder) {
  245. this->file = file;
  246. this->folder = folder;
  247. refreshPreview();
  248. }
  249. void AssetImporterWindow::refreshPreview() {
  250. String prefixString;
  251. if(usePrefixCheckbox->isChecked() && prefixInput->getText() != "") {
  252. prefixString = prefixInput->getText().replace(" ", "_");
  253. }
  254. String fileList = PolycodeToolLauncher::importAssets(file, folder, addMeshesCheckbox->isChecked(), prefixString, swapZYAxisCheckbox->isChecked(), generateTangensCheckbox->isChecked(), true, false, false, false, false, false, false);
  255. setFilesToImport(fileList);
  256. }
  257. void AssetImporterWindow::setFilesToImport(String files) {
  258. clearFiles();
  259. if(files == "") {
  260. addFile("NO");
  261. addFile("IMPORTABLE");
  262. addFile("ASSETS");
  263. }else {
  264. std::vector<String> splitFiles = files.split("\n");
  265. for(int i=0; i < splitFiles.size(); i++) {
  266. addFile(splitFiles[i]);
  267. }
  268. }
  269. }
  270. AssetImporterWindow::~AssetImporterWindow() {
  271. }