|
| 1 | +{% macro render_class(cls) %} |
| 2 | + |
| 3 | +{% if cls.inherits is none %} |
| 4 | +from cpython.object cimport PyObject_GenericGetAttr, PyObject_GenericSetAttr |
| 5 | +{% endif %} |
| 6 | + |
| 7 | +cdef class {{ cls.cy_type }}({{ cls.inherits.cy_type if cls.inherits else "" }}): |
| 8 | +{% if cls.inherits is none %} |
| 9 | + |
| 10 | + {# @staticmethod |
| 11 | + cpdef inline {{ cls.cy_type}} new(): |
| 12 | + raise NotImplementedError("TODO :'(") #} |
| 13 | + |
| 14 | + def free(self): |
| 15 | + pythonscript_gdextension.object_destroy(self._gd_ptr) |
| 16 | + self._gd_ptr = NULL |
| 17 | + |
| 18 | + def __init__(self): |
| 19 | + raise RuntimeError( |
| 20 | + f"Use `new()` method to instantiate non-refcounted Godot object (and don't forget to free it !)" |
| 21 | + ) |
| 22 | + |
| 23 | + def __repr__(self): |
| 24 | + return f"<{type(self).__name__} wrapper on 0x{<size_t>self._gd_ptr:x}>" |
| 25 | + |
| 26 | + def _set_gd_ptr(self, ptr): |
| 27 | + # /!\ doing `<GDExtensionObjectPtr>ptr` would return the address of |
| 28 | + # the PyObject instead of casting it value ! |
| 29 | + self._gd_ptr = <GDExtensionObjectPtr><size_t>ptr |
| 30 | + # Note if the object is a reference, we stole it from the caller given we |
| 31 | + # don't call `Reference.reference` here |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def _from_ptr(cls, ptr): |
| 35 | + cdef object wrapper = cls.__new__(cls) |
| 36 | + wrapper._set_gd_ptr(ptr) |
| 37 | + return wrapper |
| 38 | + |
| 39 | +{# |
| 40 | + @staticmethod |
| 41 | + cdef inline Object cast_from_variant(const godot_variant *p_gdvar): |
| 42 | + cdef godot_object *ptr = gdapi10.godot_variant_as_object(p_gdvar) |
| 43 | + # Retreive class |
| 44 | + cdef GDString classname = GDString.__new__(GDString) |
| 45 | + with nogil: |
| 46 | + gdapi10.godot_method_bind_ptrcall( |
| 47 | + __methbind__Object__get_class, |
| 48 | + ptr, |
| 49 | + NULL, |
| 50 | + &classname._gd_data |
| 51 | + ) |
| 52 | + return globals()[str(classname)]._from_ptr(<size_t>ptr) |
| 53 | +
|
| 54 | + @staticmethod |
| 55 | + cdef inline Object cast_from_ptr(godot_object *ptr): |
| 56 | + # Retreive class |
| 57 | + cdef GDString classname = GDString.__new__(GDString) |
| 58 | + with nogil: |
| 59 | + gdapi10.godot_method_bind_ptrcall( |
| 60 | + __methbind__Object__get_class, |
| 61 | + ptr, |
| 62 | + NULL, |
| 63 | + &classname._gd_data |
| 64 | + ) |
| 65 | + return globals()[str(classname)]._from_ptr(<size_t>ptr) |
| 66 | +#} |
| 67 | + |
| 68 | + def __eq__(self, other): |
| 69 | + try: |
| 70 | + return self._gd_ptr == (<{{ cls.cy_type }}>other)._gd_ptr |
| 71 | + except TypeError: |
| 72 | + return False |
| 73 | + |
| 74 | + def __ne__(self, other): |
| 75 | + try: |
| 76 | + return self._gd_ptr != (<{{ cls.cy_type }}>other)._gd_ptr |
| 77 | + except TypeError: |
| 78 | + return True |
| 79 | + |
| 80 | + def __getattr__(self, name): |
| 81 | + cdef GDString gdname = GDString(name) |
| 82 | + cdef GDString gdnamefield = GDString("name") |
| 83 | + |
| 84 | + # If a script is attached to the object, we expose here it methods |
| 85 | + if not hasattr(type(self), '__exposed_python_class'): |
| 86 | + if self.has_method(name): |
| 87 | + |
| 88 | + def _call(*args): |
| 89 | + return {{ cls.cy_type }}.callv(self, gdname, GDArray(args)) |
| 90 | + |
| 91 | + return _call |
| 92 | + # from functools import partial |
| 93 | + # return partial(self.call, gdname) |
| 94 | + |
| 95 | + elif any(x for x in self.get_property_list() if x[gdnamefield] == gdname): |
| 96 | + # TODO: Godot currently lacks a `has_property` method |
| 97 | + return self.get(gdname) |
| 98 | + |
| 99 | + raise AttributeError( |
| 100 | + f"`{type(self).__name__}` object has no attribute `{name}`" |
| 101 | + ) |
| 102 | + |
| 103 | + def __setattr__(self, name, value): |
| 104 | + cdef GDString gdname = GDString(name) |
| 105 | + cdef GDString gdnamefield = GDString("name") |
| 106 | + |
| 107 | + if hasattr(type(self), '__exposed_python_class'): |
| 108 | + PyObject_GenericSetAttr(self, name, value) |
| 109 | + return |
| 110 | + |
| 111 | + # Could retrieve the item inside the Godot class, try to look into |
| 112 | + # the attached script if it has one |
| 113 | + else: |
| 114 | + if any(x for x in self.get_property_list() if x[gdnamefield] == gdname): |
| 115 | + # TODO: Godot currently lacks a `has_property` method |
| 116 | + self.set(name, value) |
| 117 | + return |
| 118 | + |
| 119 | + raise AttributeError( |
| 120 | + f"`{type(self).__name__}` object has no attribute `{name}`" |
| 121 | + ) |
| 122 | + |
| 123 | + def call(self, name, *args): |
| 124 | + return self.callv(name, GDArray(args)) |
| 125 | +{% else %} |
| 126 | + pass |
| 127 | +{% endif %} |
| 128 | + |
| 129 | + @staticmethod |
| 130 | + cdef {{ cls.cy_type }} from_ptr(gd_object_t ptr): |
| 131 | + # Call to __new__ bypasses __init__ constructor |
| 132 | + cdef {{ cls.cy_type }} wrapper = {{ cls.cy_type }}.__new__({{ cls.cy_type }}) |
| 133 | + wrapper._gd_ptr = ptr |
| 134 | + # Note if the object is a reference, we stole it from the caller given we |
| 135 | + # don't call `Reference.reference` here |
| 136 | + return wrapper |
| 137 | + |
| 138 | +{% endmacro %} |
0 commit comments