JSScene.cpp 9.3 KB

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