JSScene.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <Atomic/Resource/ResourceCache.h>
  5. #include <Atomic/IO/File.h>
  6. #include <Atomic/Scene/Node.h>
  7. #include <Atomic/Scene/Scene.h>
  8. #include <Atomic/Graphics/Camera.h>
  9. #include "JSScene.h"
  10. #include "JSComponent.h"
  11. #include "JSVM.h"
  12. namespace Atomic
  13. {
  14. void jsapi_init_scene_serializable(JSVM* vm);
  15. static int Node_CreateJSComponent(duk_context* ctx)
  16. {
  17. String path = duk_require_string(ctx, 0);
  18. bool hasArgs = false;
  19. int argIdx = -1;
  20. if (duk_get_top(ctx) > 1 && duk_is_object(ctx, 1))
  21. {
  22. hasArgs = true;
  23. argIdx = 1;
  24. }
  25. duk_push_this(ctx);
  26. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  27. ResourceCache* cache = node->GetContext()->GetSubsystem<ResourceCache>();
  28. JSComponentFile* file = cache->GetResource<JSComponentFile>(path);
  29. if (!file)
  30. {
  31. LOGERRORF("Unable to load component file %s", path.CString());
  32. duk_push_undefined(ctx);
  33. return 1;
  34. }
  35. JSComponent* jsc = file->CreateJSComponent();
  36. node->AddComponent(jsc, jsc->GetID(), LOCAL);
  37. jsc->InitInstance(hasArgs, argIdx);
  38. js_push_class_object_instance(ctx, jsc, "JSComponent");
  39. return 1;
  40. }
  41. static int Node_GetJSComponent(duk_context* ctx)
  42. {
  43. String path = duk_require_string(ctx, 0);
  44. duk_push_this(ctx);
  45. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  46. PODVector<JSComponent*> components;
  47. node->GetComponents<JSComponent>(components, true);
  48. for (unsigned i = 0; i < components.Size(); i++)
  49. {
  50. JSComponent* component = components[i];
  51. if (component->MatchScriptName(path)) {
  52. js_push_class_object_instance(ctx, component, "Component");
  53. return 1;
  54. }
  55. }
  56. duk_push_null(ctx);
  57. return 1;
  58. }
  59. static int Node_GetChildrenWithComponent(duk_context* ctx)
  60. {
  61. StringHash type = duk_to_string(ctx, 0);
  62. bool recursive = false;
  63. if (duk_get_top(ctx) == 2)
  64. if (duk_get_boolean(ctx, 1))
  65. recursive = true;
  66. duk_push_this(ctx);
  67. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  68. PODVector<Node*> dest;
  69. node->GetChildrenWithComponent(dest, type, recursive);
  70. duk_push_array(ctx);
  71. for (unsigned i = 0; i < dest.Size(); i++)
  72. {
  73. js_push_class_object_instance(ctx, dest[i], "Node");
  74. duk_put_prop_index(ctx, -2, i);
  75. }
  76. return 1;
  77. }
  78. static int Node_GetChildrenWithName(duk_context* ctx)
  79. {
  80. StringHash nameHash = duk_to_string(ctx, 0);
  81. bool recursive = false;
  82. if (duk_get_top(ctx) == 2)
  83. if (duk_get_boolean(ctx, 1))
  84. recursive = true;
  85. duk_push_this(ctx);
  86. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  87. PODVector<Node*> dest;
  88. node->GetChildrenWithName(dest, nameHash, recursive);
  89. duk_push_array(ctx);
  90. for (unsigned i = 0; i < dest.Size(); i++)
  91. {
  92. js_push_class_object_instance(ctx, dest[i], "Node");
  93. duk_put_prop_index(ctx, -2, i);
  94. }
  95. return 1;
  96. }
  97. static int Node_GetComponents(duk_context* ctx)
  98. {
  99. bool recursive = false;
  100. StringHash typeHash = Component::GetTypeStatic();
  101. if (duk_get_top(ctx) > 0)
  102. {
  103. if (duk_is_string(ctx, 0) && strlen(duk_get_string(ctx, 0)))
  104. typeHash = duk_get_string(ctx, 0);
  105. }
  106. if (duk_get_top(ctx) > 1)
  107. recursive = duk_require_boolean(ctx, 1) ? true : false;
  108. duk_push_this(ctx);
  109. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  110. PODVector<Component*> dest;
  111. node->GetComponents(dest, typeHash, 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], "Component");
  116. duk_put_prop_index(ctx, -2, i);
  117. }
  118. return 1;
  119. }
  120. static int Node_GetChildAtIndex(duk_context* ctx)
  121. {
  122. duk_push_this(ctx);
  123. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  124. unsigned idx = (unsigned) duk_to_number(ctx, 0);
  125. if (node->GetNumChildren() <= idx)
  126. {
  127. duk_push_null(ctx);
  128. return 1;
  129. }
  130. Node* child = node->GetChild(idx);
  131. js_push_class_object_instance(ctx, child, "Node");
  132. return 1;
  133. }
  134. static int Node_SaveXML(duk_context* ctx)
  135. {
  136. File* file = js_to_class_instance<File>(ctx, 0, 0);
  137. duk_push_this(ctx);
  138. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  139. duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
  140. return 1;
  141. }
  142. static int Scene_LoadXML(duk_context* ctx)
  143. {
  144. JSVM* vm = JSVM::GetJSVM(ctx);
  145. String filename = duk_to_string(ctx, 0);
  146. ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
  147. SharedPtr<File> file = cache->GetFile(filename);
  148. if (!file->IsOpen())
  149. {
  150. duk_push_false(ctx);
  151. return 1;
  152. }
  153. duk_push_this(ctx);
  154. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  155. bool success = scene->LoadXML(*file);
  156. if (success)
  157. duk_push_true(ctx);
  158. else
  159. duk_push_false(ctx);
  160. return 1;
  161. }
  162. static int Scene_GetMainCamera(duk_context* ctx)
  163. {
  164. duk_push_this(ctx);
  165. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  166. PODVector<Node*> cameraNodes;
  167. Camera* camera = 0;
  168. scene->GetChildrenWithComponent(cameraNodes, Camera::GetTypeStatic(), true);
  169. if (cameraNodes.Size())
  170. {
  171. camera = cameraNodes[0]->GetComponent<Camera>();
  172. }
  173. if (!camera)
  174. {
  175. duk_push_null(ctx);
  176. return 1;
  177. }
  178. js_push_class_object_instance(ctx, camera, "Camera");
  179. return 1;
  180. }
  181. void jsapi_init_scene(JSVM* vm)
  182. {
  183. duk_context* ctx = vm->GetJSContext();
  184. jsapi_init_scene_serializable(vm);
  185. js_class_get_prototype(ctx, "Atomic", "Node");
  186. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  187. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  188. duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
  189. duk_put_prop_string(ctx, -2, "getChildrenWithName");
  190. duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
  191. duk_put_prop_string(ctx, -2, "getComponents");
  192. duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
  193. duk_put_prop_string(ctx, -2, "createJSComponent");
  194. duk_push_c_function(ctx, Node_GetJSComponent, 1);
  195. duk_put_prop_string(ctx, -2, "getJSComponent");
  196. duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
  197. duk_put_prop_string(ctx, -2, "getChildAtIndex");
  198. duk_push_c_function(ctx, Node_SaveXML, 1);
  199. duk_put_prop_string(ctx, -2, "saveXML");
  200. duk_pop(ctx);
  201. js_class_get_prototype(ctx, "Atomic", "Scene");
  202. duk_push_c_function(ctx, Scene_LoadXML, 1);
  203. duk_put_prop_string(ctx, -2, "loadXML");
  204. duk_push_c_function(ctx, Scene_GetMainCamera, 0);
  205. duk_put_prop_string(ctx, -2, "getMainCamera");
  206. duk_pop(ctx);
  207. }
  208. }