main.tscript 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. //---------------------------------------------------------------------------------------------
  23. // Path to the folder that contains the editors we will load.
  24. //---------------------------------------------------------------------------------------------
  25. $Tools::resourcePath = "tools/";
  26. // These must be loaded first, in this order, before anything else is loaded
  27. $Tools::loadFirst = "editorClasses base worldEditor assetBrowser";
  28. //---------------------------------------------------------------------------------------------
  29. // Object that holds the simObject id that the materialEditor uses to interpret its material list
  30. //---------------------------------------------------------------------------------------------
  31. $Tools::materialEditorList = "";
  32. if(!$Tools::loaded)
  33. {
  34. new Settings(EditorSettings) { file = "tools/settings.xml"; };
  35. EditorSettings.read();
  36. ModuleDatabase.scanModules( "tools", false );
  37. ModuleDatabase.LoadGroup( "Tools" );
  38. //We may need to lean on certain EditorSettings, and specifically default values if the settings.xml
  39. //isn't found
  40. exec("tools/worldEditor/scripts/editorPrefs.ed." @ $TorqueScriptFileExtension);
  41. exec( "tools/gui/profiles.ed." @ $TorqueScriptFileExtension );
  42. exec("tools/gui/EditorLoadingGui.gui");
  43. }
  44. function EditorIsActive()
  45. {
  46. return ( isObject(EditorGui) && Canvas.getContent() == EditorGui.getId() );
  47. }
  48. function GuiEditorIsActive()
  49. {
  50. return ( isObject(GuiEditorGui) && Canvas.getContent() == GuiEditorGui.getId() );
  51. }
  52. function loadKeybindings()
  53. {
  54. Parent::loadKeybindings();
  55. }
  56. // Start-up.
  57. function onStart()
  58. {
  59. //First, we want to ensure we don't inadvertently clean up our editor objects by leaving them in the MissionCleanup group, so lets change that real fast
  60. $instantGroup = "";
  61. pushInstantGroup();
  62. echo( " % - Initializing Tools" );
  63. // Default file path when saving from the editor (such as prefabs)
  64. if ($Pref::WorldEditor::LastPath $= "")
  65. {
  66. $Pref::WorldEditor::LastPath = getMainDotCsDir();
  67. }
  68. // Common GUI stuff.
  69. exec( "./gui/cursors.ed." @ $TorqueScriptFileExtension );
  70. exec( "./gui/messageBoxes/messageBox.ed." @ $TorqueScriptFileExtension );
  71. // Make sure we get editor profiles before any GUI's
  72. // BUG: these dialogs are needed earlier in the init sequence, and should be moved to
  73. // common, along with the guiProfiles they depend on.
  74. exec( "./gui/guiDialogs.ed." @ $TorqueScriptFileExtension );
  75. //%toggle = $Scripts::ignoreDSOs;
  76. //$Scripts::ignoreDSOs = true;
  77. $ignoredDatablockSet = new SimSet();
  78. // fill the list of editors
  79. $editors[count] = getWordCount( $Tools::loadFirst );
  80. for ( %i = 0; %i < $editors[count]; %i++ )
  81. {
  82. $editors[%i] = getWord( $Tools::loadFirst, %i );
  83. }
  84. %pattern = $Tools::resourcePath @ "/*/main." @ $TorqueScriptFileExtension;
  85. %folder = findFirstFile( %pattern );
  86. if ( %folder $= "")
  87. {
  88. // if we have absolutely no matches for main.tscript, we look for main.tscript.dso
  89. %pattern = $Tools::resourcePath @ "/*/main." @ $TorqueScriptFileExtension @ ".dso";
  90. %folder = findFirstFile( %pattern );
  91. }
  92. while ( %folder !$= "" )
  93. {
  94. if( filePath( %folder ) !$= "tools" ) // Skip the actual 'tools' folder...we want the children
  95. {
  96. %folder = filePath( %folder );
  97. %editor = fileName( %folder );
  98. if ( IsDirectory( %folder ) )
  99. {
  100. // Yes, this sucks and should be done better
  101. if ( strstr( $Tools::loadFirst, %editor ) == -1 )
  102. {
  103. $editors[$editors[count]] = %editor;
  104. $editors[count]++;
  105. }
  106. }
  107. }
  108. %folder = findNextFile( %pattern );
  109. }
  110. // initialize every editor
  111. new SimSet( EditorPluginSet );
  112. %count = $editors[count];
  113. for ( %i = 0; %i < %count; %i++ )
  114. {
  115. exec( "./" @ $editors[%i] @ "/main." @ $TorqueScriptFileExtension );
  116. %initializeFunction = "initialize" @ $editors[%i];
  117. if( isFunction( %initializeFunction ) )
  118. call( %initializeFunction );
  119. }
  120. // Popuplate the default SimObject icons that
  121. // are used by the various editors.
  122. EditorIconRegistry::loadFromPath( "tools/classIcons/" );
  123. // Load up the tools resources. All the editors are initialized at this point, so
  124. // resources can override, redefine, or add functionality.
  125. Tools::LoadResources( $Tools::resourcePath );
  126. //Now, go through and load any tool-group modules
  127. ModuleDatabase.setModuleExtension("module");
  128. //Any common tool modules
  129. ModuleDatabase.scanModules( "tools", false );
  130. ModuleDatabase.LoadGroup( "Tools" );
  131. //Now that we're done loading, we can set the instant group back
  132. popInstantGroup();
  133. $instantGroup = MissionCleanup;
  134. pushInstantGroup();
  135. $Tools::loaded = true;
  136. }
  137. function startToolTime(%tool)
  138. {
  139. if($toolDataToolCount $= "")
  140. $toolDataToolCount = 0;
  141. if($toolDataToolEntry[%tool] !$= "true")
  142. {
  143. $toolDataToolEntry[%tool] = "true";
  144. $toolDataToolList[$toolDataToolCount] = %tool;
  145. $toolDataToolCount++;
  146. $toolDataClickCount[%tool] = 0;
  147. }
  148. $toolDataStartTime[%tool] = getSimTime();
  149. $toolDataClickCount[%tool]++;
  150. }
  151. function endToolTime(%tool)
  152. {
  153. %startTime = 0;
  154. if($toolDataStartTime[%tool] !$= "")
  155. %startTime = $toolDataStartTime[%tool];
  156. if($toolDataTotalTime[%tool] $= "")
  157. $toolDataTotalTime[%tool] = 0;
  158. $toolDataTotalTime[%tool] += getSimTime() - %startTime;
  159. }
  160. function dumpToolData()
  161. {
  162. %count = $toolDataToolCount;
  163. for(%i=0; %i<%count; %i++)
  164. {
  165. %tool = $toolDataToolList[%i];
  166. %totalTime = $toolDataTotalTime[%tool];
  167. if(%totalTime $= "")
  168. %totalTime = 0;
  169. %clickCount = $toolDataClickCount[%tool];
  170. echo("---");
  171. echo("Tool: " @ %tool);
  172. echo("Time (seconds): " @ %totalTime / 1000);
  173. echo("Activated: " @ %clickCount);
  174. echo("---");
  175. }
  176. }
  177. // Shutdown.
  178. function onExit()
  179. {
  180. if( EditorGui.isInitialized )
  181. EditorGui.shutdown();
  182. // Free all the icon images in the registry.
  183. EditorIconRegistry::clear();
  184. // Save any Layouts we might be using
  185. //GuiFormManager::SaveLayout(LevelBuilder, Default, User);
  186. %count = $editors[count];
  187. for (%i = 0; %i < %count; %i++)
  188. {
  189. %destroyFunction = "destroy" @ $editors[%i];
  190. if( isFunction( %destroyFunction ) )
  191. call( %destroyFunction );
  192. }
  193. // write out our settings xml file
  194. EditorSettings.write();
  195. }
  196. function EditorCreateFakeGameSession(%fileName)
  197. {
  198. // Create a local game server and connect to it.
  199. if(isObject(ServerGroup))
  200. ServerGroup.delete();
  201. new SimGroup(ServerGroup);
  202. if(isObject(ServerConnection))
  203. ServerConnection.delete();
  204. new GameConnection(ServerConnection);
  205. // This calls GameConnection::onConnect.
  206. ServerConnection.connectLocal();
  207. $instantGroup = ServerGroup;
  208. $Game::MissionGroup = "MissionGroup";
  209. exec(%fileName);
  210. }
  211. function fastLoadWorldEdit(%val)
  212. {
  213. if(%val || %val $= "")
  214. {
  215. if(!$Tools::loaded)
  216. {
  217. displayEditorLoadingGui();
  218. onStart();
  219. hideEditorLoadingGui();
  220. }
  221. %timerId = startPrecisionTimer();
  222. if( GuiEditorIsActive() )
  223. toggleGuiEditor(1);
  224. if( !$missionRunning )
  225. {
  226. if(EditorSettings.value("WorldEditor/LevelLoad/LoadMode", "Editor Default Scene") $= "Editor Default Scene")
  227. {
  228. EditorNewLevel("ToolsModule:DefaultEditorLevel");
  229. }
  230. else
  231. {
  232. //go back through our recent levels list to find the most recent valid editor level.
  233. //if NONE work, then just load the default editor scene
  234. %recentLevels = EditorSettings.value("WorldEditor/recentLevelsList");
  235. %recentCount = getTokenCount(%recentLevels, ",");
  236. %loadedScene = false;
  237. for(%i=0; %i < %recentCount; %i++)
  238. {
  239. %recentEntry = getToken(%recentLevels, ",", %i);
  240. if(AssetDatabase.isDeclaredAsset(%recentEntry))
  241. {
  242. EditorOpenMission(%recentEntry);
  243. %loadedScene = true;
  244. break;
  245. }
  246. }
  247. if(!%loadedScene)
  248. {
  249. EditorNewLevel("ToolsModule:DefaultEditorLevel");
  250. }
  251. }
  252. }
  253. else
  254. {
  255. pushInstantGroup();
  256. if ( !isObject( Editor ) )
  257. {
  258. Editor::create();
  259. MissionCleanup.add( Editor );
  260. MissionCleanup.add( Editor.getUndoManager() );
  261. }
  262. if( EditorIsActive() )
  263. {
  264. if (theLevelInfo.type $= "DemoScene")
  265. {
  266. commandToServer('dropPlayerAtCamera');
  267. Editor.close("SceneGui");
  268. }
  269. else
  270. {
  271. %playGUIName = ProjectSettings.value("UI/playGUIName");
  272. Editor.close(%playGUIName);
  273. }
  274. }
  275. else
  276. {
  277. displayEditorLoadingGui();
  278. Editor.open();
  279. // Cancel the scheduled event to prevent
  280. // the level from cycling after it's duration
  281. // has elapsed.
  282. cancel($Game::Schedule);
  283. if (theLevelInfo.type $= "DemoScene")
  284. commandToServer('dropCameraAtPlayer', true);
  285. hideEditorLoadingGui();
  286. }
  287. popInstantGroup();
  288. }
  289. %elapsed = stopPrecisionTimer( %timerId );
  290. warn( "Time spent in toggleEditor() : " @ %elapsed / 1000.0 @ " s" );
  291. }
  292. }
  293. function fastLoadGUIEdit(%val)
  294. {
  295. if(%val)
  296. {
  297. if(!$Tools::loaded)
  298. {
  299. displayEditorLoadingGui();
  300. onStart();
  301. hideEditorLoadingGui();
  302. }
  303. toggleGuiEditor(true);
  304. }
  305. }
  306. function Tools::LoadResources( %path )
  307. {
  308. %resourcesPath = %path @ "resources/";
  309. %resourcesList = getDirectoryList( %resourcesPath );
  310. %wordCount = getFieldCount( %resourcesList );
  311. for( %i = 0; %i < %wordCount; %i++ )
  312. {
  313. %resource = GetField( %resourcesList, %i );
  314. if( isFile( %resourcesPath @ %resource @ "/resourceDatabase." @ $TorqueScriptFileExtension) )
  315. ResourceObject::load( %path, %resource );
  316. }
  317. }
  318. //This lets us fast-load the editor from the menu
  319. GlobalActionMap.bind(keyboard, "F11", fastLoadWorldEdit);
  320. GlobalActionMap.bind(keyboard, "F10", fastLoadGUIEdit);