JSScene.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Resource/ResourceCache.h>
  23. #include <Atomic/Resource/XMLFile.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/Scene/Node.h>
  26. #include <Atomic/Scene/Scene.h>
  27. #include <Atomic/Scene/PrefabComponent.h>
  28. #include <Atomic/Graphics/Camera.h>
  29. #ifdef ATOMIC_3D
  30. #include <Atomic/Physics/RigidBody.h>
  31. #endif
  32. #include "JSScene.h"
  33. #include "JSComponent.h"
  34. #include "JSVM.h"
  35. namespace Atomic
  36. {
  37. void jsapi_init_scene_serializable(JSVM* vm);
  38. static int Node_CreateJSComponent(duk_context* ctx)
  39. {
  40. String path = duk_require_string(ctx, 0);
  41. bool hasArgs = false;
  42. int argIdx = -1;
  43. if (duk_get_top(ctx) > 1 && duk_is_object(ctx, 1))
  44. {
  45. hasArgs = true;
  46. argIdx = 1;
  47. }
  48. duk_push_this(ctx);
  49. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  50. ResourceCache* cache = node->GetContext()->GetSubsystem<ResourceCache>();
  51. JSComponentFile* file = cache->GetResource<JSComponentFile>(path);
  52. if (!file)
  53. {
  54. LOGERRORF("Unable to load component file %s", path.CString());
  55. duk_push_undefined(ctx);
  56. return 1;
  57. }
  58. JSComponent* jsc = file->CreateJSComponent();
  59. node->AddComponent(jsc, jsc->GetID(), LOCAL);
  60. jsc->InitInstance(hasArgs, argIdx);
  61. js_push_class_object_instance(ctx, jsc, "JSComponent");
  62. return 1;
  63. }
  64. static int Node_GetJSComponent(duk_context* ctx)
  65. {
  66. String path = duk_require_string(ctx, 0);
  67. duk_push_this(ctx);
  68. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  69. PODVector<JSComponent*> components;
  70. node->GetComponents<JSComponent>(components, true);
  71. for (unsigned i = 0; i < components.Size(); i++)
  72. {
  73. JSComponent* component = components[i];
  74. if (component->MatchScriptName(path)) {
  75. js_push_class_object_instance(ctx, component, "Component");
  76. return 1;
  77. }
  78. }
  79. duk_push_null(ctx);
  80. return 1;
  81. }
  82. static int Node_GetChildrenWithComponent(duk_context* ctx)
  83. {
  84. StringHash type = duk_to_string(ctx, 0);
  85. bool recursive = false;
  86. if (duk_get_top(ctx) == 2)
  87. if (duk_get_boolean(ctx, 1))
  88. recursive = true;
  89. duk_push_this(ctx);
  90. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  91. PODVector<Node*> dest;
  92. node->GetChildrenWithComponent(dest, type, recursive);
  93. duk_push_array(ctx);
  94. for (unsigned i = 0; i < dest.Size(); i++)
  95. {
  96. js_push_class_object_instance(ctx, dest[i], "Node");
  97. duk_put_prop_index(ctx, -2, i);
  98. }
  99. return 1;
  100. }
  101. static int Node_GetChildrenWithName(duk_context* ctx)
  102. {
  103. StringHash nameHash = duk_to_string(ctx, 0);
  104. bool recursive = false;
  105. if (duk_get_top(ctx) == 2)
  106. if (duk_get_boolean(ctx, 1))
  107. recursive = true;
  108. duk_push_this(ctx);
  109. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  110. PODVector<Node*> dest;
  111. node->GetChildrenWithName(dest, nameHash, recursive);
  112. duk_push_array(ctx);
  113. for (unsigned i = 0; i < dest.Size(); i++)
  114. {
  115. js_push_class_object_instance(ctx, dest[i], "Node");
  116. duk_put_prop_index(ctx, -2, i);
  117. }
  118. return 1;
  119. }
  120. static int Node_GetComponents(duk_context* ctx)
  121. {
  122. bool recursive = false;
  123. StringHash typeHash = Component::GetTypeStatic();
  124. if (duk_get_top(ctx) > 0)
  125. {
  126. if (duk_is_string(ctx, 0) && strlen(duk_get_string(ctx, 0)))
  127. typeHash = duk_get_string(ctx, 0);
  128. }
  129. if (duk_get_top(ctx) > 1)
  130. recursive = duk_require_boolean(ctx, 1) ? true : false;
  131. duk_push_this(ctx);
  132. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  133. PODVector<Component*> dest;
  134. node->GetComponents(dest, typeHash, recursive);
  135. duk_push_array(ctx);
  136. for (unsigned i = 0; i < dest.Size(); i++)
  137. {
  138. if (js_push_class_object_instance(ctx, dest[i], "Component"))
  139. duk_put_prop_index(ctx, -2, i);
  140. }
  141. return 1;
  142. }
  143. static int Node_GetChildAtIndex(duk_context* ctx)
  144. {
  145. duk_push_this(ctx);
  146. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  147. unsigned idx = (unsigned) duk_to_number(ctx, 0);
  148. if (node->GetNumChildren() <= idx)
  149. {
  150. duk_push_null(ctx);
  151. return 1;
  152. }
  153. Node* child = node->GetChild(idx);
  154. js_push_class_object_instance(ctx, child, "Node");
  155. return 1;
  156. }
  157. static int Node_SaveXML(duk_context* ctx)
  158. {
  159. File* file = js_to_class_instance<File>(ctx, 0, 0);
  160. duk_push_this(ctx);
  161. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  162. duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
  163. return 1;
  164. }
  165. static int Node_LoadPrefab(duk_context* ctx)
  166. {
  167. const char* prefabName = duk_require_string(ctx, 0);
  168. duk_push_this(ctx);
  169. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  170. PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
  171. prefabComponent->SetPrefabGUID(prefabName);
  172. duk_push_boolean(ctx, 1);
  173. return 1;
  174. }
  175. static int Node_CreateChildPrefab(duk_context* ctx)
  176. {
  177. const char* childName = duk_require_string(ctx, 0);
  178. const char* prefabName = duk_require_string(ctx, 1);
  179. duk_push_this(ctx);
  180. Node* parent = js_to_class_instance<Node>(ctx, -1, 0);
  181. Node* node = parent->CreateChild();
  182. PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
  183. prefabComponent->SetPrefabGUID(prefabName);
  184. // override what node name is in prefab
  185. node->SetName(childName);
  186. js_push_class_object_instance(ctx, node, "Node");
  187. return 1;
  188. }
  189. static int Scene_LoadXML(duk_context* ctx)
  190. {
  191. JSVM* vm = JSVM::GetJSVM(ctx);
  192. String filename = duk_to_string(ctx, 0);
  193. ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
  194. SharedPtr<File> file = cache->GetFile(filename);
  195. if (!file->IsOpen())
  196. {
  197. duk_push_false(ctx);
  198. return 1;
  199. }
  200. duk_push_this(ctx);
  201. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  202. bool success = scene->LoadXML(*file);
  203. if (success)
  204. duk_push_true(ctx);
  205. else
  206. duk_push_false(ctx);
  207. return 1;
  208. }
  209. static int Scene_GetMainCamera(duk_context* ctx)
  210. {
  211. duk_push_this(ctx);
  212. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  213. PODVector<Node*> cameraNodes;
  214. Camera* camera = 0;
  215. scene->GetChildrenWithComponent(cameraNodes, Camera::GetTypeStatic(), true);
  216. if (cameraNodes.Size())
  217. {
  218. camera = cameraNodes[0]->GetComponent<Camera>();
  219. }
  220. if (!camera)
  221. {
  222. duk_push_null(ctx);
  223. return 1;
  224. }
  225. js_push_class_object_instance(ctx, camera, "Camera");
  226. return 1;
  227. }
  228. void jsapi_init_scene(JSVM* vm)
  229. {
  230. duk_context* ctx = vm->GetJSContext();
  231. jsapi_init_scene_serializable(vm);
  232. js_class_get_prototype(ctx, "Atomic", "Node");
  233. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  234. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  235. duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
  236. duk_put_prop_string(ctx, -2, "getChildrenWithName");
  237. duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
  238. duk_put_prop_string(ctx, -2, "getComponents");
  239. duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
  240. duk_put_prop_string(ctx, -2, "createJSComponent");
  241. duk_push_c_function(ctx, Node_GetJSComponent, 1);
  242. duk_put_prop_string(ctx, -2, "getJSComponent");
  243. duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
  244. duk_put_prop_string(ctx, -2, "getChildAtIndex");
  245. duk_push_c_function(ctx, Node_SaveXML, 1);
  246. duk_put_prop_string(ctx, -2, "saveXML");
  247. duk_push_c_function(ctx, Node_LoadPrefab, 1);
  248. duk_put_prop_string(ctx, -2, "loadPrefab");
  249. duk_push_c_function(ctx, Node_CreateChildPrefab, 2);
  250. duk_put_prop_string(ctx, -2, "createChildPrefab");
  251. duk_pop(ctx);
  252. js_class_get_prototype(ctx, "Atomic", "Scene");
  253. duk_push_c_function(ctx, Scene_LoadXML, 1);
  254. duk_put_prop_string(ctx, -2, "loadXML");
  255. duk_push_c_function(ctx, Scene_GetMainCamera, 0);
  256. duk_put_prop_string(ctx, -2, "getMainCamera");
  257. duk_pop(ctx);
  258. }
  259. }