JSScene.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "JSScene.h"
  9. #include "JSComponent.h"
  10. #include "JSVM.h"
  11. namespace Atomic
  12. {
  13. void jsapi_init_scene_serializable(JSVM* vm);
  14. static int Node_CreateJSComponent(duk_context* ctx)
  15. {
  16. String path = duk_require_string(ctx, 0);
  17. bool hasArgs = false;
  18. int argIdx;
  19. if (duk_get_top(ctx) > 1 && duk_is_object(ctx, 1))
  20. {
  21. hasArgs = true;
  22. argIdx = 1;
  23. }
  24. duk_push_this(ctx);
  25. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  26. ResourceCache* cache = node->GetContext()->GetSubsystem<ResourceCache>();
  27. JSComponentFile* file = cache->GetResource<JSComponentFile>(path);
  28. if (!file)
  29. {
  30. LOGERRORF("Unable to load component file %s", path.CString());
  31. duk_push_undefined(ctx);
  32. return 1;
  33. }
  34. JSComponent* jsc = file->CreateJSComponent();
  35. node->AddComponent(jsc, jsc->GetID(), LOCAL);
  36. jsc->InitInstance(hasArgs, argIdx);
  37. js_push_class_object_instance(ctx, jsc, "JSComponent");
  38. return 1;
  39. }
  40. static int Node_GetChildrenWithComponent(duk_context* ctx)
  41. {
  42. StringHash type = duk_to_string(ctx, 0);
  43. bool recursive = false;
  44. if (duk_get_top(ctx) == 2)
  45. if (duk_get_boolean(ctx, 1))
  46. recursive = true;
  47. duk_push_this(ctx);
  48. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  49. PODVector<Node*> dest;
  50. node->GetChildrenWithComponent(dest, type, recursive);
  51. duk_push_array(ctx);
  52. for (unsigned i = 0; i < dest.Size(); i++)
  53. {
  54. js_push_class_object_instance(ctx, dest[i], "Node");
  55. duk_put_prop_index(ctx, -2, i);
  56. }
  57. return 1;
  58. }
  59. static int Node_GetChildrenWithName(duk_context* ctx)
  60. {
  61. StringHash nameHash = 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->GetChildrenWithName(dest, nameHash, 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_GetComponents(duk_context* ctx)
  79. {
  80. bool recursive = false;
  81. StringHash typeHash = Component::GetTypeStatic();
  82. if (duk_get_top(ctx) > 0)
  83. {
  84. if (duk_is_string(ctx, 0) && strlen(duk_get_string(ctx, 0)))
  85. typeHash = duk_get_string(ctx, 0);
  86. }
  87. if (duk_get_top(ctx) > 1)
  88. recursive = duk_require_boolean(ctx, 1) ? true : false;
  89. duk_push_this(ctx);
  90. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  91. PODVector<Component*> dest;
  92. node->GetComponents(dest, typeHash, recursive);
  93. duk_push_array(ctx);
  94. for (unsigned i = 0; i < dest.Size(); i++)
  95. {
  96. js_push_class_object_instance(ctx, dest[i], "Component");
  97. duk_put_prop_index(ctx, -2, i);
  98. }
  99. return 1;
  100. }
  101. static int Node_GetChildAtIndex(duk_context* ctx)
  102. {
  103. duk_push_this(ctx);
  104. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  105. unsigned idx = (unsigned) duk_to_number(ctx, 0);
  106. if (node->GetNumChildren() <= idx)
  107. {
  108. duk_push_null(ctx);
  109. return 1;
  110. }
  111. Node* child = node->GetChild(idx);
  112. js_push_class_object_instance(ctx, child, "Node");
  113. return 1;
  114. }
  115. static int Node_SaveXML(duk_context* ctx)
  116. {
  117. File* file = js_to_class_instance<File>(ctx, 0, 0);
  118. duk_push_this(ctx);
  119. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  120. duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
  121. return 1;
  122. }
  123. static int Scene_LoadXML(duk_context* ctx)
  124. {
  125. JSVM* vm = JSVM::GetJSVM(ctx);
  126. String filename = duk_to_string(ctx, 0);
  127. ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
  128. SharedPtr<File> file = cache->GetFile(filename);
  129. if (!file->IsOpen())
  130. {
  131. duk_push_false(ctx);
  132. return 1;
  133. }
  134. duk_push_this(ctx);
  135. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  136. bool success = scene->LoadXML(*file);
  137. if (success)
  138. duk_push_true(ctx);
  139. else
  140. duk_push_false(ctx);
  141. return 1;
  142. }
  143. void jsapi_init_scene(JSVM* vm)
  144. {
  145. duk_context* ctx = vm->GetJSContext();
  146. jsapi_init_scene_serializable(vm);
  147. js_class_get_prototype(ctx, "Atomic", "Node");
  148. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  149. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  150. duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
  151. duk_put_prop_string(ctx, -2, "getChildrenWithName");
  152. duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
  153. duk_put_prop_string(ctx, -2, "getComponents");
  154. duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
  155. duk_put_prop_string(ctx, -2, "createJSComponent");
  156. duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
  157. duk_put_prop_string(ctx, -2, "getChildAtIndex");
  158. duk_push_c_function(ctx, Node_SaveXML, 1);
  159. duk_put_prop_string(ctx, -2, "saveXML");
  160. duk_pop(ctx);
  161. js_class_get_prototype(ctx, "Atomic", "Scene");
  162. duk_push_c_function(ctx, Scene_LoadXML, 1);
  163. duk_put_prop_string(ctx, -2, "loadXML");
  164. duk_pop(ctx);
  165. }
  166. }