Esempio n. 1
0
class ColorChannel(Struct):
    _base_data = get_default("color_channel")
    _dump = _color_dump
    _convert = _color_convert
    _collect = _color_collect

    def __init__(self, special_directive: str = None, **properties) -> None:
        super().__init__(**properties)

        if special_directive is not None:
            self.set_id(special_directive)

    def __hash__(self) -> int:
        return hash(self.id)

    def __eq__(self, other: Struct) -> bool:
        if not isinstance(other, type(self)):
            return NotImplemented
        return self.data == other.data

    def set_id(self, directive: str) -> ColorChannel:
        """Set ColorID of ``self`` according to the directive, e.g. ``BG`` or ``color:bg``."""
        self.edit(id=get_id(_get_dir(directive, "color")))
        return self

    def set_color(self, color: Any) -> ColorChannel:
        """Set ``rgb`` of ``self`` to ``color``."""
        self.edit(**dict(zip("rgb", _define_color(color).to_rgb())))
        return self

    def get_color(self) -> Color:
        """Attempt to get color of ``self``."""
        return _make_color(self)

    exec(_color_code)
Esempio n. 2
0
class LevelAPI(Struct):
    _convert = None
    _collect = None
    _dump = _level_dump
    _base_data = get_default("api")

    def __init__(self, **properties) -> None:
        super().__init__(**properties)

    def __repr__(self) -> str:
        info = {
            "id": self.id,
            "version": self.version,
            "name": self.name,
        }
        return make_repr(self, info)

    def dump(self) -> None:
        raise EditorError("Level API can not be dumped.")

    def open_editor(self) -> Editor:
        from gd.api.editor import Editor  # *circular imports*

        return Editor.launch(self, "level_string")

    def is_verified(self) -> bool:
        return bool(self.verified)

    def is_uploaded(self) -> bool:
        return bool(self.uploaded)

    def is_original(self) -> bool:
        return bool(self.original)

    def is_unlisted(self) -> bool:
        return bool(self.unlisted)

    @classmethod
    def from_mapping(cls, mapping) -> LevelAPI:
        self = cls()
        self.data = _process_level(mapping)
        return self

    @classmethod
    def from_string(cls, string: str) -> None:
        raise EditorError("Level API can not be created from string.")

    exec(_level_code)
Esempio n. 3
0
class Header(Struct):
    _base_data = get_default("header")
    _dump = _header_dump
    _convert = _header_convert
    _collect = _header_collect

    def __init__(self, **properties) -> None:
        super().__init__(**properties)
        self.colorhook()

    def copy(self) -> Header:
        copy = super().copy()

        if self.colors:
            copy.edit(colors=self.copy_colors())

        return copy

    def copy_colors(self) -> ColorCollection:
        if not self.colors:
            return ColorCollection()
        return ColorCollection(color.copy() for color in self.colors)

    def colorhook(self) -> None:
        if not self.colors:
            self.colors = ColorCollection.create(DEFAULT_COLORS)
        else:
            self.colors = ColorCollection.create(self.colors)

    @classmethod
    def from_mapping(cls, mapping) -> Header:
        self = super().from_mapping(mapping)
        self.colorhook()
        return self

    def dump(self) -> str:
        header = self.copy()

        if self.colors:
            header.edit(colors=self.colors.dump())

        return super(type(header), header).dump()

    exec(_header_code)
Esempio n. 4
0
 def __init__(self, main: str = "", levels: str = "") -> None:
     self.main = Part(main, get_default("main"))
     self.levels = Part(levels, get_default("levels"))
Esempio n. 5
0
class Object(Struct):
    _base_data = get_default("object")
    _dump = _object_dump
    _convert = _object_convert
    _collect = _object_collect

    def set_id(self, directive: str) -> Object:
        """Set ``id`` of ``self`` according to the directive, e.g. ``trigger:move``."""
        self.edit(id=get_id(directive))
        return self

    def set_z_layer(self, directive: str) -> Object:
        """Set ``z_layer`` of ``self`` according to the directive, e.g. ``layer:t1`` or ``b3``."""
        self.edit(z_layer=get_id(_get_dir(directive, "layer"), ret_enum=True))
        return self

    def set_easing(self, directive: str) -> Object:
        """Set ``easing`` of ``self`` according to the directive, e.g. ``sine_in_out``."""
        self.edit(easing=get_id(_get_dir(directive, "easing"), ret_enum=True))
        return self

    def set_color(self, color: Any) -> Object:
        """Set ``rgb`` of ``self`` to ``color``."""
        self.edit(**zip("rgb", _define_color(color).to_rgb()))
        return self

    def get_color(self) -> Color:
        """Attempt to get color of ``self``."""
        return _make_color(self)

    def add_groups(self, *groups: Iterable[int]) -> Object:
        """Add ``groups`` to ``self.groups``."""
        if self.groups is None:
            self.groups = set(groups)

        else:
            self.groups.update(groups)

        return self

    def get_pos(self) -> Tuple[Number, Number]:
        """Tuple[Union[:class:`float`, :class:`int`], Union[:class:`float`, :class:`int`]]:
        ``x`` and ``y`` coordinates of ``self``.
        """
        return (self.x, self.y)

    def set_pos(self, x: Number, y: Number) -> Object:
        """Set ``x`` and ``y`` position of ``self`` to given values."""
        self.x, self.y = x, y
        return self

    def move(self, x: Number = 0, y: Number = 0) -> Object:
        """Add ``x`` and ``y`` to coordinates of ``self``."""
        self.x += x
        self.y += y
        return self

    def rotate(self, deg: Number = 0) -> Object:
        """Add ``deg`` to ``rotation`` of ``self``."""
        if self.rotation is None:
            self.rotation = deg
        else:
            self.rotation += deg

        return self

    def is_checked(self):
        """:class:`bool`: indicates if ``self.portal_checked`` is true."""
        return bool(self.portal_checked)

    exec(_object_code)