/
button.py
76 lines (60 loc) · 1.93 KB
/
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
import pygame
from pygame.locals import *
import util
class Button:
def __init__(self,text,func,topleft):
self.text = text
self.func = func
self.image = util.get_image(util.BUTTON)
self.pushed_image = util.get_image(util.PUSHED_BUTTON)
self.rect = self.image.get_rect()
self.rect.topleft = topleft
self.pushed = 0
def draw(self,screen):
self.draw_background(screen)
self.draw_text(screen)
def draw_background(self,screen):
if self.pushed:
i = self.pushed_image
else:
i = self.image
screen.blit(i,self.rect)
def draw_text(self,screen):
t = util.render_font(self.text)
textpos = t.get_rect()
textpos.center = self.rect.center
screen.blit(t,textpos)
def is_within(self,point):
x,y = point
if self.rect.collidepoint(x,y):
return 1
return 0
def handle_event(self,event):
handled =0
if event.type is MOUSEBUTTONUP:
self.pushed = 0
if self.is_within(event.pos):
self.func()
handled = 1
elif event.type is MOUSEBUTTONDOWN:
if self.is_within(event.pos):
self.pushed = 1
return handled
class ToggleButton(Button):
def __init__(self,text,topleft):
Button.__init__(self,text,self.toggle,topleft)
self.on = 0
def set_other(self,other):
self.other = other
def toggle(self):
if not self.on:
self.on = 1
self.other.on = 0
def draw_text(self,screen):
if self.on:
t = util.render_font(self.text,util.HIGHLIGHT_COLOR)
else:
t = util.render_font(self.text,util.OFF_COLOR)
textpos = t.get_rect()
textpos.center = self.rect.center
screen.blit(t,textpos)