-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.py
162 lines (129 loc) · 4.63 KB
/
buttons.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
from vex import *
class ButtonUi:
class Button:
def __init__(self):
self.index = 0
self.xpos = 0
self.ypos = 0
self.width = 80
self.height = 80
self.color = Color.WHITE
self.text = ''
self.alttext = ''
self.state = False
self.toggle = False
self.callback = None
def set_toggle(self, toggle, text):
self.toggle = toggle
if text is not None:
self.alttext = text
else:
self.alttext = self.text
return self
def set_size(self, width, height):
self.width = width
self.height = height
return self
def set_color(self, color):
self.color = color
return self
def __init__(self):
self.brain = Brain()
self._buttons = []
self._enabled = True
self.brain.screen.pressed(self._screen_press)
self.brain.screen.released(self._screen_release)
@staticmethod
def _find_button(b, xpos, ypos):
if xpos < b.xpos or xpos > (b.xpos + b.width):
return False
if ypos < b.ypos or ypos > (b.ypos + b.height):
return False
return True
def _draw_button(self, b, bHighlight):
if bHighlight:
self.brain.screen.draw_rectangle(
b.xpos, b.ypos, b.width, b.height, Color(0x808080))
else:
self.brain.screen.draw_rectangle(
b.xpos, b.ypos, b.width, b.height, b.color)
self.brain.screen.draw_rectangle(
b.xpos, b.ypos, b.width, b.height, Color.TRANSPARENT)
self.brain.screen.set_fill_color(Color.BLACK)
self.brain.screen.set_pen_color(Color.WHITE)
self.brain.screen.set_font(FontType.MONO20)
if b.toggle and b.state:
text = b.alttext
else:
text = b.text
# we need to add twxt width to python VM, this will do for now
textwidth = len(text) * 10
self.brain.screen.print_at(
text, opaque=False, x=b.xpos + (b.width-textwidth)/2, y=b.ypos + b.height/2 + 10)
def _draw_buttons(self):
for b in self._buttons:
self._draw_button(b, False)
def _screen_press(self):
if not self._enabled:
return
xpos = self.brain.screen.x_position()
ypos = self.brain.screen.y_position()
for b in self._buttons:
if self._find_button(b, xpos, ypos):
if b.toggle is True:
b.state = not b.state
else:
b.state = True
self._draw_button(b, True)
if b.callback is not None:
b.callback(b.index, b.state)
return
def _screen_release(self):
if not self._enabled:
return
for b in self._buttons:
if not b.toggle:
if b.state:
b.state = False
if b.callback is not None:
b.callback(b.index, b.state)
self._draw_buttons()
def add_button(self, x, y, text, callback):
b = ButtonUi.Button()
b.index = len(self._buttons)
b.xpos = x
b.ypos = y
b.text = text
b.callback = callback
self._buttons.append(b)
return b
def enable(self):
self._enabled = True
def disable(self):
self._enabled = False
def display(self, bClearScreen=False):
if bClearScreen:
self.brain.screen.clear_screen()
self._draw_buttons()
# ----------------------------------------------------------
def userTouchAction(index, state):
if index == 0 and not state:
brain.screen.print_at("Button 1 pressed ", x=150, y=150)
if index == 1 and not state:
brain.screen.print_at("Button 2 pressed ", x=150, y=150)
if index == 2 and not state:
brain.screen.print_at("Button 3 pressed ", x=150, y=150)
if index == 3 and not state:
brain.screen.print_at("Button 4 pressed ", x=150, y=150)
# ----------------------------------------------------------
'''
# create ui object
ui = ButtonUi()
# add some buttons
ui.add_button(50, 20, "PLAY", userTouchAction).set_color(Color.RED)
ui.add_button(150, 20, "STOP", userTouchAction).set_color(Color.BLUE)
ui.add_button(250, 20, "FWD", userTouchAction).set_color(Color(0x208020))
ui.add_button(350, 20, "REV", userTouchAction).set_color(Color(0x404040))
ui.add_button(50, 120, "OFF", userTouchAction).set_color(Color(0x804040)).set_toggle(True, "ON")
ui.display()
'''