JSSceneSerializable.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. HashMap<String, VariantType>::ConstIterator itr = fields.Find(name);
  137. Variant v;
  138. switch (itr->second_)
  139. {
  140. case VAR_BOOL:
  141. v = false;
  142. break;
  143. case VAR_STRING:
  144. v = "";
  145. break;
  146. case VAR_FLOAT:
  147. v = 0.0f;
  148. break;
  149. case VAR_VECTOR3:
  150. v = Vector3::ZERO;
  151. break;
  152. default:
  153. break;
  154. }
  155. js_push_variant(ctx, v);
  156. return 1;
  157. }
  158. }
  159. }
  160. duk_push_undefined(ctx);
  161. return 1;
  162. }
  163. static int Serializable_GetAttributes(duk_context* ctx)
  164. {
  165. duk_push_this(ctx);
  166. Serializable* serial = js_to_class_instance<Serializable>(ctx, -1, 0);
  167. unsigned type = serial->GetType().Value();
  168. duk_get_global_string(ctx, "__atomic_scene_serializable_attributes");
  169. duk_get_prop_index(ctx, -1, type);
  170. // return cached array of attrinfo
  171. if (duk_is_object(ctx, -1))
  172. return 1;
  173. const Vector<AttributeInfo>* attrs = serial->GetAttributes();
  174. duk_push_array(ctx);
  175. duk_dup(ctx, -1);
  176. duk_put_prop_index(ctx, -4, type);
  177. unsigned count = 0;
  178. if (attrs)
  179. {
  180. count = attrs->Size();
  181. for (unsigned i = 0; i < attrs->Size(); i++)
  182. {
  183. const AttributeInfo* attr = &attrs->At(i);
  184. if (attr->mode_ & AM_NOEDIT)
  185. continue;
  186. duk_push_object(ctx);
  187. duk_push_number(ctx, (double) attr->type_);
  188. duk_put_prop_string(ctx, -2, "type");
  189. duk_push_string(ctx, attr->name_.CString());
  190. duk_put_prop_string(ctx, -2, "name");
  191. duk_push_number(ctx, (double) attr->mode_);
  192. duk_put_prop_string(ctx, -2, "mode");
  193. duk_push_string(ctx,attr->defaultValue_.ToString().CString());
  194. duk_put_prop_string(ctx, -2, "defaultValue");
  195. duk_push_boolean(ctx, 0);
  196. duk_put_prop_string(ctx, -2, "field");
  197. duk_push_array(ctx);
  198. const char** enumPtr = attr->enumNames_;
  199. unsigned enumCount = 0;
  200. if (enumPtr)
  201. {
  202. while (*enumPtr)
  203. {
  204. duk_push_string(ctx, *enumPtr);
  205. duk_put_prop_index(ctx, -2, enumCount++);
  206. enumPtr++;
  207. }
  208. }
  209. duk_put_prop_string(ctx, -2, "enumNames");
  210. // store attr object
  211. duk_put_prop_index(ctx, -2, i);
  212. }
  213. }
  214. // dynamic script fields
  215. if (serial->GetType() == JSComponent::GetTypeStatic())
  216. {
  217. JSComponent* jsc = (JSComponent*) serial;
  218. JSComponentFile* file = jsc->GetComponentFile();
  219. const HashMap<String, VariantType>& fields = file->GetFields();
  220. if (fields.Size())
  221. {
  222. HashMap<String, VariantType>::ConstIterator itr = fields.Begin();
  223. while (itr != fields.End())
  224. {
  225. duk_push_object(ctx);
  226. duk_push_number(ctx, (double) itr->second_);
  227. duk_put_prop_string(ctx, -2, "type");
  228. duk_push_string(ctx, itr->first_.CString());
  229. duk_put_prop_string(ctx, -2, "name");
  230. duk_push_number(ctx, (double) AM_DEFAULT);
  231. duk_put_prop_string(ctx, -2, "mode");
  232. duk_push_string(ctx,"");
  233. duk_put_prop_string(ctx, -2, "defaultValue");
  234. duk_push_boolean(ctx, 1);
  235. duk_put_prop_string(ctx, -2, "field");
  236. duk_push_array(ctx);
  237. duk_put_prop_string(ctx, -2, "enumNames");
  238. // store attr object
  239. duk_put_prop_index(ctx, -2, count++);
  240. itr++;
  241. }
  242. }
  243. }
  244. return 1;
  245. }
  246. void jsapi_init_scene_serializable(JSVM* vm)
  247. {
  248. duk_context* ctx = vm->GetJSContext();
  249. // cached attr
  250. duk_push_object(ctx);
  251. duk_put_global_string(ctx, "__atomic_scene_serializable_attributes");
  252. js_class_get_prototype(ctx, "Atomic", "Serializable");
  253. duk_push_c_function(ctx, Serializable_GetAttributes, 0);
  254. duk_put_prop_string(ctx, -2, "getAttributes");
  255. duk_push_c_function(ctx, Serializable_GetAttribute, 1);
  256. duk_put_prop_string(ctx, -2, "getAttribute");
  257. duk_push_c_function(ctx, Serializable_SetAttribute, 2);
  258. duk_put_prop_string(ctx, -2, "setAttribute");
  259. duk_pop(ctx);
  260. }
  261. }