]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blob - displayio/_area.py
3eba700590bb7bb03249aad980ad2d9bc1f53275
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / _area.py
1 # SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
2 # SPDX-FileCopyrightText: 2021 James Carr
3 #
4 # SPDX-License-Identifier: MIT
5
6 """
7 `displayio._area`
8 ================================================================================
9
10 Area for Blinka Displayio
11
12 **Software and Dependencies:**
13
14 * Adafruit Blinka:
15   https://github.com/adafruit/Adafruit_Blinka/releases
16
17 * Author(s): James Carr, Melissa LeBlanc-Williams
18
19 """
20
21 from __future__ import annotations
22
23 __version__ = "0.0.0+auto.0"
24 __repo__ = "https://github.com/adafruit/Adafruit_Blinka_Displayio.git"
25
26
27 class Area:
28     # pylint: disable=invalid-name,missing-function-docstring
29     """Area Class to represent an area to be updated. Currently not used."""
30
31     def __init__(self, x1: int = 0, y1: int = 0, x2: int = 0, y2: int = 0):
32         self.x1 = x1
33         self.y1 = y1
34         self.x2 = x2
35         self.y2 = y2
36         self.next = None
37
38     def __str__(self):
39         return f"Area TL({self.x1},{self.y1}) BR({self.x2},{self.y2})"
40
41     def _copy_into(self, dst) -> None:
42         dst.x1 = self.x1
43         dst.y1 = self.y1
44         dst.x2 = self.x2
45         dst.y2 = self.y2
46
47     def _scale(self, scale: int) -> None:
48         self.x1 *= scale
49         self.y1 *= scale
50         self.x2 *= scale
51         self.y2 *= scale
52
53     def _shift(self, dx: int, dy: int) -> None:
54         self.x1 += dx
55         self.y1 += dy
56         self.x2 += dx
57         self.y2 += dy
58
59     def _compute_overlap(self, other, overlap) -> bool:
60         a = self
61         overlap.x1 = max(a.x1, other.x1)
62         overlap.x2 = min(a.x2, other.x2)
63
64         if overlap.x1 >= overlap.x2:
65             return False
66
67         overlap.y1 = max(a.y1, other.y1)
68         overlap.y2 = min(a.y2, other.y2)
69
70         return overlap.y1 < overlap.y2
71
72     def _empty(self):
73         return (self.x1 == self.x2) or (self.y1 == self.y2)
74
75     def _canon(self):
76         if self.x1 > self.x2:
77             self.x1, self.x2 = self.x2, self.x1
78         if self.y1 > self.y2:
79             self.y1, self.y2 = self.y2, self.y1
80
81     def _union(self, other, union):
82         # pylint: disable=protected-access
83         if self._empty():
84             self._copy_into(union)
85             return
86         if other._empty():
87             other._copy_into(union)
88             return
89
90         union.x1 = min(self.x1, other.x1)
91         union.y1 = min(self.y1, other.y1)
92         union.x2 = max(self.x2, other.x2)
93         union.y2 = max(self.y2, other.y2)
94
95     def width(self) -> int:
96         return self.x2 - self.x1
97
98     def height(self) -> int:
99         return self.y2 - self.y1
100
101     def size(self) -> int:
102         return self.width() * self.height()
103
104     def __eq__(self, other):
105         if not isinstance(other, Area):
106             return False
107
108         return (
109             self.x1 == other.x1
110             and self.y1 == other.y1
111             and self.x2 == other.x2
112             and self.y2 == other.y2
113         )
114
115     @staticmethod
116     def _transform_within(
117         mirror_x: bool,
118         mirror_y: bool,
119         transpose_xy: bool,
120         original: Area,
121         whole: Area,
122         transformed: Area,
123     ):
124         # pylint: disable=too-many-arguments
125         # Original and whole must be in the same coordinate space.
126         if mirror_x:
127             transformed.x1 = whole.x1 + (whole.x2 - original.x2)
128             transformed.x2 = whole.x2 - (original.x1 - whole.x1)
129         else:
130             transformed.x1 = original.x1
131             transformed.x2 = original.x2
132
133         if mirror_y:
134             transformed.y1 = whole.y1 + (whole.y2 - original.y2)
135             transformed.y2 = whole.y2 - (original.y1 - whole.y1)
136         else:
137             transformed.y1 = original.y1
138             transformed.y2 = original.y2
139
140         if transpose_xy:
141             y1 = transformed.y1
142             y2 = transformed.y2
143             transformed.y1 = whole.y1 + (transformed.x1 - whole.x1)
144             transformed.y2 = whole.y1 + (transformed.x2 - whole.x1)
145             transformed.x1 = whole.x1 + (y1 - whole.y1)
146             transformed.x2 = whole.x1 + (y2 - whole.y1)