JSScene.cpp 5.1 KB

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