Browse Source

Fix player launch from IDE (and not editor), don't initialize instance until main update so the initializer function is valid, add a Node.getJSComponent() with file resolution, add a way to get scene's "MainCamera" which is defined as first camera ATM

Josh Engebretson 10 years ago
parent
commit
9e36a11940

+ 11 - 0
Source/AtomicEditorWork/Application/AEPlayerApp.cpp

@@ -25,6 +25,7 @@
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/IO/Log.h>
 #include <Atomic/IO/IOEvents.h>
+#include <Atomic/Input/InputEvents.h>
 #include <Atomic/Core/Main.h>
 #include <Atomic/Core/ProcessUtils.h>
 #include <Atomic/Resource/ResourceCache.h>
@@ -145,6 +146,16 @@ void AEPlayerApplication::Start()
     context_->RegisterSubsystem(new AtomicPlayer::Player(context_));
     AtomicPlayer::jsapi_init_atomicplayer(vm_);
 
+    if (!playerMode->launchedByEditor())
+    {
+        JSVM* vm = JSVM::GetJSVM(0);
+
+        if (!vm->ExecuteMain())
+        {
+            SendEvent(E_EXITREQUESTED);
+        }
+
+    }
     return;
 }
 

+ 3 - 2
Source/AtomicEditorWork/PlayerMode/AEPlayerMode.cpp

@@ -17,7 +17,8 @@ namespace AtomicEditor
 
 PlayerMode::PlayerMode(Context* context) :
     Object(context),
-    brokerActive_(false)
+    brokerActive_(false),
+    launchedByEditor_(false)
 {
     fd_[0] = INVALID_IPCHANDLE_VALUE;
     fd_[1] = INVALID_IPCHANDLE_VALUE;
@@ -109,10 +110,10 @@ void PlayerMode::ProcessArguments() {
 
     if (id > 0 && fd_[0] != INVALID_IPCHANDLE_VALUE && fd_[1] != INVALID_IPCHANDLE_VALUE)
     {        
+        launchedByEditor_ = true;
         SubscribeToEvent(E_IPCINITIALIZE, HANDLER(PlayerMode, HandleIPCInitialize));
         ipc_->InitWorker((unsigned) id, fd_[0], fd_[1]);
     }
-
 }
 
 void PlayerMode::HandleJSError(StringHash eventType, VariantMap& eventData)

+ 3 - 1
Source/AtomicEditorWork/PlayerMode/AEPlayerMode.h

@@ -23,6 +23,8 @@ public:
     /// Destruct.
     virtual ~PlayerMode();
 
+    bool launchedByEditor() { return launchedByEditor_; }
+
 private:
 
     void ProcessArguments();
@@ -32,9 +34,9 @@ private:
     void HandleIPCInitialize(StringHash eventType, VariantMap& eventData);
 
     IPCHandle fd_[2];
-
     WeakPtr<IPC> ipc_;
     bool brokerActive_;
+    bool launchedByEditor_;
 
 };
 

+ 35 - 7
Source/AtomicJS/Javascript/JSComponent.cpp

@@ -124,6 +124,7 @@ JSComponent::JSComponent(Context* context) :
     Component(context),
     updateEventMask_(USE_UPDATE | USE_POSTUPDATE | USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE),
     currentEventMask_(0),
+    instanceInitialized_(false),
     started_(false),
     destroyed_(false),
     scriptClassInstance_(false),
@@ -201,8 +202,6 @@ void JSComponent::UpdateReferences(bool remove)
 
 void JSComponent::ApplyAttributes()
 {
-    if (!started_)
-        InitInstance();
 }
 
 void JSComponent::InitInstance(bool hasArgs, int argIdx)
@@ -298,11 +297,7 @@ void JSComponent::InitInstance(bool hasArgs, int argIdx)
 
     duk_set_top(ctx, top);
 
-    if (!started_)
-    {
-        started_ = true;
-        Start();
-    }
+    instanceInitialized_ = true;
 
 }
 
@@ -360,6 +355,15 @@ void JSComponent::DelayedStart()
 
 void JSComponent::Update(float timeStep)
 {
+    if (!instanceInitialized_)
+        InitInstance();
+
+    if (!started_)
+    {
+        started_ = true;
+        Start();
+    }
+
     static String name = "update";
     CallScriptMethod(name, true, timeStep);
 }
@@ -544,6 +548,30 @@ bool JSComponent::LoadXML(const XMLElement& source, bool setInstanceDefault)
     return success;
 }
 
+bool JSComponent::MatchScriptName(const String& path)
+{
+    if (componentFile_.Null())
+        return false;
+
+    String _path = path;
+    _path.Replace(".js", "", false);
+
+    const String& name = componentFile_->GetName();
+
+    if (_path == name)
+        return true;
+
+    String pathName, fileName, ext;
+    SplitPath(name, pathName, fileName, ext);
+
+    if (fileName == _path)
+        return true;
+
+
+    return false;
+
+}
+
 void JSComponent::SetComponentFile(JSComponentFile* cfile)
 {
     componentFile_ = cfile;

+ 4 - 0
Source/AtomicJS/Javascript/JSComponent.h

@@ -70,6 +70,9 @@ public:
     ResourceRef GetScriptAttr() const;
     JSComponentFile* GetComponentFile() { return componentFile_; }
 
+    /// Match script name
+    bool MatchScriptName(const String& path);
+
     /// Handle enabled/disabled state change. Changes update event subscription.
     virtual void OnSetEnabled();
 
@@ -133,6 +136,7 @@ private:
     /// Current event subscription mask.
     unsigned char currentEventMask_;
 
+    bool instanceInitialized_;
     bool started_;
     bool destroyed_;
     bool scriptClassInstance_;

+ 56 - 0
Source/AtomicJS/Javascript/JSScene.cpp

@@ -6,6 +6,7 @@
 #include <Atomic/IO/File.h>
 #include <Atomic/Scene/Node.h>
 #include <Atomic/Scene/Scene.h>
+#include <Atomic/Graphics/Camera.h>
 
 #include "JSScene.h"
 #include "JSComponent.h"
@@ -51,6 +52,31 @@ static int Node_CreateJSComponent(duk_context* ctx)
     return 1;
 }
 
+static int Node_GetJSComponent(duk_context* ctx)
+{
+    String path = duk_require_string(ctx, 0);
+
+    duk_push_this(ctx);
+    Node* node = js_to_class_instance<Node>(ctx, -1, 0);
+
+    PODVector<JSComponent*> components;
+    node->GetComponents<JSComponent>(components, true);
+
+    for (unsigned i = 0; i < components.Size(); i++)
+    {
+        JSComponent* component = components[i];
+        if (component->MatchScriptName(path)) {
+
+            js_push_class_object_instance(ctx, component, "Component");
+            return 1;
+
+        }
+
+    }
+    duk_push_null(ctx);
+    return 1;
+}
+
 static int Node_GetChildrenWithComponent(duk_context* ctx)
 {
     StringHash type = duk_to_string(ctx, 0);
@@ -199,6 +225,32 @@ static int Scene_LoadXML(duk_context* ctx)
 
 }
 
+static int Scene_GetMainCamera(duk_context* ctx)
+{
+    duk_push_this(ctx);
+    Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
+
+    PODVector<Node*> cameraNodes;
+    Camera* camera = 0;
+    scene->GetChildrenWithComponent(cameraNodes, Camera::GetTypeStatic(), true);
+    if (cameraNodes.Size())
+    {
+        camera = cameraNodes[0]->GetComponent<Camera>();
+    }
+
+    if (!camera)
+    {
+        duk_push_null(ctx);
+        return 1;
+    }
+
+    js_push_class_object_instance(ctx, camera, "Camera");
+
+    return 1;
+
+}
+
+
 
 void jsapi_init_scene(JSVM* vm)
 {
@@ -215,6 +267,8 @@ void jsapi_init_scene(JSVM* vm)
     duk_put_prop_string(ctx, -2, "getComponents");
     duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
     duk_put_prop_string(ctx, -2, "createJSComponent");
+    duk_push_c_function(ctx, Node_GetJSComponent, 1);
+    duk_put_prop_string(ctx, -2, "getJSComponent");
     duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
     duk_put_prop_string(ctx, -2, "getChildAtIndex");
     duk_push_c_function(ctx, Node_SaveXML, 1);
@@ -225,6 +279,8 @@ void jsapi_init_scene(JSVM* vm)
     js_class_get_prototype(ctx, "Atomic", "Scene");
     duk_push_c_function(ctx, Scene_LoadXML, 1);
     duk_put_prop_string(ctx, -2, "loadXML");
+    duk_push_c_function(ctx, Scene_GetMainCamera, 0);
+    duk_put_prop_string(ctx, -2, "getMainCamera");
     duk_pop(ctx);
 
 }