-
Notifications
You must be signed in to change notification settings - Fork 15
/
geometry.py
242 lines (198 loc) · 7.89 KB
/
geometry.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#
# Copyright (c) 2014 Garrett Brown
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
from collections import namedtuple
import math
SQRT2 = math.sqrt(2)
BUTTON_RECTANGLE = 'rectangle'
BUTTON_CIRCLE = 'circle'
BUTTON_ELLIPSE = 'ellipse'
BUTTON_DPAD = 'dpad'
Direction = namedtuple('Direction', 'xstep ystep')
DIRECTION_UP = Direction( 0, -1)
DIRECTION_UPRIGHT = Direction( 1, -1)
DIRECTION_RIGHT = Direction( 1, 0)
DIRECTION_DOWNRIGHT = Direction( 1, 1)
DIRECTION_DOWN = Direction( 0, 1)
DIRECTION_DOWNLEFT = Direction(-1, 1)
DIRECTION_LEFT = Direction(-1, 0)
DIRECTION_UPLEFT = Direction(-1, -1)
Point = namedtuple('Point', 'x y')
Vector = namedtuple('Vector', 'pos dir')
class Button(object):
def __init__(self, geometry):
self._geometry = geometry
def Type(self):
return self._geometry
def StartPoints(self):
return [ ]
@staticmethod
def FromNode(node):
if node.tag =='button':
geometry = node.attrib.get('geometry')
if geometry == BUTTON_RECTANGLE:
return Rectangle.FromNode(node)
elif geometry == BUTTON_CIRCLE:
return Circle.FromNode(node)
elif geometry == BUTTON_ELLIPSE:
return Ellipse.FromNode(node)
elif node.tag == BUTTON_DPAD:
return Dpad.FromNode(node)
return None
class Rectangle(Button):
def __init__(self, x1, y1, x2, y2):
super(Rectangle, self).__init__(BUTTON_RECTANGLE)
self._x1, self._x2 = ((x1, x2) if x1 <= x2 else (x2, x1))
self._y1, self._y2 = ((y1, y2) if y1 <= y2 else (y2, y1))
def Point1(self):
return Point(self._x1, self._y1)
def Point2(self):
return Point(self._x2, self._y2)
def Coords(self):
return [self.Point1(), self.Point2()]
def Center(self):
return Point((self._x1 + self._x2) / 2, (self._y1 + self._y2) / 2)
def Width(self):
return self._x2 - self._x1
def Height(self):
return self._y2 - self._y1
def StartPoints(self):
self._x1_5 = (self._x1 + self._x2) / 2
self._y1_5 = (self._y1 + self._y2) / 2
top = Vector(Point(self._x1_5, self._y1), DIRECTION_UP)
right = Vector(Point(self._x2, self._y1_5), DIRECTION_RIGHT)
down = Vector(Point(self._x1_5, self._y2), DIRECTION_DOWN)
left = Vector(Point(self._x1, self._y1_5), DIRECTION_LEFT)
return [top, right, down, left]
@staticmethod
def FromNode(node):
try:
x1 = int(node.attrib.get('x1'))
y1 = int(node.attrib.get('y1'))
x2 = int(node.attrib.get('x2'))
y2 = int(node.attrib.get('y2'))
return Rectangle(x1, y1, x2, y2)
except:
pass
return None
class Circle(Button):
def __init__(self, x, y, r):
super(Circle, self).__init__(BUTTON_CIRCLE)
self._x = x
self._y = y
self._r = r
def Center(self):
return self._x, self._y
def Radius(self):
return self._r
def StartPoints(self):
self._r2 = int(self._r * SQRT2 / 2)
top = Vector(Point(self._x, self._y - self._r), DIRECTION_UP)
topright = Vector(Point(self._x + self._r2, self._y - self._r2), DIRECTION_UPRIGHT)
right = Vector(Point(self._x + self._r, self._y), DIRECTION_RIGHT)
downright = Vector(Point(self._x + self._r2, self._y + self._r2), DIRECTION_DOWNRIGHT)
down = Vector(Point(self._x, self._y + self._r), DIRECTION_DOWN)
downleft = Vector(Point(self._x - self._r2, self._y + self._r2), DIRECTION_DOWNLEFT)
left = Vector(Point(self._x - self._r, self._y), DIRECTION_LEFT)
topleft = Vector(Point(self._x - self._r2, self._y - self._r2), DIRECTION_UPLEFT)
return [top, topright, right, downright, down, downleft, left, topleft]
@staticmethod
def FromNode(node):
try:
x = int(node.attrib.get('x'))
y = int(node.attrib.get('y'))
r = int(node.attrib.get('radius'))
return Circle(x, y, r)
except:
pass
return None
class Ellipse(Button):
def __init__(self, x, y, rx, ry):
super(Ellipse, self).__init__(BUTTON_ELLIPSE)
self._x = x
self._y = y
self._rx = rx
self._ry = ry
def Center(self):
return self._x, self._y
def Axes(self):
return self._rx, self._ry
def StartPoints(self):
top = Vector(Point(self._x, self._y - self._ry), DIRECTION_UP)
right = Vector(Point(self._x + self._rx, self._y), DIRECTION_RIGHT)
down = Vector(Point(self._x, self._y + self._ry), DIRECTION_DOWN)
left = Vector(Point(self._x - self._rx, self._y), DIRECTION_LEFT)
return [top, right, down, left]
@staticmethod
def FromNode(node):
try:
x = int(node.attrib.get('x'))
y = int(node.attrib.get('y'))
rx = int(node.attrib.get('rx'))
ry = int(node.attrib.get('ry'))
return Ellipse(x, y, rx, ry)
except:
pass
return None
class Dpad(Button):
def __init__(self, up, right, down, left):
super(Dpad, self).__init__(BUTTON_DPAD)
self._up = up
self._right = right
self._down = down
self._left = left
def Directions(self):
return self._up, self._right, self._down, self._left
def StartPoints(self):
return [self._up.StartPoints()[0],
self._right.StartPoints()[1],
self._down.StartPoints()[2],
self._left.StartPoints()[3]]
@staticmethod
def FromNode(node):
buttons = [ ]
for button_child in node:
button = Button.FromNode(button_child)
if button:
buttons.append(button)
up = None
right = None
down = None
left = None
if len(buttons) == 4:
for i in range(len(buttons)):
if up is None or buttons[i].Center()[1] < up.Center()[1]:
up_index = i
up = buttons[i]
buttons = buttons[0 : up_index] + buttons[up_index + 1 : ]
for i in range(len(buttons)):
if right is None or buttons[i].Center()[0] > right.Center()[0]:
right_index = i
right = buttons[i]
buttons = buttons[0 : right_index] + buttons[right_index + 1 : ]
for i in range(len(buttons)):
if down is None or buttons[i].Center()[1] > down.Center()[1]:
down_index = i
down = buttons[i]
buttons = buttons[0 : down_index] + buttons[down_index + 1 : ]
left = buttons[0]
return Dpad(up, right, down, left)
return None