summaryrefslogtreecommitdiff
path: root/src/sliceitoff/display/scaling.py
blob: b4292b40f75fb9976072aa9909b1e1c6da03b773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
""" display.scaling - for converting internal resolution to actual screen """

import pygame

from .static import INTERNAL_WIDTH, INTERNAL_HEIGHT

class Scaling():
    """ Holds data and methods needed for coordinate conversion """
    factor = 0.02
    left = 0
    top = 0
    resolution = (0,0)
    center = (0,0)
    borders = (pygame.Rect(0,0,0,0), pygame.Rect(0,0,0,0))
    active = pygame.Rect(0,0,0,0)

    @staticmethod
    def area_to_rect(area: tuple) -> pygame.Rect:
        """ converts area coordinates to pygame.Rect"""
        return pygame.Rect(
                area[0] * __class__.factor + __class__.left,
                area[1] * __class__.factor + __class__.top,
                area[2] * __class__.factor,
                area[3] * __class__.factor)

    @staticmethod
    def scale_to_display(coords: tuple ) -> tuple:
        """ Converts internal coordinates to display coodinates """
        return (
                coords[0] * __class__.factor + __class__.left,
                coords[1] * __class__.factor + __class__.top)

    @staticmethod
    def scale_to_internal(coords: tuple ) -> tuple:
        """ Converts display coordinates to internal coodinates """
        x = coords[0] - __class__.left
        x = max(x, 0) // __class__.factor
        x = min(x, INTERNAL_WIDTH - 1)
        y = coords[1] - __class__.top
        y = max(y, 0) // __class__.factor
        y = min(y, INTERNAL_HEIGHT - 1)
        return (x, y)

    @staticmethod
    def update_scaling(size: tuple) -> None:
        """ Calculates new scaling and positionin according given
        actual resolution """
        __class__.resolution = size
        __class__.center = (size[0]/2,size[1]/2)
        if size[0] / size[1] <= INTERNAL_WIDTH / INTERNAL_HEIGHT:
            __class__.factor = size[0] / INTERNAL_WIDTH
            __class__.left = 0
            __class__.top = (size[1] - INTERNAL_HEIGHT * __class__.factor) // 2
            __class__.borders = (
                    pygame.Rect(
                            0,
                            0,
                            size[0],
                            __class__.top),
                    pygame.Rect(
                            0,
                            size[1] - __class__.top,
                            size[0],
                            __class__.top),
                    )
        else:
            __class__.factor = size[1] / INTERNAL_HEIGHT
            __class__.left = (size[0] - INTERNAL_WIDTH * __class__.factor) // 2
            __class__.top = 0
            __class__.borders = (
                    pygame.Rect(
                            0,
                            0,
                            __class__.left,
                            size[1]),
                    pygame.Rect(
                            size[0] - __class__.left,
                            0,
                            __class__.left,
                            size[1]),
                    )
        __class__.active = pygame.Rect(
                __class__.left,
                __class__.top,
                INTERNAL_WIDTH * __class__.factor,
                INTERNAL_HEIGHT * __class__.factor)