Beispiel #1
0
 def __init__(self, items=()):
     if not items:
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new(self._gd_ptr)
     elif isinstance(items, Array):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new_copy(self._gd_ptr, items._gd_ptr)
     elif isinstance(items, PoolColorArray):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new_pool_color_array(self._gd_ptr, items._gd_ptr)
     elif isinstance(items, PoolVector3Array):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new_pool_vector3_array(self._gd_ptr, items._gd_ptr)
     elif isinstance(items, PoolVector2Array):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new_pool_vector2_array(self._gd_ptr, items._gd_ptr)
     elif isinstance(items, PoolStringArray):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new_pool_string_array(self._gd_ptr, items._gd_ptr)
     elif isinstance(items, PoolRealArray):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new_pool_real_array(self._gd_ptr, items._gd_ptr)
     elif isinstance(items, PoolIntArray):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new_pool_int_array(self._gd_ptr, items._gd_ptr)
     elif isinstance(items, PoolByteArray):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new_pool_byte_array(self._gd_ptr, items._gd_ptr)
     elif hasattr(items, '__iter__') and not isinstance(items, (str, bytes)):
         self._gd_ptr = godot_array_alloc()
         lib.godot_array_new(self._gd_ptr)
         for x in items:
             self.append(x)
     else:
         raise TypeError('Param `items` should be of type `Array` or `Pool*Array`')
Beispiel #2
0
 def get_class_properties(cls, classname):
     properties = []
     ret = godot_array_alloc()
     lib.godot_array_new(ret)
     gd_classname = godot_string_from_pyobj(classname)
     gd_true = godot_bool_alloc(True)
     args = ffi.new("void*[2]", [gd_classname, gd_true])
     # 2nd arg should be false, which what we get by not initializing it
     lib.godot_method_bind_ptrcall(cls._meth_get_property_list,
                                   cls._instance, args, ret)
     for i in range(lib.godot_array_size(ret)):
         var = lib.godot_array_get(ret, i)
         gddict = lib.godot_variant_as_dictionary(ffi.addressof(var))
         propdict = Dictionary.build_from_gdobj(gddict)
         properties.append(propdict)
     return properties
Beispiel #3
0
def _build_script_manifest(cls):
    def _build_signal_info(signal):
        methinfo = Dictionary()
        methinfo["name"] = signal.name
        # Dummy data, only name is important here
        methinfo["args"] = Array()
        methinfo["default_args"] = Array()
        methinfo["return"] = None
        methinfo["flags"] = lib.METHOD_FLAG_FROM_SCRIPT
        return methinfo

    def _build_method_info(meth, methname):
        spec = inspect.getfullargspec(meth)
        methinfo = Dictionary()
        methinfo["name"] = methname
        # TODO: Handle classmethod/staticmethod
        methinfo["args"] = Array(spec.args)
        methinfo["default_args"] = Array()  # TODO
        # TODO: use annotation to determine return type ?
        methinfo["return"] = None
        methinfo["flags"] = lib.METHOD_FLAG_FROM_SCRIPT
        methinfo["rpc_mode"] = getattr(meth, "__rpc",
                                       lib.GODOT_METHOD_RPC_MODE_DISABLED)
        return methinfo

    def _build_property_info(prop):
        propinfo = Dictionary()
        propinfo["name"] = prop.name
        propinfo["type"] = py_to_gd_type(prop.type)
        propinfo["hint"] = prop.hint
        propinfo["hint_string"] = prop.hint_string
        propinfo["usage"] = prop.usage
        propinfo["default_value"] = prop.default
        propinfo["rset_mode"] = prop.rpc
        return propinfo

    manifest = ffi.new("godot_pluginscript_script_manifest*")
    manifest.data = connect_handle(cls)
    # TODO: should be able to use lib.godot_string_new_with_wide_string directly
    gdname = godot_string_from_pyobj(cls.__name__)
    lib.godot_string_name_new(ffi.addressof(manifest.name), gdname)
    if cls.__bases__:
        # Only one Godot parent class (checked at class definition time)
        godot_parent_class = next(
            (b for b in cls.__bases__ if issubclass(b, BaseObject)))
        if godot_parent_class.__dict__.get("__is_godot_native_class"):
            path = godot_parent_class.__name__
        else:
            # Pluginscript wants us to return the parent as a path
            path = "res://%s.py" % "/".join(
                cls.__bases__[0].__module__.split("."))
        gdbase = godot_string_from_pyobj(path)
        lib.godot_string_name_new(ffi.addressof(manifest.base), gdbase)
    manifest.is_tool = cls.__tool
    lib.godot_dictionary_new(ffi.addressof(manifest.member_lines))
    lib.godot_array_new(ffi.addressof(manifest.methods))
    methods = Array()
    # TODO: include inherited in exposed methods ? Expose Godot base class' ones ?
    # for methname in vars(cls):
    for methname in dir(cls):
        meth = getattr(cls, methname)
        if not inspect.isfunction(meth) or meth.__name__.startswith("__"):
            continue

        methinfo = _build_method_info(meth, methname)
        methods.append(methinfo)

    signals = Array()
    for signal in cls.__signals.values():
        signalinfo = _build_signal_info(signal)
        signals.append(signalinfo)

    properties = Array()
    for prop in cls.__exported.values():
        property_info = _build_property_info(prop)
        properties.append(property_info)

    lib.godot_array_new_copy(ffi.addressof(manifest.methods), methods._gd_ptr)
    lib.godot_array_new_copy(ffi.addressof(manifest.signals), signals._gd_ptr)
    lib.godot_array_new_copy(ffi.addressof(manifest.properties),
                             properties._gd_ptr)
    return manifest
def _build_script_manifest(cls):
    from godot.bindings import Dictionary, Array

    def _build_signal_info(signal):
        methinfo = Dictionary()
        methinfo['name'] = signal.name
        # Dummy data, only name is important here
        methinfo['args'] = Array()
        methinfo['default_args'] = Array()
        methinfo['return'] = None
        methinfo['flags'] = lib.METHOD_FLAG_FROM_SCRIPT
        return methinfo

    def _build_method_info(meth, methname):
        spec = inspect.getfullargspec(meth)
        methinfo = Dictionary()
        methinfo['name'] = methname
        # TODO: Handle classmethod/staticmethod
        methinfo['args'] = Array(spec.args)
        methinfo['default_args'] = Array()  # TODO
        # TODO: use annotation to determine return type ?
        methinfo['return'] = None
        methinfo['flags'] = lib.METHOD_FLAG_FROM_SCRIPT
        methinfo['rpc_mode'] = getattr(meth, '__rpc',
                                       lib.GODOT_METHOD_RPC_MODE_DISABLED)
        return methinfo

    def _build_property_info(prop):
        propinfo = Dictionary()
        propinfo['name'] = prop.name
        propinfo['type'] = py_to_gd_type(prop.type)
        propinfo['hint'] = prop.hint
        propinfo['hint_string'] = prop.hint_string
        propinfo['usage'] = prop.usage
        propinfo['default_value'] = prop.default
        propinfo['rset_mode'] = prop.rpc
        return propinfo

    manifest = ffi.new('godot_pluginscript_script_manifest*')
    manifest.data = connect_handle(cls)
    gdname = godot_string_from_pyobj(cls.__name__)
    lib.godot_string_name_new(ffi.addressof(manifest.name), gdname)
    if cls.__bases__ and issubclass(cls.__bases__[0], BaseObject):
        gdbase = godot_string_from_pyobj(cls.__bases__[0].__name__)
        lib.godot_string_name_new(ffi.addressof(manifest.base), gdbase)
    manifest.is_tool = cls.__tool
    lib.godot_dictionary_new(ffi.addressof(manifest.member_lines))
    lib.godot_array_new(ffi.addressof(manifest.methods))
    methods = Array()
    # TODO: include inherited in exposed methods ? Expose Godot base class' ones ?
    # for methname in vars(cls):
    for methname in dir(cls):
        meth = getattr(cls, methname)
        if not inspect.isfunction(meth) or meth.__name__.startswith('__'):
            continue
        methinfo = _build_method_info(meth, methname)
        methods.append(methinfo)

    signals = Array()
    for signal in cls.__signals.values():
        signalinfo = _build_signal_info(signal)
        signals.append(signalinfo)

    properties = Array()
    for prop in cls.__exported.values():
        property_info = _build_property_info(prop)
        properties.append(property_info)

    lib.godot_array_new_copy(ffi.addressof(manifest.methods), methods._gd_ptr)
    lib.godot_array_new_copy(ffi.addressof(manifest.signals), signals._gd_ptr)
    lib.godot_array_new_copy(ffi.addressof(manifest.properties),
                             properties._gd_ptr)
    return manifest