main.tscript 13 KB

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