diff options
author | George Marques <george@gmarqu.es> | 2021-08-19 14:51:41 -0300 |
---|---|---|
committer | Bastiaan Olij <mux213@gmail.com> | 2021-09-27 23:08:08 +1000 |
commit | b3a4a2cf9360cad559d11eeab8940c1cfc0b32d0 (patch) | |
tree | b06ac4a05a7904a0ebd7f468f32a8012fda64883 /test/src | |
parent | 8bcf32a61946620017f61568f4aa10070db4bd4e (diff) | |
download | redot-cpp-b3a4a2cf9360cad559d11eeab8940c1cfc0b32d0.tar.gz |
Add sample test project
Diffstat (limited to 'test/src')
-rw-r--r-- | test/src/example.cpp | 91 | ||||
-rw-r--r-- | test/src/example.h | 50 | ||||
-rw-r--r-- | test/src/register_types.cpp | 40 | ||||
-rw-r--r-- | test/src/register_types.h | 7 |
4 files changed, 188 insertions, 0 deletions
diff --git a/test/src/example.cpp b/test/src/example.cpp new file mode 100644 index 0000000..27be6a1 --- /dev/null +++ b/test/src/example.cpp @@ -0,0 +1,91 @@ +#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; + +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); + ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something); + ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const); + + { + 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); + } + + // Properties. + ClassDB::bind_method(D_METHOD("get_custom_position"), &Example::get_custom_position); + ClassDB::bind_method(D_METHOD("set_custom_position", "position"), &Example::set_custom_position); + ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "custom_position"), "set_custom_position", "get_custom_position"); + + // 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); + + BIND_CONSTANT(CONSTANT_WITHOUT_ENUM); + + // Virtual function override. + BIND_VIRTUAL_METHOD(_has_point); +} + +// Methods. +void Example::simple_func() { + UtilityFunctions::print("Simple func called."); +} + +void Example::simple_const_func() const { + UtilityFunctions::print("Simple const func called."); +} + +String Example::return_something(const String &base) { + UtilityFunctions::print("Return something called."); + return base; +} + +Viewport *Example::return_something_const() const { + UtilityFunctions::print("Return something const called."); + if (is_inside_tree()) { + Viewport *result = get_viewport(); + return result; + } + return nullptr; +} + +Variant Example::varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error) { + UtilityFunctions::print("Varargs called with ", String::num(arg_count), " arguments"); + return arg_count; +} + +void Example::emit_custom_signal(const String &name, int value) { + emit_signal("custom_signal", name, value); +} + +// Properties. +void Example::set_custom_position(const Vector2 &pos) { + custom_position = pos; +} + +Vector2 Example::get_custom_position() const { + return custom_position; +} + +// Virtual function override. +bool Example::_has_point(const Vector2 &point) { + Label *label = get_node<Label>("Label"); + label->set_text("Got point: " + Variant(point).stringify()); + + return false; +} diff --git a/test/src/example.h b/test/src/example.h new file mode 100644 index 0000000..0b054e5 --- /dev/null +++ b/test/src/example.h @@ -0,0 +1,50 @@ +#ifndef EXAMPLE_CLASS_H +#define EXAMPLE_CLASS_H + +#include <godot_cpp/classes/control.hpp> +#include <godot_cpp/classes/global_constants.hpp> +#include <godot_cpp/classes/viewport.hpp> + +#include <godot_cpp/core/binder_common.hpp> + +using namespace godot; + +class Example : public Control { + GDCLASS(Example, Control); + +protected: + static void _bind_methods(); + +private: + Vector2 custom_position; + +public: + // Constants. + enum Constants { + FIRST, + ANSWER_TO_EVERYTHING = 42, + }; + + enum { + CONSTANT_WITHOUT_ENUM = 314, + }; + + // Functions + void simple_func(); + void simple_const_func() const; + String return_something(const String &base); + Viewport *return_something_const() const; + Variant varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error); + void emit_custom_signal(const String &name, int value); + + // Property + void set_custom_position(const Vector2 &pos); + Vector2 get_custom_position() const; + + // Virtual function override + virtual bool _has_point(const Vector2 &point); +}; + +VARIANT_ENUM_CAST(Example, Constants); + +#endif // ! EXAMPLE_CLASS_H diff --git a/test/src/register_types.cpp b/test/src/register_types.cpp new file mode 100644 index 0000000..78fc2aa --- /dev/null +++ b/test/src/register_types.cpp @@ -0,0 +1,40 @@ +#include "register_types.h" + +#include <godot/gdnative_interface.h> + +#include <godot_cpp/core/class_db.hpp> +#include <godot_cpp/core/defs.hpp> +#include <godot_cpp/godot.hpp> + +#include "example.h" + +using namespace godot; + +void register_example_types() { + ClassDB::register_class<Example>(); +} + +void unregister_example_types() {} + +extern "C" { + +// Initialization. + +GDNativeBool GDN_EXPORT example_library_init(const GDNativeInterface *p_interface, const GDNativeExtensionClassLibraryPtr p_library, GDNativeInitialization *r_initialization) { + GDNativeBool result = godot::GDExtensionBinding::init(p_interface, p_library, r_initialization); + + if (result) { + register_example_types(); + } + + return result; +} + +void GDN_EXPORT initialize_level(void *userdata, GDNativeInitializationLevel p_level) { + godot::GDExtensionBinding::initialize_level(userdata, p_level); +} + +void GDN_EXPORT deinitialize_level(void *userdata, GDNativeInitializationLevel p_level) { + godot::GDExtensionBinding::deinitialize_level(userdata, p_level); +} +} diff --git a/test/src/register_types.h b/test/src/register_types.h new file mode 100644 index 0000000..7678483 --- /dev/null +++ b/test/src/register_types.h @@ -0,0 +1,7 @@ +#ifndef EXAMPLE_REGISTER_TYPES_H +#define EXAMPLE_REGISTER_TYPES_H + +void register_example_types(); +void unregister_example_types(); + +#endif // ! EXAMPLE_REGISTER_TYPES_H |