2
0

example.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /* godot-cpp integration testing project.
  2. *
  3. * This is free and unencumbered software released into the public domain.
  4. */
  5. #include "example.h"
  6. #include <godot_cpp/core/class_db.hpp>
  7. #include <godot_cpp/classes/global_constants.hpp>
  8. #include <godot_cpp/classes/label.hpp>
  9. #include <godot_cpp/classes/multiplayer_api.hpp>
  10. #include <godot_cpp/classes/multiplayer_peer.hpp>
  11. #include <godot_cpp/classes/os.hpp>
  12. #include <godot_cpp/variant/utility_functions.hpp>
  13. using namespace godot;
  14. class MyCallableCustom : public CallableCustom {
  15. public:
  16. virtual uint32_t hash() const {
  17. return 27;
  18. }
  19. virtual String get_as_text() const {
  20. return "<MyCallableCustom>";
  21. }
  22. static bool compare_equal_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  23. return p_a == p_b;
  24. }
  25. virtual CompareEqualFunc get_compare_equal_func() const {
  26. return &MyCallableCustom::compare_equal_func;
  27. }
  28. static bool compare_less_func(const CallableCustom *p_a, const CallableCustom *p_b) {
  29. return (void *)p_a < (void *)p_b;
  30. }
  31. virtual CompareLessFunc get_compare_less_func() const {
  32. return &MyCallableCustom::compare_less_func;
  33. }
  34. bool is_valid() const {
  35. return true;
  36. }
  37. virtual ObjectID get_object() const {
  38. return ObjectID();
  39. }
  40. virtual int get_argument_count(bool &r_is_valid) const {
  41. r_is_valid = true;
  42. return 2;
  43. }
  44. virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, GDExtensionCallError &r_call_error) const {
  45. r_return_value = "Hi";
  46. r_call_error.error = GDEXTENSION_CALL_OK;
  47. }
  48. };
  49. void ExampleRef::set_id(int p_id) {
  50. id = p_id;
  51. }
  52. int ExampleRef::get_id() const {
  53. return id;
  54. }
  55. void ExampleRef::_notification(int p_what) {
  56. if (p_what == NOTIFICATION_POSTINITIALIZE) {
  57. post_initialized = true;
  58. }
  59. }
  60. void ExampleRef::_bind_methods() {
  61. ClassDB::bind_method(D_METHOD("set_id", "id"), &ExampleRef::set_id);
  62. ClassDB::bind_method(D_METHOD("get_id"), &ExampleRef::get_id);
  63. ClassDB::bind_method(D_METHOD("was_post_initialized"), &ExampleRef::was_post_initialized);
  64. ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
  65. }
  66. ExampleRef::ExampleRef() {
  67. id = 0;
  68. }
  69. ExampleRef::~ExampleRef() {
  70. }
  71. int Example::test_static(int p_a, int p_b) {
  72. return p_a + p_b;
  73. }
  74. void Example::test_static2() {
  75. //UtilityFunctions::print(" void static");
  76. }
  77. int Example::def_args(int p_a, int p_b) {
  78. return p_a + p_b;
  79. }
  80. void Example::_notification(int p_what) {
  81. if (p_what == NOTIFICATION_READY) {
  82. Dictionary opts;
  83. opts["rpc_mode"] = MultiplayerAPI::RPC_MODE_AUTHORITY;
  84. opts["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
  85. opts["call_local"] = true;
  86. opts["channel"] = 0;
  87. rpc_config("test_rpc", opts);
  88. }
  89. //UtilityFunctions::print("Notification: ", String::num(p_what));
  90. }
  91. bool Example::_set(const StringName &p_name, const Variant &p_value) {
  92. String name = p_name;
  93. if (name.begins_with("dproperty")) {
  94. int64_t index = name.get_slicec('_', 1).to_int();
  95. dprop[index] = p_value;
  96. return true;
  97. }
  98. if (name == "property_from_list") {
  99. property_from_list = p_value;
  100. return true;
  101. }
  102. return false;
  103. }
  104. bool Example::_get(const StringName &p_name, Variant &r_ret) const {
  105. String name = p_name;
  106. if (name.begins_with("dproperty")) {
  107. int64_t index = name.get_slicec('_', 1).to_int();
  108. r_ret = dprop[index];
  109. return true;
  110. }
  111. if (name == "property_from_list") {
  112. r_ret = property_from_list;
  113. return true;
  114. }
  115. return false;
  116. }
  117. String Example::_to_string() const {
  118. return "[ GDExtension::Example <--> Instance ID:" + uitos(get_instance_id()) + " ]";
  119. }
  120. void Example::_get_property_list(List<PropertyInfo> *p_list) const {
  121. p_list->push_back(PropertyInfo(Variant::VECTOR3, "property_from_list"));
  122. for (int i = 0; i < 3; i++) {
  123. p_list->push_back(PropertyInfo(Variant::VECTOR2, "dproperty_" + itos(i)));
  124. }
  125. }
  126. bool Example::_property_can_revert(const StringName &p_name) const {
  127. if (p_name == StringName("property_from_list") && property_from_list != Vector3(42, 42, 42)) {
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. };
  133. bool Example::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  134. if (p_name == StringName("property_from_list")) {
  135. r_property = Vector3(42, 42, 42);
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. };
  141. void Example::_validate_property(PropertyInfo &p_property) const {
  142. String name = p_property.name;
  143. // Test hiding the "mouse_filter" property from the editor.
  144. if (name == "mouse_filter") {
  145. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  146. }
  147. }
  148. void Example::_bind_methods() {
  149. // Methods.
  150. ClassDB::bind_method(D_METHOD("simple_func"), &Example::simple_func);
  151. ClassDB::bind_method(D_METHOD("simple_const_func"), &Example::simple_const_func);
  152. ClassDB::bind_method(D_METHOD("custom_ref_func", "ref"), &Example::custom_ref_func);
  153. ClassDB::bind_method(D_METHOD("custom_const_ref_func", "ref"), &Example::custom_const_ref_func);
  154. ClassDB::bind_method(D_METHOD("image_ref_func", "image"), &Example::image_ref_func);
  155. ClassDB::bind_method(D_METHOD("image_const_ref_func", "image"), &Example::image_const_ref_func);
  156. ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
  157. ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
  158. ClassDB::bind_method(D_METHOD("return_empty_ref"), &Example::return_empty_ref);
  159. ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
  160. ClassDB::bind_method(D_METHOD("extended_ref_checks", "ref"), &Example::extended_ref_checks);
  161. ClassDB::bind_method(D_METHOD("is_object_binding_set_by_parent_constructor"), &Example::is_object_binding_set_by_parent_constructor);
  162. ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
  163. ClassDB::bind_method(D_METHOD("test_tarray_arg", "array"), &Example::test_tarray_arg);
  164. ClassDB::bind_method(D_METHOD("test_tarray"), &Example::test_tarray);
  165. ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
  166. ClassDB::bind_method(D_METHOD("test_node_argument"), &Example::test_node_argument);
  167. ClassDB::bind_method(D_METHOD("test_string_ops"), &Example::test_string_ops);
  168. ClassDB::bind_method(D_METHOD("test_str_utility"), &Example::test_str_utility);
  169. ClassDB::bind_method(D_METHOD("test_string_is_forty_two"), &Example::test_string_is_forty_two);
  170. ClassDB::bind_method(D_METHOD("test_string_resize"), &Example::test_string_resize);
  171. ClassDB::bind_method(D_METHOD("test_typed_array_of_packed"), &Example::test_typed_array_of_packed);
  172. ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
  173. ClassDB::bind_method(D_METHOD("test_vector_init_list"), &Example::test_vector_init_list);
  174. ClassDB::bind_method(D_METHOD("test_object_cast_to_node", "object"), &Example::test_object_cast_to_node);
  175. ClassDB::bind_method(D_METHOD("test_object_cast_to_control", "object"), &Example::test_object_cast_to_control);
  176. ClassDB::bind_method(D_METHOD("test_object_cast_to_example", "object"), &Example::test_object_cast_to_example);
  177. ClassDB::bind_method(D_METHOD("test_variant_vector2i_conversion", "variant"), &Example::test_variant_vector2i_conversion);
  178. ClassDB::bind_method(D_METHOD("test_variant_int_conversion", "variant"), &Example::test_variant_int_conversion);
  179. ClassDB::bind_method(D_METHOD("test_variant_float_conversion", "variant"), &Example::test_variant_float_conversion);
  180. ClassDB::bind_method(D_METHOD("test_object_is_valid", "variant"), &Example::test_object_is_valid);
  181. ClassDB::bind_method(D_METHOD("test_add_child", "node"), &Example::test_add_child);
  182. ClassDB::bind_method(D_METHOD("test_set_tileset", "tilemap", "tileset"), &Example::test_set_tileset);
  183. ClassDB::bind_method(D_METHOD("test_variant_call", "variant"), &Example::test_variant_call);
  184. ClassDB::bind_method(D_METHOD("test_callable_mp"), &Example::test_callable_mp);
  185. ClassDB::bind_method(D_METHOD("test_callable_mp_ret"), &Example::test_callable_mp_ret);
  186. ClassDB::bind_method(D_METHOD("test_callable_mp_retc"), &Example::test_callable_mp_retc);
  187. ClassDB::bind_method(D_METHOD("test_callable_mp_static"), &Example::test_callable_mp_static);
  188. ClassDB::bind_method(D_METHOD("test_callable_mp_static_ret"), &Example::test_callable_mp_static_ret);
  189. ClassDB::bind_method(D_METHOD("test_custom_callable"), &Example::test_custom_callable);
  190. ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
  191. ClassDB::bind_method(D_METHOD("test_variant_iterator", "input"), &Example::test_variant_iterator);
  192. ClassDB::bind_method(D_METHOD("test_rpc", "value"), &Example::test_rpc);
  193. ClassDB::bind_method(D_METHOD("test_send_rpc", "value"), &Example::test_send_rpc);
  194. ClassDB::bind_method(D_METHOD("return_last_rpc_arg"), &Example::return_last_rpc_arg);
  195. ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
  196. ClassDB::bind_method(D_METHOD("callable_bind"), &Example::callable_bind);
  197. ClassDB::bind_method(D_METHOD("test_post_initialize"), &Example::test_post_initialize);
  198. GDVIRTUAL_BIND(_do_something_virtual, "name", "value");
  199. ClassDB::bind_method(D_METHOD("test_virtual_implemented_in_script"), &Example::test_virtual_implemented_in_script);
  200. GDVIRTUAL_BIND(_do_something_virtual_with_control, "control");
  201. ClassDB::bind_method(D_METHOD("test_use_engine_singleton"), &Example::test_use_engine_singleton);
  202. ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
  203. ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
  204. ClassDB::bind_static_method("Example", D_METHOD("test_library_path"), &Example::test_library_path);
  205. {
  206. MethodInfo mi;
  207. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  208. mi.name = "varargs_func";
  209. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func", &Example::varargs_func, mi);
  210. }
  211. {
  212. MethodInfo mi;
  213. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  214. mi.name = "varargs_func_nv";
  215. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_nv", &Example::varargs_func_nv, mi);
  216. }
  217. {
  218. MethodInfo mi;
  219. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  220. mi.name = "varargs_func_void";
  221. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_void", &Example::varargs_func_void, mi);
  222. }
  223. // Properties.
  224. ADD_GROUP("Test group", "group_");
  225. ADD_SUBGROUP("Test subgroup", "group_subgroup_");
  226. ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
  227. ClassDB::bind_method(D_METHOD("get_v4"), &Example::get_v4);
  228. ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
  229. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
  230. // Signals.
  231. ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
  232. ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
  233. // Constants.
  234. BIND_ENUM_CONSTANT(FIRST);
  235. BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
  236. BIND_BITFIELD_FLAG(FLAG_ONE);
  237. BIND_BITFIELD_FLAG(FLAG_TWO);
  238. BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
  239. BIND_ENUM_CONSTANT(OUTSIDE_OF_CLASS);
  240. }
  241. bool Example::has_object_instance_binding() const {
  242. return internal::gdextension_interface_object_get_instance_binding(_owner, internal::token, nullptr);
  243. }
  244. Example::Example() :
  245. object_instance_binding_set_by_parent_constructor(has_object_instance_binding()) {
  246. // Test conversion, to ensure users can use all parent class functions at this time.
  247. // It would crash if instance binding still not be initialized.
  248. Variant v = Variant(this);
  249. Object *o = (Object *)v;
  250. //UtilityFunctions::print("Constructor.");
  251. }
  252. Example::~Example() {
  253. //UtilityFunctions::print("Destructor.");
  254. }
  255. // Methods.
  256. void Example::simple_func() {
  257. emit_custom_signal("simple_func", 3);
  258. }
  259. void Example::simple_const_func() const {
  260. ((Example *)this)->emit_custom_signal("simple_const_func", 4);
  261. }
  262. int Example::custom_ref_func(Ref<ExampleRef> p_ref) {
  263. return p_ref.is_valid() ? p_ref->get_id() : -1;
  264. }
  265. int Example::custom_const_ref_func(const Ref<ExampleRef> &p_ref) {
  266. return p_ref.is_valid() ? p_ref->get_id() : -1;
  267. }
  268. String Example::image_ref_func(Ref<Image> p_image) {
  269. return p_image.is_valid() ? String("valid") : String("invalid");
  270. }
  271. String Example::image_const_ref_func(const Ref<Image> &p_image) {
  272. return p_image.is_valid() ? String("valid") : String("invalid");
  273. }
  274. String Example::return_something(const String &base) {
  275. return base + String("42");
  276. }
  277. Viewport *Example::return_something_const() const {
  278. if (is_inside_tree()) {
  279. Viewport *result = get_viewport();
  280. return result;
  281. }
  282. return nullptr;
  283. }
  284. Ref<ExampleRef> Example::return_empty_ref() const {
  285. Ref<ExampleRef> ref;
  286. return ref;
  287. }
  288. ExampleRef *Example::return_extended_ref() const {
  289. // You can instance and return a refcounted object like this, but keep in mind that refcounting starts with the returned object
  290. // and it will be destroyed when all references are destroyed. If you store this pointer you run the risk of having a pointer
  291. // to a destroyed object.
  292. return memnew(ExampleRef());
  293. }
  294. Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
  295. // This is therefore the preferred way of instancing and returning a refcounted object:
  296. Ref<ExampleRef> ref;
  297. ref.instantiate();
  298. return ref;
  299. }
  300. Variant Example::varargs_func(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  301. return arg_count;
  302. }
  303. int Example::varargs_func_nv(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  304. return 42 + arg_count;
  305. }
  306. void Example::varargs_func_void(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  307. emit_custom_signal("varargs_func_void", arg_count + 1);
  308. }
  309. void Example::emit_custom_signal(const String &name, int value) {
  310. emit_signal("custom_signal", name, value);
  311. }
  312. bool Example::is_object_binding_set_by_parent_constructor() const {
  313. return object_instance_binding_set_by_parent_constructor;
  314. }
  315. Array Example::test_array() const {
  316. Array arr;
  317. arr.resize(2);
  318. arr[0] = Variant(1);
  319. arr[1] = Variant(2);
  320. return arr;
  321. }
  322. String Example::test_string_ops() const {
  323. String s = String("A");
  324. s += "B";
  325. s += "C";
  326. s += char32_t(0x010E);
  327. s = s + "E";
  328. return s;
  329. }
  330. String Example::test_str_utility() const {
  331. return UtilityFunctions::str("Hello, ", "World", "! The answer is ", 42);
  332. }
  333. bool Example::test_string_is_forty_two(const String &p_string) const {
  334. return strcmp(p_string.utf8().ptr(), "forty two") == 0;
  335. }
  336. String Example::test_string_resize(String p_string) const {
  337. int orig_len = p_string.length();
  338. p_string.resize(orig_len + 3);
  339. char32_t *data = p_string.ptrw();
  340. data[orig_len + 0] = '!';
  341. data[orig_len + 1] = '?';
  342. data[orig_len + 2] = '\0';
  343. return p_string;
  344. }
  345. TypedArray<PackedInt32Array> Example::test_typed_array_of_packed() const {
  346. TypedArray<PackedInt32Array> arr;
  347. PackedInt32Array packed_arr1;
  348. packed_arr1.push_back(1);
  349. packed_arr1.push_back(2);
  350. arr.push_back(packed_arr1);
  351. PackedInt32Array packed_arr2;
  352. packed_arr2.push_back(3);
  353. packed_arr2.push_back(4);
  354. arr.push_back(packed_arr2);
  355. return arr;
  356. }
  357. int Example::test_vector_ops() const {
  358. PackedInt32Array arr;
  359. arr.push_back(10);
  360. arr.push_back(20);
  361. arr.push_back(30);
  362. arr.push_back(45);
  363. int ret = 0;
  364. for (const int32_t &E : arr) {
  365. ret += E;
  366. }
  367. return ret;
  368. }
  369. int Example::test_vector_init_list() const {
  370. PackedInt32Array arr = { 10, 20, 30, 45 };
  371. int ret = 0;
  372. for (const int32_t &E : arr) {
  373. ret += E;
  374. }
  375. return ret;
  376. }
  377. Callable Example::test_callable_mp() {
  378. return callable_mp(this, &Example::unbound_method1);
  379. }
  380. Callable Example::test_callable_mp_ret() {
  381. return callable_mp(this, &Example::unbound_method2);
  382. }
  383. Callable Example::test_callable_mp_retc() const {
  384. return callable_mp(this, &Example::unbound_method3);
  385. }
  386. Callable Example::test_callable_mp_static() const {
  387. return callable_mp_static(&Example::unbound_static_method1);
  388. }
  389. Callable Example::test_callable_mp_static_ret() const {
  390. return callable_mp_static(&Example::unbound_static_method2);
  391. }
  392. Callable Example::test_custom_callable() const {
  393. return Callable(memnew(MyCallableCustom));
  394. }
  395. void Example::unbound_method1(Object *p_object, String p_string, int p_int) {
  396. String test = "unbound_method1: ";
  397. test += p_object->get_class();
  398. test += " - " + p_string;
  399. emit_custom_signal(test, p_int);
  400. }
  401. String Example::unbound_method2(Object *p_object, String p_string, int p_int) {
  402. String test = "unbound_method2: ";
  403. test += p_object->get_class();
  404. test += " - " + p_string;
  405. test += " - " + itos(p_int);
  406. return test;
  407. }
  408. String Example::unbound_method3(Object *p_object, String p_string, int p_int) const {
  409. String test = "unbound_method3: ";
  410. test += p_object->get_class();
  411. test += " - " + p_string;
  412. test += " - " + itos(p_int);
  413. return test;
  414. }
  415. void Example::unbound_static_method1(Example *p_object, String p_string, int p_int) {
  416. String test = "unbound_static_method1: ";
  417. test += p_object->get_class();
  418. test += " - " + p_string;
  419. p_object->emit_custom_signal(test, p_int);
  420. }
  421. String Example::unbound_static_method2(Object *p_object, String p_string, int p_int) {
  422. String test = "unbound_static_method2: ";
  423. test += p_object->get_class();
  424. test += " - " + p_string;
  425. test += " - " + itos(p_int);
  426. return test;
  427. }
  428. int Example::test_tarray_arg(const TypedArray<int64_t> &p_array) {
  429. int sum = 0;
  430. for (int i = 0; i < p_array.size(); i++) {
  431. sum += (int)p_array[i];
  432. }
  433. return sum;
  434. }
  435. TypedArray<Vector2> Example::test_tarray() const {
  436. TypedArray<Vector2> arr;
  437. arr.resize(2);
  438. arr[0] = Vector2(1, 2);
  439. arr[1] = Vector2(2, 3);
  440. return arr;
  441. }
  442. Dictionary Example::test_dictionary() const {
  443. Dictionary dict;
  444. dict["hello"] = "world";
  445. dict["foo"] = "bar";
  446. return dict;
  447. }
  448. Example *Example::test_node_argument(Example *p_node) const {
  449. return p_node;
  450. }
  451. bool Example::test_object_cast_to_node(Object *p_object) const {
  452. return Object::cast_to<Node>(p_object) != nullptr;
  453. }
  454. bool Example::test_object_cast_to_control(Object *p_object) const {
  455. return Object::cast_to<Control>(p_object) != nullptr;
  456. }
  457. bool Example::test_object_cast_to_example(Object *p_object) const {
  458. return Object::cast_to<Example>(p_object) != nullptr;
  459. }
  460. Vector2i Example::test_variant_vector2i_conversion(const Variant &p_variant) const {
  461. return p_variant;
  462. }
  463. int Example::test_variant_int_conversion(const Variant &p_variant) const {
  464. return p_variant;
  465. }
  466. float Example::test_variant_float_conversion(const Variant &p_variant) const {
  467. return p_variant;
  468. }
  469. bool Example::test_object_is_valid(const Variant &p_variant) const {
  470. return static_cast<bool>(p_variant.get_validated_object());
  471. }
  472. void Example::test_add_child(Node *p_node) {
  473. add_child(p_node);
  474. }
  475. void Example::test_set_tileset(TileMap *p_tilemap, const Ref<TileSet> &p_tileset) const {
  476. p_tilemap->set_tileset(p_tileset);
  477. }
  478. Variant Example::test_variant_call(Variant p_variant) {
  479. return p_variant.call("test", "hello");
  480. }
  481. BitField<Example::Flags> Example::test_bitfield(BitField<Flags> flags) {
  482. return flags;
  483. }
  484. Variant Example::test_variant_iterator(const Variant &p_input) {
  485. Array output;
  486. Variant iter;
  487. bool is_init_valid = true;
  488. if (!p_input.iter_init(iter, is_init_valid)) {
  489. if (!is_init_valid) {
  490. return "iter_init: not valid";
  491. }
  492. return output;
  493. }
  494. bool is_iter_next_valid = true;
  495. bool is_iter_get_valid = true;
  496. do {
  497. if (!is_iter_next_valid) {
  498. return "iter_next: not valid";
  499. }
  500. Variant value = p_input.iter_get(iter, is_iter_get_valid);
  501. if (!is_iter_get_valid) {
  502. return "iter_get: not valid";
  503. }
  504. output.push_back(((int)value) + 5);
  505. } while (p_input.iter_next(iter, is_iter_next_valid));
  506. if (!is_iter_next_valid) {
  507. return "iter_next: not valid";
  508. }
  509. return output;
  510. }
  511. void Example::test_rpc(int p_value) {
  512. last_rpc_arg = p_value;
  513. }
  514. void Example::test_send_rpc(int p_value) {
  515. rpc("test_rpc", p_value);
  516. }
  517. int Example::return_last_rpc_arg() {
  518. return last_rpc_arg;
  519. }
  520. void Example::callable_bind() {
  521. Callable c = Callable(this, "emit_custom_signal").bind("bound", 11);
  522. c.call();
  523. }
  524. // Properties.
  525. void Example::set_custom_position(const Vector2 &pos) {
  526. custom_position = pos;
  527. }
  528. Vector2 Example::get_custom_position() const {
  529. return custom_position;
  530. }
  531. Vector4 Example::get_v4() const {
  532. return Vector4(1.2, 3.4, 5.6, 7.8);
  533. }
  534. bool Example::test_post_initialize() const {
  535. Ref<ExampleRef> new_example_ref;
  536. new_example_ref.instantiate();
  537. return new_example_ref->was_post_initialized();
  538. }
  539. // Virtual function override.
  540. bool Example::_has_point(const Vector2 &point) const {
  541. Label *label = get_node<Label>("Label");
  542. label->set_text("Got point: " + Variant(point).stringify());
  543. return false;
  544. }
  545. void Example::_input(const Ref<InputEvent> &event) {
  546. const InputEventKey *key_event = Object::cast_to<const InputEventKey>(*event);
  547. if (key_event) {
  548. emit_custom_signal(String("_input: ") + key_event->get_key_label(), key_event->get_unicode());
  549. }
  550. }
  551. void ExampleBase::_bind_methods() {
  552. ClassDB::bind_method(D_METHOD("get_value1"), &ExampleBase::get_value1);
  553. ClassDB::bind_method(D_METHOD("get_value2"), &ExampleBase::get_value2);
  554. }
  555. void ExampleBase::_notification(int p_what) {
  556. if (p_what == NOTIFICATION_ENTER_TREE) {
  557. value1 = 11;
  558. value2 = 22;
  559. }
  560. }
  561. void ExampleChild::_notification(int p_what) {
  562. if (p_what == NOTIFICATION_ENTER_TREE) {
  563. value2 = 33;
  564. }
  565. }
  566. String Example::test_virtual_implemented_in_script(const String &p_name, int p_value) {
  567. String ret;
  568. if (GDVIRTUAL_CALL(_do_something_virtual, p_name, p_value, ret)) {
  569. return ret;
  570. }
  571. return "Unimplemented";
  572. }
  573. String Example::test_use_engine_singleton() const {
  574. return OS::get_singleton()->get_name();
  575. }
  576. String Example::test_library_path() {
  577. String library_path;
  578. internal::gdextension_interface_get_library_path(internal::library, library_path._native_ptr());
  579. return library_path;
  580. }
  581. void ExampleRuntime::_bind_methods() {
  582. ClassDB::bind_method(D_METHOD("set_prop_value", "value"), &ExampleRuntime::set_prop_value);
  583. ClassDB::bind_method(D_METHOD("get_prop_value"), &ExampleRuntime::get_prop_value);
  584. ADD_PROPERTY(PropertyInfo(Variant::INT, "prop_value"), "set_prop_value", "get_prop_value");
  585. }
  586. void ExampleRuntime::set_prop_value(int p_prop_value) {
  587. prop_value = p_prop_value;
  588. }
  589. int ExampleRuntime::get_prop_value() const {
  590. return prop_value;
  591. }
  592. ExampleRuntime::ExampleRuntime() {
  593. }
  594. ExampleRuntime::~ExampleRuntime() {
  595. }