| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- //
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <Atomic/Resource/ResourceCache.h>
- #include <Atomic/Resource/XMLFile.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/Scene/Node.h>
- #include <Atomic/Scene/Scene.h>
- #include <Atomic/Scene/PrefabComponent.h>
- #include <Atomic/Graphics/Camera.h>
- #ifdef ATOMIC_3D
- #include <Atomic/Physics/RigidBody.h>
- #endif
- #include "JSScene.h"
- #include "JSComponent.h"
- #include "JSVM.h"
- namespace Atomic
- {
- void jsapi_init_scene_serializable(JSVM* vm);
- static int Node_CreateJSComponent(duk_context* ctx)
- {
- String path = duk_require_string(ctx, 0);
- if (Atomic::GetExtension(path).Empty()) {
- path += ".js";
- }
- bool hasArgs = false;
- int argIdx = -1;
- if (duk_get_top(ctx) > 1 && duk_is_object(ctx, 1))
- {
- hasArgs = true;
- argIdx = 1;
- }
- duk_push_this(ctx);
- Node* node = js_to_class_instance<Node>(ctx, -1, 0);
- ResourceCache* cache = node->GetContext()->GetSubsystem<ResourceCache>();
- JSComponentFile* file = cache->GetResource<JSComponentFile>(path);
- if (!file)
- {
- ATOMIC_LOGERRORF("Unable to load component file %s", path.CString());
- duk_push_undefined(ctx);
- return 1;
- }
- SharedPtr<JSComponent> jsc = file->CreateJSComponent();
- node->AddComponent(jsc, jsc->GetID(), LOCAL);
- jsc->InitInstance(hasArgs, argIdx);
- js_push_class_object_instance(ctx, jsc, "JSComponent");
- return 1;
- }
- static int Node_GetJSComponent(duk_context* ctx)
- {
- String path = duk_require_string(ctx, 0);
- bool recursive = false;
- if (duk_get_top(ctx) == 2)
- if (duk_get_boolean(ctx, 1))
- recursive = true;
- duk_push_this(ctx);
- Node* node = js_to_class_instance<Node>(ctx, -1, 0);
- PODVector<JSComponent*> components;
- node->GetComponents<JSComponent>(components, recursive);
- for (unsigned i = 0; i < components.Size(); i++)
- {
- JSComponent* component = components[i];
- if (component->MatchScriptName(path)) {
- if(!component->IsInstanceInitialized())
- component->InitInstance();
- 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);
- bool recursive = false;
- if (duk_get_top(ctx) == 2)
- if (duk_get_boolean(ctx, 1))
- recursive = true;
- duk_push_this(ctx);
- Node* node = js_to_class_instance<Node>(ctx, -1, 0);
- PODVector<Node*> dest;
- node->GetChildrenWithComponent(dest, type, recursive);
- duk_push_array(ctx);
- for (unsigned i = 0; i < dest.Size(); i++)
- {
- js_push_class_object_instance(ctx, dest[i], "Node");
- duk_put_prop_index(ctx, -2, i);
- }
- return 1;
- }
- static int Node_GetChildrenWithName(duk_context* ctx)
- {
- StringHash nameHash = duk_to_string(ctx, 0);
- bool recursive = false;
- if (duk_get_top(ctx) == 2)
- if (duk_get_boolean(ctx, 1))
- recursive = true;
- duk_push_this(ctx);
- Node* node = js_to_class_instance<Node>(ctx, -1, 0);
- PODVector<Node*> dest;
- node->GetChildrenWithName(dest, nameHash, recursive);
- duk_push_array(ctx);
- for (unsigned i = 0; i < dest.Size(); i++)
- {
- js_push_class_object_instance(ctx, dest[i], "Node");
- duk_put_prop_index(ctx, -2, i);
- }
- return 1;
- }
- static int Node_GetComponents(duk_context* ctx)
- {
- bool recursive = false;
- StringHash typeHash = Component::GetTypeStatic();
- if (duk_get_top(ctx) > 0)
- {
- if (duk_is_string(ctx, 0) && strlen(duk_get_string(ctx, 0)))
- typeHash = duk_get_string(ctx, 0);
- }
- if (duk_get_top(ctx) > 1)
- recursive = duk_require_boolean(ctx, 1) ? true : false;
- duk_push_this(ctx);
- Node* node = js_to_class_instance<Node>(ctx, -1, 0);
- PODVector<Component*> dest;
- node->GetComponents(dest, typeHash, recursive);
- duk_push_array(ctx);
- int count = 0;
- for (unsigned i = 0; i < dest.Size(); i++)
- {
- if (js_push_class_object_instance(ctx, dest[i], dest[i]->GetTypeName().CString()))
- {
- duk_put_prop_index(ctx, -2, count++);
- }
- }
- return 1;
- }
- static int Node_GetChildAtIndex(duk_context* ctx)
- {
- duk_push_this(ctx);
- Node* node = js_to_class_instance<Node>(ctx, -1, 0);
- unsigned idx = (unsigned) duk_to_number(ctx, 0);
- if (node->GetNumChildren() <= idx)
- {
- duk_push_null(ctx);
- return 1;
- }
- Node* child = node->GetChild(idx);
- js_push_class_object_instance(ctx, child, "Node");
- return 1;
- }
- static int Node_SaveXML(duk_context* ctx)
- {
- File* file = js_to_class_instance<File>(ctx, 0, 0);
- duk_push_this(ctx);
- Node* node = js_to_class_instance<Node>(ctx, -1, 0);
- duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
- return 1;
- }
- static int Node_LoadPrefab(duk_context* ctx)
- {
- const char* prefabName = duk_require_string(ctx, 0);
- duk_push_this(ctx);
- Node* node = js_to_class_instance<Node>(ctx, -1, 0);
- PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
- prefabComponent->SetPrefabGUID(prefabName);
- duk_push_boolean(ctx, 1);
- return 1;
- }
- static int Node_CreateChildPrefab(duk_context* ctx)
- {
- const char* childName = duk_require_string(ctx, 0);
- const char* prefabName = duk_require_string(ctx, 1);
- duk_push_this(ctx);
- Node* parent = js_to_class_instance<Node>(ctx, -1, 0);
- Node* node = parent->CreateChild();
- PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
- prefabComponent->SetPrefabGUID(prefabName);
- // override what node name is in prefab
- node->SetName(childName);
- js_push_class_object_instance(ctx, node, "Node");
- return 1;
- }
- static int Scene_LoadXML(duk_context* ctx)
- {
- JSVM* vm = JSVM::GetJSVM(ctx);
- String filename = duk_to_string(ctx, 0);
- ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
- SharedPtr<File> file = cache->GetFile(filename);
- if (!file->IsOpen())
- {
- duk_push_false(ctx);
- return 1;
- }
- duk_push_this(ctx);
- Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
- bool success = scene->LoadXML(*file);
- if (success)
- duk_push_true(ctx);
- else
- duk_push_false(ctx);
- return 1;
- }
- 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)
- {
- duk_context* ctx = vm->GetJSContext();
- jsapi_init_scene_serializable(vm);
- js_class_get_prototype(ctx, "Atomic", "Node");
- duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
- duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
- duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
- duk_put_prop_string(ctx, -2, "getChildrenWithName");
- duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
- 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);
- duk_put_prop_string(ctx, -2, "saveXML");
- duk_push_c_function(ctx, Node_LoadPrefab, 1);
- duk_put_prop_string(ctx, -2, "loadPrefab");
- duk_push_c_function(ctx, Node_CreateChildPrefab, 2);
- duk_put_prop_string(ctx, -2, "createChildPrefab");
- duk_pop(ctx);
- 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);
- }
- }
|