main.tscript 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. exec( "./editorClasses/gui/panels/navPanelProfiles.ed." @ $TorqueScriptFileExtension );
  72. // Make sure we get editor profiles before any GUI's
  73. // BUG: these dialogs are needed earlier in the init sequence, and should be moved to
  74. // common, along with the guiProfiles they depend on.
  75. exec( "./gui/guiDialogs.ed." @ $TorqueScriptFileExtension );
  76. //%toggle = $Scripts::ignoreDSOs;
  77. //$Scripts::ignoreDSOs = true;
  78. $ignoredDatablockSet = new SimSet();
  79. // fill the list of editors
  80. $editors[count] = getWordCount( $Tools::loadFirst );
  81. for ( %i = 0; %i < $editors[count]; %i++ )
  82. {
  83. $editors[%i] = getWord( $Tools::loadFirst, %i );
  84. }
  85. %pattern = $Tools::resourcePath @ "/*/main." @ $TorqueScriptFileExtension;
  86. %folder = findFirstFile( %pattern );
  87. if ( %folder $= "")
  88. {
  89. // if we have absolutely no matches for main.tscript, we look for main.tscript.dso
  90. %pattern = $Tools::resourcePath @ "/*/main." @ $TorqueScriptFileExtension @ ".dso";
  91. %folder = findFirstFile( %pattern );
  92. }
  93. while ( %folder !$= "" )
  94. {
  95. if( filePath( %folder ) !$= "tools" ) // Skip the actual 'tools' folder...we want the children
  96. {
  97. %folder = filePath( %folder );
  98. %editor = fileName( %folder );
  99. if ( IsDirectory( %folder ) )
  100. {
  101. // Yes, this sucks and should be done better
  102. if ( strstr( $Tools::loadFirst, %editor ) == -1 )
  103. {
  104. $editors[$editors[count]] = %editor;
  105. $editors[count]++;
  106. }
  107. }
  108. }
  109. %folder = findNextFile( %pattern );
  110. }
  111. // initialize every editor
  112. new SimSet( EditorPluginSet );
  113. %count = $editors[count];
  114. for ( %i = 0; %i < %count; %i++ )
  115. {
  116. exec( "./" @ $editors[%i] @ "/main." @ $TorqueScriptFileExtension );
  117. %initializeFunction = "initialize" @ $editors[%i];
  118. if( isFunction( %initializeFunction ) )
  119. call( %initializeFunction );
  120. }
  121. // Popuplate the default SimObject icons that
  122. // are used by the various editors.
  123. EditorIconRegistry::loadFromPath( "tools/classIcons/" );
  124. // Load up the tools resources. All the editors are initialized at this point, so
  125. // resources can override, redefine, or add functionality.
  126. Tools::LoadResources( $Tools::resourcePath );
  127. //Now, go through and load any tool-group modules
  128. ModuleDatabase.setModuleExtension("module");
  129. //Any common tool modules
  130. ModuleDatabase.scanModules( "tools", false );
  131. ModuleDatabase.LoadGroup( "Tools" );
  132. //Now that we're done loading, we can set the instant group back
  133. popInstantGroup();
  134. $instantGroup = MissionCleanup;
  135. pushInstantGroup();
  136. $Tools::loaded = true;
  137. }
  138. function startToolTime(%tool)
  139. {
  140. if($toolDataToolCount $= "")
  141. $toolDataToolCount = 0;
  142. if($toolDataToolEntry[%tool] !$= "true")
  143. {
  144. $toolDataToolEntry[%tool] = "true";
  145. $toolDataToolList[$toolDataToolCount] = %tool;
  146. $toolDataToolCount++;
  147. $toolDataClickCount[%tool] = 0;
  148. }
  149. $toolDataStartTime[%tool] = getSimTime();
  150. $toolDataClickCount[%tool]++;
  151. }
  152. function endToolTime(%tool)
  153. {
  154. %startTime = 0;
  155. if($toolDataStartTime[%tool] !$= "")
  156. %startTime = $toolDataStartTime[%tool];
  157. if($toolDataTotalTime[%tool] $= "")
  158. $toolDataTotalTime[%tool] = 0;
  159. $toolDataTotalTime[%tool] += getSimTime() - %startTime;
  160. }
  161. function dumpToolData()
  162. {
  163. %count = $toolDataToolCount;
  164. for(%i=0; %i<%count; %i++)
  165. {
  166. %tool = $toolDataToolList[%i];
  167. %totalTime = $toolDataTotalTime[%tool];
  168. if(%totalTime $= "")
  169. %totalTime = 0;
  170. %clickCount = $toolDataClickCount[%tool];
  171. echo("---");
  172. echo("Tool: " @ %tool);
  173. echo("Time (seconds): " @ %totalTime / 1000);
  174. echo("Activated: " @ %clickCount);
  175. echo("---");
  176. }
  177. }
  178. // Shutdown.
  179. function onExit()
  180. {
  181. if( EditorGui.isInitialized )
  182. EditorGui.shutdown();
  183. // Free all the icon images in the registry.
  184. EditorIconRegistry::clear();
  185. // Save any Layouts we might be using
  186. //GuiFormManager::SaveLayout(LevelBuilder, Default, User);
  187. %count = $editors[count];
  188. for (%i = 0; %i < %count; %i++)
  189. {
  190. %destroyFunction = "destroy" @ $editors[%i];
  191. if( isFunction( %destroyFunction ) )
  192. call( %destroyFunction );
  193. }
  194. // write out our settings xml file
  195. EditorSettings.write();
  196. }
  197. function EditorCreateFakeGameSession(%fileName)
  198. {
  199. // Create a local game server and connect to it.
  200. if(isObject(ServerGroup))
  201. ServerGroup.delete();
  202. new SimGroup(ServerGroup);
  203. if(isObject(ServerConnection))
  204. ServerConnection.delete();
  205. new GameConnection(ServerConnection);
  206. // This calls GameConnection::onConnect.
  207. ServerConnection.connectLocal();
  208. $instantGroup = ServerGroup;
  209. $Game::MissionGroup = "MissionGroup";
  210. exec(%fileName);
  211. }
  212. function fastLoadWorldEdit(%val)
  213. {
  214. if(%val || %val $= "")
  215. {
  216. if(!$Tools::loaded)
  217. {
  218. displayEditorLoadingGui();
  219. onStart();
  220. hideEditorLoadingGui();
  221. }
  222. %timerId = startPrecisionTimer();
  223. if( GuiEditorIsActive() )
  224. toggleGuiEditor(1);
  225. if( !$missionRunning )
  226. {
  227. EditorNewLevel("ToolsModule:DefaultEditorLevel");
  228. }
  229. else
  230. {
  231. pushInstantGroup();
  232. if ( !isObject( Editor ) )
  233. {
  234. Editor::create();
  235. MissionCleanup.add( Editor );
  236. MissionCleanup.add( Editor.getUndoManager() );
  237. }
  238. if( EditorIsActive() )
  239. {
  240. if (theLevelInfo.type $= "DemoScene")
  241. {
  242. commandToServer('dropPlayerAtCamera');
  243. Editor.close("SceneGui");
  244. }
  245. else
  246. {
  247. %playGUIName = ProjectSettings.value("UI/playGUIName");
  248. Editor.close(%playGUIName);
  249. }
  250. }
  251. else
  252. {
  253. displayEditorLoadingGui();
  254. Editor.open();
  255. // Cancel the scheduled event to prevent
  256. // the level from cycling after it's duration
  257. // has elapsed.
  258. cancel($Game::Schedule);
  259. if (theLevelInfo.type $= "DemoScene")
  260. commandToServer('dropCameraAtPlayer', true);
  261. hideEditorLoadingGui();
  262. }
  263. popInstantGroup();
  264. }
  265. %elapsed = stopPrecisionTimer( %timerId );
  266. warn( "Time spent in toggleEditor() : " @ %elapsed / 1000.0 @ " s" );
  267. }
  268. }
  269. function fastLoadGUIEdit(%val)
  270. {
  271. if(%val)
  272. {
  273. if(!$Tools::loaded)
  274. {
  275. displayEditorLoadingGui();
  276. onStart();
  277. hideEditorLoadingGui();
  278. }
  279. toggleGuiEditor(true);
  280. }
  281. }
  282. function Tools::LoadResources( %path )
  283. {
  284. %resourcesPath = %path @ "resources/";
  285. %resourcesList = getDirectoryList( %resourcesPath );
  286. %wordCount = getFieldCount( %resourcesList );
  287. for( %i = 0; %i < %wordCount; %i++ )
  288. {
  289. %resource = GetField( %resourcesList, %i );
  290. if( isFile( %resourcesPath @ %resource @ "/resourceDatabase." @ $TorqueScriptFileExtension) )
  291. ResourceObject::load( %path, %resource );
  292. }
  293. }
  294. //This lets us fast-load the editor from the menu
  295. GlobalActionMap.bind(keyboard, "F11", fastLoadWorldEdit);
  296. GlobalActionMap.bind(keyboard, "F10", fastLoadGUIEdit);