JSSceneSerializable.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. VariantType variantType = VAR_NONE;
  40. bool isAttr = false;
  41. if (attributes)
  42. {
  43. for (unsigned i = 0; i < attributes->Size(); i++)
  44. {
  45. const AttributeInfo* attr = &attributes->At(i);
  46. if (!attr->name_.Compare(name))
  47. {
  48. isAttr = true;
  49. variantType = attr->type_;
  50. break;
  51. }
  52. }
  53. }
  54. JSComponent* jsc = NULL;
  55. // check dynamic
  56. if (!isAttr)
  57. {
  58. if (serial->GetType() == JSComponent::GetTypeStatic())
  59. {
  60. jsc = (JSComponent*) serial;
  61. JSComponentFile* file = jsc->GetComponentFile();
  62. const HashMap<String, VariantType>& fields = file->GetFields();
  63. if (fields.Contains(name))
  64. {
  65. HashMap<String, VariantType>::ConstIterator itr = fields.Find(name);
  66. variantType = itr->second_;
  67. }
  68. }
  69. }
  70. if (variantType == VAR_NONE)
  71. return 0;
  72. if (variantType == VAR_QUATERNION)
  73. {
  74. Vector3 v3 = v.GetVector3();
  75. Quaternion q;
  76. q.FromEulerAngles(v3.x_, v3.y_, v3.z_);
  77. v = q;
  78. }
  79. else if (variantType == VAR_COLOR)
  80. {
  81. Vector4 v4 = v.GetVector4();
  82. Color c(v4.x_, v4.y_, v4.z_, v4.w_ );
  83. v = c;
  84. }
  85. else if (variantType == VAR_INT)
  86. {
  87. v = (int) v.GetFloat();
  88. }
  89. if (isAttr)
  90. {
  91. serial->SetAttribute(name, v);
  92. return 0;
  93. }
  94. // check dynamic
  95. if (jsc)
  96. {
  97. VariantMap& values = jsc->GetFieldValues();
  98. values[name] = v;
  99. }
  100. return 0;
  101. }
  102. static int Serializable_GetAttribute(duk_context* ctx)
  103. {
  104. const char* name = duk_to_string(ctx, 0);
  105. duk_push_this(ctx);
  106. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  107. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  108. if (attrs)
  109. {
  110. for (unsigned i = 0; i < attrs->Size(); i++)
  111. {
  112. const AttributeInfo* attr = &attrs->At(i);
  113. if (!attr->name_.Compare(name))
  114. {
  115. // FIXME: this is a double lookup
  116. js_push_variant(ctx, serial->GetAttribute(name));
  117. return 1;
  118. }
  119. }
  120. }
  121. if (serial->GetType() == JSComponent::GetTypeStatic())
  122. {
  123. JSComponent* jsc = (JSComponent*) serial;
  124. JSComponentFile* file = jsc->GetComponentFile();
  125. const HashMap<String, VariantType>& fields = file->GetFields();
  126. if (fields.Contains(name))
  127. {
  128. VariantMap& values = jsc->GetFieldValues();
  129. if (values.Contains(name))
  130. {
  131. js_push_variant(ctx, values[name]);
  132. return 1;
  133. }
  134. else
  135. {
  136. Variant v;
  137. file->GetDefaultFieldValue(name, v);
  138. js_push_variant(ctx, v);
  139. return 1;
  140. }
  141. }
  142. }
  143. duk_push_undefined(ctx);
  144. return 1;
  145. }
  146. static int Serializable_GetAttributes(duk_context* ctx)
  147. {
  148. duk_push_this(ctx);
  149. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  150. unsigned type = serial->GetType().Value();
  151. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  152. duk_get_prop_index(ctx, -1, type);
  153. // return cached array of attrinfo, unless JSComponent which has dynamic fields
  154. if (serial->GetType() != JSComponent::GetTypeStatic() && duk_is_object(ctx, -1))
  155. return 1;
  156. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  157. duk_push_array(ctx);
  158. duk_dup(ctx, -1);
  159. duk_put_prop_index(ctx, -4, type);
  160. unsigned count = 0;
  161. if (attrs)
  162. {
  163. count = attrs->Size();
  164. for (unsigned i = 0; i < attrs->Size(); i++)
  165. {
  166. const AttributeInfo* attr = &attrs->At(i);
  167. if (attr->mode_ & AM_NOEDIT)
  168. continue;
  169. duk_push_object(ctx);
  170. duk_push_number(ctx, (double) attr->type_);
  171. duk_put_prop_string(ctx, -2, "type");
  172. duk_push_string(ctx, attr->name_.CString());
  173. duk_put_prop_string(ctx, -2, "name");
  174. duk_push_number(ctx, (double) attr->mode_);
  175. duk_put_prop_string(ctx, -2, "mode");
  176. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  177. duk_put_prop_string(ctx, -2, "defaultValue");
  178. duk_push_boolean(ctx, 0);
  179. duk_put_prop_string(ctx, -2, "field");
  180. duk_push_array(ctx);
  181. const char** enumPtr = attr->enumNames_;
  182. unsigned enumCount = 0;
  183. if (enumPtr)
  184. {
  185. while (*enumPtr)
  186. {
  187. duk_push_string(ctx, *enumPtr);
  188. duk_put_prop_index(ctx, -2, enumCount++);
  189. enumPtr++;
  190. }
  191. }
  192. duk_put_prop_string(ctx, -2, "enumNames");
  193. // store attr object
  194. duk_put_prop_index(ctx, -2, i);
  195. }
  196. }
  197. // dynamic script fields
  198. if (serial->GetType() == JSComponent::GetTypeStatic())
  199. {
  200. JSComponent* jsc = (JSComponent*) serial;
  201. JSComponentFile* file = jsc->GetComponentFile();
  202. const HashMap<String, VariantType>& fields = file->GetFields();
  203. if (fields.Size())
  204. {
  205. HashMap<String, VariantType>::ConstIterator itr = fields.Begin();
  206. while (itr != fields.End())
  207. {
  208. duk_push_object(ctx);
  209. duk_push_number(ctx, (double) itr->second_);
  210. duk_put_prop_string(ctx, -2, "type");
  211. duk_push_string(ctx, itr->first_.CString());
  212. duk_put_prop_string(ctx, -2, "name");
  213. duk_push_number(ctx, (double) AM_DEFAULT);
  214. duk_put_prop_string(ctx, -2, "mode");
  215. duk_push_string(ctx,"");
  216. duk_put_prop_string(ctx, -2, "defaultValue");
  217. duk_push_boolean(ctx, 1);
  218. duk_put_prop_string(ctx, -2, "field");
  219. duk_push_array(ctx);
  220. duk_put_prop_string(ctx, -2, "enumNames");
  221. // store attr object
  222. duk_put_prop_index(ctx, -2, count++);
  223. itr++;
  224. }
  225. }
  226. }
  227. return 1;
  228. }
  229. void jsapi_init_scene_serializable(JSVM* vm)
  230. {
  231. duk_context* ctx = vm->GetJSContext();
  232. // cached attr
  233. duk_push_object(ctx);
  234. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  235. js_class_get_prototype(ctx, "Atomic", "Serializable");
  236. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  237. duk_put_prop_string(ctx, -2, "getAttributes");
  238. duk_push_c_function(ctx, Serializable_GetAttribute, 1);
  239. duk_put_prop_string(ctx, -2, "getAttribute");
  240. duk_push_c_function(ctx, Serializable_SetAttribute, 2);
  241. duk_put_prop_string(ctx, -2, "setAttribute");
  242. duk_pop(ctx);
  243. }
  244. }