JSScene.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <Atomic/Resource/ResourceCache.h>
  5. #include <Atomic/Resource/XMLFile.h>
  6. #include <Atomic/IO/File.h>
  7. #include <Atomic/Scene/Node.h>
  8. #include <Atomic/Scene/Scene.h>
  9. #include <Atomic/Scene/PrefabComponent.h>
  10. #include <Atomic/Graphics/Camera.h>
  11. #ifdef ATOMIC_3D
  12. #include <Atomic/Physics/RigidBody.h>
  13. #endif
  14. #include "JSScene.h"
  15. #include "JSComponent.h"
  16. #include "JSVM.h"
  17. namespace Atomic
  18. {
  19. void jsapi_init_scene_serializable(JSVM* vm);
  20. static int Node_CreateJSComponent(duk_context* ctx)
  21. {
  22. String path = duk_require_string(ctx, 0);
  23. bool hasArgs = false;
  24. int argIdx = -1;
  25. if (duk_get_top(ctx) > 1 && duk_is_object(ctx, 1))
  26. {
  27. hasArgs = true;
  28. argIdx = 1;
  29. }
  30. duk_push_this(ctx);
  31. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  32. ResourceCache* cache = node->GetContext()->GetSubsystem<ResourceCache>();
  33. JSComponentFile* file = cache->GetResource<JSComponentFile>(path);
  34. if (!file)
  35. {
  36. LOGERRORF("Unable to load component file %s", path.CString());
  37. duk_push_undefined(ctx);
  38. return 1;
  39. }
  40. JSComponent* jsc = file->CreateJSComponent();
  41. node->AddComponent(jsc, jsc->GetID(), LOCAL);
  42. jsc->InitInstance(hasArgs, argIdx);
  43. js_push_class_object_instance(ctx, jsc, "JSComponent");
  44. return 1;
  45. }
  46. static int Node_GetJSComponent(duk_context* ctx)
  47. {
  48. String path = duk_require_string(ctx, 0);
  49. duk_push_this(ctx);
  50. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  51. PODVector<JSComponent*> components;
  52. node->GetComponents<JSComponent>(components, true);
  53. for (unsigned i = 0; i < components.Size(); i++)
  54. {
  55. JSComponent* component = components[i];
  56. if (component->MatchScriptName(path)) {
  57. js_push_class_object_instance(ctx, component, "Component");
  58. return 1;
  59. }
  60. }
  61. duk_push_null(ctx);
  62. return 1;
  63. }
  64. static int Node_GetChildrenWithComponent(duk_context* ctx)
  65. {
  66. StringHash type = duk_to_string(ctx, 0);
  67. bool recursive = false;
  68. if (duk_get_top(ctx) == 2)
  69. if (duk_get_boolean(ctx, 1))
  70. recursive = true;
  71. duk_push_this(ctx);
  72. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  73. PODVector<Node*> dest;
  74. node->GetChildrenWithComponent(dest, type, recursive);
  75. duk_push_array(ctx);
  76. for (unsigned i = 0; i < dest.Size(); i++)
  77. {
  78. js_push_class_object_instance(ctx, dest[i], "Node");
  79. duk_put_prop_index(ctx, -2, i);
  80. }
  81. return 1;
  82. }
  83. static int Node_GetChildrenWithName(duk_context* ctx)
  84. {
  85. StringHash nameHash = duk_to_string(ctx, 0);
  86. bool recursive = false;
  87. if (duk_get_top(ctx) == 2)
  88. if (duk_get_boolean(ctx, 1))
  89. recursive = true;
  90. duk_push_this(ctx);
  91. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  92. PODVector<Node*> dest;
  93. node->GetChildrenWithName(dest, nameHash, recursive);
  94. duk_push_array(ctx);
  95. for (unsigned i = 0; i < dest.Size(); i++)
  96. {
  97. js_push_class_object_instance(ctx, dest[i], "Node");
  98. duk_put_prop_index(ctx, -2, i);
  99. }
  100. return 1;
  101. }
  102. static int Node_GetComponents(duk_context* ctx)
  103. {
  104. bool recursive = false;
  105. StringHash typeHash = Component::GetTypeStatic();
  106. if (duk_get_top(ctx) > 0)
  107. {
  108. if (duk_is_string(ctx, 0) && strlen(duk_get_string(ctx, 0)))
  109. typeHash = duk_get_string(ctx, 0);
  110. }
  111. if (duk_get_top(ctx) > 1)
  112. recursive = duk_require_boolean(ctx, 1) ? true : false;
  113. duk_push_this(ctx);
  114. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  115. PODVector<Component*> dest;
  116. node->GetComponents(dest, typeHash, recursive);
  117. duk_push_array(ctx);
  118. for (unsigned i = 0; i < dest.Size(); i++)
  119. {
  120. if (js_push_class_object_instance(ctx, dest[i], "Component"))
  121. duk_put_prop_index(ctx, -2, i);
  122. }
  123. return 1;
  124. }
  125. static int Node_GetChildAtIndex(duk_context* ctx)
  126. {
  127. duk_push_this(ctx);
  128. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  129. unsigned idx = (unsigned) duk_to_number(ctx, 0);
  130. if (node->GetNumChildren() <= idx)
  131. {
  132. duk_push_null(ctx);
  133. return 1;
  134. }
  135. Node* child = node->GetChild(idx);
  136. js_push_class_object_instance(ctx, child, "Node");
  137. return 1;
  138. }
  139. static int Node_SaveXML(duk_context* ctx)
  140. {
  141. File* file = js_to_class_instance<File>(ctx, 0, 0);
  142. duk_push_this(ctx);
  143. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  144. duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
  145. return 1;
  146. }
  147. static int Node_LoadPrefab(duk_context* ctx)
  148. {
  149. const char* prefabName = duk_require_string(ctx, 0);
  150. duk_push_this(ctx);
  151. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  152. PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
  153. prefabComponent->SetPrefabGUID(prefabName);
  154. duk_push_boolean(ctx, 1);
  155. return 1;
  156. }
  157. static int Node_CreateChildPrefab(duk_context* ctx)
  158. {
  159. const char* childName = duk_require_string(ctx, 0);
  160. const char* prefabName = duk_require_string(ctx, 1);
  161. duk_push_this(ctx);
  162. Node* parent = js_to_class_instance<Node>(ctx, -1, 0);
  163. Node* node = parent->CreateChild(childName);
  164. PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
  165. prefabComponent->SetPrefabGUID(prefabName);
  166. js_push_class_object_instance(ctx, node, "Node");
  167. return 1;
  168. }
  169. static int Scene_LoadXML(duk_context* ctx)
  170. {
  171. JSVM* vm = JSVM::GetJSVM(ctx);
  172. String filename = duk_to_string(ctx, 0);
  173. ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
  174. SharedPtr<File> file = cache->GetFile(filename);
  175. if (!file->IsOpen())
  176. {
  177. duk_push_false(ctx);
  178. return 1;
  179. }
  180. duk_push_this(ctx);
  181. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  182. bool success = scene->LoadXML(*file);
  183. if (success)
  184. duk_push_true(ctx);
  185. else
  186. duk_push_false(ctx);
  187. return 1;
  188. }
  189. static int Scene_GetMainCamera(duk_context* ctx)
  190. {
  191. duk_push_this(ctx);
  192. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  193. PODVector<Node*> cameraNodes;
  194. Camera* camera = 0;
  195. scene->GetChildrenWithComponent(cameraNodes, Camera::GetTypeStatic(), true);
  196. if (cameraNodes.Size())
  197. {
  198. camera = cameraNodes[0]->GetComponent<Camera>();
  199. }
  200. if (!camera)
  201. {
  202. duk_push_null(ctx);
  203. return 1;
  204. }
  205. js_push_class_object_instance(ctx, camera, "Camera");
  206. return 1;
  207. }
  208. void jsapi_init_scene(JSVM* vm)
  209. {
  210. duk_context* ctx = vm->GetJSContext();
  211. jsapi_init_scene_serializable(vm);
  212. js_class_get_prototype(ctx, "Atomic", "Node");
  213. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  214. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  215. duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
  216. duk_put_prop_string(ctx, -2, "getChildrenWithName");
  217. duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
  218. duk_put_prop_string(ctx, -2, "getComponents");
  219. duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
  220. duk_put_prop_string(ctx, -2, "createJSComponent");
  221. duk_push_c_function(ctx, Node_GetJSComponent, 1);
  222. duk_put_prop_string(ctx, -2, "getJSComponent");
  223. duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
  224. duk_put_prop_string(ctx, -2, "getChildAtIndex");
  225. duk_push_c_function(ctx, Node_SaveXML, 1);
  226. duk_put_prop_string(ctx, -2, "saveXML");
  227. duk_push_c_function(ctx, Node_LoadPrefab, 1);
  228. duk_put_prop_string(ctx, -2, "loadPrefab");
  229. duk_push_c_function(ctx, Node_CreateChildPrefab, 2);
  230. duk_put_prop_string(ctx, -2, "createChildPrefab");
  231. duk_pop(ctx);
  232. js_class_get_prototype(ctx, "Atomic", "Scene");
  233. duk_push_c_function(ctx, Scene_LoadXML, 1);
  234. duk_put_prop_string(ctx, -2, "loadXML");
  235. duk_push_c_function(ctx, Scene_GetMainCamera, 0);
  236. duk_put_prop_string(ctx, -2, "getMainCamera");
  237. duk_pop(ctx);
  238. }
  239. }