JSScene.cpp 4.6 KB

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