JSScene.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "Precompiled.h"
  5. #include "../Javascript/JSScene.h"
  6. #include "../Javascript/JSComponent.h"
  7. #include "../Javascript/JSVM.h"
  8. #include "../Scene/Node.h"
  9. namespace Atomic
  10. {
  11. static int Node_CreateJSComponent(duk_context* ctx)
  12. {
  13. duk_push_this(ctx);
  14. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  15. JSComponent* jsc = node->CreateComponent<JSComponent>();
  16. jsc->SetClassName(duk_to_string(ctx, 0));
  17. js_push_class_object_instance(ctx, jsc, "JSComponent");
  18. return 1;
  19. }
  20. static int Node_GetChildrenWithComponent(duk_context* ctx)
  21. {
  22. StringHash type = duk_to_string(ctx, 0);
  23. bool recursive = false;
  24. if (duk_get_top(ctx) == 2)
  25. if (duk_get_boolean(ctx, 1))
  26. recursive = true;
  27. duk_push_this(ctx);
  28. Node* node = js_to_class_instance<Node>(ctx, -1, 0);
  29. PODVector<Node*> dest;
  30. node->GetChildrenWithComponent(dest, type, recursive);
  31. duk_push_array(ctx);
  32. for (unsigned i = 0; i < dest.Size(); i++)
  33. {
  34. js_push_class_object_instance(ctx, dest[i], "Node");
  35. duk_put_prop_index(ctx, -2, i);
  36. }
  37. return 1;
  38. }
  39. void jsapi_init_scene(JSVM* vm)
  40. {
  41. duk_context* ctx = vm->GetJSContext();
  42. js_class_get_prototype(ctx, "Node");
  43. duk_push_c_function(ctx, Node_GetChildrenWithComponent, DUK_VARARGS);
  44. duk_put_prop_string(ctx, -2, "getChildrenWithComponent");
  45. duk_push_c_function(ctx, Node_CreateJSComponent, 1);
  46. duk_put_prop_string(ctx, -2, "createJSComponent");
  47. duk_pop(ctx);
  48. }
  49. }