JSScene.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. 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<JSComponent*> components;
  74. node->GetComponents<JSComponent>(components, recursive);
  75. for (unsigned i = 0; i < components.Size(); i++)
  76. {
  77. JSComponent* component = components[i];
  78. if (component->MatchScriptName(path)) {
  79. js_push_class_object_instance(ctx, component, "Component");
  80. return 1;
  81. }
  82. }
  83. duk_push_null(ctx);
  84. return 1;
  85. }
  86. static int Node_GetChildrenWithComponent(duk_context* ctx)
  87. {
  88. StringHash type = duk_to_string(ctx, 0);
  89. bool recursive = false;
  90. if (duk_get_top(ctx) == 2)
  91. if (duk_get_boolean(ctx, 1))
  92. recursive = true;
  93. duk_push_this(ctx);
  94. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  95. PODVector<Node*> dest;
  96. node->GetChildrenWithComponent(dest, type, recursive);
  97. duk_push_array(ctx);
  98. for (unsigned i = 0; i < dest.Size(); i++)
  99. {
  100. js_push_class_object_instance(ctx, dest[i], "Node");
  101. duk_put_prop_index(ctx, -2, i);
  102. }
  103. return 1;
  104. }
  105. static int Node_GetChildrenWithName(duk_context* ctx)
  106. {
  107. StringHash nameHash = duk_to_string(ctx, 0);
  108. bool recursive = false;
  109. if (duk_get_top(ctx) == 2)
  110. if (duk_get_boolean(ctx, 1))
  111. recursive = true;
  112. duk_push_this(ctx);
  113. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  114. PODVector<Node*> dest;
  115. node->GetChildrenWithName(dest, nameHash, recursive);
  116. duk_push_array(ctx);
  117. for (unsigned i = 0; i < dest.Size(); i++)
  118. {
  119. js_push_class_object_instance(ctx, dest[i], "Node");
  120. duk_put_prop_index(ctx, -2, i);
  121. }
  122. return 1;
  123. }
  124. static int Node_GetComponents(duk_context* ctx)
  125. {
  126. bool recursive = false;
  127. StringHash typeHash = Component::GetTypeStatic();
  128. if (duk_get_top(ctx) > 0)
  129. {
  130. if (duk_is_string(ctx, 0) && strlen(duk_get_string(ctx, 0)))
  131. typeHash = duk_get_string(ctx, 0);
  132. }
  133. if (duk_get_top(ctx) > 1)
  134. recursive = duk_require_boolean(ctx, 1) ? true : false;
  135. duk_push_this(ctx);
  136. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  137. PODVector<Component*> dest;
  138. node->GetComponents(dest, typeHash, recursive);
  139. duk_push_array(ctx);
  140. int count = 0;
  141. for (unsigned i = 0; i < dest.Size(); i++)
  142. {
  143. if (js_push_class_object_instance(ctx, dest[i], dest[i]->GetTypeName().CString()))
  144. {
  145. duk_put_prop_index(ctx, -2, count++);
  146. }
  147. }
  148. return 1;
  149. }
  150. static int Node_GetChildAtIndex(duk_context* ctx)
  151. {
  152. duk_push_this(ctx);
  153. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  154. unsigned idx = (unsigned) duk_to_number(ctx, 0);
  155. if (node->GetNumChildren() <= idx)
  156. {
  157. duk_push_null(ctx);
  158. return 1;
  159. }
  160. Node* child = node->GetChild(idx);
  161. js_push_class_object_instance(ctx, child, "Node");
  162. return 1;
  163. }
  164. static int Node_SaveXML(duk_context* ctx)
  165. {
  166. File* file = js_to_class_instance<File>(ctx, 0, 0);
  167. duk_push_this(ctx);
  168. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  169. duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
  170. return 1;
  171. }
  172. static int Node_LoadPrefab(duk_context* ctx)
  173. {
  174. const char* prefabName = duk_require_string(ctx, 0);
  175. duk_push_this(ctx);
  176. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  177. PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
  178. prefabComponent->SetPrefabGUID(prefabName);
  179. duk_push_boolean(ctx, 1);
  180. return 1;
  181. }
  182. static int Node_CreateChildPrefab(duk_context* ctx)
  183. {
  184. const char* childName = duk_require_string(ctx, 0);
  185. const char* prefabName = duk_require_string(ctx, 1);
  186. duk_push_this(ctx);
  187. Node* parent = js_to_class_instance<Node>(ctx, -1, 0);
  188. Node* node = parent->CreateChild();
  189. PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
  190. prefabComponent->SetPrefabGUID(prefabName);
  191. // override what node name is in prefab
  192. node->SetName(childName);
  193. js_push_class_object_instance(ctx, node, "Node");
  194. return 1;
  195. }
  196. static int Scene_LoadXML(duk_context* ctx)
  197. {
  198. JSVM* vm = JSVM::GetJSVM(ctx);
  199. String filename = duk_to_string(ctx, 0);
  200. ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
  201. SharedPtr<File> file = cache->GetFile(filename);
  202. if (!file->IsOpen())
  203. {
  204. duk_push_false(ctx);
  205. return 1;
  206. }
  207. duk_push_this(ctx);
  208. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  209. bool success = scene->LoadXML(*file);
  210. if (success)
  211. duk_push_true(ctx);
  212. else
  213. duk_push_false(ctx);
  214. return 1;
  215. }
  216. static int Scene_GetMainCamera(duk_context* ctx)
  217. {
  218. duk_push_this(ctx);
  219. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  220. PODVector<Node*> cameraNodes;
  221. Camera* camera = 0;
  222. scene->GetChildrenWithComponent(cameraNodes, Camera::GetTypeStatic(), true);
  223. if (cameraNodes.Size())
  224. {
  225. camera = cameraNodes[0]->GetComponent<Camera>();
  226. }
  227. if (!camera)
  228. {
  229. duk_push_null(ctx);
  230. return 1;
  231. }
  232. js_push_class_object_instance(ctx, camera, "Camera");
  233. return 1;
  234. }
  235. void jsapi_init_scene(JSVM* vm)
  236. {
  237. duk_context* ctx = vm->GetJSContext();
  238. jsapi_init_scene_serializable(vm);
  239. js_class_get_prototype(ctx, "Atomic", "Node");
  240. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  241. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  242. duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
  243. duk_put_prop_string(ctx, -2, "getChildrenWithName");
  244. duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
  245. duk_put_prop_string(ctx, -2, "getComponents");
  246. duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
  247. duk_put_prop_string(ctx, -2, "createJSComponent");
  248. duk_push_c_function(ctx, Node_GetJSComponent, 1);
  249. duk_put_prop_string(ctx, -2, "getJSComponent");
  250. duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
  251. duk_put_prop_string(ctx, -2, "getChildAtIndex");
  252. duk_push_c_function(ctx, Node_SaveXML, 1);
  253. duk_put_prop_string(ctx, -2, "saveXML");
  254. duk_push_c_function(ctx, Node_LoadPrefab, 1);
  255. duk_put_prop_string(ctx, -2, "loadPrefab");
  256. duk_push_c_function(ctx, Node_CreateChildPrefab, 2);
  257. duk_put_prop_string(ctx, -2, "createChildPrefab");
  258. duk_pop(ctx);
  259. js_class_get_prototype(ctx, "Atomic", "Scene");
  260. duk_push_c_function(ctx, Scene_LoadXML, 1);
  261. duk_put_prop_string(ctx, -2, "loadXML");
  262. duk_push_c_function(ctx, Scene_GetMainCamera, 0);
  263. duk_put_prop_string(ctx, -2, "getMainCamera");
  264. duk_pop(ctx);
  265. }
  266. }