Browse Source

JSComponents can now take construction arguments

Josh Engebretson 10 years ago
parent
commit
dfeb2a9d94

+ 2 - 16
Source/AtomicJS/Javascript/JSAtomic.cpp

@@ -201,9 +201,8 @@ static void js_atomic_destroy_node(Node* node, duk_context* ctx, bool root = fal
 
 
          if (component->GetType() == JSComponent::GetTypeStatic())
          if (component->GetType() == JSComponent::GetTypeStatic())
          {
          {
-             // FIX COMPONENTS
-             //JSComponent* jscomponent = (JSComponent*) component;
-             //jscomponent->SetDestroyed();
+             JSComponent* jscomponent = (JSComponent*) component;
+             jscomponent->SetDestroyed();
          }
          }
 
 
          component->UnsubscribeFromAllEvents();
          component->UnsubscribeFromAllEvents();
@@ -212,19 +211,6 @@ static void js_atomic_destroy_node(Node* node, duk_context* ctx, bool root = fal
     node->RemoveAllComponents();
     node->RemoveAllComponents();
     node->UnsubscribeFromAllEvents();
     node->UnsubscribeFromAllEvents();
 
 
-    if (node->GetParent())
-    {
-        assert(node->Refs() >= 2);
-        node->Remove();
-    }
-
-    int top = duk_get_top(ctx);
-    duk_push_global_stash(ctx);
-    duk_get_prop_index(ctx, -1, JS_GLOBALSTASH_INDEX_NODE_REGISTRY);
-    duk_push_pointer(ctx, (void*) node);
-    duk_del_prop(ctx, -2);
-    duk_pop_2(ctx);
-    assert(top = duk_get_top(ctx));
 }
 }
 
 
 static void js_atomic_destroy_scene(Scene* scene, duk_context* ctx)
 static void js_atomic_destroy_scene(Scene* scene, duk_context* ctx)

+ 28 - 8
Source/AtomicJS/Javascript/JSComponent.cpp

@@ -50,6 +50,7 @@ JSComponent::JSComponent(Context* context) :
     updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
     updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
     currentEventMask_(0),
     currentEventMask_(0),
     started_(false),
     started_(false),
+    destroyed_(false),
     delayedStartCalled_(false),
     delayedStartCalled_(false),
     loading_(false)
     loading_(false)
 {
 {
@@ -119,10 +120,9 @@ void JSComponent::UpdateReferences(bool remove)
 
 
 void JSComponent::ApplyAttributes()
 void JSComponent::ApplyAttributes()
 {
 {
-
 }
 }
 
 
-void JSComponent::InitModule()
+void JSComponent::InitModule(bool hasArgs, int argIdx)
 {
 {
     if (context_->GetEditorContext() || componentFile_.Null())
     if (context_->GetEditorContext() || componentFile_.Null())
         return;
         return;
@@ -192,6 +192,25 @@ void JSComponent::InitModule()
         duk_pop(ctx);
         duk_pop(ctx);
     }
     }
 
 
+    // apply args if any
+    if (hasArgs)
+    {
+        // push self
+        js_push_class_object_instance(ctx, this, "JSComponent");
+
+        duk_enum(ctx, argIdx, DUK_ENUM_OWN_PROPERTIES_ONLY);
+
+        while (duk_next(ctx, -1, 1)) {
+
+            duk_put_prop(ctx, -4);
+
+        }
+
+        // pop self and enum object
+        duk_pop_2(ctx);
+
+    }
+
 
 
     duk_get_prop_string(ctx, -1, "component");
     duk_get_prop_string(ctx, -1, "component");
     if (!duk_is_function(ctx, -1))
     if (!duk_is_function(ctx, -1))
@@ -212,6 +231,12 @@ void JSComponent::InitModule()
 
 
     duk_set_top(ctx, top);
     duk_set_top(ctx, top);
 
 
+    if (!started_)
+    {
+        started_ = true;
+        Start();
+    }
+
 }
 }
 
 
 void JSComponent::CallScriptMethod(const String& name, bool passValue, float value)
 void JSComponent::CallScriptMethod(const String& name, bool passValue, float value)
@@ -373,12 +398,7 @@ void JSComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
 {
 {
     using namespace SceneUpdate;
     using namespace SceneUpdate;
 
 
-    if (!started_)
-    {
-        started_ = true;
-        InitModule();
-        Start();
-    }
+    assert(!destroyed_);
 
 
     // Execute user-defined delayed start function before first update
     // Execute user-defined delayed start function before first update
     if (!delayedStartCalled_)
     if (!delayedStartCalled_)

+ 7 - 1
Source/AtomicJS/Javascript/JSComponent.h

@@ -82,6 +82,10 @@ public:
     void SetScriptAttr(const ResourceRef& value);
     void SetScriptAttr(const ResourceRef& value);
     void SetComponentFile(JSComponentFile* cfile, bool loading = false);
     void SetComponentFile(JSComponentFile* cfile, bool loading = false);
 
 
+    void SetDestroyed() { destroyed_ = true; }
+
+    void InitModule(bool hasArgs = false, int argIdx = 0);
+
 protected:
 protected:
     /// Handle scene node being assigned at creation.
     /// Handle scene node being assigned at creation.
     virtual void OnNodeSet(Node* node);
     virtual void OnNodeSet(Node* node);
@@ -117,7 +121,6 @@ private:
     /// Called on physics post-update, fixed timestep.
     /// Called on physics post-update, fixed timestep.
     virtual void FixedPostUpdate(float timeStep);
     virtual void FixedPostUpdate(float timeStep);
 
 
-    void InitModule();
     void UpdateReferences(bool remove = false);
     void UpdateReferences(bool remove = false);
 
 
     /// Requested event subscription mask.
     /// Requested event subscription mask.
@@ -126,10 +129,13 @@ private:
     unsigned char currentEventMask_;
     unsigned char currentEventMask_;
 
 
     bool started_;
     bool started_;
+    bool destroyed_;
 
 
     /// Flag for delayed start.
     /// Flag for delayed start.
     bool delayedStartCalled_;
     bool delayedStartCalled_;
 
 
+
+
     bool loading_;
     bool loading_;
     WeakPtr<JSVM> vm_;
     WeakPtr<JSVM> vm_;
     SharedPtr<JSComponentFile> componentFile_;
     SharedPtr<JSComponentFile> componentFile_;

+ 10 - 1
Source/AtomicJS/Javascript/JSScene.cpp

@@ -20,6 +20,14 @@ static int Node_CreateJSComponent(duk_context* ctx)
 {
 {
     String path = duk_require_string(ctx, 0);
     String path = duk_require_string(ctx, 0);
 
 
+    bool hasArgs = false;
+    int argIdx;
+    if (duk_get_top(ctx) > 1 && duk_is_object(ctx, 1))
+    {
+        hasArgs = true;
+        argIdx = 1;
+    }
+
     duk_push_this(ctx);
     duk_push_this(ctx);
     Node* node = js_to_class_instance<Node>(ctx, -1, 0);
     Node* node = js_to_class_instance<Node>(ctx, -1, 0);
     JSComponent* jsc = node->CreateComponent<JSComponent>();
     JSComponent* jsc = node->CreateComponent<JSComponent>();
@@ -28,6 +36,7 @@ static int Node_CreateJSComponent(duk_context* ctx)
     JSComponentFile* file = cache->GetResource<JSComponentFile>(path);
     JSComponentFile* file = cache->GetResource<JSComponentFile>(path);
 
 
     jsc->SetComponentFile(file);
     jsc->SetComponentFile(file);
+    jsc->InitModule(hasArgs, argIdx);
 
 
     js_push_class_object_instance(ctx, jsc, "JSComponent");
     js_push_class_object_instance(ctx, jsc, "JSComponent");
     return 1;
     return 1;
@@ -195,7 +204,7 @@ void jsapi_init_scene(JSVM* vm)
     duk_put_prop_string(ctx, -2, "getChildrenWithName");
     duk_put_prop_string(ctx, -2, "getChildrenWithName");
     duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
     duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
     duk_put_prop_string(ctx, -2, "getComponents");
     duk_put_prop_string(ctx, -2, "getComponents");
-    duk_push_c_function(ctx, Node_CreateJSComponent, 1);
+    duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
     duk_put_prop_string(ctx, -2, "createJSComponent");
     duk_put_prop_string(ctx, -2, "createJSComponent");
     duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
     duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
     duk_put_prop_string(ctx, -2, "getChildAtIndex");
     duk_put_prop_string(ctx, -2, "getChildAtIndex");