-
Notifications
You must be signed in to change notification settings - Fork 1
/
button.py
126 lines (102 loc) · 4.32 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
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
import pygame, view, label, event_responder, static_view
from view import *
from static_view import *
from event_responder import *
from pygame import *
class Button(View):
def __init__(self, frame, args = { 'text': "Untitled", 'normal_surface': None, 'active_surface': None }):
View.__init__(self, frame)
self.text = None
self.active = False
self.active_view = StaticView(args.get('active_surface'))
self.active_view.frame = self.bounds()
self.normal_view = StaticView(args.get('normal_surface'))
self.normal_view.frame = self.bounds()
self.label_view = None
self.text_color = (255, 0, 0)
self.active_text_color = (255, 255, 0)
self.button_is_rounded_rect = self.normal_view.surface is not None
self.update_text(args.get('text'))
self.add_event_responder(MouseButtonDownResponder(self.toggle))
self.add_event_responder(MouseButtonUpResponder(self.toggle))
def create_surfaces(self):
if self.normal_view.surface is None:
self.normal_view.surface = self.rounded_rect_surface(pygame.Color(0,255,0,0))
self.normal_view.background_color = pygame.Color(0,255,0,0)
print "creating normal surface"
if self.active_view.surface is None:
self.active_view.surface = self.rounded_rect_surface((90,90,90))
self.normal_view.background_color = pygame.Color(90,90,90)
print "creating active surface"
def toggle(self, event):
self.active = not self.active
self.create_surfaces()
if self.active:
self.normal_view.remove_from_superview()
self.add_subview(self.active_view)
self.label_view.background_color = self.active_view.background_color
if not self.button_is_rounded_rect:
self.label_view.text_color = self.active_text_color
print "active view added"
else:
self.active_view.remove_from_superview()
self.add_subview(self.normal_view)
self.label_view.background_color = self.normal_view.background_color
if not self.button_is_rounded_rect:
self.label_view.text_color = self.text_color
print "normal view added"
def autosize(self):
f = self.frame
f.size = self.label_view.get_text_size()
self.update_frame(f)
def generate_label(self):
l = label.Label(pygame.Rect((0, 0), (self.frame.width, 0)))
l.size = 24
l.text_alignment = 'center'
l.autofit_height()
l.update_text(self.text)
l.text_color = self.text_color
l.background_color = (100, 1, 50)
self.add_subview(l)
return l
def update_text(self, text):
self.text = text
self.set_needs_display()
l = self.generate_label()
self.label_view = l
def AAfilledRoundedRect(self, surface,rect,color,radius=0.4):
"""
AAfilledRoundedRect(surface,rect,color,radius=0.4)
surface : destination
rect : rectangle
color : rgb or rgba
radius : 0 <= radius <= 1
"""
color = Color(*color)
alpha = color.a
color.a = 0
pos = rect.topleft
rect.topleft = 0,0
rectangle = Surface(rect.size, SRCALPHA)
circle = Surface([min(rect.size)*3]*2, SRCALPHA)
draw.ellipse(circle,(0,0,0),circle.get_rect(),0)
circle = transform.smoothscale(circle,[int(min(rect.size)*radius)]*2)
radius = rectangle.blit(circle,(0,0))
radius.bottomright = rect.bottomright
rectangle.blit(circle,radius)
radius.topright = rect.topright
rectangle.blit(circle,radius)
radius.bottomleft = rect.bottomleft
rectangle.blit(circle,radius)
rectangle.fill((0,0,0), rect.inflate(-radius.w,0))
rectangle.fill((0,0,0), rect.inflate(0,-radius.h))
rectangle.fill(color, special_flags=BLEND_RGBA_MAX)
rectangle.fill((255,255,255,alpha), special_flags=BLEND_RGBA_MIN)
return surface.blit(rectangle,pos)
def rounded_rect_surface(self, color = (77,77,77)):
s = Surface(self.frame.size)
self.AAfilledRoundedRect(s, self.bounds(), color)
return s
def draw(self, rect):
self.label_view.bring_to_top()
View.draw(self, rect)