Пример #1
0
def get_shadow(target_img, shadow_radius=2, black=255, color_format="RGBA",
                alpha_factor=0.85, decay_mode="exponential", color=(0,0,0),
                sun_angle=30., vertical=True, angle_mode="flip", mode_value=(False, True)):
        r = target_img.get_rect()
        #the shadow will be larger in order to make free space for fadeout.
        r.inflate_ip(2*shadow_radius, 2*shadow_radius)
        img = Surface(r.size)
        img.fill((255, 255, 255, 255))
        img.blit(target_img, (shadow_radius, shadow_radius))
        if sun_angle <= 0.:
            raise Exception("Sun angle must be greater than zero.")
        elif sun_angle != 45. and vertical:
            w, h = img.get_size()
            new_h = h / tan(sun_angle * pi / 180.)
            screen_size = functions.get_screen().get_size()
            new_h = abs(int(min(new_h, max(screen_size))))
            img = scale(img, (w, new_h))
        if angle_mode == "flip":
            img = flip(img, mode_value[0], mode_value[1])
        elif self.angle_mode == "rotate":
            img = rotate(img, mode_value)
        else:
            raise Exception("angle_mode not available: " + str(angle_mode))
        shadow =             _pilshadow(img,
                                        radius=shadow_radius,
                                        black=black,
                                        alpha_factor=alpha_factor,
                                        decay_mode=decay_mode,
                                        color=color)
        #
        W, H = functions.get_screen_size()
        shadow.set_alpha(-1, RLEACCEL)
        return shadow.convert_alpha()
Пример #2
0
 def __init__(self,
              color=None,
              image=None,
              elements=None,
              normal_params=None,
              mode="scale to screen"):
     """Mode:
         None : if an image is passed, its original size is kept. Otherwise,
     a <color> (white by default) rect of the size of the screen is used as
     background image.
         'scale to screen' : if an image is passed, it is scaled to fit
     screen. Otherwise, see behaviour for None.
         'cut to screen' : if an image is passed, it is shrinked to fit
     screen. Otherwise, see behaviour for None.
     """
     super(Background, self).__init__("", elements, normal_params)
     W, H = functions.get_screen_size()
     if image:
         painter = ImageFrame(image, mode=mode)
     else:
         if color:
             painter = BasicFrame((W, H), color)
         else:
             painter = BasicFrame((W, H), (255, 255, 255))
     self.set_painter(painter)
Пример #3
0
 def __init__(self,
              img_path,
              alpha=255,
              colorkey=None,
              clip=None,
              pressed=False,
              mode=None,
              hovered=False):
     self.alpha = alpha
     self.img_path = img_path
     self.colorkey = colorkey
     self.mode = mode
     ##        print("type3",img_path)
     size = list(self.init_get_img().get_size())
     W, H = functions.get_screen_size()
     if self.mode == "cut":
         if W < size[0]:
             size[0] = W
         if H < size[1]:
             size[1] = H
     Painter.__init__(self,
                      size=size,
                      clip=clip,
                      pressed=pressed,
                      hovered=hovered)
     self._resized = False
Пример #4
0
    def get_surface(self):
        W, H = functions.get_screen_size()
        surface = self.get_image()
        if 0 < self.alpha < 255:
            surface.set_alpha(self.alpha, RLEACCEL)
        if self.mode == "scale to screen":
            surface = scale(surface, (W, H))
            self.size = (W, H)
        elif self.mode == "cut to screen":
            new_surface = Surface((W, H))
            new_surface.blit(surface, (0, 0))
            self.size = (W, H)
        elif self._resized:
            surface = scale(surface, self._resized)
        elif self.mode:
            functions.debug_msg("Unrecognized mode : ", self.mode)


##        elif self._resized:
##            surface = scale(surface, self._resized)
        if self.colorkey:
            surface.set_colorkey(self.colorkey, RLEACCEL)
        surface.set_clip(self.clip)
        if self.alpha < 255:
            return surface.convert_alpha()
        else:
            return surface.convert()
Пример #5
0
    def get_surface(self):
        W, H = functions.get_screen_size()
        if isinstance(self.img_path, str):  # load image
            surface = load_image(self.img_path)
        else:  # take image
            surface = self.img_path
        if 0 < self.alpha < 255:
            surface.set_alpha(self.alpha, RLEACCEL)
        if self.mode == "scale to screen":
            surface = scale(surface, (W, H))
            self.size = (W, H)
        elif self.mode == "cut to screen":
            new_surface = Surface((W, H))
            new_surface.blit(surface, (0, 0))
            self.size = (W, H)
        elif self._resized:
            surface = scale(surface, self._resized)
        elif self.mode:
            functions.debug_msg("Unrecognized mode : ", self.mode)
##        elif self._resized:
##            surface = scale(surface, self._resized)
        if self.colorkey:
            surface.set_colorkey(self.colorkey, RLEACCEL)
        surface.set_clip(self.clip)
        if self.alpha < 255:
            return surface.convert_alpha()
        else:
            return surface.convert()
 def __init__(self, color=None, image=None, elements=None,
              normal_params=None, mode="scale to screen", finish=True):
     """Background element for another element or menu.
     <color>: if not None, define the color for the background.
     <image>: if not None, define the image of the background.
     <Mode>:
         None : if an image is passed, its original size is kept. Otherwise,
             a <color> (white by default) rect of the size of the screen is
             used as background image.
         'scale to screen' : if an image is passed, it is scaled to fit
             screen. Otherwise, see behaviour for None.
         'cut to screen' : if an image is passed, it is shrinked to fit
             the screen. Otherwise, use behaviour for None.
     """
     super(Background, self).__init__("", elements, normal_params, finish=False)
     W, H = functions.get_screen_size()
     if image:
         painter = ImageFrame(image, mode=mode)
     else:
         if color:
             painter = BasicFrame((W, H), color)
         else:
             painter = BasicFrame((W, H), (255, 255, 255))
     self.set_painter(painter)
     if finish:
         self.finish()
Пример #7
0
    def stick_to(self, target, target_side, self_side, align=True):
        """Sides must be either 'top', 'bottom, 'left' or 'right'.
        This function moves self in order to make its <self_side> just next to
        target's <target_side>.

        Note that unless <align> = True, this does not move self along the
        orthogonal axis: e.g, stick_to(target_element, 'right', 'left') will
        move self such that self.left = target.right (using storers rects), but
        self.top might not be target.top. Then this is up to the user to move
        self on the vertical axis once self is sticked to target.
        """
        r = self.get_storer_rect()
        topleft = r.topleft
        size = r.size
        if target == "screen":
            W, H = functions.get_screen_size()
            t = Rect(0, 0, W, H)
        else:
            t = target.get_storer_rect()
        target_topleft = t.topleft
        target_size = t.size
        if target_side == "left":
            sx = topleft[0]
            tx = target_topleft[0]
            if self_side == "right":
                sx += size[0]
            self.move((tx - sx, 0))
        elif target_side == "right":
            sx = topleft[0]
            tx = target_topleft[0] + target_size[0]
            if self_side == "right":
                sx += size[0]
            self.move((tx - sx, 0))
        elif target_side == "left":
            sx = topleft[0]
            tx = target_topleft[0]
            if self_side == "right":
                sx += size[0]
            self.move((tx - sx, 0))
        elif target_side == "bottom":
            sy = topleft[1]
            ty = target_topleft[1] + target_size[1]
            if self_side == "bottom":
                sy += size[1]
            self.move((0, ty - sy))
        elif target_side == "top":
            sy = topleft[1]
            ty = target_topleft[1]
            if self_side == "bottom":
                sy += size[1]
            self.move((0, ty - sy))
        else:
            return
        if align:
            if target_side == "top" or target_side == "bottom":
                self.set_center((t.centerx, None))
            else:
                self.set_center((None, t.centery))
Пример #8
0
 def get_location(self, ref="topleft", state=constants.STATE_NORMAL):
     """Returns the element location relatively to the windows size.
     <ref> : Reference point, can be any 2D attribute of a pygame Rect.
     """
     rect = self.get_fus_rect()
     point = getattr(rect, ref)
     W, H = functions.get_screen_size()
     factor_x = float(point[0]) / W
     factor_y = float(point[1]) / H
     return (factor_x, factor_y)
Пример #9
0
 def get_location(self, ref="topleft", state=constants.STATE_NORMAL):
     """Returns the element location relatively to the windows size.
     <ref> : Reference point, can be any 2D attribute of a pygame Rect.
     """
     rect = self.get_fus_rect()
     point = getattr(rect, ref)
     W, H = functions.get_screen_size()
     factor_x = float(point[0]) / W
     factor_y = float(point[1]) / H
     return (factor_x, factor_y)
Пример #10
0
 def set_size(self, size):
     # define a way to resize (deform or cut)
     # refresh self.size
     W, H = functions.get_screen_size()
     if self.mode == "cut":
         if W < size[0]:
             size[0] = W
         if H < size[1]:
             size[1] = H
     Painter.set_size(self, size)
     self._resized = size
Пример #11
0
 def set_size(self, size):
     # define a way to resize (deform or cut)
     # refresh self.size
     W, H = functions.get_screen_size()
     if self.mode == "cut":
         if W < size[0]:
             size[0] = W
         if H < size[1]:
             size[1] = H
     Painter.set_size(self, size)
     self._resized = size
Пример #12
0
 def __init__(self, elements=None, normal_params=None, height=None):
     Element.__init__(self, "", elements, normal_params)
     h = max([e.get_storer_rect().height for e in self.get_elements()]) + 2
     store(self, mode="h", x=1, y=h / 2, align="center")
     if self.father:
         w = self.father.get_storer_rect().width
     else:
         w = functions.get_screen_size()[0]
     size = (w, h)
     painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                              pressed=True,
                                              size=size,
                                              radius=style.BOX_RADIUS)
     self.set_painter(painter)
Пример #13
0
 def __init__(self, elements=None, normal_params=None, height=None):
     Element.__init__(self, "", elements, normal_params)
     h = max([e.get_storer_rect().height for e in self.get_elements()]) + 2
     store(self, mode="h", x=1, y=h/2, align="center")
     if self.father:
         w = self.father.get_storer_rect().width
     else:
         w = functions.get_screen_size()[0]
     size = (w, h)
     painter = functions.obtain_valid_painter(painterstyle.BOX_PAINTER,
                                              pressed=True,
                                              size=size,
                                              radius=style.BOX_RADIUS)
     self.set_painter(painter)
Пример #14
0
 def __init__(self, img_path, alpha=255, colorkey=None, clip=None,
              pressed=False, mode=None, hovered=False):
     self.alpha = alpha
     self.img_path = img_path
     self.colorkey = colorkey
     self.mode = mode
     size = list(self.init_get_img().get_size())
     W, H = functions.get_screen_size()
     if self.mode == "cut":
         if W < size[0]:
             size[0] = W
         if H < size[1]:
             size[1] = H
     Painter.__init__(self, size=size, clip=clip, pressed=pressed,
                      hovered=hovered)
     self._resized = False
Пример #15
0
    def set_location(self, factors, func="set_topleft",
                     state=constants.STATE_NORMAL):
        """Set the element location relatively to the windows size.

        <factors> : A couple of number in the range [0,1] that represent the
                    x and y fraction of the screen where the element has to be
                    placed.
        <func> : If you want to set the topleft location, use 'set_topleft'
                 If you want to set the center location, use 'set_center'

        One could also use any other location-setting function that can be be
        called as func((x,y)).
        """
        W, H = functions.get_screen_size()
        x = W * factors[0]
        y = H * factors[1]
        getattr(self, func)((x, y))
Пример #16
0
    def set_location(self, factors, func="set_topleft",
                     state=constants.STATE_NORMAL):
        """Set the element location relatively to the windows size.

        <factors> : A couple of number in the range [0,1] that represent the
                    x and y fraction of the screen where the element has to be
                    placed.
        <func> : If you want to set the topleft location, use 'set_topleft'
                 If you want to set the center location, use 'set_center'

        One could also use any other location-setting function that can be be
        called as func((x,y)).
        """
        W, H = functions.get_screen_size()
        x = W * factors[0]
        y = H * factors[1]
        getattr(self, func)((x, y))
Пример #17
0
 def __init__(self, color=None, image=None, elements=None,
              normal_params=None, mode="scale to screen"):
     """Mode:
         None : if an image is passed, its original size is kept. Otherwise,
     a <color> (white by default) rect of the size of the screen is used as
     background image.
         'scale to screen' : if an image is passed, it is scaled to fit
     screen. Otherwise, see behaviour for None.
         'cut to screen' : if an image is passed, it is shrinked to fit
     screen. Otherwise, see behaviour for None.
     """
     super(Background, self).__init__("", elements, normal_params)
     W, H = functions.get_screen_size()
     if image:
         painter = ImageFrame(image, mode=mode)
     else:
         if color:
             painter = BasicFrame((W, H), color)
         else:
             painter = BasicFrame((W, H), (255, 255, 255))
     self.set_painter(painter)
Пример #18
0
def make_display_options_setter(fn,
                                const_text="",
                                sliders_length=100,
                                limvalsw=(400, None),
                                limvalsh=(400, None),
                                restart_app=True):
    from thorpy.miscgui.reaction import Reaction
    from thorpy.miscgui.metadata import MetaDataManager
    from thorpy.elements.launchers.paramsetterlauncher import ParamSetterLauncher
    from thorpy.miscgui.varset import VarSet
    import os, sys
    varset = VarSet()
    w, h = functions.get_screen_size()
    maxsize = functions.get_current_application().max_screen_size
    if limvalsw[1] is None: limvalsw = (limvalsw[0], maxsize[0])
    if limvalsh[1] is None: limvalsh = (limvalsh[0], maxsize[1])
    fullscreen = bool(functions.get_screen().get_flags() & pygame.FULLSCREEN)
    varset.add("screen_w",
               value=int(w),
               text="Screen width: ",
               limits=limvalsw)
    varset.add("screen_h",
               value=int(h),
               text="Screen height: ",
               limits=limvalsh)
    varset.add("fullscreen", value=fullscreen, text="Fullscreen")
    button = ParamSetterLauncher.make([varset],
                                      const_text,
                                      const_text,
                                      text_ok="Apply")

    def reac_func_norestart(event):
        if event.what == constants.LAUNCH_DONE:
            w, h = varset.get_value("screen_w"), varset.get_value("screen_h")
            mdm = MetaDataManager()
            mdm.read_data(fn)
            mdm.data["screen_w"] = w
            mdm.data["screen_h"] = h
            mdm.data["fullscreen"] = varset.get_value("fullscreen")
            ##            print("writing", mdm.data, fn)
            mdm.write_data(fn)
            #restart script
            flags = functions.get_screen().get_flags()
            if varset.get_value("fullscreen"):
                flags |= pygame.FULLSCREEN
            else:
                flags = 0
            pygame.display.set_mode((w, h), flags)
            functions.get_current_menu()._elements[0].get_oldest_ancester(
            ).unblit_and_reblit()
            button.launched.blit()
            button.launched.update()

    def reac_func_restart(event):
        if event.what == constants.LAUNCH_DONE:
            w, h = varset.get_value("screen_w"), varset.get_value("screen_h")
            mdm = MetaDataManager()
            mdm.read_data(fn)
            mdm.data["screen_w"] = w
            mdm.data["screen_h"] = h
            mdm.data["fullscreen"] = varset.get_value("fullscreen")
            ##            print("writing", mdm.data, fn)
            mdm.write_data(fn)
            #restart script
            python = sys.executable
            os.execl(python, python, *sys.argv)

    reac_func = reac_func_restart if restart_app else reac_func_norestart
    reac = Reaction(constants.THORPY_EVENT, reac_func, {
        "id": constants.EVENT_UNLAUNCH,
        "launcher": button.launcher
    })
    button.add_reaction(reac)
    return button
Пример #19
0
def make_display_options_setter(fn, const_text="",
                                sliders_length=100,
                                limvalsw=(400,None),
                                limvalsh=(400,None),
                                restart_app=True):
    from thorpy.miscgui.reaction import Reaction
    from thorpy.miscgui.metadata import MetaDataManager
    from thorpy.elements.launchers.paramsetterlauncher import ParamSetterLauncher
    from thorpy.miscgui.varset import VarSet
    import os, sys
    varset = VarSet()
    w,h = functions.get_screen_size()
    maxsize = functions.get_current_application().max_screen_size
    if limvalsw[1] is None: limvalsw = (limvalsw[0], maxsize[0])
    if limvalsh[1] is None: limvalsh = (limvalsh[0], maxsize[1])
    fullscreen = bool(functions.get_screen().get_flags()&pygame.FULLSCREEN)
    varset.add("screen_w", value=int(w), text="Screen width: ", limits=limvalsw)
    varset.add("screen_h", value=int(h), text="Screen height: ", limits=limvalsh)
    varset.add("fullscreen", value=fullscreen, text="Fullscreen")
    button = ParamSetterLauncher.make([varset], const_text, const_text,
                                      text_ok="Apply")
    def reac_func_norestart(event):
        if event.what == constants.LAUNCH_DONE:
            w,h = varset.get_value("screen_w"), varset.get_value("screen_h")
            mdm = MetaDataManager()
            mdm.read_data(fn)
            mdm.data["screen_w"] = w
            mdm.data["screen_h"] = h
            mdm.data["fullscreen"] = varset.get_value("fullscreen")
##            print("writing", mdm.data, fn)
            mdm.write_data(fn)
            #restart script
            flags = functions.get_screen().get_flags()
            if varset.get_value("fullscreen"):
                flags |= pygame.FULLSCREEN
            else:
                flags = 0
            pygame.display.set_mode((w,h), flags)
            functions.get_current_menu()._elements[0].get_oldest_ancester().unblit_and_reblit()
            button.launched.blit()
            button.launched.update()

    def reac_func_restart(event):
        if event.what == constants.LAUNCH_DONE:
            w,h = varset.get_value("screen_w"), varset.get_value("screen_h")
            mdm = MetaDataManager()
            mdm.read_data(fn)
            mdm.data["screen_w"] = w
            mdm.data["screen_h"] = h
            mdm.data["fullscreen"] = varset.get_value("fullscreen")
##            print("writing", mdm.data, fn)
            mdm.write_data(fn)
            #restart script
            python = sys.executable
            os.execl(python, python, * sys.argv)

    reac_func=reac_func_restart if restart_app else reac_func_norestart
    reac = Reaction(constants.THORPY_EVENT, reac_func, {"id":constants.EVENT_UNLAUNCH,
                                                        "launcher":button.launcher})
    button.add_reaction(reac)
    return button
Пример #20
0
    def stick_to(self, target, target_side, self_side, align=True):
        """Sides must be either 'top', 'bottom, 'left' or 'right'.
        This function moves self in order to make its <self_side> just next to
        target's <target_side>.

        Note that unless <align> = True, this does not move self along the
        orthogonal axis: e.g, stick_to(target_element, 'right', 'left') will
        move self such that self.left = target.right (using storers rects), but
        self.top might not be target.top. Then this is up to the user to move
        self on the vertical axis once self is sticked to target.

        <target> can either be an element, "screen" or a rect.
        """
        r = self.get_storer_rect()
        topleft = r.topleft
        size = r.size
        if target == "screen":
            W, H = functions.get_screen_size()
            t = Rect(0, 0, W, H)
        elif isinstance(target, Rect):
            t = target
        else:
            t = target.get_storer_rect()
        target_topleft = t.topleft
        target_size = t.size
        if target_side == "left":
            sx = topleft[0]
            tx = target_topleft[0]
            if self_side == "right":
                sx += size[0]
            self.move((tx - sx, 0))
        elif target_side == "right":
            sx = topleft[0]
            tx = target_topleft[0] + target_size[0]
            if self_side == "right":
                sx += size[0]
            self.move((tx - sx, 0))
        elif target_side == "left":
            sx = topleft[0]
            tx = target_topleft[0]
            if self_side == "right":
                sx += size[0]
            self.move((tx - sx, 0))
        elif target_side == "bottom":
            sy = topleft[1]
            ty = target_topleft[1] + target_size[1]
            if self_side == "bottom":
                sy += size[1]
            self.move((0, ty - sy))
        elif target_side == "top":
            sy = topleft[1]
            ty = target_topleft[1]
            if self_side == "bottom":
                sy += size[1]
            self.move((0, ty - sy))
        else:
            raise Exception("not possible")
        if align:
            if target_side == "top" or target_side == "bottom":
                self.set_center((t.centerx, None))
            else:
                self.set_center((None, t.centery))