-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_interface.py
More file actions
156 lines (121 loc) · 5.18 KB
/
user_interface.py
File metadata and controls
156 lines (121 loc) · 5.18 KB
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
import arcade
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class UserInterface:
button_list = []
@classmethod
def setup(cls, window):
"""Start, pause, previous, next"""
play_button = Button(window.fullscreen_width - 50, window.fullscreen_height - 25, 100, 50, "Play")
next_button = Button(window.fullscreen_width - 50, 25, 100, 50, "Next")
previous_button = Button(next_button.center_x - 100, next_button.center_y, 100, 50, "Previous")
cls.button_list.extend((play_button, next_button, previous_button))
@classmethod
def press_mouse(cls, x, y, window):
for button in cls.button_list:
button.check_button_press(x, y, window)
@classmethod
def release_mouse(cls):
for button in cls.button_list:
button.pressed = False
class Button:
@staticmethod
def pause_program(self, window):
window.pause = True
self.change_action("Play")
@staticmethod
def resume_program(self, window):
window.pause = False
self.change_action("Pause")
@staticmethod
def next_demo(self, window):
window.DemoKey += 1
window.setup()
@staticmethod
def previous_demo(self, window):
window.DemoKey -= 1
window.setup()
def __init__(self,
center_x, center_y,
width, height,
text,
font_size=18,
font_face="Arial",
face_color=arcade.color.LIGHT_GRAY,
highlight_color=arcade.color.WHITE,
shadow_color=arcade.color.GRAY,
button_height=2):
button_action = {"Play": Button.resume_program,
"Pause": Button.pause_program,
"Next": Button.next_demo,
"Previous": Button.previous_demo}
self.center_x = center_x
self.center_y = center_y
self.width = width
self.height = height
self.text = text
self.action_function = button_action[text]
self.font_size = font_size
self.font_face = font_face
self.face_color = face_color
self.highlight_color = highlight_color
self.shadow_color = shadow_color
self.button_height = button_height
self.pressed = False
def change_action(self, text):
button_action = {"Play": Button.resume_program,
"Pause": Button.pause_program,
"Next": Button.next_demo,
"Previous": Button.previous_demo}
self.text = text
self.action_function = button_action[text]
def draw(self):
""" Draw the button """
x = self.center_x
y = self.center_y
if self.pressed:
color_br = self.highlight_color
color_tr = self.shadow_color
x += self.button_height
y -= self.button_height
else:
color_br = self.shadow_color
color_tr = self.highlight_color
arcade.draw_rectangle_filled(self.center_x, self.center_y, self.width,
self.height, self.face_color)
# Bottom horizontal
arcade.draw_line(self.center_x - self.width / 2, self.center_y - self.height / 2,
self.center_x + self.width / 2, self.center_y - self.height / 2,
color_br, self.button_height)
# Right vertical
arcade.draw_line(self.center_x + self.width / 2, self.center_y - self.height / 2,
self.center_x + self.width / 2, self.center_y + self.height / 2,
color_br, self.button_height)
# Top horizontal
arcade.draw_line(self.center_x - self.width / 2, self.center_y + self.height / 2,
self.center_x + self.width / 2, self.center_y + self.height / 2,
color_tr, self.button_height)
# Left vertical
arcade.draw_line(self.center_x - self.width / 2, self.center_y - self.height / 2,
self.center_x - self.width / 2, self.center_y + self.height / 2,
color_tr, self.button_height)
arcade.draw_text(self.text, x, y,
arcade.color.BLACK, font_size=self.font_size,
width=self.width, align="center",
anchor_x="center", anchor_y="center")
def check_button_press(self, x, y, window):
""" Given an x, y, see if we need to register any button clicks. """
max_x = self.center_x + self.width / 2
min_x = self.center_x - self.width / 2
max_y = self.center_y + self.height / 2
min_y = self.center_y - self.height / 2
if min_x <= x <= max_x and min_y <= y <= max_y:
self.pressed = True
self.action_function(self, window)
def main():
""" Main method """
window = Test(SCREEN_WIDTH, SCREEN_HEIGHT, UserInterface, (100, 100, 100))
window.setup()
arcade.run()
if __name__ == "__main__":
main()