2024-05-29 21:06:39 +00:00
|
|
|
#ifndef UTILS_GODOT_MACROS_HPP
|
|
|
|
#define UTILS_GODOT_MACROS_HPP
|
|
|
|
|
2024-05-29 21:08:40 +00:00
|
|
|
/*! \file godot_macros.hpp
|
2024-05-29 21:06:39 +00:00
|
|
|
* \brief C-style preprocessor macros to simplify using godot's C++ API.
|
|
|
|
*/
|
2024-01-30 09:36:58 +00:00
|
|
|
|
2024-02-02 07:47:30 +00:00
|
|
|
#include "godot_cpp/classes/engine.hpp"
|
|
|
|
#include "godot_cpp/core/class_db.hpp"
|
2024-05-29 14:46:18 +00:00
|
|
|
#include "godot_cpp/variant/string.hpp"
|
2024-02-02 07:47:30 +00:00
|
|
|
|
2024-05-28 14:28:36 +00:00
|
|
|
|
2024-01-30 09:36:58 +00:00
|
|
|
#define MACRO_STRING_INNER(_Arg) #_Arg
|
|
|
|
#define MACRO_STRING(_Arg) MACRO_STRING_INNER(_Arg)
|
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDPROPERTY(PropName_, PropType_)
|
|
|
|
* \brief Register property.
|
2024-05-29 14:46:18 +00:00
|
|
|
*
|
|
|
|
* 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_)
|
2024-01-30 09:36:58 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDPROPERTY_HINTED(PropName_, PropType_, ...)
|
|
|
|
* \brief Register a hinted property.
|
2024-05-29 14:46:18 +00:00
|
|
|
*
|
|
|
|
* 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_)
|
2024-01-30 09:36:58 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDFUNCTION(FnName_)
|
|
|
|
* \brief Register a function CLASSNAME::FnName_() to godot.
|
|
|
|
*
|
2024-05-29 14:46:18 +00:00
|
|
|
* Requires setting CLASSNAME as a #define first.
|
|
|
|
*/
|
2024-05-29 21:06:39 +00:00
|
|
|
#define GDFUNCTION(FnName_) godot::ClassDB::bind_method(godot::D_METHOD(#FnName_), &CLASSNAME::FnName_)
|
2024-01-30 09:36:58 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDFUNCTION_ARGS(FnName_, ...)
|
|
|
|
* \brief Register a function CLASSNAME::FnName_(...) to godot.
|
|
|
|
*
|
2024-05-29 14:46:18 +00:00
|
|
|
* Requires setting CLASSNAME as a #define first.
|
|
|
|
*/
|
2024-05-29 21:06:39 +00:00
|
|
|
#define GDFUNCTION_ARGS(FnName_, ...) godot::ClassDB::bind_method(godot::D_METHOD(#FnName_, __VA_ARGS__), &CLASSNAME::FnName_)
|
2024-01-30 09:36:58 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDFUNCTION_STATIC(FnName_)
|
|
|
|
* \brief Register a static member function CLASSNAME::FnName_() to godot.
|
|
|
|
*
|
2024-05-29 14:46:18 +00:00
|
|
|
* Requires setting CLASSNAME as a #define first.
|
|
|
|
*/
|
2024-05-29 21:06:39 +00:00
|
|
|
#define GDFUNCTION_STATIC(FnName_) godot::ClassDB::bind_static_method(MACRO_STRING(CLASSNAME), godot::D_METHOD(#FnName_), &CLASSNAME::_FnName)
|
2024-01-30 09:36:58 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDFUNCTION_STATIC_ARGS(FnName_, ...)
|
|
|
|
* \brief Register a static member function CLASSNAME::FnName_(...) to godot.
|
|
|
|
*
|
2024-05-29 14:46:18 +00:00
|
|
|
* Requires setting CLASSNAME as a #define first.
|
|
|
|
*/
|
2024-05-29 21:06:39 +00:00
|
|
|
#define GDFUNCTION_STATIC_ARGS(FnName_, ...) godot::ClassDB::bind_static_method(MACRO_STRING(CLASSNAME), godot::D_METHOD(#FnName_, __VA_ARGS__), &CLASSNAME::FnName_)
|
2024-01-30 09:36:58 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDSIGNAL(...)
|
|
|
|
* \brief Declare a godot Observer signal.
|
2024-05-29 14:46:18 +00:00
|
|
|
*/
|
2024-05-28 14:28:36 +00:00
|
|
|
#define GDSIGNAL(...) godot::ClassDB::add_signal(MACRO_STRING(CLASSNAME), godot::MethodInfo(__VA_ARGS__))
|
2024-03-20 10:35:13 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDRESOURCETYPE(Class_)
|
|
|
|
* \brief Construct godot resource type hint.
|
2024-05-29 14:46:18 +00:00
|
|
|
*
|
|
|
|
* Use when registering properties of arrays of resource classes.
|
|
|
|
*/
|
2024-05-29 21:06:39 +00:00
|
|
|
#define GDRESOURCETYPE(Class_) godot::vformat("%s/%s:%s", godot::Variant::OBJECT, godot::PROPERTY_HINT_RESOURCE_TYPE, #Class_)
|
2024-05-31 19:16:28 +00:00
|
|
|
#define GDENUMTYPE(EnumString_) godot::vformat("%s/%s:%s", godot::Variant::INT, godot::PROPERTY_HINT_ENUM, #EnumString_)
|
2024-01-30 09:36:58 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDEDITORONLY()
|
|
|
|
* \brief Execute the rest of the function only if currently running as editor.
|
2024-05-29 14:46:18 +00:00
|
|
|
*
|
|
|
|
* Useful for _ready, _enter/_exit, _process, etc. functions.
|
|
|
|
*/
|
2024-05-28 14:28:36 +00:00
|
|
|
#define GDEDITORONLY() if(!godot::Engine::get_singleton()->is_editor_hint()) return;
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDGAMEONLY()
|
|
|
|
* \brief Execute the rest of the function only if currently running as game.
|
2024-05-29 14:46:18 +00:00
|
|
|
*
|
|
|
|
* Useful for _ready, _enter/_exit, _process, etc. functions.
|
|
|
|
*/
|
2024-05-28 14:28:36 +00:00
|
|
|
#define GDGAMEONLY() if(godot::Engine::get_singleton()->is_editor_hint()) return;
|
2024-02-02 07:47:05 +00:00
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
/*! \def GDENUM(Name_, ...)
|
|
|
|
* \brief Declare a scoped enum struct.
|
2024-05-29 14:46:18 +00:00
|
|
|
*
|
|
|
|
* Declares a struct Name_ with an enum Value and a single variable and a get_property_hint() function.
|
|
|
|
*/
|
2024-05-29 21:06:39 +00:00
|
|
|
#define GDENUM(Name_, ...)\
|
|
|
|
struct Name_ {\
|
2024-05-29 14:46:18 +00:00
|
|
|
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} {}\
|
|
|
|
}
|
|
|
|
|
2024-05-29 21:06:39 +00:00
|
|
|
#endif // !UTILS_GODOT_MACROS_HPP
|