JSSceneSerializable.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. const Vector<AttributeInfo>* attributes = serial->GetAttributes();
  39. for (unsigned i = 0; i < attributes->Size(); i++)
  40. {
  41. const AttributeInfo* attr = &attributes->At(i);
  42. if (attr->name_ == name)
  43. {
  44. if (attr->type_ == VAR_QUATERNION)
  45. {
  46. Vector3 v3 = v.GetVector3();
  47. Quaternion q;
  48. q.FromEulerAngles(v3.x_, v3.y_, v3.z_);
  49. v = q;
  50. }
  51. else if (attr->type_ == VAR_COLOR)
  52. {
  53. Vector4 v4 = v.GetVector4();
  54. Color c(v4.x_, v4.y_, v4.z_, v4.w_ );
  55. v = c;
  56. }
  57. else if (attr->type_ == VAR_INT)
  58. {
  59. v = (int) v.GetFloat();
  60. }
  61. break;
  62. }
  63. }
  64. serial->SetAttribute(name, v);
  65. return 0;
  66. }
  67. static int Serializable_GetAttribute(duk_context* ctx)
  68. {
  69. const char* name = duk_to_string(ctx, 0);
  70. duk_push_this(ctx);
  71. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  72. js_push_variant(ctx, serial->GetAttribute(name));
  73. return 1;
  74. }
  75. static int Serializable_GetAttributes(duk_context* ctx)
  76. {
  77. duk_push_this(ctx);
  78. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  79. unsigned type = serial->GetType().Value();
  80. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  81. duk_get_prop_index(ctx, -1, type);
  82. // return cached array of attrinfo
  83. if (duk_is_object(ctx, -1))
  84. return 1;
  85. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  86. duk_push_array(ctx);
  87. duk_dup(ctx, -1);
  88. duk_put_prop_index(ctx, -4, type);
  89. if (!attrs)
  90. return 1;
  91. for (unsigned i = 0; i < attrs->Size(); i++)
  92. {
  93. const AttributeInfo* attr = &attrs->At(i);
  94. if (attr->mode_ & AM_NOEDIT)
  95. continue;
  96. duk_push_object(ctx);
  97. duk_push_number(ctx, (double) attr->type_);
  98. duk_put_prop_string(ctx, -2, "type");
  99. duk_push_string(ctx, attr->name_.CString());
  100. duk_put_prop_string(ctx, -2, "name");
  101. duk_push_number(ctx, (double) attr->mode_);
  102. duk_put_prop_string(ctx, -2, "mode");
  103. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  104. duk_put_prop_string(ctx, -2, "defaultValue");
  105. duk_push_array(ctx);
  106. const char** enumPtr = attr->enumNames_;
  107. unsigned enumCount = 0;
  108. if (enumPtr)
  109. {
  110. while (*enumPtr)
  111. {
  112. duk_push_string(ctx, *enumPtr);
  113. duk_put_prop_index(ctx, -2, enumCount++);
  114. enumPtr++;
  115. }
  116. }
  117. duk_put_prop_string(ctx, -2, "enumNames");
  118. // store attr object
  119. duk_put_prop_index(ctx, -2, i);
  120. }
  121. return 1;
  122. }
  123. void jsapi_init_scene_serializable(JSVM* vm)
  124. {
  125. duk_context* ctx = vm->GetJSContext();
  126. // cached attr
  127. duk_push_object(ctx);
  128. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  129. js_class_get_prototype(ctx, "Atomic", "Serializable");
  130. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  131. duk_put_prop_string(ctx, -2, "getAttributes");
  132. duk_push_c_function(ctx, Serializable_GetAttribute, 1);
  133. duk_put_prop_string(ctx, -2, "getAttribute");
  134. duk_push_c_function(ctx, Serializable_SetAttribute, 2);
  135. duk_put_prop_string(ctx, -2, "setAttribute");
  136. duk_pop(ctx);
  137. }
  138. }