From 9c25980619eea9be6da5ff361d0422c5dab44571 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 29 May 2024 16:46:18 +0200 Subject: [PATCH] feat: improved clarity of godot_macros and added GDENUM --- godot_macros.h | 77 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/godot_macros.h b/godot_macros.h index bd13dc5..79869d4 100644 --- a/godot_macros.h +++ b/godot_macros.h @@ -3,34 +3,95 @@ #include "godot_cpp/classes/engine.hpp" #include "godot_cpp/core/class_db.hpp" +#include "godot_cpp/variant/string.hpp" #define MACRO_STRING_INNER(_Arg) #_Arg #define MACRO_STRING(_Arg) MACRO_STRING_INNER(_Arg) -#define GDPROPERTY(_PropName, _PropType) \ - godot::ClassDB::bind_method(godot::D_METHOD("get_" #_PropName), &CLASSNAME::get_##_PropName); \ - godot::ClassDB::bind_method(godot::D_METHOD("set_" #_PropName, "value"), &CLASSNAME::set_##_PropName); \ - godot::ClassDB::add_property(MACRO_STRING(CLASSNAME), godot::PropertyInfo(_PropType, #_PropName), "set_" #_PropName, "get_" #_PropName) +/*! Register property. + * + * Register variable CLASSNAME::PropName_ with variant type PropType_. + * Requires setting CLASSNAME as a #define first. Also requires a CLASSNAME::get_PropName_ and CLASSNAME::set_PropName_ to exist. + */ +#define GDPROPERTY(PropName_, PropType_) \ + godot::ClassDB::bind_method(godot::D_METHOD("get_" #PropName_), &CLASSNAME::get_##PropName_); \ + godot::ClassDB::bind_method(godot::D_METHOD("set_" #PropName_, "value"), &CLASSNAME::set_##PropName_); \ + godot::ClassDB::add_property(MACRO_STRING(CLASSNAME), godot::PropertyInfo(PropType_, #PropName_), "set_" #PropName_, "get_" #PropName_) -#define GDPROPERTY_HINTED(_PropName, _PropType, ...) \ - godot::ClassDB::bind_method(godot::D_METHOD("get_" #_PropName), &CLASSNAME::get_##_PropName); \ - godot::ClassDB::bind_method(godot::D_METHOD("set_" #_PropName, "value"), &CLASSNAME::set_##_PropName); \ - godot::ClassDB::add_property(MACRO_STRING(CLASSNAME), godot::PropertyInfo(_PropType, #_PropName, __VA_ARGS__), "set_" #_PropName, "get_" #_PropName) +/*! Register a hinted property. + * + * Register variable CLASSNAME::PropName_ with variant type PropType_. + * Requires setting CLASSNAME as a #define first, and CLASSNAME::get_PropName and CLASSNAME::set_PropName_ to exist. + */ +#define GDPROPERTY_HINTED(PropName_, PropType_, ...) \ + godot::ClassDB::bind_method(godot::D_METHOD("get_" #PropName_), &CLASSNAME::get_##PropName_); \ + godot::ClassDB::bind_method(godot::D_METHOD("set_" #PropName_, "value"), &CLASSNAME::set_##PropName_); \ + godot::ClassDB::add_property(MACRO_STRING(CLASSNAME), godot::PropertyInfo(PropType_, #PropName_, __VA_ARGS__), "set_" #PropName_, "get_" #PropName_) +/*! Register a function. + * + * Register a function CLASSNAME::_FnName() to godot. + * Requires setting CLASSNAME as a #define first. + */ #define GDFUNCTION(_FnName) godot::ClassDB::bind_method(godot::D_METHOD(#_FnName), &CLASSNAME::_FnName) +/*! Register a function with arguments. + * + * Register a function CLASSNAME::_FnName(...) to godot. + * Requires setting CLASSNAME as a #define first. + */ #define GDFUNCTION_ARGS(_FnName, ...) godot::ClassDB::bind_method(godot::D_METHOD(#_FnName, __VA_ARGS__), &CLASSNAME::_FnName) +/*! Register a static function. + * + * Register a static member function CLASSNAME::_FnName() to godot. + * Requires setting CLASSNAME as a #define first. + */ #define GDFUNCTION_STATIC(_FnName) godot::ClassDB::bind_static_method(MACRO_STRING(CLASSNAME), godot::D_METHOD(#_FnName), &CLASSNAME::_FnName) +/*! Register a static function with arguments. + * + * Register a static member function CLASSNAME::_FnName(...) to godot. + * Requires setting CLASSNAME as a #define first. + */ #define GDFUNCTION_STATIC_ARGS(_FnName, ...) godot::ClassDB::bind_static_method(MACRO_STRING(CLASSNAME), godot::D_METHOD(#_FnName, __VA_ARGS__), &CLASSNAME::_FnName) +/*! Declare a godot Observer signal. + * + * Declare the existence of a Observer signal to godot. + */ #define GDSIGNAL(...) godot::ClassDB::add_signal(MACRO_STRING(CLASSNAME), godot::MethodInfo(__VA_ARGS__)) +/*! Construct godot resource type hint. + * + * Use when registering properties of arrays of resource classes. + */ #define GDRESOURCETYPE(_Class) godot::vformat("%s/%s:%s", godot::Variant::OBJECT, godot::PROPERTY_HINT_RESOURCE_TYPE, #_Class) +/*! Execute the rest of the function only if currently running as editor. + * + * Useful for _ready, _enter/_exit, _process, etc. functions. + */ #define GDEDITORONLY() if(!godot::Engine::get_singleton()->is_editor_hint()) return; +/*! Execute the rest of the function only if currently running as game. + * + * Useful for _ready, _enter/_exit, _process, etc. functions. + */ #define GDGAMEONLY() if(godot::Engine::get_singleton()->is_editor_hint()) return; +/*! Declare a scoped enum struct. + * + * Declares a struct Name_ with an enum Value and a single variable and a get_property_hint() function. + */ +#define GDENUM(Name_, ...) struct Name_ {\ + enum Value {__VA_ARGS__};\ + private:\ + Value value{};\ + public:\ + static inline godot::String get_property_hint() { return godot::String(#__VA_ARGS__); }\ + inline Name_(Value value): value{value} {}\ + inline Name_(Name_ const &value): value{value.value} {}\ +} + #endif // !UC_GODOT_MACROS_H