main.tscript 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 windowConsole";
  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 @ ".dso";
  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;
  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. //Next, scrape through modules and scan for tools subfolders there
  111. %pattern = "data/*/editor." @ $TorqueScriptFileExtension @ ".dso";
  112. %folder = findFirstFile( %pattern );
  113. if ( %folder $= "")
  114. {
  115. // if we have absolutely no matches for main.tscript, we look for main.tscript.dso
  116. %pattern = "data/*/editor." @ $TorqueScriptFileExtension;
  117. %folder = findFirstFile( %pattern );
  118. }
  119. while ( %folder !$= "" )
  120. {
  121. if( filePath( %folder ) !$= "tools" ) // Skip the actual 'tools' folder...we want the children
  122. {
  123. %folder = filePath( %folder );
  124. %editor = fileName( %folder );
  125. if ( IsDirectory( %folder ) )
  126. {
  127. // Yes, this sucks and should be done better
  128. if ( strstr( $Tools::loadFirst, %editor ) == -1 )
  129. {
  130. $editors[$editors[count]] = %editor;
  131. $editorsPath[$editors[count]] = %folder;
  132. $editors[count]++;
  133. }
  134. }
  135. }
  136. %folder = findNextFile( %pattern );
  137. }
  138. // initialize every editor
  139. new SimSet( EditorPluginSet );
  140. %count = $editors[count];
  141. for ( %i = 0; %i < %count; %i++ )
  142. {
  143. %editorFilename = "./" @ $editors[%i] @ "/main." @ $TorqueScriptFileExtension;
  144. if(isFile(%editorFilename))
  145. exec( "./" @ $editors[%i] @ "/main." @ $TorqueScriptFileExtension );
  146. else
  147. {
  148. if($editorsPath[%i] !$= "")
  149. exec( $editorsPath[%i] @ "/editor." @ $TorqueScriptFileExtension );
  150. }
  151. %initializeFunction = "initialize" @ $editors[%i];
  152. if( isFunction( %initializeFunction ) )
  153. call( %initializeFunction );
  154. }
  155. // Popuplate the default SimObject icons that
  156. // are used by the various editors.
  157. EditorIconRegistry::loadFromPath( "tools/classIcons/" );
  158. // Load up the tools resources. All the editors are initialized at this point, so
  159. // resources can override, redefine, or add functionality.
  160. Tools::LoadResources( $Tools::resourcePath );
  161. //Now, go through and load any tool-group modules
  162. ModuleDatabase.setModuleExtension("module");
  163. //Any common tool modules
  164. ModuleDatabase.scanModules( "tools", false );
  165. ModuleDatabase.LoadGroup( "Tools" );
  166. //Now that we're done loading, we can set the instant group back
  167. popInstantGroup();
  168. $instantGroup = MissionCleanup;
  169. pushInstantGroup();
  170. $Tools::loaded = true;
  171. }
  172. function startToolTime(%tool)
  173. {
  174. if($toolDataToolCount $= "")
  175. $toolDataToolCount = 0;
  176. if($toolDataToolEntry[%tool] !$= "true")
  177. {
  178. $toolDataToolEntry[%tool] = "true";
  179. $toolDataToolList[$toolDataToolCount] = %tool;
  180. $toolDataToolCount++;
  181. $toolDataClickCount[%tool] = 0;
  182. }
  183. $toolDataStartTime[%tool] = getSimTime();
  184. $toolDataClickCount[%tool]++;
  185. }
  186. function endToolTime(%tool)
  187. {
  188. %startTime = 0;
  189. if($toolDataStartTime[%tool] !$= "")
  190. %startTime = $toolDataStartTime[%tool];
  191. if($toolDataTotalTime[%tool] $= "")
  192. $toolDataTotalTime[%tool] = 0;
  193. $toolDataTotalTime[%tool] += getSimTime() - %startTime;
  194. }
  195. function dumpToolData()
  196. {
  197. %count = $toolDataToolCount;
  198. for(%i=0; %i<%count; %i++)
  199. {
  200. %tool = $toolDataToolList[%i];
  201. %totalTime = $toolDataTotalTime[%tool];
  202. if(%totalTime $= "")
  203. %totalTime = 0;
  204. %clickCount = $toolDataClickCount[%tool];
  205. echo("---");
  206. echo("Tool: " @ %tool);
  207. echo("Time (seconds): " @ %totalTime / 1000);
  208. echo("Activated: " @ %clickCount);
  209. echo("---");
  210. }
  211. }
  212. // Shutdown.
  213. function onExit()
  214. {
  215. if( EditorGui.isInitialized )
  216. EditorGui.shutdown();
  217. // Free all the icon images in the registry.
  218. EditorIconRegistry::clear();
  219. // Save any Layouts we might be using
  220. //GuiFormManager::SaveLayout(LevelBuilder, Default, User);
  221. %count = $editors[count];
  222. for (%i = 0; %i < %count; %i++)
  223. {
  224. %destroyFunction = "destroy" @ $editors[%i];
  225. if( isFunction( %destroyFunction ) )
  226. call( %destroyFunction );
  227. }
  228. // write out our settings xml file
  229. EditorSettings.write();
  230. }
  231. function EditorCreateFakeGameSession(%fileName)
  232. {
  233. // Create a local game server and connect to it.
  234. if(isObject(ServerGroup))
  235. ServerGroup.delete();
  236. new SimGroup(ServerGroup);
  237. if(isObject(ServerConnection))
  238. ServerConnection.delete();
  239. new GameConnection(ServerConnection);
  240. // This calls GameConnection::onConnect.
  241. ServerConnection.connectLocal();
  242. $instantGroup = ServerGroup;
  243. $Game::MissionGroup = "MissionGroup";
  244. exec(%fileName);
  245. }
  246. function fastLoadWorldEdit(%val)
  247. {
  248. if(%val || %val $= "")
  249. {
  250. if(!$Tools::loaded)
  251. {
  252. displayEditorLoadingGui();
  253. onStart();
  254. hideEditorLoadingGui();
  255. }
  256. %timerId = startPrecisionTimer();
  257. if( GuiEditorIsActive() )
  258. toggleGuiEditor(1);
  259. if( !$missionRunning )
  260. {
  261. if(EditorSettings.value("WorldEditor/LevelLoad/LoadMode", "Editor Default Scene") $= "Editor Default Scene")
  262. {
  263. EditorNewLevel("ToolsModule:DefaultEditorLevel");
  264. }
  265. else
  266. {
  267. //go back through our recent levels list to find the most recent valid editor level.
  268. //if NONE work, then just load the default editor scene
  269. %recentLevels = EditorSettings.value("WorldEditor/recentLevelsList");
  270. %recentCount = getTokenCount(%recentLevels, ",");
  271. %loadedScene = false;
  272. for(%i=0; %i < %recentCount; %i++)
  273. {
  274. %recentEntry = getToken(%recentLevels, ",", %i);
  275. if(AssetDatabase.isDeclaredAsset(%recentEntry))
  276. {
  277. EditorOpenMission(%recentEntry);
  278. %loadedScene = true;
  279. break;
  280. }
  281. }
  282. if(!%loadedScene)
  283. {
  284. EditorNewLevel("ToolsModule:DefaultEditorLevel");
  285. }
  286. }
  287. }
  288. else
  289. {
  290. pushInstantGroup();
  291. if ( !isObject( Editor ) )
  292. {
  293. Editor::create();
  294. MissionCleanup.add( Editor );
  295. MissionCleanup.add( Editor.getUndoManager() );
  296. }
  297. if( EditorIsActive() )
  298. {
  299. if (theLevelInfo.type $= "DemoScene")
  300. {
  301. commandToServer('dropPlayerAtCamera');
  302. Editor.close("SceneGui");
  303. }
  304. else
  305. {
  306. %playGUIName = ProjectSettings.value("UI/playGUIName");
  307. Editor.close(%playGUIName);
  308. }
  309. }
  310. else
  311. {
  312. displayEditorLoadingGui();
  313. Editor.open();
  314. // Cancel the scheduled event to prevent
  315. // the level from cycling after it's duration
  316. // has elapsed.
  317. cancel($Game::Schedule);
  318. if (theLevelInfo.type $= "DemoScene")
  319. commandToServer('dropCameraAtPlayer', true);
  320. hideEditorLoadingGui();
  321. }
  322. popInstantGroup();
  323. }
  324. %elapsed = stopPrecisionTimer( %timerId );
  325. warn( "Time spent in toggleEditor() : " @ %elapsed / 1000.0 @ " s" );
  326. }
  327. }
  328. function fastLoadGUIEdit(%val)
  329. {
  330. if(%val)
  331. {
  332. if(!$Tools::loaded)
  333. {
  334. displayEditorLoadingGui();
  335. onStart();
  336. hideEditorLoadingGui();
  337. }
  338. toggleGuiEditor(true);
  339. }
  340. }
  341. function Tools::LoadResources( %path )
  342. {
  343. %resourcesPath = %path @ "resources/";
  344. %resourcesList = getDirectoryList( %resourcesPath );
  345. %wordCount = getFieldCount( %resourcesList );
  346. for( %i = 0; %i < %wordCount; %i++ )
  347. {
  348. %resource = GetField( %resourcesList, %i );
  349. if( isFile( %resourcesPath @ %resource @ "/resourceDatabase." @ $TorqueScriptFileExtension) )
  350. ResourceObject::load( %path, %resource );
  351. }
  352. }
  353. //This lets us fast-load the editor from the menu
  354. GlobalActionMap.bind(keyboard, "F11", fastLoadWorldEdit);
  355. GlobalActionMap.bind(keyboard, "F10", fastLoadGUIEdit);