Passed
Push — design ( fcd95c )
by Matt
02:23
created

PyDMXControl.design._screen.Screen.__events()   A

Complexity

Conditions 4

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 ceil  # Rounding
9
from threading import Thread  # Threading
10
11
import pygame  # PyGame
12
13
14
class Screen:
15
16
    def __init__(self):
17
        self.__thread = None
18
        self.__running = False
19
        self.__grid = True
20
        self.__parts = []
21
        self.block_size = 6
22
23
        # Init pygame
24
        pygame.init()
25
26
        # Create pygame screen
27
        display_info = pygame.display.Info()
28
        self.screen = pygame.display.set_mode((display_info.current_w - 50, display_info.current_h - 100))
29
30
    def add_part(self, part: 'Part'):
31
        self.__parts.append(part)
32
33
    def __events(self):
34
        for e in pygame.event.get():
35
            if e.type == pygame.KEYDOWN:
36
                if e.key == pygame.K_g:
37
                    self.__grid = not self.__grid
38
39
    def __draw_parts(self):
40
        for part in self.__parts:
41
            try:
42
                part.design_render(self)
43
            except Exception as e:
44
                print(e)
45
46
    def __start(self):
47
        self.clock = pygame.time.Clock()
48
49
        while self.__running:
50
            # Handle events
51
            self.__events()
52
53
            # Clear screen
54
            self.screen.fill((255, 255, 255))
55
56
            # Draw grid
57
            if self.__grid:
58
                w, h = pygame.display.get_surface().get_size()
59
                w = ceil(w / self.block_size)
60
                h = ceil(h / self.block_size)
61
                for y in range(h):
62
                    for x in range(w):
63
                        invert = ((x + (y % 2)) % 2 == 0)
64
                        color = [180] * 3 if invert else [240] * 3
65
                        rect = pygame.Rect(x * self.block_size, y * self.block_size, self.block_size, self.block_size)
66
                        pygame.draw.rect(self.screen, color, rect)
67
68
            # Draw parts
69
            self.__draw_parts()
70
71
            # Update display
72
            self.clock.tick(60)
73
            pygame.display.flip()
74
75
    def run(self):
76
        self.__running = True
77
        self.__start()
78
        quit()
79