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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# -*- coding: utf-8 -*-
# Utilitarios: Text, Button (abstract), ImageButton, TextButton
from widget import *
import pygame
import os
class Text(Widget):
ALIGN_LEFT = 0
ALIGN_RIGHT = 1
ALIGN_CENTER = 2
def __init__(self, container_rect, x, y, frame_rate, text, size, color, type="normal", alignment=ALIGN_LEFT, bold=False, italic=False):
self.font = get_font(size, bold, italic)
self.text = unicode(text)
self.color = color
# Render the text and calculate the size
render = self.font.render(self.text, False, color)
if alignment == Text.ALIGN_LEFT:
rect = render.get_rect(topleft=(x, y))
elif alignment == Text.ALIGN_RIGHT:
rect = render.get_rect(topright=(x, y))
else:
rect = render.get_rect(center=(x, y))
if type == "tooltip":
rect.bottomleft = (x, y)
# Make it fit in the container
if rect.right > container_rect.right:
rect.right = container_rect.right
if rect.bottom > container_rect.bottom:
rect.bottom = container_rect.bottom
Widget.__init__(self, container_rect, rect, frame_rate)
self.refresh()
def refresh(self):
background = self.get_background_rect().copy()
self.background = self.font.render(self.text, False, self.color)
self.set_dirty()
def switch_color_text(self, color):
self.color = color
self.refresh()
return (self)
class Image(Widget):
def __init__(self, container, rect, frame_rate, image):
if not isinstance(image, pygame.Surface):
image = pygame.image.load(image)
if image.get_bitsize() == 8:
self.background = image.convert()
else:
self.background = image.convert_alpha()
else:
self.background = image
Widget.__init__(self, container, pygame.Rect((rect.left, rect.top), self.background.get_rect().size), frame_rate, self.background)
class Button(Widget):
# Clase abstracta que representa un boton
def __init__(self, container, rect, frame_rate, surface, cb_click=None, cb_over=None, cb_out=None):
Widget.__init__(self, container, rect, frame_rate, surface)
self.function_on_mouse_click = cb_click
self.function_on_mouse_over = cb_over
self.function_on_mouse_out = cb_out
self.enable = True
self.set_click_sound_path("assets/sound/click.ogg")
# Override
def on_mouse_click(self):
Widget.on_mouse_click(self) # Super
if self.function_on_mouse_click and self.enable: # if there's a callback setted makes the call
self.function_on_mouse_click(self)
# Override
def on_mouse_over(self):
self.over = True
self.set_dirty()
if self.function_on_mouse_over and self.enable: # if there's a callback setted makes the call
self.function_on_mouse_over(self)
# Override
def on_mouse_out(self):
self.over = False
self.set_dirty()
if self.function_on_mouse_out and self.enable: # if there's a callback setted makes the call
self.function_on_mouse_out(self)
def set_on_mouse_click(self, fn):
self.function_on_mouse_click = fn
def set_on_mouse_over(self, fn):
self.function_on_mouse_over = fn
def set_on_mouse_out(self, fn):
self.function_on_mouse_out = fn
def draw(self, screen):
updates = Widget.draw(self, screen)
if self.visible and self.background and self.over:
copy = self.background.convert_alpha()
copy.fill((40,40,40), None, pygame.BLEND_ADD) # Makes the widget brighter
screen.blit(copy, self.rect_absolute)
return updates
class ImageButton(Button):
def __init__(self, container, rect, frame_rate, image, cb_click=None, cb_over=None, cb_out=None):
if not isinstance(image, pygame.Surface):
image = pygame.image.load(image)
if image.get_bitsize() == 8:
self.image = image.convert()
else:
self.image = image.convert_alpha()
rect.size = image.get_rect().size
Button.__init__(self, container, rect, frame_rate, image, cb_click, cb_over, cb_out)
def switch_image_background(self, image):
if not isinstance(image, pygame.Surface):
image = pygame.image.load(image).convert_alpha()
if image.get_bitsize() == 8:
image = image.convert()
else:
image = image.convert_alpha()
self.text_intro.visible = True
self.text_result.visible = False
self.background = image
self.set_dirty()
class TextButton(ImageButton):
def __init__(self, container, rect, frame_rate, text, size, color, cb_click=None, cb_over=None, cb_out=None):
self.text = Text(container, rect.x, rect.y, frame_rate, text, size, color)
ImageButton.__init__(self, container, self.text.rect_in_container, frame_rate, self.text.background, cb_click, cb_over, cb_out)
def switch_color_text(self, color):
self.background = self.text.switch_color_text(color).background
self.set_dirty()
class TextButton2(ImageButton):
def __init__(self, container, rect, frame_rate, text, size, color, background, cb_click=None, cb_over=None, cb_out=None):
self.back = background
self.text = text
self.size = size
self.color = color
rect.size = self.back.get_size()
surface = self.get_surface()
ImageButton.__init__(self, container, rect, frame_rate, surface, cb_click, cb_over, cb_out)
def switch_color_text(self, color):
self.color = color
self.background = self.get_surface()
self.set_dirty()
def get_surface(self):
font = get_font(self.size)
render = font.render(self.text, True, self.color)
surface = self.back.copy()
surface.blit(render, render.get_rect(center=surface.get_rect().center))
return surface
class TextBlock(Widget):
def __init__(self, container, x, y, frame_rate, text, size, color, type="normal", fill=True):
Widget.__init__(self, container, pygame.Rect(x, y, 0, 0), frame_rate)
self.lines = []
self.font = get_font(size)
self.color = color
self.parse_lines(text)
self.size = size
self.prepare_text_block()
self.fill = fill
if type == "tooltip":
self.rect_absolute.bottomleft = (x, y)
def parse_lines(self, text):
self.lines = []
if isinstance(text, unicode):
eol = u"\n"
else:
eol = "\n"
(b, _, a) = text.partition(eol)
self.lines.append(b)
while(a != ''):
(b, _, a) = a.partition(eol)
self.lines.append(b)
def prepare_text_block(self):
number_of_lines = 0
for l in self.lines:
number_of_lines += 1
r = self.font.render(l, False, self.color)
if r.get_rect().width > self.rect_absolute.width:
self.rect_absolute.width = r.get_rect().width
self.rect_absolute.height += r.get_rect().height
# Make it fit in the container
if self.rect_absolute.right > self.container.right:
self.rect_absolute.right = self.container.right
if self.rect_absolute.bottom > self.container.bottom:
self.rect_absolute.bottom = self.container.bottom
def draw(self, screen):
if self.visible:
number_of_lines = 0
if self.fill:
screen.fill((255, 255, 255), (self.rect_absolute))
for l in self.lines:
r = self.font.render(l, False, self.color)
screen.blit(r, (self.rect_absolute.left, self.rect_absolute.top + r.get_rect().height * number_of_lines))
number_of_lines += 1
font_dict = {} # Chaches created font instances
def get_font(size, bold=False, italic=False):
key = (size, bold, italic)
if key in font_dict:
return font_dict[key]
if bold:
font = pygame.font.Font("assets/fonts/DroidSans-Bold.ttf", size)
else:
font = pygame.font.Font("assets/fonts/DroidSans.ttf", size)
if italic:
font.set_italic(True)
font_dict[key] = font
return font
|