JSSceneSerializable.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_SetAttribute(duk_context* ctx)
  32. {
  33. const char* name = duk_to_string(ctx, 0);
  34. Variant v;
  35. js_to_variant(ctx, 1, v);
  36. duk_push_this(ctx);
  37. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  38. serial->SetAttribute(name, v);
  39. return 0;
  40. }
  41. static int Serializable_GetAttributes(duk_context* ctx)
  42. {
  43. duk_push_this(ctx);
  44. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  45. unsigned type = serial->GetType().Value();
  46. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  47. duk_get_prop_index(ctx, -1, type);
  48. // return cached array of attrinfo
  49. if (duk_is_object(ctx, -1))
  50. return 1;
  51. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  52. duk_push_array(ctx);
  53. duk_dup(ctx, -1);
  54. duk_put_prop_index(ctx, -4, type);
  55. if (!attrs)
  56. return 1;
  57. for (unsigned i = 0; i < attrs->Size(); i++)
  58. {
  59. const AttributeInfo* attr = &attrs->At(i);
  60. duk_push_object(ctx);
  61. duk_push_number(ctx, (double) attr->type_);
  62. duk_put_prop_string(ctx, -2, "type");
  63. duk_push_string(ctx, attr->name_.CString());
  64. duk_put_prop_string(ctx, -2, "name");
  65. duk_push_number(ctx, (double) attr->mode_);
  66. duk_put_prop_string(ctx, -2, "mode");
  67. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  68. duk_put_prop_string(ctx, -2, "defaultValue");
  69. duk_push_array(ctx);
  70. const char** enumPtr = attr->enumNames_;
  71. unsigned enumCount = 0;
  72. if (enumPtr)
  73. {
  74. while (*enumPtr)
  75. {
  76. duk_push_string(ctx, *enumPtr);
  77. duk_put_prop_index(ctx, -2, enumCount++);
  78. enumPtr++;
  79. }
  80. }
  81. duk_put_prop_string(ctx, -2, "enumNames");
  82. // store attr object
  83. duk_put_prop_index(ctx, -2, i);
  84. }
  85. return 1;
  86. }
  87. void jsapi_init_scene_serializable(JSVM* vm)
  88. {
  89. duk_context* ctx = vm->GetJSContext();
  90. // cached attr
  91. duk_push_object(ctx);
  92. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  93. js_class_get_prototype(ctx, "Atomic", "Serializable");
  94. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  95. duk_put_prop_string(ctx, -2, "getAttributes");
  96. duk_push_c_function(ctx, Serializable_SetAttribute, 2);
  97. duk_put_prop_string(ctx, -2, "setAttribute");
  98. duk_pop(ctx);
  99. }
  100. }