-
Notifications
You must be signed in to change notification settings - Fork 0
/
TouchScreenButton.py
51 lines (43 loc) · 1.77 KB
/
TouchScreenButton.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
#=======================================================================
# Copyright Nicholas Tuckett 2015.
# Distributed under the MIT License.
# (See accompanying file license.txt or copy at
# http://opensource.org/licenses/MIT)
#=======================================================================
#
# Style data:
# text_colour
# background_colour
# border_colour
# highlight_colour
# font
import pygame.draw
import Style
class TouchScreenButton(object):
def __init__(self, x, y, width, height, prompt, code, style):
self.x = x
self.y = y
self.width = width
self.height = height
self.prompt = prompt
self.code = code
self.style = style
self.rect = (self.x, self.y, self.width, self.height)
self.highlighted = False
self.text_surface = self.style[Style.FONT].render(self.prompt, False, self.style[Style.TEXT_COLOUR])
def render(self, surface):
if self.highlighted:
surface.fill(self.style[Style.HIGHLIGHT_COLOUR], self.rect)
else:
surface.fill(self.style[Style.BACKGROUND_COLOUR], self.rect)
pygame.draw.rect(surface, self.style[Style.BORDER_COLOUR], self.rect, self.style[Style.BORDER_WIDTH])
text_width, text_height = self.text_surface.get_size()
surface.blit(self.text_surface, (self.x + (self.width - text_width) / 2, self.y + (self.height - text_height) / 2))
def highlight(self, highlight):
self.highlighted = highlight
def hit_test(self, point):
relative_x = point[0] - self.x
if relative_x >= 0 and relative_x < self.width:
relative_y = point[1] - self.y
return relative_y >= 0 and relative_y < self.height
return False