|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from typing import Tuple |
| 3 | + |
| 4 | + |
| 5 | +@dataclass |
| 6 | +class Mode: |
| 7 | + width: int |
| 8 | + height: int |
| 9 | + freq: float |
| 10 | + |
| 11 | + def __repr__(self): |
| 12 | + return "%dx%d@%.2fHz" % (self.width, self.height, self.freq) |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class Screen: |
| 17 | + uid: str |
| 18 | + name: str |
| 19 | + active: bool = False |
| 20 | + position: Tuple[int, int] = (0, 0) |
| 21 | + mode: None | Mode = None |
| 22 | + scale: float = 1 |
| 23 | + available: list[Mode] = field(default_factory=list) |
| 24 | + transform: int = 0 |
| 25 | + |
| 26 | + def __repr__(self): |
| 27 | + return "<Screen{} {} [{}]>".format("*" if self.active else "", self.name, self.mode) |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class Rect: # {{{ |
| 32 | + x: int |
| 33 | + y: int |
| 34 | + width: int |
| 35 | + height: int |
| 36 | + |
| 37 | + def __hash__(self): |
| 38 | + return int("%d%d%d%d" % self.asTuple()) |
| 39 | + |
| 40 | + @property |
| 41 | + def topleft(self): |
| 42 | + return (self.left, self.top) |
| 43 | + |
| 44 | + @property |
| 45 | + def topright(self): |
| 46 | + return (self.right, self.top) |
| 47 | + |
| 48 | + @property |
| 49 | + def bottomleft(self): |
| 50 | + return (self.left, self.bottom) |
| 51 | + |
| 52 | + @property |
| 53 | + def bottomright(self): |
| 54 | + return (self.right, self.bottom) |
| 55 | + |
| 56 | + @property |
| 57 | + def left(self): |
| 58 | + return self.x |
| 59 | + |
| 60 | + @property |
| 61 | + def right(self): |
| 62 | + return self.x + self.width |
| 63 | + |
| 64 | + @property |
| 65 | + def top(self): |
| 66 | + return self.y + self.height |
| 67 | + |
| 68 | + @property |
| 69 | + def bottom(self): |
| 70 | + return self.y |
| 71 | + |
| 72 | + @property |
| 73 | + def center(self): |
| 74 | + return self.x + self.width // 2, self.y + self.height // 2 |
| 75 | + |
| 76 | + def contains(self, x, y): |
| 77 | + return collidepoint(self.x, self.y, self.x + self.width, self.y + self.height, x, y) |
| 78 | + |
| 79 | + def collide(self, rect): |
| 80 | + # return true if the two rectangles are overlapping in any way |
| 81 | + if rect.left >= self.right: |
| 82 | + return False |
| 83 | + if rect.right <= self.left: |
| 84 | + return False |
| 85 | + if rect.top <= self.bottom: |
| 86 | + return False |
| 87 | + return not rect.bottom >= self.top |
| 88 | + |
| 89 | + def copy(self): |
| 90 | + return Rect(self.x, self.y, self.width, self.height) |
| 91 | + |
| 92 | + def asTuple(self): |
| 93 | + return (self.x, self.y, self.width, self.height) |
| 94 | + |
| 95 | + def scaled(self, factor): |
| 96 | + return Rect( |
| 97 | + self.x * factor, |
| 98 | + self.y * factor, |
| 99 | + self.width * factor, |
| 100 | + self.height * factor, |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +# }}} |
| 105 | + |
| 106 | + |
| 107 | +def collidepoint(x: float, y: float, x2: float, y2: float, xp: float, yp: float): |
| 108 | + """Return True if the point is inside the rectangle.""" |
| 109 | + assert x < x2 |
| 110 | + assert y < y2 |
| 111 | + return x < xp < x2 and y < yp < y2 |
0 commit comments