addModuleWindow.tscript 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. function AssetBrowser_addModuleWindow::onWake(%this)
  23. {
  24. %this-->ModuleName.setText("");
  25. }
  26. function AssetBrowser_addModuleWindow::onGainFirstResponder(%this)
  27. {
  28. %this-->moduleName.setFirstResponder();
  29. }
  30. function AssetBrowser_addModuleWindow::close()
  31. {
  32. Canvas.popDialog(AssetBrowser_addModule);
  33. }
  34. function AssetBrowser_addModuleWindow::CreateNewModule(%this)
  35. {
  36. %newModuleName = %this-->moduleName.getText();
  37. if(%newModuleName $= "")
  38. return;
  39. echo("Creating a new Module named: " @ %newModuleName);
  40. %moduleFilePath = "data/" @ %newModuleName;
  41. %moduleDefinitionFilePath = %moduleFilePath @ "/" @ %newModuleName @ ".module";
  42. %moduleScriptFilePath = %moduleFilePath @ "/" @ %newModuleName @ "." @ $TorqueScriptFileExtension;
  43. %newModule = new ModuleDefinition()
  44. {
  45. ModuleId = %newModuleName;
  46. versionId = 1;
  47. ScriptFile = %newModuleName;
  48. CreateFunction="onCreate";
  49. DestroyFunction="onDestroy";
  50. Group = "Game";
  51. new DeclaredAssets()
  52. {
  53. Extension = "asset.taml";
  54. Recurse = true;
  55. };
  56. };
  57. TAMLWrite(%newModule, %moduleDefinitionFilePath);
  58. //Now generate the script file for it
  59. %file = new FileObject();
  60. %templateFile = new FileObject();
  61. %moduleTemplateCodeFilePath = AssetBrowser.templateFilesPath @ "module." @ $TorqueScriptFileExtension @ ".template";
  62. if(%file.openForWrite(%moduleScriptFilePath) && %templateFile.openForRead(%moduleTemplateCodeFilePath))
  63. {
  64. while( !%templateFile.isEOF() )
  65. {
  66. %line = %templateFile.readline();
  67. %line = strreplace( %line, "@@", %newModuleName );
  68. %file.writeline(%line);
  69. }
  70. %file.close();
  71. %templateFile.close();
  72. }
  73. else
  74. {
  75. %file.close();
  76. %templateFile.close();
  77. warnf("CreateNewModule - Something went wrong and we couldn't write the script file!");
  78. }
  79. //force a refresh of our modules list
  80. ModuleDatabase.ignoreLoadedGroups(true);
  81. ModuleDatabase.registerModule(%moduleFilePath, %newModuleName @ ".module");
  82. %success = ModuleDatabase.loadExplicit(%newModuleName, 1);
  83. ModuleDatabase.ignoreLoadedGroups(false);
  84. //force a reload of the Module lists
  85. AssetBrowser.refresh();
  86. AssetBrowser.newModuleId = %newModuleName;
  87. Canvas.popDialog(AssetBrowser_addModule);
  88. eval(AssetBrowser_addModuleWindow.callbackFunction);
  89. }
  90. function AssetBrowserModuleList::onWake(%this)
  91. {
  92. %this.refresh();
  93. }
  94. function AssetBrowserModuleList::refresh(%this)
  95. {
  96. %this.clear();
  97. //First, get our list of modules
  98. %moduleList = ModuleDatabase.findModules();
  99. %count = getWordCount(%moduleList);
  100. for(%i=0; %i < %count; %i++)
  101. {
  102. %moduleDef = getWord(%moduleList, %i);
  103. %moduleName = %moduleDef.ModuleId;
  104. %this.add(%moduleName, %i);
  105. }
  106. %this.sort();
  107. }
  108. function AssetBrowserSelModuleAddBtn::onClick(%this)
  109. {
  110. AssetBrowser.CreateNewModule("AssetBrowser_selectModule.newModuleAdded();");
  111. }