Exemple #1
0
class LoadIcon:
    """
    A spinning icon.
    """
    def __init__(self, mainScreen, x=0.0, y=0.0):
        self.main_screen = mainScreen
        self._image = OnscreenImage(
            image=self.main_screen.image_path + "load_icon.png",
            pos=(x, 0, y),
            parent=self.main_screen.gameEngine.render2d,
            scale=(0.07, 1, 0.07))
        self._image.setTransparency(TransparencyAttrib.MAlpha)
        self.spin_task = None
        self._image.hide()

    def set_pos(self, x, y):
        """
        Sets the position of the icon
        @param x: the relative x in the screen
        @param y: the relative y in the screen
        """
        self._image.set_r(0)
        self._image.set_pos(x, 0, y)

    def start(self):
        """
        Starts the spinning animation and shows it.
        """
        self._image.set_r(0)
        self._image.show()
        self.spin_task = self._image.hprInterval(duration=2, hpr=(0, 0, 360))
        self.spin_task.loop()

    def stop(self):
        """
        Stops the spinning animation and hides it.
        @return:
        """
        self.spin_task.finish()
        self._image.hide()
Exemple #2
0
class Sprite(RPObject):

    """ Simple wrapper arround OnscreenImage, providing a simpler interface """

    def __init__(self, image=None, parent=None, x=0, y=0, w=None, h=None,
                 transparent=True, near_filter=True, any_filter=True):
        """ Creates a new image, taking (x,y) as topleft coordinates.

        When near_filter is set to true, a near filter will be set to the
        texture passed. This provides sharper images.

        When any_filter is set to false, the passed image won't be modified at
        all. This enables you to display existing textures, otherwise the
        texture would get a near filter in the 3D View, too. """

        RPObject.__init__(self)

        if not isinstance(image, Texture):
            if not isinstance(image, str):
                self.warn("Invalid argument to image parameter:", image)
                return
            image = RPLoader.load_texture(image)

            if w is None or h is None:
                w, h = image.get_x_size(), image.get_y_size()
        else:
            if w is None or h is None:
                w = 10
                h = 10

        self._width, self._height = w, h
        self._initial_pos = self._translate_pos(x, y)

        self.node = OnscreenImage(
            image=image, parent=parent, pos=self._initial_pos,
            scale=(self._width / 2.0, 1, self._height / 2.0))

        if transparent:
            self.node.set_transparency(TransparencyAttrib.M_alpha)

        tex = self.node.get_texture()

        # Apply a near filter, but only if the parent has no scale, otherwise
        # it will look weird
        if near_filter and any_filter and parent.get_sx() == 1.0:
            tex.set_minfilter(SamplerState.FT_nearest)
            tex.set_magfilter(SamplerState.FT_nearest)

        if any_filter:
            tex.set_anisotropic_degree(8)
            tex.set_wrap_u(SamplerState.WM_clamp)
            tex.set_wrap_v(SamplerState.WM_clamp)

    def get_initial_pos(self):
        """ Returns the initial position of the image. This can be used for
        animations """
        return self._initial_pos

    def pos_interval(self, *args, **kwargs):
        """ Returns a pos interval, this is a wrapper around
        NodePath.posInterval """
        return self.node.posInterval(*args, **kwargs)

    def hpr_interval(self, *args, **kwargs):
        """ Returns a hpr interval, this is a wrapper around
        NodePath.hprInterval """
        return self.node.hprInterval(*args, **kwargs)

    def color_scale_interval(self, *args, **kwargs):
        """ Returns a color scale interval, this is a wrapper around
        NodePath.colorScaleInterval """
        return self.node.colorScaleInterval(*args, **kwargs)

    def set_image(self, img):
        """ Sets the current image """
        self.node.set_image(img)

    def get_width(self):
        """ Returns the width of the image in pixels """
        return self._width

    def get_height(self):
        """ Returns the height of the image in pixels """
        return self._height

    def set_pos(self, x, y):
        """ Sets the position """
        self.node.set_pos(self._translate_pos(x, y))

    def _translate_pos(self, x, y):
        """ Converts 2d coordinates to pandas coordinate system """
        return Vec3(x + self._width / 2.0, 1, -y - self._height / 2.0)

    def set_shader(self, shader):
        """ Sets a shader to be used for rendering the image """
        self.node.set_shader(shader)

    def set_shader_input(self, *args):
        """ Sets a shader input on the image """
        self.node.set_shader_input(*args)

    def remove(self):
        """ Removes the image """
        self.node.remove()

    def hide(self):
        """ Hides the image """
        self.node.hide()

    def show(self):
        """ Shows the image if it was previously hidden """
        self.node.show()
class BetterOnscreenImage(DebugObject):

    """ Simple wrapper arroun OnscreenImage, providing a simpler interface and
    better visuals """

    def __init__(self, image=None, parent=None, x=0, y=0, w=10, h=10,
                 transparent=True, nearFilter=True, anyFilter=True):
        """ Creates a new image, taking (x,y) as topleft coordinates.

        When nearFilter is set to true, a near filter will be set to the
        texture passed. This provides sharper images.

        When anyFilter is set to false, the passed image won't be modified at
        all. This enables you to display existing textures, otherwise the
        texture would get a near filter in the 3D View, too. """

        DebugObject.__init__(self, "BetterOnscreenImage")

        self.w, self.h = w, h
        self.initialPos = self.translatePos(x, y)

        self._node = OnscreenImage(
            image=image, parent=parent, pos=self.initialPos,
            scale=(w / 2.0, 1, h / 2.0))

        if transparent:
            self._node.setTransparency(TransparencyAttrib.MAlpha)

        tex = self._node.getTexture()

        if nearFilter and anyFilter:
            tex.setMinfilter(Texture.FTNearest)
            tex.setMagfilter(Texture.FTNearest)

        if anyFilter:
            tex.setAnisotropicDegree(8)
            tex.setWrapU(Texture.WMClamp)
            tex.setWrapV(Texture.WMClamp)

    def getInitialPos(self):
        """ Returns the initial position of the image. This can be used for
        animations """
        return self.initialPos

    def posInterval(self, *args, **kwargs):
        """ Returns a pos interval, this is a wrapper around
        NodePath.posInterval """
        return self._node.posInterval(*args, **kwargs)

    def hprInterval(self, *args, **kwargs):
        """ Returns a hpr interval, this is a wrapper around
        NodePath.hprInterval """
        return self._node.hprInterval(*args, **kwargs)

    def setImage(self, img):
        """ Sets the current image """
        self._node.setImage(img)

    def setPos(self, x, y):
        """ Sets the position """
        self.initialPos = self.translatePos(x, y)
        self._node.setPos(self.initialPos)

    def translatePos(self, x, y):
        """ Converts 2d coordinates to pandas coordinate system """
        return Vec3(x + self.w / 2.0, 1, -y - self.h / 2.0)

    def remove(self):
        self._node.remove()

    def hide(self):
        self._node.hide()

    def show(self):
        self._node.show()
Exemple #4
0
class Sprite(RPObject):
    """ Simple wrapper arround OnscreenImage, providing a simpler interface """
    def __init__(self,
                 image=None,
                 parent=None,
                 x=0,
                 y=0,
                 w=None,
                 h=None,
                 transparent=True,
                 near_filter=True,
                 any_filter=True):
        """ Creates a new image, taking (x,y) as topleft coordinates.

        When near_filter is set to true, a near filter will be set to the
        texture passed. This provides sharper images.

        When any_filter is set to false, the passed image won't be modified at
        all. This enables you to display existing textures, otherwise the
        texture would get a near filter in the 3D View, too. """

        RPObject.__init__(self)

        if not isinstance(image, Texture):
            if not isinstance(image, str):
                self.warn("Invalid argument to image parameter:", image)
                return
            image = RPLoader.load_texture(image)

            if w is None or h is None:
                w, h = image.get_x_size(), image.get_y_size()
        else:
            if w is None or h is None:
                w = 10
                h = 10

        self._width, self._height = w, h
        self._initial_pos = self._translate_pos(x, y)

        self.node = OnscreenImage(image=image,
                                  parent=parent,
                                  pos=self._initial_pos,
                                  scale=(self._width / 2.0, 1,
                                         self._height / 2.0))

        if transparent:
            self.node.set_transparency(TransparencyAttrib.M_alpha)

        tex = self.node.get_texture()

        # Apply a near filter, but only if the parent has no scale, otherwise
        # it will look weird
        if near_filter and any_filter and parent.get_sx() == 1.0:
            tex.set_minfilter(SamplerState.FT_nearest)
            tex.set_magfilter(SamplerState.FT_nearest)

        if any_filter:
            tex.set_anisotropic_degree(8)
            tex.set_wrap_u(SamplerState.WM_clamp)
            tex.set_wrap_v(SamplerState.WM_clamp)

    def get_initial_pos(self):
        """ Returns the initial position of the image. This can be used for
        animations """
        return self._initial_pos

    def pos_interval(self, *args, **kwargs):
        """ Returns a pos interval, this is a wrapper around
        NodePath.posInterval """
        return self.node.posInterval(*args, **kwargs)

    def hpr_interval(self, *args, **kwargs):
        """ Returns a hpr interval, this is a wrapper around
        NodePath.hprInterval """
        return self.node.hprInterval(*args, **kwargs)

    def color_scale_interval(self, *args, **kwargs):
        """ Returns a color scale interval, this is a wrapper around
        NodePath.colorScaleInterval """
        return self.node.colorScaleInterval(*args, **kwargs)

    def set_image(self, img):
        """ Sets the current image """
        self.node.set_image(img)

    def get_width(self):
        """ Returns the width of the image in pixels """
        return self._width

    def get_height(self):
        """ Returns the height of the image in pixels """
        return self._height

    def set_pos(self, x, y):
        """ Sets the position """
        self.node.set_pos(self._translate_pos(x, y))

    def _translate_pos(self, x, y):
        """ Converts 2d coordinates to pandas coordinate system """
        return Vec3(x + self._width / 2.0, 1, -y - self._height / 2.0)

    def set_shader(self, shader):
        """ Sets a shader to be used for rendering the image """
        self.node.set_shader(shader)

    def set_shader_input(self, *args):
        """ Sets a shader input on the image """
        self.node.set_shader_input(*args)

    def remove(self):
        """ Removes the image """
        self.node.remove()

    def hide(self):
        """ Hides the image """
        self.node.hide()

    def show(self):
        """ Shows the image if it was previously hidden """
        self.node.show()
class BetterOnscreenImage(DebugObject):
    """ Simple wrapper arroun OnscreenImage, providing a simpler interface and
    better visuals """
    def __init__(self,
                 image=None,
                 parent=None,
                 x=0,
                 y=0,
                 w=10,
                 h=10,
                 transparent=True,
                 nearFilter=True,
                 anyFilter=True):
        """ Creates a new image, taking (x,y) as topleft coordinates.

        When nearFilter is set to true, a near filter will be set to the
        texture passed. This provides sharper images.

        When anyFilter is set to false, the passed image won't be modified at
        all. This enables you to display existing textures, otherwise the
        texture would get a near filter in the 3D View, too. """

        DebugObject.__init__(self, "BetterOnscreenImage")

        self.w, self.h = w, h
        self.initialPos = self.translatePos(x, y)

        self._node = OnscreenImage(image=image,
                                   parent=parent,
                                   pos=self.initialPos,
                                   scale=(w / 2.0, 1, h / 2.0))

        if transparent:
            self._node.setTransparency(TransparencyAttrib.MAlpha)

        tex = self._node.getTexture()

        if nearFilter and anyFilter:
            tex.setMinfilter(Texture.FTNearest)
            tex.setMagfilter(Texture.FTNearest)

        if anyFilter:
            tex.setAnisotropicDegree(8)
            tex.setWrapU(Texture.WMClamp)
            tex.setWrapV(Texture.WMClamp)

    def getInitialPos(self):
        """ Returns the initial position of the image. This can be used for
        animations """
        return self.initialPos

    def posInterval(self, *args, **kwargs):
        """ Returns a pos interval, this is a wrapper around
        NodePath.posInterval """
        return self._node.posInterval(*args, **kwargs)

    def hprInterval(self, *args, **kwargs):
        """ Returns a hpr interval, this is a wrapper around
        NodePath.hprInterval """
        return self._node.hprInterval(*args, **kwargs)

    def setImage(self, img):
        """ Sets the current image """
        self._node.setImage(img)

    def setPos(self, x, y):
        """ Sets the position """
        self.initialPos = self.translatePos(x, y)
        self._node.setPos(self.initialPos)

    def translatePos(self, x, y):
        """ Converts 2d coordinates to pandas coordinate system """
        return Vec3(x + self.w / 2.0, 1, -y - self.h / 2.0)

    def remove(self):
        self._node.remove()

    def hide(self):
        self._node.hide()

    def show(self):
        self._node.show()