Browse Source

- Added "Scene.Create( type )" which creates a SceneObject-derived type, registers it and adds it to the scene in one operation.

MelvMay-GG 12 years ago
parent
commit
e1693a2

+ 48 - 3
engine/source/2d/scene/Scene.cc

@@ -3639,10 +3639,55 @@ void Scene::SayGoodbye( b2Joint* pJoint )
 
 //-----------------------------------------------------------------------------
 
-void Scene::SayGoodbye( b2Fixture* pFixture )
+SceneObject* Scene::create( const char* pType )
 {
-    // The scene is not currently interested in tracking fixtures
-    // so we do nothing here for now.
+    // Sanity!
+    AssertFatal( pType != NULL, "Scene::create() - Cannot create a NULL type." );
+
+    // Find the class rep.
+    AbstractClassRep* pClassRep = AbstractClassRep::findClassRep( pType ); 
+
+    // Did we find the type?
+    if ( pClassRep == NULL )
+    {
+        // No, so warn.
+        Con::warnf( "Scene::create() - Could not find type '%s' to create.", pType );
+        return NULL;
+    }
+
+    // Find the scene object rep.
+    AbstractClassRep* pSceneObjectRep = AbstractClassRep::findClassRep( "SceneObject" ); 
+
+    // Sanity!
+    AssertFatal( pSceneObjectRep != NULL,  "Scene::create() - Could not find SceneObject class rep." );
+
+    // Is the type derived from scene object?
+    if ( !pClassRep->isClass( pSceneObjectRep ) )
+    {
+        // No, so warn.
+        Con::warnf( "Scene::create() - Type '%s' is not derived from SceneObject.", pType );
+        return NULL;
+    }
+    
+    // Create the type.
+    SceneObject* pSceneObject = dynamic_cast<SceneObject*>( pClassRep->create() );
+
+    // Sanity!
+    AssertFatal( pSceneObject != NULL, "Scene::create() - Failed to create type via class rep." );
+
+    // Attemp to register the object.
+    if ( !pSceneObject->registerObject() )
+    {
+        // No, so warn.
+        Con::warnf( "Scene::create() - Failed to register type '%s'.", pType );
+        delete pSceneObject;
+        return NULL;
+    }
+
+    // Add to the scene.
+    addToScene( pSceneObject );
+
+    return pSceneObject;
 }
 
 //-----------------------------------------------------------------------------

+ 3 - 1
engine/source/2d/scene/Scene.h

@@ -629,7 +629,9 @@ public:
 
     /// Destruction listeners.
     virtual                 void SayGoodbye( b2Joint* pJoint );
-    virtual                 void SayGoodbye( b2Fixture* pFixture );
+    virtual                 void SayGoodbye( b2Fixture* pFixture )      {}
+
+    virtual SceneObject*    create( const char* pType );
 
     /// Miscellaneous.
     inline void             setBatchingEnabled( const bool enabled )    { mBatchRenderer.setBatchEnabled( enabled ); }

+ 14 - 2
engine/source/2d/scene/Scene_ScriptBinding.h

@@ -3069,8 +3069,20 @@ ConsoleMethod(Scene, getBatchingEnabled, bool, 2, 2,    "() Gets whether render
 
 //-----------------------------------------------------------------------------
 
-ConsoleMethod(Scene, setIsEditorScene, void, 3, 3, "() Sets whether this is an editor scene\n"
+ConsoleMethod(Scene, setIsEditorScene, void, 3, 3, "() Sets whether this is an editor scene.\n"
                                                             "@return No return value.")
 {
    object->setIsEditorScene(dAtob(argv[2]));
-}
+}
+
+//-----------------------------------------------------------------------------
+
+ConsoleMethod(Scene, create, const char*, 3, 3, "(type) Creates the specified scene-object derived type and adds it to the scene.\n"
+                                                "@return The scene-object or NULL if not created.")
+{
+    // Create the scene object.
+    SceneObject* pSceneObject = object->create( argv[2] );
+
+    return pSceneObject == NULL ? NULL : pSceneObject->getIdString();
+}
+