JSScene.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. for (unsigned i = 0; i < dest.Size(); i++)
  141. {
  142. if (js_push_class_object_instance(ctx, dest[i], "Component"))
  143. duk_put_prop_index(ctx, -2, i);
  144. }
  145. return 1;
  146. }
  147. static int Node_GetChildAtIndex(duk_context* ctx)
  148. {
  149. duk_push_this(ctx);
  150. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  151. unsigned idx = (unsigned) duk_to_number(ctx, 0);
  152. if (node->GetNumChildren() <= idx)
  153. {
  154. duk_push_null(ctx);
  155. return 1;
  156. }
  157. Node* child = node->GetChild(idx);
  158. js_push_class_object_instance(ctx, child, "Node");
  159. return 1;
  160. }
  161. static int Node_SaveXML(duk_context* ctx)
  162. {
  163. File* file = js_to_class_instance<File>(ctx, 0, 0);
  164. duk_push_this(ctx);
  165. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  166. duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
  167. return 1;
  168. }
  169. static int Node_LoadPrefab(duk_context* ctx)
  170. {
  171. const char* prefabName = duk_require_string(ctx, 0);
  172. duk_push_this(ctx);
  173. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  174. PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
  175. prefabComponent->SetPrefabGUID(prefabName);
  176. duk_push_boolean(ctx, 1);
  177. return 1;
  178. }
  179. static int Node_CreateChildPrefab(duk_context* ctx)
  180. {
  181. const char* childName = duk_require_string(ctx, 0);
  182. const char* prefabName = duk_require_string(ctx, 1);
  183. duk_push_this(ctx);
  184. Node* parent = js_to_class_instance<Node>(ctx, -1, 0);
  185. Node* node = parent->CreateChild();
  186. PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
  187. prefabComponent->SetPrefabGUID(prefabName);
  188. // override what node name is in prefab
  189. node->SetName(childName);
  190. js_push_class_object_instance(ctx, node, "Node");
  191. return 1;
  192. }
  193. static int Scene_LoadXML(duk_context* ctx)
  194. {
  195. JSVM* vm = JSVM::GetJSVM(ctx);
  196. String filename = duk_to_string(ctx, 0);
  197. ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
  198. SharedPtr<File> file = cache->GetFile(filename);
  199. if (!file->IsOpen())
  200. {
  201. duk_push_false(ctx);
  202. return 1;
  203. }
  204. duk_push_this(ctx);
  205. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  206. bool success = scene->LoadXML(*file);
  207. if (success)
  208. duk_push_true(ctx);
  209. else
  210. duk_push_false(ctx);
  211. return 1;
  212. }
  213. static int Scene_GetMainCamera(duk_context* ctx)
  214. {
  215. duk_push_this(ctx);
  216. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  217. PODVector<Node*> cameraNodes;
  218. Camera* camera = 0;
  219. scene->GetChildrenWithComponent(cameraNodes, Camera::GetTypeStatic(), true);
  220. if (cameraNodes.Size())
  221. {
  222. camera = cameraNodes[0]->GetComponent<Camera>();
  223. }
  224. if (!camera)
  225. {
  226. duk_push_null(ctx);
  227. return 1;
  228. }
  229. js_push_class_object_instance(ctx, camera, "Camera");
  230. return 1;
  231. }
  232. void jsapi_init_scene(JSVM* vm)
  233. {
  234. duk_context* ctx = vm->GetJSContext();
  235. jsapi_init_scene_serializable(vm);
  236. js_class_get_prototype(ctx, "Atomic", "Node");
  237. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  238. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  239. duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
  240. duk_put_prop_string(ctx, -2, "getChildrenWithName");
  241. duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
  242. duk_put_prop_string(ctx, -2, "getComponents");
  243. duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
  244. duk_put_prop_string(ctx, -2, "createJSComponent");
  245. duk_push_c_function(ctx, Node_GetJSComponent, 1);
  246. duk_put_prop_string(ctx, -2, "getJSComponent");
  247. duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
  248. duk_put_prop_string(ctx, -2, "getChildAtIndex");
  249. duk_push_c_function(ctx, Node_SaveXML, 1);
  250. duk_put_prop_string(ctx, -2, "saveXML");
  251. duk_push_c_function(ctx, Node_LoadPrefab, 1);
  252. duk_put_prop_string(ctx, -2, "loadPrefab");
  253. duk_push_c_function(ctx, Node_CreateChildPrefab, 2);
  254. duk_put_prop_string(ctx, -2, "createChildPrefab");
  255. duk_pop(ctx);
  256. js_class_get_prototype(ctx, "Atomic", "Scene");
  257. duk_push_c_function(ctx, Scene_LoadXML, 1);
  258. duk_put_prop_string(ctx, -2, "loadXML");
  259. duk_push_c_function(ctx, Scene_GetMainCamera, 0);
  260. duk_put_prop_string(ctx, -2, "getMainCamera");
  261. duk_pop(ctx);
  262. }
  263. }