JSSceneSerializable.cpp 8.4 KB

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