JSScene.cpp 9.3 KB

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