example.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/variant/utility_functions.hpp>
  10. using namespace godot;
  11. void ExampleRef::set_id(int p_id) {
  12. id = p_id;
  13. }
  14. int ExampleRef::get_id() const {
  15. return id;
  16. }
  17. void ExampleRef::_bind_methods() {
  18. ClassDB::bind_method(D_METHOD("set_id", "id"), &ExampleRef::set_id);
  19. ClassDB::bind_method(D_METHOD("get_id"), &ExampleRef::get_id);
  20. ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
  21. }
  22. ExampleRef::ExampleRef() {
  23. id = 0;
  24. }
  25. ExampleRef::~ExampleRef() {
  26. }
  27. int Example::test_static(int p_a, int p_b) {
  28. return p_a + p_b;
  29. }
  30. void Example::test_static2() {
  31. //UtilityFunctions::print(" void static");
  32. }
  33. int Example::def_args(int p_a, int p_b) {
  34. return p_a + p_b;
  35. }
  36. void Example::_notification(int p_what) {
  37. //UtilityFunctions::print("Notification: ", String::num(p_what));
  38. }
  39. bool Example::_set(const StringName &p_name, const Variant &p_value) {
  40. String name = p_name;
  41. if (name.begins_with("dproperty")) {
  42. int64_t index = name.get_slicec('_', 1).to_int();
  43. dprop[index] = p_value;
  44. return true;
  45. }
  46. if (name == "property_from_list") {
  47. property_from_list = p_value;
  48. return true;
  49. }
  50. return false;
  51. }
  52. bool Example::_get(const StringName &p_name, Variant &r_ret) const {
  53. String name = p_name;
  54. if (name.begins_with("dproperty")) {
  55. int64_t index = name.get_slicec('_', 1).to_int();
  56. r_ret = dprop[index];
  57. return true;
  58. }
  59. if (name == "property_from_list") {
  60. r_ret = property_from_list;
  61. return true;
  62. }
  63. return false;
  64. }
  65. String Example::_to_string() const {
  66. return "[ GDExtension::Example <--> Instance ID:" + uitos(get_instance_id()) + " ]";
  67. }
  68. void Example::_get_property_list(List<PropertyInfo> *p_list) const {
  69. p_list->push_back(PropertyInfo(Variant::VECTOR3, "property_from_list"));
  70. for (int i = 0; i < 3; i++) {
  71. p_list->push_back(PropertyInfo(Variant::VECTOR2, "dproperty_" + itos(i)));
  72. }
  73. }
  74. bool Example::_property_can_revert(const StringName &p_name) const {
  75. if (p_name == StringName("property_from_list") && property_from_list != Vector3(42, 42, 42)) {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. };
  81. bool Example::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  82. if (p_name == StringName("property_from_list")) {
  83. r_property = Vector3(42, 42, 42);
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. };
  89. void Example::_bind_methods() {
  90. // Methods.
  91. ClassDB::bind_method(D_METHOD("simple_func"), &Example::simple_func);
  92. ClassDB::bind_method(D_METHOD("simple_const_func"), &Example::simple_const_func);
  93. ClassDB::bind_method(D_METHOD("custom_ref_func", "ref"), &Example::custom_ref_func);
  94. ClassDB::bind_method(D_METHOD("custom_const_ref_func", "ref"), &Example::custom_const_ref_func);
  95. ClassDB::bind_method(D_METHOD("image_ref_func", "image"), &Example::image_ref_func);
  96. ClassDB::bind_method(D_METHOD("image_const_ref_func", "image"), &Example::image_const_ref_func);
  97. ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
  98. ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
  99. ClassDB::bind_method(D_METHOD("return_empty_ref"), &Example::return_empty_ref);
  100. ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
  101. ClassDB::bind_method(D_METHOD("extended_ref_checks", "ref"), &Example::extended_ref_checks);
  102. ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
  103. ClassDB::bind_method(D_METHOD("test_tarray_arg", "array"), &Example::test_tarray_arg);
  104. ClassDB::bind_method(D_METHOD("test_tarray"), &Example::test_tarray);
  105. ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
  106. ClassDB::bind_method(D_METHOD("test_node_argument"), &Example::test_node_argument);
  107. ClassDB::bind_method(D_METHOD("test_string_ops"), &Example::test_string_ops);
  108. ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
  109. ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
  110. ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
  111. ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
  112. ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
  113. {
  114. MethodInfo mi;
  115. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  116. mi.name = "varargs_func";
  117. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func", &Example::varargs_func, mi);
  118. }
  119. {
  120. MethodInfo mi;
  121. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  122. mi.name = "varargs_func_nv";
  123. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_nv", &Example::varargs_func_nv, mi);
  124. }
  125. {
  126. MethodInfo mi;
  127. mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
  128. mi.name = "varargs_func_void";
  129. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_void", &Example::varargs_func_void, mi);
  130. }
  131. // Properties.
  132. ADD_GROUP("Test group", "group_");
  133. ADD_SUBGROUP("Test subgroup", "group_subgroup_");
  134. ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
  135. ClassDB::bind_method(D_METHOD("get_v4"), &Example::get_v4);
  136. ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
  137. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
  138. // Signals.
  139. ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
  140. ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
  141. // Constants.
  142. BIND_ENUM_CONSTANT(FIRST);
  143. BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
  144. BIND_BITFIELD_FLAG(FLAG_ONE);
  145. BIND_BITFIELD_FLAG(FLAG_TWO);
  146. BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
  147. BIND_ENUM_CONSTANT(OUTSIDE_OF_CLASS);
  148. }
  149. Example::Example() {
  150. //UtilityFunctions::print("Constructor.");
  151. }
  152. Example::~Example() {
  153. //UtilityFunctions::print("Destructor.");
  154. }
  155. // Methods.
  156. void Example::simple_func() {
  157. emit_custom_signal("simple_func", 3);
  158. }
  159. void Example::simple_const_func() const {
  160. ((Example *)this)->emit_custom_signal("simple_const_func", 4);
  161. }
  162. int Example::custom_ref_func(Ref<ExampleRef> p_ref) {
  163. return p_ref.is_valid() ? p_ref->get_id() : -1;
  164. }
  165. int Example::custom_const_ref_func(const Ref<ExampleRef> &p_ref) {
  166. return p_ref.is_valid() ? p_ref->get_id() : -1;
  167. }
  168. String Example::image_ref_func(Ref<Image> p_image) {
  169. return p_image.is_valid() ? String("valid") : String("invalid");
  170. }
  171. String Example::image_const_ref_func(const Ref<Image> &p_image) {
  172. return p_image.is_valid() ? String("valid") : String("invalid");
  173. }
  174. String Example::return_something(const String &base) {
  175. return base + String("42");
  176. }
  177. Viewport *Example::return_something_const() const {
  178. if (is_inside_tree()) {
  179. Viewport *result = get_viewport();
  180. return result;
  181. }
  182. return nullptr;
  183. }
  184. Ref<ExampleRef> Example::return_empty_ref() const {
  185. Ref<ExampleRef> ref;
  186. return ref;
  187. }
  188. ExampleRef *Example::return_extended_ref() const {
  189. // You can instance and return a refcounted object like this, but keep in mind that refcounting starts with the returned object
  190. // and it will be destroyed when all references are destroyed. If you store this pointer you run the risk of having a pointer
  191. // to a destroyed object.
  192. return memnew(ExampleRef());
  193. }
  194. Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
  195. // This is therefor the prefered way of instancing and returning a refcounted object:
  196. Ref<ExampleRef> ref;
  197. ref.instantiate();
  198. return ref;
  199. }
  200. Variant Example::varargs_func(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  201. return arg_count;
  202. }
  203. int Example::varargs_func_nv(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  204. return 42 + arg_count;
  205. }
  206. void Example::varargs_func_void(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
  207. emit_custom_signal("varargs_func_void", arg_count + 1);
  208. }
  209. void Example::emit_custom_signal(const String &name, int value) {
  210. emit_signal("custom_signal", name, value);
  211. }
  212. Array Example::test_array() const {
  213. Array arr;
  214. arr.resize(2);
  215. arr[0] = Variant(1);
  216. arr[1] = Variant(2);
  217. return arr;
  218. }
  219. String Example::test_string_ops() const {
  220. String s = String("A");
  221. s += "B";
  222. s += "C";
  223. s += char32_t(0x010E);
  224. s = s + "E";
  225. return s;
  226. }
  227. int Example::test_vector_ops() const {
  228. PackedInt32Array arr;
  229. arr.push_back(10);
  230. arr.push_back(20);
  231. arr.push_back(30);
  232. arr.push_back(45);
  233. int ret = 0;
  234. for (const int32_t &E : arr) {
  235. ret += E;
  236. }
  237. return ret;
  238. }
  239. int Example::test_tarray_arg(const TypedArray<int64_t> &p_array) {
  240. int sum = 0;
  241. for (int i = 0; i < p_array.size(); i++) {
  242. sum += (int)p_array[i];
  243. }
  244. return sum;
  245. }
  246. TypedArray<Vector2> Example::test_tarray() const {
  247. TypedArray<Vector2> arr;
  248. arr.resize(2);
  249. arr[0] = Vector2(1, 2);
  250. arr[1] = Vector2(2, 3);
  251. return arr;
  252. }
  253. Dictionary Example::test_dictionary() const {
  254. Dictionary dict;
  255. dict["hello"] = "world";
  256. dict["foo"] = "bar";
  257. return dict;
  258. }
  259. Example *Example::test_node_argument(Example *p_node) const {
  260. return p_node;
  261. }
  262. BitField<Example::Flags> Example::test_bitfield(BitField<Flags> flags) {
  263. return flags;
  264. }
  265. // Properties.
  266. void Example::set_custom_position(const Vector2 &pos) {
  267. custom_position = pos;
  268. }
  269. Vector2 Example::get_custom_position() const {
  270. return custom_position;
  271. }
  272. Vector4 Example::get_v4() const {
  273. return Vector4(1.2, 3.4, 5.6, 7.8);
  274. }
  275. // Virtual function override.
  276. bool Example::_has_point(const Vector2 &point) const {
  277. Label *label = get_node<Label>("Label");
  278. label->set_text("Got point: " + Variant(point).stringify());
  279. return false;
  280. }
  281. void Example::_input(const Ref<InputEvent> &event) {
  282. const InputEventKey *key_event = Object::cast_to<const InputEventKey>(*event);
  283. if (key_event) {
  284. emit_custom_signal(String("_input: ") + key_event->get_key_label(), key_event->get_unicode());
  285. }
  286. }