Skip to content

Commit b241725

Browse files
committed
Add builtins property setter/getter inlined into .pxd
1 parent 3c51eaf commit b241725

File tree

3 files changed

+54
-28
lines changed

3 files changed

+54
-28
lines changed

src/godot/builtins.macros.pxd.j2

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#########################################################################
2+
# Render method
3+
#########################################################################
4+
5+
16
{% macro render_method_signature(spec, m) -%}
27
{{ "void" if m.return_type.is_nil else m.return_type.cy_type }} {{ m.name }}(self
38
{%- for arg in m.arguments -%}
@@ -53,3 +58,43 @@ cpdef inline {{ render_method_signature(spec, m) }}:
5358
{% endif %}
5459
{% endif %}
5560
{% endmacro %}
61+
62+
63+
#########################################################################
64+
# Render member
65+
#########################################################################
66+
67+
68+
{% macro render_member_getter_signature(spec, m) -%}
69+
{{ m.type.cy_type }} get_{{ m.name }}(self)
70+
{%- endmacro %}
71+
72+
73+
{% macro render_member_setter_signature(spec, m) -%}
74+
void set_{{ m.name }}(self, {{ m.type.cy_type }} val)
75+
{%- endmacro %}
76+
77+
78+
{% macro render_member(spec, m) %}
79+
{% if not m.is_in_struct %}
80+
{# Property unrelated to the builtin internal structure, must use Godot API method to access it #}
81+
{% if m.type.is_scalar %}
82+
{# Scalar type accessed by property (unused in extension_api.json afaik) #}
83+
cdef inline {{ render_member_getter_signature(spec, m) }}:
84+
cdef {{ m.type.c_type }} ret
85+
__{{ spec.name }}_get_{{ m.name }}(&self._gd_data, &ret)
86+
return ret
87+
cdef inline {{ render_member_setter_signature(spec,m )}}:
88+
__{{ spec.name }}_set_{{ m.name }}(&self._gd_data, &val)
89+
{% else %}
90+
{# Builtin type in the C structure (i.e. `Rect2i.end`) #}
91+
cdef inline {{ render_member_getter_signature(spec, m) }}:
92+
# Call to __new__ bypasses __init__ constructor
93+
cdef {{ m.type.cy_type }} ret = {{ m.type.cy_type }}.__new__({{ m.type.cy_type }})
94+
__{{ spec.name }}_get_{{ m.name }}(&self._gd_data, &ret._gd_data)
95+
return ret
96+
cdef inline {{ render_member_setter_signature(spec,m )}}:
97+
__{{ spec.name }}_set_{{ m.name }}(&self._gd_data, &val._gd_data)
98+
{% endif %}
99+
{% endif %}
100+
{% endmacro %}

src/godot/builtins.pxd.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{%- from 'builtins.macros.pxd.j2' import render_method -%}
1+
{%- from 'builtins.macros.pxd.j2' import render_method, render_member -%}
22
cimport cython
33

44
cdef extern from "Python.h":
@@ -46,6 +46,9 @@ cdef class {{ spec.name }}:
4646
cdef inline gd_string_t to_pystr(self):
4747
return gdstring_to_pystr(&self._gd_data)
4848
{% endif %}
49+
{% for m in spec.members %}
50+
{{ render_member(spec, m) | indent }}
51+
{% endfor %}
4952
{% for m in spec.methods %}
5053
{{ render_method(spec, m) | indent }}
5154
{% endfor %}

src/godot/builtins_pyx/class.pxi.j2

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,14 @@ def {{ name_prefix }}{{ m.name }}(self, {{ m.type.cy_type }} val):
5656

5757

5858
{% macro render_member(spec, m) %}
59-
6059
{% if not m.is_in_struct %}
6160
{# Property unrelated to the builtin internal structure, must use Godot API method to access it #}
62-
63-
{% if m.type.is_scalar %}
64-
65-
{# Scalar type accessed by property (unused in extension_api.json afaik) #}
66-
@property
67-
def {{ m.name }}(self) -> {{ m.type.py_type }}:
68-
cdef {{ m.type.c_type }} r_value
69-
__{{ spec.name }}_get_{{ m.name }}(&self._gd_data, &r_value)
70-
return r_value
71-
@{{ m.name }}.setter
72-
def {{ m.name }}(self, {{ m.type.cy_type }} val):
73-
__{{ spec.name }}_set_{{ m.name }}(&self._gd_data, &val)
74-
75-
{% else %}
76-
77-
{# Builtin type in the C structure (i.e. `Rect2i.end`) #}
7861
@property
79-
def {{ m.name }}(self) -> {{ m.type.py_type }}:
80-
# Call to __new__ bypasses __init__ constructor
81-
cdef {{ m.type.cy_type }} ret = {{ m.type.cy_type }}.__new__({{ m.type.cy_type }})
82-
__{{ spec.name }}_get_{{ m.name }}(&self._gd_data, &ret._gd_data)
83-
return ret
62+
def {{ m.name }}(self):
63+
return {{ spec.name }}.get_{{ m.name }}(self)
8464
@{{ m.name }}.setter
85-
def {{ m.name }}(self, {{ m.type.cy_type }} val):
86-
__{{ spec.name }}_set_{{ m.name }}(&self._gd_data, &val._gd_data)
87-
{% endif %}
88-
65+
def {{ m.name }}(self, {{ m.type.cy_type }} val{{ "" if m.type.is_scalar else " not None"}}):
66+
{{ spec.name }}.set_{{ m.name }}(self, val)
8967
{% else %}
9068
{# Property that is contains in the builtin C structure #}
9169

@@ -104,7 +82,7 @@ def {{ m.name }}(self, {{ m.type.cy_type }} val):
10482

10583
{# Also expose the member, but as a functions so it's clearer this works on copies #}
10684
{% for subm in m.type.builtin_spec.c_struct_members %}
107-
def get_{{ m.name }}(self) -> {{ m.type.py_type }}:
85+
def get_{{ m.name }}(self):
10886
{# Transparent builtins are guaranteed to have `is_stack_only == True` #}
10987
# Call to __new__ bypasses __init__ constructor
11088
cdef {{ m.type.cy_type }} ret = {{ m.type.cy_type }}.__new__({{ m.type.cy_type }})

0 commit comments

Comments
 (0)