Beispiel #1
0
    def _render(self):
        if not self._render_hash_changed(
                self._rect.size, self._slider_rect.x, self._slider_rect.y,
                self._slider_rect.width, self._slider_rect.height):
            return

        self._surface = make_surface(*self._rect.size)
        self._surface.fill(self._page_ctrl_color)

        # Render slider
        if self._shadow:
            lit_rect = pygame.Rect(self._slider_rect)
            slider_rect = lit_rect.inflate(-self._shadow_offset * 2,
                                           -self._shadow_offset * 2)
            shadow_rect = lit_rect.inflate(-self._shadow_offset,
                                           -self._shadow_offset)
            shadow_rect = shadow_rect.move(self._shadow_tuple[0] / 2,
                                           self._shadow_tuple[1] / 2)

            pygame.draw.rect(self._surface, self._font_color, lit_rect)
            pygame.draw.rect(self._surface, self._shadow_color, shadow_rect)
            pygame.draw.rect(self._surface, self._slider_color, slider_rect)
        else:
            pygame.draw.rect(self._surface, self._slider_color,
                             self._slider_rect)
Beispiel #2
0
def make_world(width, height):
    """
    Create a test surface.

    :param width: Width in pixels
    :param height: Height in pixels
    :return: World surface
    """
    world = make_surface(width, height)
    world.fill((200, 200, 200))

    color = [70, 20, 20]
    maxx = len(list(range(100, width, 200)))
    maxy = len(list(range(100, height, 200)))
    numberx = 0
    for x in range(100, width, 200):
        numbery = 0
        for y in range(100, height, 200):
            if numberx in (0, maxx - 1) or numbery in (0, maxy - 1):
                # White circles to delimit world boundaries
                pygame.draw.circle(world, (255, 255, 255), (x, y), 100, 10)
            else:
                pygame.draw.circle(world, color, (x, y), 100, 10)
                if color[0] + 15 < 255:
                    color[0] += 15
                elif color[1] + 15 < 255:
                    color[1] += 15
                else:
                    color[2] += 15
            numbery += 1
        numberx += 1

    return world
Beispiel #3
0
    def _previsualize_color(self, surface):
        """
        Changes the color of the previsualization box.

        :param surface: Surface to draw
        :type surface: pygame.surface.Surface, None
        :return: None
        """
        r, g, b = self.get_value()
        if r == -1 or g == -1 or b == -1:  # Remove previsualization if invalid color
            self._previsualization_surface = None
            return

        # If previsualization surface is None or the color changed
        if self._last_r != r or self._last_b != b or self._last_g != g or self._previsualization_surface is None:
            _width = self._prev_size * self._rect.height
            if _width == 0 or self._rect.height == 0:
                self._previsualization_surface = None
                return
            self._previsualization_surface = make_surface(
                _width, self._rect.height)
            self._previsualization_surface.fill((r, g, b))
            self._last_r = r
            self._last_g = g
            self._last_b = b
            _posx = self._rect.x + self._rect.width - self._prev_size * self._rect.height + self._rect.height / 10
            _posy = self._rect.y - 1
            self._previsualization_position = (_posx, _posy)

        # Draw the surface
        if surface is not None:
            surface.blit(self._previsualization_surface,
                         self._previsualization_position)
Beispiel #4
0
    def _render_string(self, string, color):
        """
        Render text and turn it into a surface.

        :param string: Text to render
        :type string: basestring
        :param color: Text color
        :type color: tuple
        :return: Text surface
        :rtype: pygame.surface.SurfaceType
        """
        text = self.font_render_string(string, color)

        # Create surface
        surface = make_surface(text.get_width(), text.get_height(), alpha=True)

        # Draw shadow first
        if self._shadow:
            text_bg = self._font.render(string, self._font_antialias,
                                        self._shadow_color)
            surface.blit(text_bg, self._shadow_tuple)

        surface.blit(text, (0, 0))
        new_width = surface.get_size()[0]
        new_height = surface.get_size()[1]

        if self._max_width is not None and new_width > self._max_width:
            surface = pygame.transform.smoothscale(
                surface, (self._max_width, new_height))

        return surface
Beispiel #5
0
    def draw(self, surface):
        self._render()

        if self._bgcolor:
            bg = make_surface(self._width, self._rect.height + 5)
            bg.fill(self._bgcolor)
            surface.blit(bg, self._rect.topleft)

        gfxdraw.filled_polygon(surface, self._polygon_pos, self._font_color)

        if self.mouse_enabled and self._backbox:
            pygame.draw.rect(surface, self._font_selected_color,
                             self._backbox_rect, 1)
            pygame.draw.polygon(surface, self._font_selected_color,
                                self._backbox_pos)

        surface.blit(self._surface, (5 + self._rect.topleft[0] + self._offsetx,
                                     self._rect.topleft[1] + self._offsety))
Beispiel #6
0
def make_world(width, height, text=''):
    """
    Create a test surface.

    :param width: Width in pixels
    :type width: int
    :param height: Height in pixels
    :type height: int
    :param text: Text to write
    :type: basestring
    :return: World surface
    """
    world = make_surface(width, height)
    world.fill((210, 210, 210))
    font = pygame.font.SysFont('arial', 20)

    posy = 60
    for line in text.splitlines():
        text = font.render(str(line), True, (0, 0, 0))
        world.blit(text, (60, posy))
        posy += text.get_height() + 10

    for x in range(0, width, 10):
        if x % 100 == 0 and x != 0:
            pygame.draw.line(world, (255, 0, 0), (x, 0), (x, 20))
            pygame.draw.line(world, (180, 180, 180), (x, 80), (x, height))
            tick = font.render(str(x), True, (255, 0, 0))
            world.blit(tick, (x - tick.get_width() / 2, 25))
        else:
            pygame.draw.line(world, (255, 0, 0), (x, 0), (x, 10))
    for y in range(0, height, 10):
        if y % 100 == 0 and y != 0:
            pygame.draw.line(world, (255, 0, 0), (0, y), (20, y))
            pygame.draw.line(world, (180, 180, 180), (80, y), (width, y))
            tick = font.render(str(y), True, (255, 0, 0))
            world.blit(tick, (25, y - tick.get_height() / 2))
        else:
            pygame.draw.line(world, (255, 0, 0), (0, y), (10, y))

    return world
Beispiel #7
0
    def __init__(
        self,
        area_width,
        area_height,
        area_color=None,
        scrollbar_color=(235, 235, 235),
        scrollbar_slider_color=(200, 200, 200),
        scrollbar_slider_pad=0,
        scrollbar_thick=20,
        scrollbars=(_locals.POSITION_SOUTH, _locals.POSITION_EAST),
        shadow=False,
        shadow_color=(0, 0, 0),
        shadow_offset=2,
        shadow_position=_locals.POSITION_SOUTHEAST,
        world=None,
    ):
        assert isinstance(area_width, (int, float))
        assert isinstance(area_height, (int, float))
        assert isinstance(scrollbar_slider_pad, (int, float))
        assert isinstance(scrollbar_thick, (int, float))
        assert isinstance(shadow, bool)
        assert isinstance(shadow_offset, (int, float))

        assert_color(scrollbar_color)
        assert_color(scrollbar_slider_color)
        assert_color(shadow_color)
        assert_position(shadow_position)

        self._rect = pygame.Rect(0.0, 0.0, area_width, area_height)
        self._world = world
        self._scrollbars = []
        self._scrollbar_positions = tuple(set(scrollbars))  # Ensure unique
        self._scrollbar_thick = scrollbar_thick
        self._bg_surface = None

        if area_color:
            self._bg_surface = make_surface(area_width, area_height)
            self._bg_surface.fill(area_color)

        self._view_rect = self.get_view_rect()

        for pos in self._scrollbar_positions:  # type:str
            assert_position(pos)
            if pos == _locals.POSITION_EAST or pos == _locals.POSITION_WEST:
                sbar = ScrollBar(self._view_rect.height,
                                 (0, max(1, self.get_hidden_height())),
                                 orientation=_locals.ORIENTATION_VERTICAL,
                                 slider_pad=scrollbar_slider_pad,
                                 slider_color=scrollbar_slider_color,
                                 page_ctrl_thick=scrollbar_thick,
                                 page_ctrl_color=scrollbar_color,
                                 onchange=self._on_vertical_scroll)
            else:
                sbar = ScrollBar(self._view_rect.width,
                                 (0, max(1, self.get_hidden_width())),
                                 slider_pad=scrollbar_slider_pad,
                                 slider_color=scrollbar_slider_color,
                                 page_ctrl_thick=scrollbar_thick,
                                 page_ctrl_color=scrollbar_color,
                                 onchange=self._on_horizontal_scroll)
            sbar.set_shadow(enabled=shadow,
                            color=shadow_color,
                            position=shadow_position,
                            offset=shadow_offset)
            sbar.set_controls(joystick=False)

            self._scrollbars.append(sbar)

        self._apply_size_changes()
Beispiel #8
0
 def _render(self):
     if self._surface is not None:
         return
     self._surface = make_surface(1, 1, alpha=True)
     self._rect.width = 0.0
     self._rect.height = 0.0