Godot.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. #ifndef GODOT_H
  2. #define GODOT_H
  3. #include <cstdlib>
  4. #include <godot.h>
  5. #include <godot_cpp/core/CoreTypes.hpp>
  6. #include <godot_cpp/core/Variant.hpp>
  7. namespace godot {
  8. #define GODOT_CLASS(Name, Base) \
  9. public: inline static char *___get_type_name() { return (char *) #Name; } \
  10. inline static char *___get_base_type_name() { return (char *) #Base; } \
  11. inline Name(godot_object *o) { __core_object = o; } \
  12. inline Name(const Variant& obj) { __core_object = ((Object) obj).__core_object; } \
  13. private:
  14. // instance and destroy funcs
  15. template<class T>
  16. void *_godot_class_instance_func(godot_object *p, void *method_data)
  17. {
  18. T *d = new T(p);
  19. d->_init();
  20. return d;
  21. }
  22. template<class T>
  23. void _godot_class_destroy_func(godot_object *p, void *method_data, void *data)
  24. {
  25. T *d = (T *) data;
  26. delete d;
  27. }
  28. template<class T>
  29. void register_class()
  30. {
  31. godot_instance_create_func create = {};
  32. create.create_func = _godot_class_instance_func<T>;
  33. godot_instance_destroy_func destroy = {};
  34. destroy.destroy_func = _godot_class_destroy_func<T>;
  35. godot_script_register_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  36. T::_register_methods();
  37. }
  38. template<class T>
  39. void register_tool_class()
  40. {
  41. godot_instance_create_func create = {};
  42. create.create_func = _godot_class_instance_func<T>;
  43. godot_instance_destroy_func destroy = {};
  44. destroy.destroy_func = _godot_class_destroy_func<T>;
  45. godot_script_register_tool_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  46. T::_register_methods();
  47. }
  48. // method registering
  49. typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
  50. template<class T, class R, class ...args>
  51. char *___get_method_class_name(R (T::*p)(args... a))
  52. {
  53. return T::___get_type_name();
  54. }
  55. // wohooo, let the fun begin.
  56. template<class T, class R>
  57. struct _WrappedMethod0 {
  58. R (T::*f)();
  59. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  60. {
  61. godot_variant v;
  62. godot_variant_new_nil(&v);
  63. T *obj = (T *) user_data;
  64. _WrappedMethod0<T, R> *method = (_WrappedMethod0<T, R>*) method_data;
  65. Variant *var = (Variant *) &v;
  66. *var = (obj->*(method->f))();
  67. return v;
  68. }
  69. };
  70. template<class T>
  71. struct _WrappedMethod0<T, void> {
  72. void (T::*f)();
  73. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  74. {
  75. godot_variant v;
  76. godot_variant_new_nil(&v);
  77. T *obj = (T *) user_data;
  78. _WrappedMethod0<T, void> *method = (_WrappedMethod0<T, void>*) method_data;
  79. (obj->*(method->f))();
  80. return v;
  81. }
  82. };
  83. template<class T, class R>
  84. void *___make_wrapper_function(R (T::*f)())
  85. {
  86. _WrappedMethod0<T, R> *p = (_WrappedMethod0<T, R> *) malloc(sizeof(_WrappedMethod0<T, R>));
  87. p->f = f;
  88. return (void *) p;
  89. }
  90. template<class T, class R>
  91. __godot_wrapper_method ___get_wrapper_function(R (T::*f)())
  92. {
  93. return (__godot_wrapper_method) &_WrappedMethod0<T, R>::__wrapped_method;
  94. }
  95. template<class T, class R, class A0>
  96. struct _WrappedMethod1 {
  97. R (T::*f)(A0);
  98. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  99. {
  100. godot_variant v;
  101. godot_variant_new_nil(&v);
  102. T *obj = (T *) user_data;
  103. _WrappedMethod1<T, R, A0> *method = (_WrappedMethod1<T, R, A0>*) method_data;
  104. Variant *var = (Variant *) &v;
  105. Variant **arg = (Variant **) args;
  106. *var = (obj->*(method->f))(*arg[0]);
  107. return v;
  108. }
  109. };
  110. template<class T, class A0>
  111. struct _WrappedMethod1<T, void, A0> {
  112. void (T::*f)(A0);
  113. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  114. {
  115. godot_variant v;
  116. godot_variant_new_nil(&v);
  117. T *obj = (T *) user_data;
  118. _WrappedMethod1<T, void, A0> *method = (_WrappedMethod1<T, void, A0>*) method_data;
  119. Variant **arg = (Variant **) args;
  120. (obj->*(method->f))(*arg[0]);
  121. return v;
  122. }
  123. };
  124. template<class T, class R, class A0>
  125. void *___make_wrapper_function(R (T::*f)(A0))
  126. {
  127. _WrappedMethod1<T, R, A0> *p = (_WrappedMethod1<T, R, A0> *) malloc(sizeof(_WrappedMethod1<T, R, A0>));
  128. p->f = f;
  129. return (void *) p;
  130. }
  131. template<class T, class R, class A0>
  132. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0))
  133. {
  134. return (__godot_wrapper_method) &_WrappedMethod1<T, R, A0>::__wrapped_method;
  135. }
  136. template<class T, class R, class A0, class A1>
  137. struct _WrappedMethod2 {
  138. R (T::*f)(A0, A1);
  139. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  140. {
  141. godot_variant v;
  142. godot_variant_new_nil(&v);
  143. T *obj = (T *) user_data;
  144. _WrappedMethod2<T, R, A0, A1> *method = (_WrappedMethod2<T, R, A0, A1>*) method_data;
  145. Variant *var = (Variant *) &v;
  146. Variant **arg = (Variant **) args;
  147. *var = (obj->*(method->f))(*arg[0], *arg[1]);
  148. return v;
  149. }
  150. };
  151. template<class T, class A0, class A1>
  152. struct _WrappedMethod2<T, void, A0, A1> {
  153. void (T::*f)(A0, A1);
  154. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  155. {
  156. godot_variant v;
  157. godot_variant_new_nil(&v);
  158. T *obj = (T *) user_data;
  159. _WrappedMethod2<T, void, A0, A1> *method = (_WrappedMethod2<T, void, A0, A1>*) method_data;
  160. Variant **arg = (Variant **) args;
  161. (obj->*(method->f))(*arg[0], *arg[1]);
  162. return v;
  163. }
  164. };
  165. template<class T, class R, class A0, class A1>
  166. void *___make_wrapper_function(R (T::*f)(A0, A1))
  167. {
  168. _WrappedMethod2<T, R, A0, A1> *p = (_WrappedMethod2<T, R, A0, A1> *) malloc(sizeof(_WrappedMethod2<T, R, A0, A1>));
  169. p->f = f;
  170. return (void *) p;
  171. }
  172. template<class T, class R, class A0, class A1>
  173. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1))
  174. {
  175. return (__godot_wrapper_method) &_WrappedMethod2<T, R, A0, A1>::__wrapped_method;
  176. }
  177. template<class T, class R, class A0, class A1, class A2>
  178. struct _WrappedMethod3 {
  179. R (T::*f)(A0, A1, A2);
  180. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  181. {
  182. godot_variant v;
  183. godot_variant_new_nil(&v);
  184. T *obj = (T *) user_data;
  185. _WrappedMethod3<T, R, A0, A1, A2> *method = (_WrappedMethod3<T, R, A0, A1, A2>*) method_data;
  186. Variant *var = (Variant *) &v;
  187. Variant **arg = (Variant **) args;
  188. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2]);
  189. return v;
  190. }
  191. };
  192. template<class T, class A0, class A1, class A2>
  193. struct _WrappedMethod3<T, void, A0, A1, A2> {
  194. void (T::*f)(A0, A1, A2);
  195. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  196. {
  197. godot_variant v;
  198. godot_variant_new_nil(&v);
  199. T *obj = (T *) user_data;
  200. _WrappedMethod3<T, void, A0, A1, A2> *method = (_WrappedMethod3<T, void, A0, A1, A2>*) method_data;
  201. Variant **arg = (Variant **) args;
  202. (obj->*(method->f))(*arg[0], *arg[1], *arg[2]);
  203. return v;
  204. }
  205. };
  206. template<class T, class R, class A0, class A1, class A2>
  207. void *___make_wrapper_function(R (T::*f)(A0, A1, A2))
  208. {
  209. _WrappedMethod3<T, R, A0, A1, A2> *p = (_WrappedMethod3<T, R, A0, A1, A2> *) malloc(sizeof(_WrappedMethod3<T, R, A0, A1, A2>));
  210. p->f = f;
  211. return (void *) p;
  212. }
  213. template<class T, class R, class A0, class A1, class A2>
  214. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2))
  215. {
  216. return (__godot_wrapper_method) &_WrappedMethod3<T, R, A0, A1, A2>::__wrapped_method;
  217. }
  218. template<class T, class R, class A0, class A1, class A2, class A3>
  219. struct _WrappedMethod4 {
  220. R (T::*f)(A0, A1, A2, A3);
  221. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  222. {
  223. godot_variant v;
  224. godot_variant_new_nil(&v);
  225. T *obj = (T *) user_data;
  226. _WrappedMethod4<T, R, A0, A1, A2, A3> *method = (_WrappedMethod4<T, R, A0, A1, A2, A3>*) method_data;
  227. Variant *var = (Variant *) &v;
  228. Variant **arg = (Variant **) args;
  229. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3]);
  230. return v;
  231. }
  232. };
  233. template<class T, class A0, class A1, class A2, class A3>
  234. struct _WrappedMethod4<T, void, A0, A1, A2, A3> {
  235. void (T::*f)(A0, A1, A2, A3);
  236. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  237. {
  238. godot_variant v;
  239. godot_variant_new_nil(&v);
  240. T *obj = (T *) user_data;
  241. _WrappedMethod4<T, void, A0, A1, A2, A3> *method = (_WrappedMethod4<T, void, A0, A1, A2, A3>*) method_data;
  242. Variant **arg = (Variant **) args;
  243. (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3]);
  244. return v;
  245. }
  246. };
  247. template<class T, class R, class A0, class A1, class A2, class A3>
  248. void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3))
  249. {
  250. _WrappedMethod4<T, R, A0, A1, A2, A3> *p = (_WrappedMethod4<T, R, A0, A1, A2, A3> *) malloc(sizeof(_WrappedMethod4<T, R, A0, A1, A2, A3>));
  251. p->f = f;
  252. return (void *) p;
  253. }
  254. template<class T, class R, class A0, class A1, class A2, class A3>
  255. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3))
  256. {
  257. return (__godot_wrapper_method) &_WrappedMethod4<T, R, A0, A1, A2, A3>::__wrapped_method;
  258. }
  259. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  260. struct _WrappedMethod5 {
  261. R (T::*f)(A0, A1, A2, A3, A4);
  262. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  263. {
  264. godot_variant v;
  265. godot_variant_new_nil(&v);
  266. T *obj = (T *) user_data;
  267. _WrappedMethod5<T, R, A0, A1, A2, A3, A4> *method = (_WrappedMethod5<T, R, A0, A1, A2, A3, A4>*) method_data;
  268. Variant *var = (Variant *) &v;
  269. Variant **arg = (Variant **) args;
  270. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]);
  271. return v;
  272. }
  273. };
  274. template<class T, class A0, class A1, class A2, class A3, class A4>
  275. struct _WrappedMethod5<T, void, A0, A1, A2, A3, A4> {
  276. void (T::*f)(A0, A1, A2, A3, A4);
  277. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  278. {
  279. godot_variant v;
  280. godot_variant_new_nil(&v);
  281. T *obj = (T *) user_data;
  282. _WrappedMethod5<T, void, A0, A1, A2, A3, A4> *method = (_WrappedMethod5<T, void, A0, A1, A2, A3, A4>*) method_data;
  283. Variant **arg = (Variant **) args;
  284. (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]);
  285. return v;
  286. }
  287. };
  288. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  289. void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4))
  290. {
  291. _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>));
  292. p->f = f;
  293. return (void *) p;
  294. }
  295. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  296. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4))
  297. {
  298. return (__godot_wrapper_method) &_WrappedMethod5<T, R, A0, A1, A2, A3, A4>::__wrapped_method;
  299. }
  300. template<class M>
  301. void register_method(char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED)
  302. {
  303. godot_instance_method method = {};
  304. method.method_data = ___make_wrapper_function(method_ptr);
  305. method.free_func = free;
  306. method.method = (__godot_wrapper_method) ___get_wrapper_function(method_ptr);
  307. godot_method_attributes attr = {};
  308. attr.rpc_type = rpc_type;
  309. godot_script_register_method(___get_method_class_name(method_ptr), name, attr, method);
  310. }
  311. template<class T, class P>
  312. struct _PropertySetFunc {
  313. void (T::*f)(P);
  314. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value)
  315. {
  316. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *) method_data;
  317. T *obj = (T *) user_data;
  318. Variant *v = (Variant *) &value;
  319. (obj->*(set_func->f))(*v);
  320. }
  321. };
  322. template<class T, class P>
  323. struct _PropertyGetFunc {
  324. P (T::*f)();
  325. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data)
  326. {
  327. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *) method_data;
  328. T *obj = (T *) user_data;
  329. godot_variant var;
  330. godot_variant_new_nil(&var);
  331. Variant *v = (Variant *) &var;
  332. *v = (obj->*(get_func->f))();
  333. return var;
  334. }
  335. };
  336. template<class T, class P>
  337. 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)
  338. {
  339. Variant def_val = default_value;
  340. godot_property_attributes attr = {};
  341. attr.type = def_val.get_type();
  342. attr.default_value = *(godot_variant *) &def_val;
  343. attr.hint = hint;
  344. attr.rset_type = rpc_mode;
  345. attr.usage = usage;
  346. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *) malloc(sizeof(_PropertySetFunc<T, P>));
  347. wrapped_set->f = setter;
  348. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *) malloc(sizeof(_PropertyGetFunc<T, P>));
  349. wrapped_get->f = getter;
  350. godot_property_set_func set_func = {};
  351. set_func.method_data = (void *) wrapped_set;
  352. set_func.free_func = free;
  353. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  354. godot_property_get_func get_func = {};
  355. get_func.method_data = (void *) wrapped_get;
  356. get_func.free_func = free;
  357. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  358. godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func);
  359. }
  360. }
  361. #endif // GODOT_H