addModuleWindow.tscript 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. eval(AssetBrowser_addModuleWindow.callbackFunction);
  34. }
  35. function AssetBrowser_addModuleWindow::CreateNewModule(%this)
  36. {
  37. %newModuleName = %this-->moduleName.getText();
  38. if(%newModuleName $= "")
  39. return;
  40. echo("Creating a new Module named: " @ %newModuleName);
  41. %moduleFilePath = "data/" @ %newModuleName;
  42. %moduleDefinitionFilePath = %moduleFilePath @ "/" @ %newModuleName @ ".module";
  43. %moduleScriptFilePath = %moduleFilePath @ "/" @ %newModuleName @ "." @ $TorqueScriptFileExtension;
  44. %newModule = new ModuleDefinition()
  45. {
  46. ModuleId = %newModuleName;
  47. versionId = 1;
  48. ScriptFile = %newModuleName @ "." @ $TorqueScriptFileExtension;
  49. CreateFunction="onCreate";
  50. DestroyFunction="onDestroy";
  51. Group = "Game";
  52. new DeclaredAssets()
  53. {
  54. Extension = "asset.taml";
  55. Recurse = true;
  56. };
  57. };
  58. TAMLWrite(%newModule, %moduleDefinitionFilePath);
  59. //Now generate the script file for it
  60. %file = new FileObject();
  61. %templateFile = new FileObject();
  62. %moduleTemplateCodeFilePath = AssetBrowser.templateFilesPath @ "module." @ $TorqueScriptFileExtension @ ".template";
  63. if(%file.openForWrite(%moduleScriptFilePath) && %templateFile.openForRead(%moduleTemplateCodeFilePath))
  64. {
  65. while( !%templateFile.isEOF() )
  66. {
  67. %line = %templateFile.readline();
  68. %line = strreplace( %line, "@@", %newModuleName );
  69. %file.writeline(%line);
  70. echo(%line);
  71. }
  72. %file.close();
  73. %templateFile.close();
  74. }
  75. else
  76. {
  77. %file.close();
  78. %templateFile.close();
  79. warnf("CreateNewModule - Something went wrong and we couldn't write the script file!");
  80. }
  81. //force a refresh of our modules list
  82. ModuleDatabase.ignoreLoadedGroups(true);
  83. ModuleDatabase.scanModules();
  84. %success = ModuleDatabase.loadExplicit(%newModuleName, 1);
  85. ModuleDatabase.ignoreLoadedGroups(false);
  86. //force a reload of the Module lists
  87. AssetBrowser.refresh();
  88. AssetBrowser.newModuleId = %newModuleName;
  89. Canvas.popDialog(AssetBrowser_addModule);
  90. eval(AssetBrowser_addModuleWindow.callbackFunction);
  91. }
  92. function AssetBrowserModuleList::onWake(%this)
  93. {
  94. %this.refresh();
  95. }
  96. function AssetBrowserModuleList::refresh(%this)
  97. {
  98. %this.clear();
  99. //First, get our list of modules
  100. %moduleList = ModuleDatabase.findModules();
  101. %count = getWordCount(%moduleList);
  102. for(%i=0; %i < %count; %i++)
  103. {
  104. %moduleName = getWord(%moduleList, %i);
  105. %this.add(%moduleName.ModuleId, %i);
  106. }
  107. }