Passed
Push — design ( fcd95c...f28e51 )
by Matt
01:17
created

PyDMXControl.design.parts._Text.Text.__init__()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 8
dl 0
loc 10
rs 9.9
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
"""
2
 *  PyDMXControl: A Python 3 module to control DMX using uDMX.
3
 *                Featuring fixture profiles, built-in effects and a web control panel.
4
 *  <https://github.com/MattIPv4/PyDMXControl/>
5
 *  Copyright (C) 2018 Matt Cowley (MattIPv4) ([email protected])
6
"""
7
8
from math import floor
9
from typing import Union, List, Tuple
10
11
import pygame
12
13
from ._Part import Part
14
15
16
class Text(Part):
17
18
    def __init__(self, x: int, y: int, text: str, *,
19
                 scale: float = 1, color: Union[List[int], Tuple[int]] = (0, 0, 0),
20
                 background_color: Union[List[int], Tuple[int], None] = (255, 255, 255, 190)):
21
        super().__init__()
22
        self.__text = text
23
        self.__scale = scale
24
        self.__color = color
25
        self.__bg_color = background_color
26
        self.__font = pygame.font.SysFont("monospace", int(26 * self.__scale), bold=True)
27
        self.set_pos(x, y)
28
29
    def design_render(self, screen: 'Screen') -> Tuple[int, int, pygame.Surface]:
30
        # Generate text
31
        text = self.__font.render(self.__text, True, self.__color)
32
33
        # Resize text
34
        tx, ty = text.get_size()
35
        text = pygame.transform.scale(text, (
36
            int(tx * screen.block_size * 0.15),
37
            int(ty * screen.block_size * 0.15)
38
        ))
39
40
        # Generate background
41
        if self.__bg_color is not None:
42
            padding = screen.block_size * 0.25
43
            tx, ty = text.get_size()
44
            surface = pygame.Surface((int(tx + padding * 2), int(ty + padding * 2)), pygame.SRCALPHA, 32)
45
            surface = surface.convert_alpha()
46
            surface.fill(self.__bg_color)
47
            surface.blit(text, (padding, padding))
48
        else:
49
            surface = text
50
51
        # Render
52
        x, y = surface.get_size()
53
        x = int((self._x * screen.block_size) - floor(x / 2))
54
        y = int((self._y * screen.block_size) - floor(y / 2))
55
        return x, y, surface
56