JSSceneSerializable.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /*
  14. /// Attribute type.
  15. VariantType type_;
  16. /// Name.
  17. String name_;
  18. /// Byte offset from start of object.
  19. unsigned offset_;
  20. /// Enum names.
  21. const char** enumNames_;
  22. /// Helper object for accessor mode.
  23. SharedPtr<AttributeAccessor> accessor_;
  24. /// Default value for network replication.
  25. Variant defaultValue_;
  26. /// Attribute mode: whether to use for serialization, network replication, or both.
  27. unsigned mode_;
  28. /// Attribute data pointer if elsewhere than in the Serializable.
  29. void* ptr_;
  30. */
  31. static int Serializable_GetAttributes(duk_context* ctx)
  32. {
  33. duk_push_this(ctx);
  34. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  35. unsigned type = serial->GetType().Value();
  36. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  37. duk_get_prop_index(ctx, -1, type);
  38. // return cached array of attrinfo
  39. if (duk_is_object(ctx, -1))
  40. return 1;
  41. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  42. duk_push_array(ctx);
  43. duk_dup(ctx, -1);
  44. duk_put_prop_index(ctx, -4, type);
  45. for (unsigned i = 0; i < attrs->Size(); i++)
  46. {
  47. const AttributeInfo* attr = &attrs->At(i);
  48. duk_push_object(ctx);
  49. duk_push_number(ctx, (double) attr->type_);
  50. duk_put_prop_string(ctx, -2, "type");
  51. duk_push_string(ctx, attr->name_.CString());
  52. duk_put_prop_string(ctx, -2, "name");
  53. duk_push_number(ctx, (double) attr->mode_);
  54. duk_put_prop_string(ctx, -2, "mode");
  55. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  56. duk_put_prop_string(ctx, -2, "defaultValue");
  57. duk_push_array(ctx);
  58. const char** enumPtr = attr->enumNames_;
  59. unsigned enumCount = 0;
  60. if (enumPtr)
  61. {
  62. while (*enumPtr)
  63. {
  64. duk_push_string(ctx, *enumPtr);
  65. duk_put_prop_index(ctx, -2, enumCount++);
  66. }
  67. }
  68. duk_put_prop_string(ctx, -2, "enumNames");
  69. // store attr object
  70. duk_put_prop_index(ctx, -2, i);
  71. }
  72. return 1;
  73. }
  74. void jsapi_init_scene_serializable(JSVM* vm)
  75. {
  76. duk_context* ctx = vm->GetJSContext();
  77. // cached attr
  78. duk_push_object(ctx);
  79. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  80. js_class_get_prototype(ctx, "Atomic", "Serializable");
  81. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  82. duk_put_prop_string(ctx, -2, "getAttributes");
  83. duk_pop(ctx);
  84. }
  85. }