PolycodeProjectEditor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 "PolycodeProjectEditor.h"
  20. #include "OSBasics.h"
  21. #include "PolycodeFrame.h"
  22. #include "PolycodeConsole.h"
  23. extern UIColorPicker *globalColorPicker;
  24. extern UIGlobalMenu *globalMenu;
  25. extern PolycodeFrame *globalFrame;
  26. PolycodeProjectEditorFactory::PolycodeProjectEditorFactory(PolycodeProjectManager *projectManager) : PolycodeEditorFactory() {
  27. this->projectManager = projectManager;
  28. extensions.push_back("polyproject");
  29. }
  30. PolycodeEditor *PolycodeProjectEditorFactory::createEditor() {
  31. PolycodeProjectEditor *editor = new PolycodeProjectEditor(projectManager);
  32. return editor;
  33. }
  34. ProjectFontEntry::ProjectFontEntry(String fontPath, String fontName) : UIElement() {
  35. this->fontPath = fontPath;
  36. removeButton = new UIImageButton("Images/remove_icon.png");
  37. removeButton->setPosition(0, 5);
  38. removeButton->addEventListener(this, UIEvent::CLICK_EVENT);
  39. addChild(removeButton);
  40. fontNameInput = new UITextInput(false, 100, 13);
  41. fontNameInput->setText(fontName);
  42. fontNameInput->setPosition(20, 0);
  43. fontNameInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  44. addChild(fontNameInput);
  45. OSFileEntry entry = OSFileEntry(fontPath, OSFileEntry::TYPE_FILE);
  46. fontFileLabel = new UILabel(entry.name, 12);
  47. fontFileLabel->color.a = 1.0;
  48. addChild(fontFileLabel);
  49. fontFileLabel->setPosition(140, 3);
  50. CoreServices::getInstance()->getFontManager()->registerFont(fontName, fontPath);
  51. }
  52. void ProjectFontEntry::handleEvent(Event *event) {
  53. if(event->getDispatcher() == fontNameInput && event->getEventCode() == UIEvent::CHANGE_EVENT && event->getEventType() == "UIEvent") {
  54. FontEntry *entry = CoreServices::getInstance()->getFontManager()->getFontEntryByFontPath(fontPath);
  55. if(entry) {
  56. entry->fontName = fontNameInput->getText();
  57. }
  58. }
  59. if(event->getDispatcher() == removeButton && event->getEventCode() == UIEvent::CLICK_EVENT && event->getEventType() == "UIEvent") {
  60. FontEntry *entry = CoreServices::getInstance()->getFontManager()->getFontEntryByFontPath(fontPath);
  61. CoreServices::getInstance()->getFontManager()->removeFontEntry(entry, false);
  62. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  63. }
  64. }
  65. ProjectFontEntry::~ProjectFontEntry() {
  66. }
  67. PolycodeProjectEditor::PolycodeProjectEditor(PolycodeProjectManager *projectManager) : PolycodeEditor(true){
  68. isLoading = true;
  69. this->projectManager = projectManager;
  70. Config *conf = CoreServices::getInstance()->getConfig();
  71. String fontName = conf->getStringValue("Polycode", "uiDefaultFontName");
  72. int fontSize = conf->getNumericValue("Polycode", "uiDefaultFontSize");
  73. Number padding = conf->getNumericValue("Polycode", "uiWindowSkinPadding");
  74. headerBg = new UIRect(10,10);
  75. addChild(headerBg);
  76. headerBg->setAnchorPoint(-1.0, -1.0, 0.0);
  77. headerBg->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderBgColor"));
  78. UILabel *label = new UILabel("PROJECT SETTINGS", 18, "section", Label::ANTIALIAS_FULL);
  79. label->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderFontColor"));
  80. addChild(label);
  81. label->setPosition(10, 3);
  82. moduleSettingsWindow = new UIElement();
  83. moduleSettingsWindow->setPosition(350,10);
  84. addChild(moduleSettingsWindow);
  85. Number lastYPos = 40;
  86. label = new UILabel("PROJECT MODULES", 18, "section", Label::ANTIALIAS_FULL);
  87. label->color.setColorHexFromString(conf->getStringValue("Polycode", "uiSectionFontColor"));
  88. moduleSettingsWindow->addChild(label);
  89. label->setPosition(0, lastYPos);
  90. lastYPos += 40;
  91. String polycodeBasePath = CoreServices::getInstance()->getCore()->getDefaultWorkingDirectory();
  92. std::vector<OSFileEntry> moduleFolders = OSBasics::parseFolder(polycodeBasePath+"/Standalone/Modules", false);
  93. for(int i=0; i < moduleFolders.size(); i++) {
  94. OSFileEntry entry = moduleFolders[i];
  95. if(entry.type == OSFileEntry::TYPE_FOLDER) {
  96. UICheckBox *moduleCheckBox = new UICheckBox(entry.name, false);
  97. moduleCheckBox->setPosition(0, lastYPos);
  98. lastYPos += moduleCheckBox->getHeight() + 5;
  99. moduleSettingsWindow->addChild(moduleCheckBox);
  100. moduleCheckboxes.push_back(moduleCheckBox);
  101. }
  102. }
  103. lastYPos += 20;
  104. label = new UILabel("PROJECT FONTS", 18, "section", Label::ANTIALIAS_FULL);
  105. label->color.setColorHexFromString(conf->getStringValue("Polycode", "uiSectionFontColor"));
  106. moduleSettingsWindow->addChild(label);
  107. label->setPosition(0, lastYPos);
  108. lastYPos += 30;
  109. fontEntryBase = new UIElement();
  110. moduleSettingsWindow->addChild(fontEntryBase);
  111. fontEntryBase->setPosition(0, lastYPos);
  112. addFontButton = new UIButton("Add Font", 100);
  113. fontEntryBase->addChild(addFontButton);
  114. addFontButton->addEventListener(this, UIEvent::CLICK_EVENT);
  115. mainSettingsWindow = new UIElement();
  116. mainSettingsWindow->setPosition(0,10);
  117. addChild(mainSettingsWindow);
  118. UILabel *label2 = new UILabel(L"DEFAULT VIDEO OPTIONS", 18, "section", Label::ANTIALIAS_FULL);
  119. label2->color.setColorHexFromString(conf->getStringValue("Polycode", "uiSectionFontColor"));
  120. mainSettingsWindow->addChild(label2);
  121. label2->setPosition(padding, 40);
  122. label2 = new UILabel(L"Width", fontSize, fontName, Label::ANTIALIAS_FULL);
  123. mainSettingsWindow->addChild(label2);
  124. label2->setPosition(padding + 6, 80);
  125. defaultWidthInput = new UITextInput(false, 60, 12);
  126. mainSettingsWindow->addChild(defaultWidthInput);
  127. defaultWidthInput->setPosition(label2->getPosition().x-6, label2->getPosition().y+18);
  128. defaultWidthInput->setNumberOnly(true);
  129. label2 = new UILabel(L"Height", fontSize, fontName, Label::ANTIALIAS_FULL);
  130. mainSettingsWindow->addChild(label2);
  131. label2->setPosition(padding + 80 + 6, 80);
  132. defaultHeightInput = new UITextInput(false, 60, 12);
  133. mainSettingsWindow->addChild(defaultHeightInput);
  134. defaultHeightInput->setPosition(label2->getPosition().x-6, label2->getPosition().y+18);
  135. defaultHeightInput->setNumberOnly(true);
  136. label2 = new UILabel(L"Anti-aliasing", fontSize, fontName, Label::ANTIALIAS_FULL);
  137. mainSettingsWindow->addChild(label2);
  138. label2->setPosition(padding + 160 + 6, 80);
  139. aaLevelComboBox = new UIComboBox(globalMenu, 120);
  140. aaLevelComboBox->addComboItem("No AA");
  141. aaLevelComboBox->addComboItem("2x MSAA");
  142. aaLevelComboBox->addComboItem("4x MSAA");
  143. aaLevelComboBox->addComboItem("6x MSAA");
  144. aaLevelComboBox->setPosition(label2->getPosition().x-6, label2->getPosition().y+18);
  145. label2 = new UILabel(L"Texture filtering mode:", fontSize, fontName, Label::ANTIALIAS_FULL);
  146. mainSettingsWindow->addChild(label2);
  147. label2->setPosition(padding + 6, defaultHeightInput->getPosition().y+30);
  148. texFilteringComboBox = new UIComboBox(globalMenu, 280);
  149. texFilteringComboBox->addComboItem("Nearest Neighbor");
  150. texFilteringComboBox->addComboItem("Linear");
  151. texFilteringComboBox->setPosition(label2->getPosition().x - 6, label2->getPosition().y+18);
  152. label2 = new UILabel(L"Anisotropic filtering:", fontSize, fontName, Label::ANTIALIAS_FULL);
  153. mainSettingsWindow->addChild(label2);
  154. label2->setPosition(padding + 6, texFilteringComboBox->getPosition().y+30);
  155. afLevelComboBox = new UIComboBox(globalMenu, 280);
  156. afLevelComboBox->addComboItem("No Anisotropic Filtering");
  157. afLevelComboBox->addComboItem("1x Anisotropic Filtering");
  158. afLevelComboBox->addComboItem("2x Anisotropic Filtering");
  159. afLevelComboBox->addComboItem("4x Anisotropic Filtering");
  160. afLevelComboBox->addComboItem("8x Anisotropic Filtering");
  161. afLevelComboBox->addComboItem("16x Anisotropic Filtering");
  162. afLevelComboBox->setPosition(label2->getPosition().x-6, label2->getPosition().y+18);
  163. label2 = new UILabel(L"Framerate", fontSize, fontName, Label::ANTIALIAS_FULL);
  164. mainSettingsWindow->addChild(label2);
  165. label2->setPosition(padding + 6, afLevelComboBox->getPosition().y+30);
  166. framerateInput = new UITextInput(false, 60, 12);
  167. mainSettingsWindow->addChild(framerateInput);
  168. framerateInput->setPosition(label2->getPosition().x-6, label2->getPosition().y+18);
  169. framerateInput->setNumberOnly(true);
  170. vSyncCheckBox = new UICheckBox("V-Sync", false);
  171. vSyncCheckBox->setPosition(label2->getPosition().x + 80, label2->getPosition().y+18);
  172. mainSettingsWindow->addChild(vSyncCheckBox);
  173. label2 = new UILabel(L"STARTUP OPTIONS", 18, "section", Label::ANTIALIAS_FULL);
  174. label2->color.setColorHexFromString(conf->getStringValue("Polycode", "uiSectionFontColor"));
  175. mainSettingsWindow->addChild(label2);
  176. label2->setPosition(padding, vSyncCheckBox->getPosition().y+vSyncCheckBox->getHeight()+20);
  177. label2 = new UILabel(L"Entry point file", fontSize, fontName, Label::ANTIALIAS_FULL);
  178. mainSettingsWindow->addChild(label2);
  179. label2->setPosition(padding + 6, vSyncCheckBox->getPosition().y+80);
  180. entryPointInput = new UITextInput(false, 200, 12);
  181. mainSettingsWindow->addChild(entryPointInput);
  182. entryPointInput->setPosition(label2->getPosition().x - 6, label2->getPosition().y+18);
  183. mainSettingsWindow->addChild(afLevelComboBox);
  184. mainSettingsWindow->addChild(aaLevelComboBox);
  185. mainSettingsWindow->addChild(texFilteringComboBox);
  186. label2 = new UILabel(L"Background color:", fontSize, fontName, Label::ANTIALIAS_FULL);
  187. mainSettingsWindow->addChild(label2);
  188. label2->setPosition(padding, entryPointInput->getPosition().y+entryPointInput->getHeight()+10);
  189. bgColorBox = new UIColorBox(globalColorPicker, Color(1.0, 0.5, 0.0, 0.9), 30,30);
  190. bgColorBox->setPosition(label2->getPosition().x, label2->getPosition().y+18);
  191. mainSettingsWindow->addChild(bgColorBox);
  192. vSyncCheckBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  193. defaultWidthInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  194. defaultHeightInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  195. framerateInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  196. aaLevelComboBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  197. afLevelComboBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  198. texFilteringComboBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  199. entryPointInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  200. bgColorBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  201. isLoading = false;
  202. }
  203. PolycodeProjectEditor::~PolycodeProjectEditor() {
  204. }
  205. void PolycodeProjectEditor::refreshFontEntries() {
  206. for(int i=0; i < fontEntries.size(); i++) {
  207. fontEntries[i]->setPosition(0, 30*i);
  208. }
  209. addFontButton->setPosition(0, (fontEntries.size() * 30) + 10);
  210. if(!isLoading) {
  211. setHasChanges(true);
  212. }
  213. }
  214. void PolycodeProjectEditor::handleEvent(Event *event) {
  215. if(event->getEventType() == "UIEvent") {
  216. if(event->getDispatcher() == vSyncCheckBox || event->getDispatcher() == defaultWidthInput || event->getDispatcher() == defaultHeightInput || event->getDispatcher() == framerateInput || event->getDispatcher() == aaLevelComboBox || event->getDispatcher() == afLevelComboBox || event->getDispatcher() == texFilteringComboBox || event->getDispatcher() == entryPointInput || event->getDispatcher() == bgColorBox) {
  217. if(!isLoading) {
  218. setHasChanges(true);
  219. }
  220. }
  221. }
  222. if(event->getDispatcher() == addFontButton && event->getEventCode() == UIEvent::CLICK_EVENT && event->getEventType() == "UIEvent") {
  223. globalFrame->assetBrowser->addEventListener(this, UIEvent::OK_EVENT);
  224. std::vector<String> extensions;
  225. extensions.push_back("ttf");
  226. extensions.push_back("otf");
  227. globalFrame->showAssetBrowser(extensions);
  228. } else if(event->getDispatcher() == globalFrame->assetBrowser && event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::OK_EVENT) {
  229. String newFontPath = globalFrame->assetBrowser->getSelectedAssetPath();
  230. newFontPath = newFontPath.replace(parentProject->getRootFolder()+"/", "");
  231. bool hasFont = false;
  232. for(int i=0; i < fontEntries.size(); i++) {
  233. if(fontEntries[i]->fontPath == newFontPath) {
  234. hasFont = true;
  235. break;
  236. }
  237. }
  238. if(!hasFont) {
  239. ProjectFontEntry *newEntry = new ProjectFontEntry(newFontPath, "font_name");
  240. newEntry->addEventListener(this, Event::CHANGE_EVENT);
  241. fontEntryBase->addChild(newEntry);
  242. fontEntries.push_back(newEntry);
  243. refreshFontEntries();
  244. }
  245. globalFrame->assetBrowser->removeAllHandlersForListener(this);
  246. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  247. globalFrame->hideModal();
  248. }
  249. bool doRefresh = false;
  250. for(int i=0; i < fontEntries.size(); i++) {
  251. if(event->getDispatcher() == fontEntries[i]) {
  252. fontEntryBase->removeChild(fontEntries[i]);
  253. fontEntries.erase(fontEntries.begin()+i);
  254. doRefresh = true;
  255. break;
  256. }
  257. }
  258. if(doRefresh) {
  259. refreshFontEntries();
  260. }
  261. }
  262. bool PolycodeProjectEditor::openFile(OSFileEntry filePath) {
  263. isLoading = true;
  264. associatedProject = projectManager->getProjectByProjectFile(filePath.fullPath);
  265. if(!associatedProject) {
  266. return false;
  267. }
  268. entryPointInput->setText(associatedProject->data.entryPoint);
  269. defaultWidthInput->setText(String::IntToString(associatedProject->data.defaultWidth));
  270. defaultHeightInput->setText(String::IntToString(associatedProject->data.defaultHeight));
  271. vSyncCheckBox->setChecked(associatedProject->data.vSync);
  272. unsigned int aaMap[7] = {0,1,1,1,2,2,3};
  273. aaLevelComboBox->setSelectedIndex(aaMap[associatedProject->data.aaLevel]);
  274. unsigned int afMap[17] = {0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5};
  275. afLevelComboBox->setSelectedIndex(afMap[associatedProject->data.anisotropy]);
  276. framerateInput->setText(String::IntToString(associatedProject->data.frameRate));
  277. if(associatedProject->data.filteringMode == "nearest") {
  278. texFilteringComboBox->setSelectedIndex(0);
  279. } else {
  280. texFilteringComboBox->setSelectedIndex(1);
  281. }
  282. for(int i=0; i < associatedProject->data.fonts.size(); i++) {
  283. ProjectFontData fontData = associatedProject->data.fonts[i];
  284. ProjectFontEntry *newEntry = new ProjectFontEntry(fontData.fontPath, fontData.fontName);
  285. newEntry->addEventListener(this, Event::CHANGE_EVENT);
  286. fontEntryBase->addChild(newEntry);
  287. fontEntries.push_back(newEntry);
  288. }
  289. for(int i=0; i < associatedProject->data.modules.size(); i++) {
  290. bool hasModule = false;
  291. for(int j=0; j < moduleCheckboxes.size(); j++) {
  292. if(moduleCheckboxes[j]->getCaptionLabel() == associatedProject->data.modules[i]) {
  293. moduleCheckboxes[j]->setChecked(true);
  294. hasModule = true;
  295. }
  296. }
  297. if(!hasModule) {
  298. PolycodeConsole::print("WARNING: MISSING MODULE: "+associatedProject->data.modules[i]);
  299. }
  300. }
  301. bgColorBox->setBoxColor(Color(associatedProject->data.backgroundColorR, associatedProject->data.backgroundColorG, associatedProject->data.backgroundColorB, 1.0));
  302. PolycodeEditor::openFile(filePath);
  303. refreshFontEntries();
  304. isLoading = false;
  305. return true;
  306. }
  307. void PolycodeProjectEditor::Resize(int x, int y) {
  308. headerBg->Resize(x, 30);
  309. PolycodeEditor::Resize(x,y);
  310. }
  311. void PolycodeProjectEditor::saveFile() {
  312. if(!associatedProject) {
  313. return;
  314. }
  315. associatedProject->data.frameRate = atoi(framerateInput->getText().c_str());
  316. associatedProject->data.defaultWidth = atoi(defaultWidthInput->getText().c_str());
  317. associatedProject->data.defaultHeight = atoi(defaultHeightInput->getText().c_str());
  318. associatedProject->data.entryPoint = entryPointInput->getText();
  319. associatedProject->data.backgroundColorR = bgColorBox->getSelectedColor().r;
  320. associatedProject->data.backgroundColorG = bgColorBox->getSelectedColor().g;
  321. associatedProject->data.backgroundColorB = bgColorBox->getSelectedColor().b;
  322. associatedProject->data.modules.clear();
  323. for(int j=0; j < moduleCheckboxes.size(); j++) {
  324. if(moduleCheckboxes[j]->isChecked()) {
  325. associatedProject->data.modules.push_back(moduleCheckboxes[j]->getCaptionLabel());
  326. }
  327. }
  328. associatedProject->data.fonts.clear();
  329. for(int j=0; j < fontEntries.size(); j++) {
  330. ProjectFontData fontData = ProjectFontData(fontEntries[j]->fontNameInput->getText(), fontEntries[j]->fontPath);
  331. associatedProject->data.fonts.push_back(fontData);
  332. }
  333. unsigned int afMap[6] = {0,1,2,4,8,16};
  334. unsigned int aaMap[4] = {0,2,4,6};
  335. String filteringMap[2] = {"nearest", "linear"};
  336. associatedProject->data.filteringMode = filteringMap[texFilteringComboBox->getSelectedIndex()];
  337. associatedProject->data.aaLevel = aaMap[aaLevelComboBox->getSelectedIndex()];
  338. associatedProject->data.anisotropy = afMap[afLevelComboBox->getSelectedIndex()];
  339. associatedProject->data.vSync = vSyncCheckBox->isChecked();
  340. associatedProject->saveFile();
  341. setHasChanges(false);
  342. }