JSScene.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 Node_SaveXML(duk_context* ctx)
  99. {
  100. File* file = js_to_class_instance<File>(ctx, 0, 0);
  101. duk_push_this(ctx);
  102. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  103. duk_push_boolean(ctx, node->SaveXML(*file) ? 1 : 0);
  104. return 1;
  105. }
  106. static int Scene_LoadXML(duk_context* ctx)
  107. {
  108. JSVM* vm = JSVM::GetJSVM(ctx);
  109. String filename = duk_to_string(ctx, 0);
  110. ResourceCache* cache = vm->GetSubsystem<ResourceCache>();
  111. SharedPtr<File> file = cache->GetFile(filename);
  112. if (!file->IsOpen())
  113. {
  114. duk_push_false(ctx);
  115. return 1;
  116. }
  117. duk_push_this(ctx);
  118. Scene* scene = js_to_class_instance<Scene>(ctx, -1, 0);
  119. bool success = scene->LoadXML(*file);
  120. if (success)
  121. duk_push_true(ctx);
  122. else
  123. duk_push_false(ctx);
  124. return 1;
  125. }
  126. void jsapi_init_scene(JSVM* vm)
  127. {
  128. duk_context* ctx = vm->GetJSContext();
  129. jsapi_init_scene_serializable(vm);
  130. js_class_get_prototype(ctx, "Atomic", "Node");
  131. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  132. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  133. duk_push_c_function(ctx, Node_GetChildrenWithName, DUK_VARARGS);
  134. duk_put_prop_string(ctx, -2, "getChildrenWithName");
  135. duk_push_c_function(ctx, Node_GetComponents, DUK_VARARGS);
  136. duk_put_prop_string(ctx, -2, "getComponents");
  137. duk_push_c_function(ctx, Node_CreateJSComponent, 1);
  138. duk_put_prop_string(ctx, -2, "createJSComponent");
  139. duk_push_c_function(ctx, Node_GetChildAtIndex, 1);
  140. duk_put_prop_string(ctx, -2, "getChildAtIndex");
  141. duk_push_c_function(ctx, Node_SaveXML, 1);
  142. duk_put_prop_string(ctx, -2, "saveXML");
  143. duk_pop(ctx);
  144. js_class_get_prototype(ctx, "Atomic", "Scene");
  145. duk_push_c_function(ctx, Scene_LoadXML, 1);
  146. duk_put_prop_string(ctx, -2, "loadXML");
  147. duk_pop(ctx);
  148. }
  149. }