JSScene.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. JSComponent* jsc = node->CreateComponent<JSComponent>();
  27. ResourceCache* cache = node->GetContext()->GetSubsystem<ResourceCache>();
  28. JSComponentFile* file = cache->GetResource<JSComponentFile>(path);
  29. jsc->SetComponentFile(file);
  30. jsc->InitInstance(hasArgs, argIdx);
  31. js_push_class_object_instance(ctx, jsc, "JSComponent");
  32. return 1;
  33. }
  34. static int Node_GetChildrenWithComponent(duk_context* ctx)
  35. {
  36. StringHash type = duk_to_string(ctx, 0);
  37. bool recursive = false;
  38. if (duk_get_top(ctx) == 2)
  39. if (duk_get_boolean(ctx, 1))
  40. recursive = true;
  41. duk_push_this(ctx);
  42. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  43. PODVector<Node*> dest;
  44. node->GetChildrenWithComponent(dest, type, recursive);
  45. duk_push_array(ctx);
  46. for (unsigned i = 0; i < dest.Size(); i++)
  47. {
  48. js_push_class_object_instance(ctx, dest[i], "Node");
  49. duk_put_prop_index(ctx, -2, i);
  50. }
  51. return 1;
  52. }
  53. static int Node_GetChildrenWithName(duk_context* ctx)
  54. {
  55. StringHash nameHash = duk_to_string(ctx, 0);
  56. bool recursive = false;
  57. if (duk_get_top(ctx) == 2)
  58. if (duk_get_boolean(ctx, 1))
  59. recursive = true;
  60. duk_push_this(ctx);
  61. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  62. PODVector<Node*> dest;
  63. node->GetChildrenWithName(dest, nameHash, recursive);
  64. duk_push_array(ctx);
  65. for (unsigned i = 0; i < dest.Size(); i++)
  66. {
  67. js_push_class_object_instance(ctx, dest[i], "Node");
  68. duk_put_prop_index(ctx, -2, i);
  69. }
  70. return 1;
  71. }
  72. static int Node_GetComponents(duk_context* ctx)
  73. {
  74. bool recursive = false;
  75. StringHash typeHash = Component::GetTypeStatic();
  76. if (duk_get_top(ctx) > 0)
  77. {
  78. if (duk_is_string(ctx, 0) && strlen(duk_get_string(ctx, 0)))
  79. typeHash = duk_get_string(ctx, 0);
  80. }
  81. if (duk_get_top(ctx) > 1)
  82. recursive = duk_require_boolean(ctx, 1) ? true : false;
  83. duk_push_this(ctx);
  84. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  85. PODVector<Component*> dest;
  86. node->GetComponents(dest, typeHash, recursive);
  87. duk_push_array(ctx);
  88. for (unsigned i = 0; i < dest.Size(); i++)
  89. {
  90. js_push_class_object_instance(ctx, dest[i], "Component");
  91. duk_put_prop_index(ctx, -2, i);
  92. }
  93. return 1;
  94. }
  95. static int Node_GetChildAtIndex(duk_context* ctx)
  96. {
  97. duk_push_this(ctx);
  98. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  99. unsigned idx = (unsigned) duk_to_number(ctx, 0);
  100. if (node->GetNumChildren() <= idx)
  101. {
  102. duk_push_null(ctx);
  103. return 1;
  104. }
  105. Node* child = node->GetChild(idx);
  106. js_push_class_object_instance(ctx, child, "Node");
  107. return 1;
  108. }
  109. static int Node_SaveXML(duk_context* ctx)
  110. {
  111. File* file = js_to_class_instance<File>(ctx, 0, 0);
  112. duk_push_this(ctx);
  113. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  114. duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
  115. return 1;
  116. }
  117. static int Scene_LoadXML(duk_context* ctx)
  118. {
  119. JSVM* vm = JSVM::GetJSVM(ctx);
  120. String filename = duk_to_string(ctx, 0);
  121. ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
  122. SharedPtr<File> file = cache->GetFile(filename);
  123. if (!file->IsOpen())
  124. {
  125. duk_push_false(ctx);
  126. return 1;
  127. }
  128. duk_push_this(ctx);
  129. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  130. bool success = scene->LoadXML(*file);
  131. if (success)
  132. duk_push_true(ctx);
  133. else
  134. duk_push_false(ctx);
  135. return 1;
  136. }
  137. void jsapi_init_scene(JSVM* vm)
  138. {
  139. duk_context* ctx = vm->GetJSContext();
  140. jsapi_init_scene_serializable(vm);
  141. js_class_get_prototype(ctx, "Atomic", "Node");
  142. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  143. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  144. duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
  145. duk_put_prop_string(ctx, -2, "getChildrenWithName");
  146. duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
  147. duk_put_prop_string(ctx, -2, "getComponents");
  148. duk_push_c_function(ctx, Node_CreateJSComponent, DUK_VARARGS);
  149. duk_put_prop_string(ctx, -2, "createJSComponent");
  150. duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
  151. duk_put_prop_string(ctx, -2, "getChildAtIndex");
  152. duk_push_c_function(ctx, Node_SaveXML, 1);
  153. duk_put_prop_string(ctx, -2, "saveXML");
  154. duk_pop(ctx);
  155. js_class_get_prototype(ctx, "Atomic", "Scene");
  156. duk_push_c_function(ctx, Scene_LoadXML, 1);
  157. duk_put_prop_string(ctx, -2, "loadXML");
  158. duk_pop(ctx);
  159. }
  160. }