2022-08-22 16:14:33 +00:00
|
|
|
/* godot-cpp integration testing project.
|
|
|
|
*
|
|
|
|
* This is free and unencumbered software released into the public domain.
|
|
|
|
*/
|
2021-09-08 18:11:12 +00:00
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
#include "example.h"
|
|
|
|
|
|
|
|
#include <godot_cpp/core/class_db.hpp>
|
|
|
|
|
|
|
|
#include <godot_cpp/classes/global_constants.hpp>
|
|
|
|
#include <godot_cpp/classes/label.hpp>
|
|
|
|
#include <godot_cpp/variant/utility_functions.hpp>
|
|
|
|
|
|
|
|
using namespace godot;
|
|
|
|
|
2023-04-29 16:56:33 +00:00
|
|
|
void ExampleRef::set_id(int p_id) {
|
|
|
|
id = p_id;
|
|
|
|
}
|
2022-12-05 11:04:08 +00:00
|
|
|
|
2023-02-02 18:01:31 +00:00
|
|
|
int ExampleRef::get_id() const {
|
2022-12-05 11:04:08 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExampleRef::_bind_methods() {
|
2023-04-29 16:56:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_id", "id"), &ExampleRef::set_id);
|
2022-12-05 11:04:08 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_id"), &ExampleRef::get_id);
|
2023-04-29 16:56:33 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
|
2022-12-05 11:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 15:11:50 +00:00
|
|
|
ExampleRef::ExampleRef() {
|
2023-04-29 16:56:33 +00:00
|
|
|
id = 0;
|
2021-09-24 15:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExampleRef::~ExampleRef() {
|
|
|
|
}
|
|
|
|
|
2022-05-04 11:27:08 +00:00
|
|
|
int Example::test_static(int p_a, int p_b) {
|
|
|
|
return p_a + p_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Example::test_static2() {
|
2023-04-29 16:56:33 +00:00
|
|
|
//UtilityFunctions::print(" void static");
|
2022-05-04 11:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Example::def_args(int p_a, int p_b) {
|
|
|
|
return p_a + p_b;
|
|
|
|
}
|
|
|
|
|
2022-08-19 07:30:06 +00:00
|
|
|
void Example::_notification(int p_what) {
|
2023-04-29 16:56:33 +00:00
|
|
|
//UtilityFunctions::print("Notification: ", String::num(p_what));
|
2022-08-19 07:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Example::_set(const StringName &p_name, const Variant &p_value) {
|
2022-09-02 07:41:24 +00:00
|
|
|
String name = p_name;
|
|
|
|
if (name.begins_with("dproperty")) {
|
2022-11-16 21:43:44 +00:00
|
|
|
int64_t index = name.get_slicec('_', 1).to_int();
|
2022-09-02 07:41:24 +00:00
|
|
|
dprop[index] = p_value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (name == "property_from_list") {
|
2022-08-19 07:30:06 +00:00
|
|
|
property_from_list = p_value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Example::_get(const StringName &p_name, Variant &r_ret) const {
|
2022-09-02 07:41:24 +00:00
|
|
|
String name = p_name;
|
|
|
|
if (name.begins_with("dproperty")) {
|
2022-11-16 21:43:44 +00:00
|
|
|
int64_t index = name.get_slicec('_', 1).to_int();
|
2022-09-02 07:41:24 +00:00
|
|
|
r_ret = dprop[index];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (name == "property_from_list") {
|
2022-08-19 07:30:06 +00:00
|
|
|
r_ret = property_from_list;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String Example::_to_string() const {
|
2022-11-16 21:43:44 +00:00
|
|
|
return "[ GDExtension::Example <--> Instance ID:" + uitos(get_instance_id()) + " ]";
|
2022-08-19 07:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Example::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::VECTOR3, "property_from_list"));
|
2022-09-02 07:41:24 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::VECTOR2, "dproperty_" + itos(i)));
|
|
|
|
}
|
2022-08-19 07:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Example::_property_can_revert(const StringName &p_name) const {
|
|
|
|
if (p_name == StringName("property_from_list") && property_from_list != Vector3(42, 42, 42)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool Example::_property_get_revert(const StringName &p_name, Variant &r_property) const {
|
|
|
|
if (p_name == StringName("property_from_list")) {
|
|
|
|
r_property = Vector3(42, 42, 42);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
void Example::_bind_methods() {
|
|
|
|
// Methods.
|
|
|
|
ClassDB::bind_method(D_METHOD("simple_func"), &Example::simple_func);
|
|
|
|
ClassDB::bind_method(D_METHOD("simple_const_func"), &Example::simple_const_func);
|
2023-04-29 16:56:33 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("custom_ref_func", "ref"), &Example::custom_ref_func);
|
|
|
|
ClassDB::bind_method(D_METHOD("custom_const_ref_func", "ref"), &Example::custom_const_ref_func);
|
|
|
|
ClassDB::bind_method(D_METHOD("image_ref_func", "image"), &Example::image_ref_func);
|
|
|
|
ClassDB::bind_method(D_METHOD("image_const_ref_func", "image"), &Example::image_const_ref_func);
|
2021-08-19 17:51:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
|
|
|
|
ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
|
2022-12-05 11:04:08 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("return_empty_ref"), &Example::return_empty_ref);
|
2021-09-24 15:11:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
|
2022-12-05 11:04:08 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("extended_ref_checks", "ref"), &Example::extended_ref_checks);
|
2021-11-18 03:05:14 +00:00
|
|
|
|
2021-09-16 07:21:40 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
|
2022-09-15 07:33:07 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("test_tarray_arg", "array"), &Example::test_tarray_arg);
|
|
|
|
ClassDB::bind_method(D_METHOD("test_tarray"), &Example::test_tarray);
|
2021-11-18 03:05:14 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
|
2023-01-10 10:48:21 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("test_node_argument"), &Example::test_node_argument);
|
2022-11-28 12:47:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("test_string_ops"), &Example::test_string_ops);
|
|
|
|
ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
|
2021-08-19 17:51:41 +00:00
|
|
|
|
2022-12-08 05:34:19 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("test_bitfield", "flags"), &Example::test_bitfield);
|
|
|
|
|
2022-05-04 11:27:08 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("def_args", "a", "b"), &Example::def_args, DEFVAL(100), DEFVAL(200));
|
|
|
|
|
|
|
|
ClassDB::bind_static_method("Example", D_METHOD("test_static", "a", "b"), &Example::test_static);
|
|
|
|
ClassDB::bind_static_method("Example", D_METHOD("test_static2"), &Example::test_static2);
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
{
|
|
|
|
MethodInfo mi;
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
|
|
|
|
mi.name = "varargs_func";
|
|
|
|
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func", &Example::varargs_func, mi);
|
|
|
|
}
|
|
|
|
|
2022-03-29 11:44:07 +00:00
|
|
|
{
|
|
|
|
MethodInfo mi;
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
|
|
|
|
mi.name = "varargs_func_nv";
|
|
|
|
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_nv", &Example::varargs_func_nv, mi);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
MethodInfo mi;
|
|
|
|
mi.arguments.push_back(PropertyInfo(Variant::STRING, "some_argument"));
|
|
|
|
mi.name = "varargs_func_void";
|
|
|
|
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "varargs_func_void", &Example::varargs_func_void, mi);
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
// Properties.
|
2021-09-21 13:55:05 +00:00
|
|
|
ADD_GROUP("Test group", "group_");
|
|
|
|
ADD_SUBGROUP("Test subgroup", "group_subgroup_");
|
2021-09-15 03:56:20 +00:00
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position);
|
2022-07-20 20:49:08 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_v4"), &Example::get_v4);
|
2021-08-19 17:51:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position);
|
2021-09-15 03:56:20 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "group_subgroup_custom_position"), "set_custom_position", "get_custom_position");
|
2021-08-19 17:51:41 +00:00
|
|
|
|
|
|
|
// Signals.
|
|
|
|
ADD_SIGNAL(MethodInfo("custom_signal", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::INT, "value")));
|
|
|
|
ClassDB::bind_method(D_METHOD("emit_custom_signal", "name", "value"), &Example::emit_custom_signal);
|
|
|
|
|
|
|
|
// Constants.
|
|
|
|
BIND_ENUM_CONSTANT(FIRST);
|
|
|
|
BIND_ENUM_CONSTANT(ANSWER_TO_EVERYTHING);
|
|
|
|
|
2022-12-08 05:34:19 +00:00
|
|
|
BIND_BITFIELD_FLAG(FLAG_ONE);
|
|
|
|
BIND_BITFIELD_FLAG(FLAG_TWO);
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
BIND_CONSTANT(CONSTANT_WITHOUT_ENUM);
|
2022-12-08 05:34:19 +00:00
|
|
|
BIND_ENUM_CONSTANT(OUTSIDE_OF_CLASS);
|
2021-08-19 17:51:41 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 07:21:40 +00:00
|
|
|
Example::Example() {
|
2023-04-29 16:56:33 +00:00
|
|
|
//UtilityFunctions::print("Constructor.");
|
2021-09-16 07:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Example::~Example() {
|
2023-04-29 16:56:33 +00:00
|
|
|
//UtilityFunctions::print("Destructor.");
|
2021-09-16 07:21:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
// Methods.
|
|
|
|
void Example::simple_func() {
|
2023-04-29 16:56:33 +00:00
|
|
|
emit_custom_signal("simple_func", 3);
|
2021-08-19 17:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Example::simple_const_func() const {
|
2023-04-29 16:56:33 +00:00
|
|
|
((Example *)this)->emit_custom_signal("simple_const_func", 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Example::custom_ref_func(Ref<ExampleRef> p_ref) {
|
|
|
|
return p_ref.is_valid() ? p_ref->get_id() : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Example::custom_const_ref_func(const Ref<ExampleRef> &p_ref) {
|
|
|
|
return p_ref.is_valid() ? p_ref->get_id() : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
String Example::image_ref_func(Ref<Image> p_image) {
|
|
|
|
return p_image.is_valid() ? String("valid") : String("invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
String Example::image_const_ref_func(const Ref<Image> &p_image) {
|
|
|
|
return p_image.is_valid() ? String("valid") : String("invalid");
|
2021-08-19 17:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String Example::return_something(const String &base) {
|
2023-04-29 16:56:33 +00:00
|
|
|
return base + String("42");
|
2021-08-19 17:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Viewport *Example::return_something_const() const {
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
Viewport *result = get_viewport();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-12-05 11:04:08 +00:00
|
|
|
Ref<ExampleRef> Example::return_empty_ref() const {
|
|
|
|
Ref<ExampleRef> ref;
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2021-09-24 15:11:50 +00:00
|
|
|
ExampleRef *Example::return_extended_ref() const {
|
2022-12-05 11:04:08 +00:00
|
|
|
// You can instance and return a refcounted object like this, but keep in mind that refcounting starts with the returned object
|
|
|
|
// and it will be destroyed when all references are destroyed. If you store this pointer you run the risk of having a pointer
|
|
|
|
// to a destroyed object.
|
2021-09-24 15:11:50 +00:00
|
|
|
return memnew(ExampleRef());
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:49:53 +00:00
|
|
|
Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
|
2022-12-05 11:04:08 +00:00
|
|
|
// This is therefor the prefered way of instancing and returning a refcounted object:
|
2021-09-28 13:49:53 +00:00
|
|
|
Ref<ExampleRef> ref;
|
|
|
|
ref.instantiate();
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2022-12-07 13:11:44 +00:00
|
|
|
Variant Example::varargs_func(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
|
2021-08-19 17:51:41 +00:00
|
|
|
return arg_count;
|
|
|
|
}
|
|
|
|
|
2022-12-07 13:11:44 +00:00
|
|
|
int Example::varargs_func_nv(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
|
2023-04-29 16:56:33 +00:00
|
|
|
return 42 + arg_count;
|
2022-03-29 11:44:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 13:11:44 +00:00
|
|
|
void Example::varargs_func_void(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error) {
|
2023-04-29 16:56:33 +00:00
|
|
|
emit_custom_signal("varargs_func_void", arg_count + 1);
|
2022-03-29 11:44:07 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
void Example::emit_custom_signal(const String &name, int value) {
|
|
|
|
emit_signal("custom_signal", name, value);
|
|
|
|
}
|
|
|
|
|
2021-09-16 07:21:40 +00:00
|
|
|
Array Example::test_array() const {
|
|
|
|
Array arr;
|
|
|
|
|
|
|
|
arr.resize(2);
|
|
|
|
arr[0] = Variant(1);
|
|
|
|
arr[1] = Variant(2);
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2022-11-28 12:47:55 +00:00
|
|
|
String Example::test_string_ops() const {
|
|
|
|
String s = String("A");
|
|
|
|
s += "B";
|
|
|
|
s += "C";
|
|
|
|
s += char32_t(0x010E);
|
|
|
|
s = s + "E";
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Example::test_vector_ops() const {
|
|
|
|
PackedInt32Array arr;
|
|
|
|
arr.push_back(10);
|
|
|
|
arr.push_back(20);
|
|
|
|
arr.push_back(30);
|
|
|
|
arr.push_back(45);
|
|
|
|
int ret = 0;
|
|
|
|
for (const int32_t &E : arr) {
|
|
|
|
ret += E;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-04-29 16:56:33 +00:00
|
|
|
int Example::test_tarray_arg(const TypedArray<int64_t> &p_array) {
|
|
|
|
int sum = 0;
|
2022-09-15 07:33:07 +00:00
|
|
|
for (int i = 0; i < p_array.size(); i++) {
|
2023-04-29 16:56:33 +00:00
|
|
|
sum += (int)p_array[i];
|
2022-09-15 07:33:07 +00:00
|
|
|
}
|
2023-04-29 16:56:33 +00:00
|
|
|
return sum;
|
2022-09-15 07:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TypedArray<Vector2> Example::test_tarray() const {
|
|
|
|
TypedArray<Vector2> arr;
|
|
|
|
|
|
|
|
arr.resize(2);
|
|
|
|
arr[0] = Vector2(1, 2);
|
|
|
|
arr[1] = Vector2(2, 3);
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2021-11-18 03:05:14 +00:00
|
|
|
Dictionary Example::test_dictionary() const {
|
|
|
|
Dictionary dict;
|
|
|
|
|
|
|
|
dict["hello"] = "world";
|
|
|
|
dict["foo"] = "bar";
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2023-04-29 16:56:33 +00:00
|
|
|
Example *Example::test_node_argument(Example *p_node) const {
|
|
|
|
return p_node;
|
|
|
|
}
|
|
|
|
|
2022-12-08 05:34:19 +00:00
|
|
|
BitField<Example::Flags> Example::test_bitfield(BitField<Flags> flags) {
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
// Properties.
|
|
|
|
void Example::set_custom_position(const Vector2 &pos) {
|
|
|
|
custom_position = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 Example::get_custom_position() const {
|
|
|
|
return custom_position;
|
|
|
|
}
|
|
|
|
|
2022-07-20 20:49:08 +00:00
|
|
|
Vector4 Example::get_v4() const {
|
|
|
|
return Vector4(1.2, 3.4, 5.6, 7.8);
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:51:41 +00:00
|
|
|
// Virtual function override.
|
2021-08-19 23:03:11 +00:00
|
|
|
bool Example::_has_point(const Vector2 &point) const {
|
2021-08-19 17:51:41 +00:00
|
|
|
Label *label = get_node<Label>("Label");
|
|
|
|
label->set_text("Got point: " + Variant(point).stringify());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|