-
Notifications
You must be signed in to change notification settings - Fork 1
/
p5_button.py
173 lines (118 loc) · 4.81 KB
/
p5_button.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
#Copyright (c) 2008, Media Modifications Ltd.
#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 p5 import P5
class P5Button(P5):
def __init__(self):
P5.__init__(self)
self.noloop()
self._butts = []
self._buttonPressed = False
def button_press(self, widget, event):
P5.button_press(self, widget, event)
#iterate through the buttons to see if you've pressed any down
bp = False
for i in range ( 0, len(self._butts) ):
if (self._butts[i]._enabled):
contains = self._butts[i].contains(event.x, event.y)
self._butts[i]._pressed = contains
if (contains):
bp = True
self._buttonPressed = bp
self.redraw()
def button_release(self, widget, event):
P5.button_release(self, widget, event)
self._buttonPressed = False
pressed = []
#iterate through the buttons to see if you've released on any
for i in range ( 0, len(self._butts) ):
if (self._butts[i]._enabled):
if (self._butts[i]._pressed):
if (self._butts[i].contains(event.x, event.y)):
pressed.append( self._butts[i] )
if (self._butts[i]._toggle):
self._butts[i]._pressed = not self._butts[i]._pressed
else:
self._butts[i]._pressed = False
for i in range( 0, len(pressed) ):
pressed[i].doPressed()
self.redraw()
class Polygon:
def __init__( self, xs, ys ):
self.setPoints( xs, ys )
def setPoints( self, xs, ys ):
self._xs = xs
self._ys = ys
self._boundingX = self._xs[0]
self._boundingY = self._ys[0]
self._boundingW = self._xs[0]
self._boundingH = self._ys[0]
for i in range ( 1, len(self._xs) ):
if (self._xs[i] > self._boundingW):
self._boundingW = self._xs[i]
if (self._ys[i] > self._boundingH):
self._boundingH = self._ys[i]
if (self._xs[i] < self._boundingX):
self._boundingX = self._xs[i]
if (self._ys[i] < self._boundingY):
self._boundingY = self._ys[i]
def contains( self, mx, my ):
if (not self.bbox_contains(mx, my)):
return False
#insert simple path tracing check on the polygon here
return True
def bbox_contains( self, mx, my ):
if ( not((mx>=self._boundingX) and (my>=self._boundingY) and (mx<self._boundingW) and (my<self._boundingH)) ):
return False
else:
return True
class Button:
def __init__(self, poly, offX, offY):
self._poly = poly
self._offX = offX
self._offY = offY
self._enabled = True
self._pressed = False
self._toggle = False
self._listeners = []
self._actionCommand = None
self._img = None
def setOffsets(self, offs):
self._offX = offs[0]
self._offY = offs[1]
def addActionListener(self, listen):
self._listeners.append(listen)
def removeActionListener(self, listen):
try:
self._listeners.remove(listen)
except ValueError:
pass
def setActionCommand(self, command):
self._actionCommand = command
def getActionCommand(self):
return self._actionCommand
def setImage(self, img):
self._img = img
def contains( self, mx, my ):
x = mx - self._offX
y = my - self._offY
contains = self._poly.contains( x, y )
return contains
def doPressed( self ):
for i in range ( 0, len(self._listeners) ):
self._listeners[i].fireButton( self._actionCommand )
def isImg( self ):
return self._img != None