Godot.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. #ifndef GODOT_HPP
  2. #define GODOT_HPP
  3. #include <cstdlib>
  4. #include <godot.h>
  5. #include <CoreTypes.hpp>
  6. #include <Variant.hpp>
  7. #include <Object.hpp>
  8. #include <GodotGlobal.hpp>
  9. namespace godot {
  10. template<class T>
  11. class GodotScript {
  12. public:
  13. T *owner;
  14. // GodotScript() {}
  15. void _init() {}
  16. static char *___get_base_type_name()
  17. {
  18. return T::___get_class_name();
  19. }
  20. static void _register_methods() {}
  21. };
  22. #if !defined(_WIN32)
  23. #define GD_EXPORT
  24. #else
  25. #define GD_EXPORT __declspec(dllexport)
  26. #endif
  27. #define GODOT_NATIVE_INIT(arg) extern "C" void GD_EXPORT godot_native_init(arg)
  28. #define GODOT_NATIVE_TERMINATE(arg) extern "C" void GD_EXPORT godot_native_terminate(arg)
  29. #define GODOT_CLASS(Name) \
  30. public: inline static char *___get_type_name() { return (char *) #Name; } \
  31. private:
  32. #define GODOT_SUBCLASS(Name, Base) \
  33. public: inline static char *___get_type_name() { return (char *) #Name; } \
  34. inline static char *___get_base_type_name() { return (char *) #Base; } \
  35. inline Name(godot_object *o) : Base(o) {} \
  36. private:
  37. template<class T>
  38. struct _ArgCast {
  39. static T _arg_cast(Variant a)
  40. {
  41. return (T) a;
  42. }
  43. };
  44. template<class T>
  45. struct _ArgCast<T*> {
  46. static T *_arg_cast(Variant a)
  47. {
  48. return (T *) ((Object *) a);
  49. }
  50. };
  51. template<class T>
  52. T *as(Object *obj)
  53. {
  54. return (T *) godot_native_get_userdata(obj);
  55. }
  56. // instance and destroy funcs
  57. template<class T>
  58. void *_godot_class_instance_func(godot_object *p, void *method_data)
  59. {
  60. T *d = new T();
  61. *(godot_object **) &d->owner = p;
  62. d->_init();
  63. return d;
  64. }
  65. template<class T>
  66. void _godot_class_destroy_func(godot_object *p, void *method_data, void *data)
  67. {
  68. T *d = (T *) data;
  69. delete d;
  70. }
  71. template<class T>
  72. void register_class()
  73. {
  74. godot_instance_create_func create = {};
  75. create.create_func = _godot_class_instance_func<T>;
  76. godot_instance_destroy_func destroy = {};
  77. destroy.destroy_func = _godot_class_destroy_func<T>;
  78. godot_script_register_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  79. T::_register_methods();
  80. }
  81. template<class T>
  82. void register_tool_class()
  83. {
  84. godot_instance_create_func create = {};
  85. create.create_func = _godot_class_instance_func<T>;
  86. godot_instance_destroy_func destroy = {};
  87. destroy.destroy_func = _godot_class_destroy_func<T>;
  88. godot_script_register_tool_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  89. T::_register_methods();
  90. }
  91. // method registering
  92. typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
  93. template<class T, class R, class ...args>
  94. char *___get_method_class_name(R (T::*p)(args... a))
  95. {
  96. return T::___get_type_name();
  97. }
  98. // wohooo, let the fun begin.
  99. template<class T, class R>
  100. struct _WrappedMethod0 {
  101. R (T::*f)();
  102. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  103. {
  104. godot_variant v;
  105. godot_variant_new_nil(&v);
  106. T *obj = (T *) user_data;
  107. _WrappedMethod0<T, R> *method = (_WrappedMethod0<T, R>*) method_data;
  108. Variant *var = (Variant *) &v;
  109. *var = (obj->*(method->f))();
  110. return v;
  111. }
  112. };
  113. template<class T>
  114. struct _WrappedMethod0<T, void> {
  115. void (T::*f)();
  116. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  117. {
  118. godot_variant v;
  119. godot_variant_new_nil(&v);
  120. T *obj = (T *) user_data;
  121. _WrappedMethod0<T, void> *method = (_WrappedMethod0<T, void>*) method_data;
  122. (obj->*(method->f))();
  123. return v;
  124. }
  125. };
  126. template<class T, class R>
  127. void *___make_wrapper_function(R (T::*f)())
  128. {
  129. _WrappedMethod0<T, R> *p = (_WrappedMethod0<T, R> *) malloc(sizeof(_WrappedMethod0<T, R>));
  130. p->f = f;
  131. return (void *) p;
  132. }
  133. template<class T, class R>
  134. __godot_wrapper_method ___get_wrapper_function(R (T::*f)())
  135. {
  136. return (__godot_wrapper_method) &_WrappedMethod0<T, R>::__wrapped_method;
  137. }
  138. template<class T, class R, class A0>
  139. struct _WrappedMethod1 {
  140. R (T::*f)(A0);
  141. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  142. {
  143. godot_variant v;
  144. godot_variant_new_nil(&v);
  145. T *obj = (T *) user_data;
  146. _WrappedMethod1<T, R, A0> *method = (_WrappedMethod1<T, R, A0>*) method_data;
  147. Variant *var = (Variant *) &v;
  148. Variant **arg = (Variant **) args;
  149. *var = (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]));
  150. return v;
  151. }
  152. };
  153. template<class T, class A0>
  154. struct _WrappedMethod1<T, void, A0> {
  155. void (T::*f)(A0);
  156. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  157. {
  158. godot_variant v;
  159. godot_variant_new_nil(&v);
  160. T *obj = (T *) user_data;
  161. _WrappedMethod1<T, void, A0> *method = (_WrappedMethod1<T, void, A0>*) method_data;
  162. Variant **arg = (Variant **) args;
  163. (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]));
  164. return v;
  165. }
  166. };
  167. template<class T, class R, class A0>
  168. void *___make_wrapper_function(R (T::*f)(A0))
  169. {
  170. _WrappedMethod1<T, R, A0> *p = (_WrappedMethod1<T, R, A0> *) malloc(sizeof(_WrappedMethod1<T, R, A0>));
  171. p->f = f;
  172. return (void *) p;
  173. }
  174. template<class T, class R, class A0>
  175. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0))
  176. {
  177. return (__godot_wrapper_method) &_WrappedMethod1<T, R, A0>::__wrapped_method;
  178. }
  179. template<class T, class R, class A0, class A1>
  180. struct _WrappedMethod2 {
  181. R (T::*f)(A0, A1);
  182. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  183. {
  184. godot_variant v;
  185. godot_variant_new_nil(&v);
  186. T *obj = (T *) user_data;
  187. _WrappedMethod2<T, R, A0, A1> *method = (_WrappedMethod2<T, R, A0, A1>*) method_data;
  188. Variant *var = (Variant *) &v;
  189. Variant **arg = (Variant **) args;
  190. *var = (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]), _ArgCast<A1>::_arg_cast(*arg[1]));
  191. return v;
  192. }
  193. };
  194. template<class T, class A0, class A1>
  195. struct _WrappedMethod2<T, void, A0, A1> {
  196. void (T::*f)(A0, A1);
  197. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  198. {
  199. godot_variant v;
  200. godot_variant_new_nil(&v);
  201. T *obj = (T *) user_data;
  202. _WrappedMethod2<T, void, A0, A1> *method = (_WrappedMethod2<T, void, A0, A1>*) method_data;
  203. Variant **arg = (Variant **) args;
  204. (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]), _ArgCast<A1>::_arg_cast(*arg[1]));
  205. return v;
  206. }
  207. };
  208. template<class T, class R, class A0, class A1>
  209. void *___make_wrapper_function(R (T::*f)(A0, A1))
  210. {
  211. _WrappedMethod2<T, R, A0, A1> *p = (_WrappedMethod2<T, R, A0, A1> *) malloc(sizeof(_WrappedMethod2<T, R, A0, A1>));
  212. p->f = f;
  213. return (void *) p;
  214. }
  215. template<class T, class R, class A0, class A1>
  216. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1))
  217. {
  218. return (__godot_wrapper_method) &_WrappedMethod2<T, R, A0, A1>::__wrapped_method;
  219. }
  220. template<class T, class R, class A0, class A1, class A2>
  221. struct _WrappedMethod3 {
  222. R (T::*f)(A0, A1, A2);
  223. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  224. {
  225. godot_variant v;
  226. godot_variant_new_nil(&v);
  227. T *obj = (T *) user_data;
  228. _WrappedMethod3<T, R, A0, A1, A2> *method = (_WrappedMethod3<T, R, A0, A1, A2>*) method_data;
  229. Variant *var = (Variant *) &v;
  230. Variant **arg = (Variant **) args;
  231. *var = (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]), _ArgCast<A1>::_arg_cast(*arg[1]), _ArgCast<A2>::_arg_cast(*arg[2]));
  232. return v;
  233. }
  234. };
  235. template<class T, class A0, class A1, class A2>
  236. struct _WrappedMethod3<T, void, A0, A1, A2> {
  237. void (T::*f)(A0, A1, A2);
  238. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  239. {
  240. godot_variant v;
  241. godot_variant_new_nil(&v);
  242. T *obj = (T *) user_data;
  243. _WrappedMethod3<T, void, A0, A1, A2> *method = (_WrappedMethod3<T, void, A0, A1, A2>*) method_data;
  244. Variant **arg = (Variant **) args;
  245. (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]), _ArgCast<A1>::_arg_cast(*arg[1]), _ArgCast<A2>::_arg_cast(*arg[2]));
  246. return v;
  247. }
  248. };
  249. template<class T, class R, class A0, class A1, class A2>
  250. void *___make_wrapper_function(R (T::*f)(A0, A1, A2))
  251. {
  252. _WrappedMethod3<T, R, A0, A1, A2> *p = (_WrappedMethod3<T, R, A0, A1, A2> *) malloc(sizeof(_WrappedMethod3<T, R, A0, A1, A2>));
  253. p->f = f;
  254. return (void *) p;
  255. }
  256. template<class T, class R, class A0, class A1, class A2>
  257. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2))
  258. {
  259. return (__godot_wrapper_method) &_WrappedMethod3<T, R, A0, A1, A2>::__wrapped_method;
  260. }
  261. template<class T, class R, class A0, class A1, class A2, class A3>
  262. struct _WrappedMethod4 {
  263. R (T::*f)(A0, A1, A2, A3);
  264. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  265. {
  266. godot_variant v;
  267. godot_variant_new_nil(&v);
  268. T *obj = (T *) user_data;
  269. _WrappedMethod4<T, R, A0, A1, A2, A3> *method = (_WrappedMethod4<T, R, A0, A1, A2, A3>*) method_data;
  270. Variant *var = (Variant *) &v;
  271. Variant **arg = (Variant **) args;
  272. *var = (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]), _ArgCast<A1>::_arg_cast(*arg[1]), _ArgCast<A2>::_arg_cast(*arg[2]), _ArgCast<A3>::_arg_cast(*arg[3]));
  273. return v;
  274. }
  275. };
  276. template<class T, class A0, class A1, class A2, class A3>
  277. struct _WrappedMethod4<T, void, A0, A1, A2, A3> {
  278. void (T::*f)(A0, A1, A2, A3);
  279. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  280. {
  281. godot_variant v;
  282. godot_variant_new_nil(&v);
  283. T *obj = (T *) user_data;
  284. _WrappedMethod4<T, void, A0, A1, A2, A3> *method = (_WrappedMethod4<T, void, A0, A1, A2, A3>*) method_data;
  285. Variant **arg = (Variant **) args;
  286. (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]), _ArgCast<A1>::_arg_cast(*arg[1]), _ArgCast<A2>::_arg_cast(*arg[2]), _ArgCast<A3>::_arg_cast(*arg[3]));
  287. return v;
  288. }
  289. };
  290. template<class T, class R, class A0, class A1, class A2, class A3>
  291. void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3))
  292. {
  293. _WrappedMethod4<T, R, A0, A1, A2, A3> *p = (_WrappedMethod4<T, R, A0, A1, A2, A3> *) malloc(sizeof(_WrappedMethod4<T, R, A0, A1, A2, A3>));
  294. p->f = f;
  295. return (void *) p;
  296. }
  297. template<class T, class R, class A0, class A1, class A2, class A3>
  298. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3))
  299. {
  300. return (__godot_wrapper_method) &_WrappedMethod4<T, R, A0, A1, A2, A3>::__wrapped_method;
  301. }
  302. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  303. struct _WrappedMethod5 {
  304. R (T::*f)(A0, A1, A2, A3, A4);
  305. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  306. {
  307. godot_variant v;
  308. godot_variant_new_nil(&v);
  309. T *obj = (T *) user_data;
  310. _WrappedMethod5<T, R, A0, A1, A2, A3, A4> *method = (_WrappedMethod5<T, R, A0, A1, A2, A3, A4>*) method_data;
  311. Variant *var = (Variant *) &v;
  312. Variant **arg = (Variant **) args;
  313. *var = (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]), _ArgCast<A1>::_arg_cast(*arg[1]), _ArgCast<A2>::_arg_cast(*arg[2]), _ArgCast<A3>::_arg_cast(*arg[3]), _ArgCast<A4>::_arg_cast(*arg[4]));
  314. return v;
  315. }
  316. };
  317. template<class T, class A0, class A1, class A2, class A3, class A4>
  318. struct _WrappedMethod5<T, void, A0, A1, A2, A3, A4> {
  319. void (T::*f)(A0, A1, A2, A3, A4);
  320. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  321. {
  322. godot_variant v;
  323. godot_variant_new_nil(&v);
  324. T *obj = (T *) user_data;
  325. _WrappedMethod5<T, void, A0, A1, A2, A3, A4> *method = (_WrappedMethod5<T, void, A0, A1, A2, A3, A4>*) method_data;
  326. Variant **arg = (Variant **) args;
  327. (obj->*(method->f))(_ArgCast<A0>::_arg_cast(*arg[0]), _ArgCast<A1>::_arg_cast(*arg[1]), _ArgCast<A2>::_arg_cast(*arg[2]), _ArgCast<A3>::_arg_cast(*arg[3]), _ArgCast<A4>::_arg_cast(*arg[4]));
  328. return v;
  329. }
  330. };
  331. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  332. void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4))
  333. {
  334. _WrappedMethod5<T, R, A0, A1, A2, A3, A4> *p = (_WrappedMethod5<T, R, A0, A1, A2, A3, A4> *) malloc(sizeof(_WrappedMethod5<T, R, A0, A1, A2, A3, A4>));
  335. p->f = f;
  336. return (void *) p;
  337. }
  338. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  339. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4))
  340. {
  341. return (__godot_wrapper_method) &_WrappedMethod5<T, R, A0, A1, A2, A3, A4>::__wrapped_method;
  342. }
  343. template<class M>
  344. void register_method(char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED)
  345. {
  346. godot_instance_method method = {};
  347. method.method_data = ___make_wrapper_function(method_ptr);
  348. method.free_func = free;
  349. method.method = (__godot_wrapper_method) ___get_wrapper_function(method_ptr);
  350. godot_method_attributes attr = {};
  351. attr.rpc_type = rpc_type;
  352. godot_script_register_method(___get_method_class_name(method_ptr), name, attr, method);
  353. }
  354. template<class T, class P>
  355. struct _PropertySetFunc {
  356. void (T::*f)(P);
  357. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value)
  358. {
  359. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *) method_data;
  360. T *obj = (T *) user_data;
  361. Variant *v = (Variant *) &value;
  362. (obj->*(set_func->f))(_ArgCast<P>::_arg_cast(*v));
  363. }
  364. };
  365. template<class T, class P>
  366. struct _PropertyGetFunc {
  367. P (T::*f)();
  368. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data)
  369. {
  370. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *) method_data;
  371. T *obj = (T *) user_data;
  372. godot_variant var;
  373. godot_variant_new_nil(&var);
  374. Variant *v = (Variant *) &var;
  375. *v = (obj->*(get_func->f))();
  376. return var;
  377. }
  378. };
  379. template<class T, class P>
  380. struct _PropertyDefaultSetFunc {
  381. P (T::*f);
  382. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value)
  383. {
  384. _PropertyDefaultSetFunc<T, P> *set_func = (_PropertyDefaultSetFunc<T, P> *) method_data;
  385. T *obj = (T *) user_data;
  386. Variant *v = (Variant *) &value;
  387. (obj->*(set_func->f)) = _ArgCast<P>::_arg_cast(*v);
  388. }
  389. };
  390. template<class T, class P>
  391. struct _PropertyDefaultGetFunc {
  392. P (T::*f);
  393. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data)
  394. {
  395. _PropertyDefaultGetFunc<T, P> *get_func = (_PropertyDefaultGetFunc<T, P> *) method_data;
  396. T *obj = (T *) user_data;
  397. godot_variant var;
  398. godot_variant_new_nil(&var);
  399. Variant *v = (Variant *) &var;
  400. *v = (obj->*(get_func->f));
  401. return var;
  402. }
  403. };
  404. template<class T, class P>
  405. void register_property(char *name, P (T::*var), P default_value, godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED, godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT, godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "")
  406. {
  407. Variant def_val = default_value;
  408. usage = (godot_property_usage_flags) ((int) usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE);
  409. if (def_val.get_type() == Variant::OBJECT) {
  410. Object *o = def_val;
  411. if (o && o->is_class("Resource")) {
  412. hint = (godot_property_hint) ((int) hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE);
  413. hint_string = o->get_class();
  414. }
  415. }
  416. godot_string *_hint_string = (godot_string*) &hint_string;
  417. godot_property_attributes attr = {};
  418. attr.type = def_val.get_type();
  419. attr.default_value = *(godot_variant *) &def_val;
  420. attr.hint = hint;
  421. attr.rset_type = rpc_mode;
  422. attr.usage = usage;
  423. attr.hint_string = *_hint_string;
  424. _PropertyDefaultSetFunc<T, P> *wrapped_set = (_PropertyDefaultSetFunc<T, P> *) malloc(sizeof(_PropertyDefaultSetFunc<T, P>));
  425. wrapped_set->f = var;
  426. _PropertyDefaultGetFunc<T, P> *wrapped_get = (_PropertyDefaultGetFunc<T, P> *) malloc(sizeof(_PropertyDefaultGetFunc<T, P>));
  427. wrapped_get->f = var;
  428. godot_property_set_func set_func = {};
  429. set_func.method_data = (void *) wrapped_set;
  430. set_func.free_func = free;
  431. set_func.set_func = &_PropertyDefaultSetFunc<T, P>::_wrapped_setter;
  432. godot_property_get_func get_func = {};
  433. get_func.method_data = (void *) wrapped_get;
  434. get_func.free_func = free;
  435. get_func.get_func = &_PropertyDefaultGetFunc<T, P>::_wrapped_getter;
  436. godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func);
  437. }
  438. template<class T, class P>
  439. void register_property(char *name, void (T::*setter)(P), P (T::*getter)(), P default_value, godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED, godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT, godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "")
  440. {
  441. Variant def_val = default_value;
  442. godot_property_attributes attr = {};
  443. attr.type = def_val.get_type();
  444. attr.default_value = *(godot_variant *) &def_val;
  445. attr.hint = hint;
  446. attr.rset_type = rpc_mode;
  447. attr.usage = usage;
  448. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *) malloc(sizeof(_PropertySetFunc<T, P>));
  449. wrapped_set->f = setter;
  450. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *) malloc(sizeof(_PropertyGetFunc<T, P>));
  451. wrapped_get->f = getter;
  452. godot_property_set_func set_func = {};
  453. set_func.method_data = (void *) wrapped_set;
  454. set_func.free_func = free;
  455. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  456. godot_property_get_func get_func = {};
  457. get_func.method_data = (void *) wrapped_get;
  458. get_func.free_func = free;
  459. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  460. godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func);
  461. }
  462. template<class T>
  463. void register_signal(String name, Dictionary args = Dictionary())
  464. {
  465. godot_signal signal = {};
  466. signal.name = *(godot_string *)&name;
  467. signal.num_args = args.size();
  468. signal.num_default_args = 0;
  469. signal.args = (godot_signal_argument*) godot_alloc(sizeof(godot_signal_argument) * signal.num_args);
  470. memset((void *) signal.args, 0, sizeof(godot_signal_argument) * signal.num_args);
  471. for (int i = 0; i < signal.num_args; i++) {
  472. // Array entry = args[i];
  473. // String name = entry[0];
  474. String name = args.keys()[i];
  475. godot_string *_key = (godot_string *)&name;
  476. godot_string_new(&signal.args[i].name);
  477. godot_string_copy_string(&signal.args[i].name, _key);
  478. // if (entry.size() > 1) {
  479. // signal.args[i].type = entry[1];
  480. // }
  481. signal.args[i].type = args.values()[i];
  482. }
  483. godot_script_register_signal(T::___get_type_name(), &signal);
  484. for (int i = 0; i < signal.num_args; i++) {
  485. godot_string_destroy(&signal.args[i].name);
  486. }
  487. }
  488. }
  489. #endif // GODOT_H