toys.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. Sandbox.ActiveToy = "";
  23. //-----------------------------------------------------------------------------
  24. function scanForToys()
  25. {
  26. // Find the toy modules.
  27. %toyModules = ModuleDatabase.findModuleTypes( "toy", false );
  28. // Do we have an existing set of sandbox toys?
  29. if ( !isObject(SandboxToys) )
  30. {
  31. // No, so create one.
  32. new SimSet(SandboxToys);
  33. }
  34. // Clear the sandbox toys.
  35. SandboxToys.clear();
  36. // Fetch toy module count.
  37. %toyModuleCount = getWordCount( %toyModules );
  38. // Add toys.
  39. for ( %i = 0; %i < %toyModuleCount; %i++ )
  40. {
  41. // Fetch module definition.
  42. %moduleDefinition = getWord( %toyModules, %i );
  43. // Add to toy sandbox toys.
  44. SandboxToys.add( %moduleDefinition );
  45. }
  46. }
  47. //-----------------------------------------------------------------------------
  48. function loadToy( %moduleDefinition )
  49. {
  50. // Sanity!
  51. if ( !isObject( %moduleDefinition ) )
  52. {
  53. error( "Cannot load toy as the specified toy is not available." );
  54. return;
  55. }
  56. // Unload the active toy.
  57. unloadToy();
  58. // Create a sandbox scene.
  59. createSandboxScene();
  60. // Now is a good time to purge any idle assets.
  61. AssetDatabase.purgeAssets();
  62. // Set active toy.
  63. // This must be done here in-case a toy depends on it during its initialization.
  64. Sandbox.ActiveToy = %moduleDefinition;
  65. // Load the toy.
  66. if ( !ModuleDatabase.loadExplicit( %moduleDefinition.ModuleId, %moduleDefinition.VersionId ) )
  67. {
  68. error( "Failed to load the toy '" @ %moduleDefinition.ModuleId @ "'." );
  69. return;
  70. }
  71. // Add the scene so it's unloaded when the toy module is.
  72. %moduleDefinition.ScopeSet.add( SandboxScene );
  73. // Add toy scope-set as a listener.
  74. SandboxWindow.addInputListener( %moduleDefinition.ScopeSet );
  75. }
  76. //-----------------------------------------------------------------------------
  77. function unloadToy()
  78. {
  79. // Finish if no active toy loaded.
  80. if ( !isObject(Sandbox.ActiveToy) )
  81. return;
  82. // Delete any custom controls added by the toy.
  83. ToyCustomControls.deleteObjects();
  84. resetCustomControls();
  85. // Unload the toy.
  86. if ( !ModuleDatabase.unloadExplicit( Sandbox.ActiveToy.moduleId ) )
  87. {
  88. error( "Failed to unload the toy '" @ Sandbox.ActiveToy.ModuleId @ "'." );
  89. }
  90. // Reset active toy.
  91. Sandbox.ActiveToy = "";
  92. }