main.cs 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";
  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. //We may need to lean on certain EditorSettings, and specifically default values if the settings.xml
  37. //isn't found
  38. exec("tools/worldEditor/scripts/editorPrefs.ed.cs");
  39. exec( "tools/gui/profiles.ed.cs" );
  40. exec("tools/gui/EditorLoadingGui.gui");
  41. }
  42. function EditorIsActive()
  43. {
  44. return ( isObject(EditorGui) && Canvas.getContent() == EditorGui.getId() );
  45. }
  46. function GuiEditorIsActive()
  47. {
  48. return ( isObject(GuiEditorGui) && Canvas.getContent() == GuiEditorGui.getId() );
  49. }
  50. function loadKeybindings()
  51. {
  52. Parent::loadKeybindings();
  53. }
  54. // Start-up.
  55. function onStart()
  56. {
  57. //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
  58. $instantGroup = "";
  59. pushInstantGroup();
  60. echo( " % - Initializing Tools" );
  61. // Default file path when saving from the editor (such as prefabs)
  62. if ($Pref::WorldEditor::LastPath $= "")
  63. {
  64. $Pref::WorldEditor::LastPath = getMainDotCsDir();
  65. }
  66. // Common GUI stuff.
  67. exec( "./gui/cursors.ed.cs" );
  68. exec( "./gui/messageBoxes/messageBox.ed.cs" );
  69. exec( "./editorClasses/gui/panels/navPanelProfiles.ed.cs" );
  70. // Make sure we get editor profiles before any GUI's
  71. // BUG: these dialogs are needed earlier in the init sequence, and should be moved to
  72. // common, along with the guiProfiles they depend on.
  73. exec( "./gui/guiDialogs.ed.cs" );
  74. //%toggle = $Scripts::ignoreDSOs;
  75. //$Scripts::ignoreDSOs = true;
  76. $ignoredDatablockSet = new SimSet();
  77. // fill the list of editors
  78. $editors[count] = getWordCount( $Tools::loadFirst );
  79. for ( %i = 0; %i < $editors[count]; %i++ )
  80. {
  81. $editors[%i] = getWord( $Tools::loadFirst, %i );
  82. }
  83. %pattern = $Tools::resourcePath @ "/*/main.cs";
  84. %folder = findFirstFile( %pattern );
  85. if ( %folder $= "")
  86. {
  87. // if we have absolutely no matches for main.cs, we look for main.cs.dso
  88. %pattern = $Tools::resourcePath @ "/*/main.cs.dso";
  89. %folder = findFirstFile( %pattern );
  90. }
  91. while ( %folder !$= "" )
  92. {
  93. if( filePath( %folder ) !$= "tools" ) // Skip the actual 'tools' folder...we want the children
  94. {
  95. %folder = filePath( %folder );
  96. %editor = fileName( %folder );
  97. if ( IsDirectory( %folder ) )
  98. {
  99. // Yes, this sucks and should be done better
  100. if ( strstr( $Tools::loadFirst, %editor ) == -1 )
  101. {
  102. $editors[$editors[count]] = %editor;
  103. $editors[count]++;
  104. }
  105. }
  106. }
  107. %folder = findNextFile( %pattern );
  108. }
  109. // initialize every editor
  110. new SimSet( EditorPluginSet );
  111. %count = $editors[count];
  112. for ( %i = 0; %i < %count; %i++ )
  113. {
  114. exec( "./" @ $editors[%i] @ "/main.cs" );
  115. %initializeFunction = "initialize" @ $editors[%i];
  116. if( isFunction( %initializeFunction ) )
  117. call( %initializeFunction );
  118. }
  119. // Popuplate the default SimObject icons that
  120. // are used by the various editors.
  121. EditorIconRegistry::loadFromPath( "tools/classIcons/" );
  122. // Load up the tools resources. All the editors are initialized at this point, so
  123. // resources can override, redefine, or add functionality.
  124. Tools::LoadResources( $Tools::resourcePath );
  125. //Now, go through and load any tool-group modules
  126. ModuleDatabase.setModuleExtension("module");
  127. //Any common tool modules
  128. ModuleDatabase.scanModules( "tools", false );
  129. ModuleDatabase.LoadGroup( "Tools" );
  130. //Now that we're done loading, we can set the instant group back
  131. popInstantGroup();
  132. $instantGroup = MissionCleanup;
  133. pushInstantGroup();
  134. $Tools::loaded = true;
  135. }
  136. function startToolTime(%tool)
  137. {
  138. if($toolDataToolCount $= "")
  139. $toolDataToolCount = 0;
  140. if($toolDataToolEntry[%tool] !$= "true")
  141. {
  142. $toolDataToolEntry[%tool] = "true";
  143. $toolDataToolList[$toolDataToolCount] = %tool;
  144. $toolDataToolCount++;
  145. $toolDataClickCount[%tool] = 0;
  146. }
  147. $toolDataStartTime[%tool] = getSimTime();
  148. $toolDataClickCount[%tool]++;
  149. }
  150. function endToolTime(%tool)
  151. {
  152. %startTime = 0;
  153. if($toolDataStartTime[%tool] !$= "")
  154. %startTime = $toolDataStartTime[%tool];
  155. if($toolDataTotalTime[%tool] $= "")
  156. $toolDataTotalTime[%tool] = 0;
  157. $toolDataTotalTime[%tool] += getSimTime() - %startTime;
  158. }
  159. function dumpToolData()
  160. {
  161. %count = $toolDataToolCount;
  162. for(%i=0; %i<%count; %i++)
  163. {
  164. %tool = $toolDataToolList[%i];
  165. %totalTime = $toolDataTotalTime[%tool];
  166. if(%totalTime $= "")
  167. %totalTime = 0;
  168. %clickCount = $toolDataClickCount[%tool];
  169. echo("---");
  170. echo("Tool: " @ %tool);
  171. echo("Time (seconds): " @ %totalTime / 1000);
  172. echo("Activated: " @ %clickCount);
  173. echo("---");
  174. }
  175. }
  176. // Shutdown.
  177. function onExit()
  178. {
  179. if( EditorGui.isInitialized )
  180. EditorGui.shutdown();
  181. // Free all the icon images in the registry.
  182. EditorIconRegistry::clear();
  183. // Save any Layouts we might be using
  184. //GuiFormManager::SaveLayout(LevelBuilder, Default, User);
  185. %count = $editors[count];
  186. for (%i = 0; %i < %count; %i++)
  187. {
  188. %destroyFunction = "destroy" @ $editors[%i];
  189. if( isFunction( %destroyFunction ) )
  190. call( %destroyFunction );
  191. }
  192. // write out our settings xml file
  193. EditorSettings.write();
  194. }
  195. function EditorCreateFakeGameSession(%fileName)
  196. {
  197. // Create a local game server and connect to it.
  198. if(isObject(ServerGroup))
  199. ServerGroup.delete();
  200. new SimGroup(ServerGroup);
  201. if(isObject(ServerConnection))
  202. ServerConnection.delete();
  203. new GameConnection(ServerConnection);
  204. // This calls GameConnection::onConnect.
  205. ServerConnection.connectLocal();
  206. $instantGroup = ServerGroup;
  207. $Game::MissionGroup = "MissionGroup";
  208. exec(%file);
  209. }
  210. function fastLoadWorldEdit(%val)
  211. {
  212. if(%val || %val $= "")
  213. {
  214. if(!$Tools::loaded)
  215. {
  216. canvas.pushDialog( EditorLoadingGui );
  217. canvas.repaint();
  218. onStart();
  219. canvas.popDialog(EditorLoadingGui);
  220. }
  221. %timerId = startPrecisionTimer();
  222. if( GuiEditorIsActive() )
  223. toggleGuiEditor(1);
  224. if( !$missionRunning )
  225. {
  226. EditorNewLevel("ToolsModule:DefaultEditorLevel");
  227. }
  228. else
  229. {
  230. pushInstantGroup();
  231. if ( !isObject( Editor ) )
  232. {
  233. Editor::create();
  234. MissionCleanup.add( Editor );
  235. MissionCleanup.add( Editor.getUndoManager() );
  236. }
  237. if( EditorIsActive() )
  238. {
  239. if (theLevelInfo.type $= "DemoScene")
  240. {
  241. commandToServer('dropPlayerAtCamera');
  242. Editor.close("SceneGui");
  243. }
  244. else
  245. {
  246. %playGUIName = ProjectSettings.value("UI/playGUIName");
  247. Editor.close(%playGUIName);
  248. }
  249. }
  250. else
  251. {
  252. canvas.pushDialog( EditorLoadingGui );
  253. canvas.repaint();
  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. canvas.popDialog(EditorLoadingGui);
  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. canvas.pushDialog( EditorLoadingGui );
  276. canvas.repaint();
  277. onStart();
  278. canvas.popDialog(EditorLoadingGui);
  279. }
  280. toggleGuiEditor(true);
  281. }
  282. }
  283. function Tools::LoadResources( %path )
  284. {
  285. %resourcesPath = %path @ "resources/";
  286. %resourcesList = getDirectoryList( %resourcesPath );
  287. %wordCount = getFieldCount( %resourcesList );
  288. for( %i = 0; %i < %wordCount; %i++ )
  289. {
  290. %resource = GetField( %resourcesList, %i );
  291. if( isFile( %resourcesPath @ %resource @ "/resourceDatabase.cs") )
  292. ResourceObject::load( %path, %resource );
  293. }
  294. }
  295. //This lets us fast-load the editor from the menu
  296. GlobalActionMap.bind(keyboard, "F11", fastLoadWorldEdit);
  297. GlobalActionMap.bind(keyboard, "F10", fastLoadGUIEdit);