author | Radek Brich <radek.brich@devl.cz> |
Fri, 28 Mar 2014 19:58:59 +0100 | |
changeset 97 | 0c2e0c09ba5c |
parent 94 | e50dae408fe9 |
child 104 | 742e504ec053 |
permissions | -rw-r--r-- |
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
1 |
class Point: |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
2 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
3 |
"""Point in cartesian space. |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
4 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
5 |
Implements attribute access (.x, .y) and list-like access([0],[1]). |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
6 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
7 |
""" |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
8 |
|
97
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
9 |
def __init__(self, *args, **kwargs): |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
10 |
self.x = 0 |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
11 |
self.y = 0 |
0c2e0c09ba5c
Add TextField widget, keypress event, cursor.
Radek Brich <radek.brich@devl.cz>
parents:
94
diff
changeset
|
12 |
self.update(*args, **kwargs) |
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
13 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
14 |
def __getitem__(self, key): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
15 |
return (self.x, self.y)[key] |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
16 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
17 |
def __repr__(self): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
18 |
return 'Point(x={0.x},y={0.y})'.format(self) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
19 |
|
94
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
20 |
def move(self, relx, rely): |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
21 |
self.x += relx |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
22 |
self.y += rely |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
23 |
|
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
24 |
def moved(self, relx, rely): |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
25 |
return Point(self.x + relx, self.y + rely) |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
26 |
|
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
27 |
def update(self, *args, **kwargs): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
28 |
"""Update point. |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
29 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
30 |
Accepts positional or keyword arguments: |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
31 |
- (point: Point) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
32 |
- (x: int, y: int) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
33 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
34 |
""" |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
35 |
if len(args) == 2: |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
36 |
self.x, self.y = args |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
37 |
elif len(args) == 1: |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
38 |
self.x, self.y = args[0] |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
39 |
elif len(args): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
40 |
raise ValueError('Too many args.') |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
41 |
for key, val in kwargs.items(): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
42 |
if key == 'point': |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
43 |
self.x, self.y = val |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
44 |
elif key in ('x', 'y'): |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
45 |
setattr(self, key, val) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
46 |
else: |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
47 |
raise ValueError('Bad keyword arg: %r' % key) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
48 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
49 |
|
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
50 |
class Size: |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
51 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
52 |
"""Size class. |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
53 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
54 |
Implements attribute access (.w, .h) and list-like access([0],[1]). |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
55 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
56 |
""" |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
57 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
58 |
def __init__(self, *args, **kwargs): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
59 |
self.w = 0 |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
60 |
self.h = 0 |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
61 |
self.update(*args, **kwargs) |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
62 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
63 |
def __getitem__(self, key): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
64 |
return (self.w, self.h)[key] |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
65 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
66 |
def __repr__(self): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
67 |
return 'Size(w={0.w},h={0.h})'.format(self) |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
68 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
69 |
def update(self, *args, **kwargs): |
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
70 |
"""Update size. |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
71 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
72 |
Accepts positional or keyword arguments: |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
73 |
- (size: Size) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
74 |
- (w: int, h: int) |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
75 |
|
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
76 |
""" |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
77 |
if len(args) == 2: |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
78 |
self.w, self.h = args |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
79 |
elif len(args) == 1: |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
80 |
self.w, self.h = args[0] |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
81 |
elif len(args): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
82 |
raise ValueError('Too many args.') |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
83 |
for key, val in kwargs.items(): |
87
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
84 |
if key == 'size': |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
85 |
self.w, self.h = val |
ee5ea9671f28
Add core Widget, Container. Add widgets Label.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
86 |
elif key in ('w', 'h'): |
86
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
87 |
setattr(self, key, val) |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
88 |
else: |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
89 |
raise ValueError('Bad keyword arg: %r' % key) |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
90 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
91 |
def readonly(self): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
92 |
return ReadonlySize(self) |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
93 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
94 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
95 |
class ReadonlySize: |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
96 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
97 |
"""Wrapper for Size which makes it read-only.""" |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
98 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
99 |
def __init__(self, size): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
100 |
self._size = size |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
101 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
102 |
@property |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
103 |
def w(self): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
104 |
return self._size.w |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
105 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
106 |
@property |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
107 |
def h(self): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
108 |
return self._size.h |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
109 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
110 |
def __getitem__(self, key): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
111 |
return self._size[key] |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
112 |
|
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
113 |
def __repr__(self): |
0978fb755d31
Add core Application (adjusted), Window (new version), Signal (replaces Emitter), Size (adjusted). Add application demo.
Radek Brich <radek.brich@devl.cz>
parents:
diff
changeset
|
114 |
return 'ReadonlySize(w={0.w},h={0.h})'.format(self._size) |
91
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
115 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
116 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
117 |
class Rect: |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
118 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
119 |
"""Rectangle is defined by coordinates and size.""" |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
120 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
121 |
def __init__(self, x=0, y=0, w=0, h=0): |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
122 |
self.x = x |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
123 |
self.y = y |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
124 |
self.w = w |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
125 |
self.h = h |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
126 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
127 |
def __repr__(self): |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
128 |
return 'Rect(x={0.x},y={0.y},w={0.w},h={0.h})'.format(self) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
129 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
130 |
def __contains__(self, point): |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
131 |
"""Test if point is positioned inside rectangle. |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
132 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
133 |
`point` should be either Point or pair of int values. |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
134 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
135 |
""" |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
136 |
x, y = point |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
137 |
return (self.x <= x < self.x + self.w |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
138 |
and self.y <= y < self.y + self.h) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
139 |
|
94
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
140 |
def moved(self, relx, rely): |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
141 |
"""Return new Rect with same size, moved by `relx`, `rely`.""" |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
142 |
return Rect(self.x + relx, self.y + rely, self.w, self.h) |
e50dae408fe9
Add origin to Buffer. Use it to simplify hierarchical drawing.
Radek Brich <radek.brich@devl.cz>
parents:
91
diff
changeset
|
143 |
|
91
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
144 |
def intersect(self, other): |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
145 |
x1 = max(self.x, other.x) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
146 |
y1 = max(self.y, other.y) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
147 |
x2 = min(self.x + self.w, other.x + other.w) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
148 |
y2 = min(self.y + self.h, other.y + other.h) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
149 |
if x1 >= x2 or y1 >= y2: |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
150 |
return Rect() |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
151 |
return Rect(x1, y1, x2-x1, y2-y1) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
152 |
|
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
153 |
def union(self, other): |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
154 |
x = min(self.x, other.x) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
155 |
y = min(self.y, other.y) |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
156 |
w = max(self.x + self.w, other.x + other.w) - x |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
157 |
h = max(self.y + self.h, other.y + other.h) - y |
de80e140b0ec
Add clipping for Buffer draw operations.
Radek Brich <radek.brich@devl.cz>
parents:
87
diff
changeset
|
158 |
return Rect(x, y, w, h) |