JSScene.cpp 4.0 KB

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