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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
# -*- coding: utf-8 -*-
# Utilitarios: Text, Button (abstract), ImageButton, TextButton
from widget import *
import pygame
import os
from game_manager import *
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)
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.over = False
self.enable = True
def set_tooltip(self, text):
self.tooltip = text
def set_super_tooltip(self, text):
self.super_tooltip = text
def on_mouse_click(self):
if (self.function_on_mouse_click and self.enable): # if there's a callback setted makes the call
self.function_on_mouse_click(self)
def on_mouse_over(self):
if (self.function_on_mouse_over and self.enable): # if there's a callback setted makes the call
self.function_on_mouse_over(self)
def on_mouse_out(self):
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
class ImageButton(Button):
def __init__(self, container, rect, frame_rate, image, cb_click=None, cb_over=None, cb_out=None):
self.image = image
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 = self.image.get_rect().size
Button.__init__(self, container, rect, frame_rate, self.image, cb_click, cb_over, cb_out)
def switch_image_background(self, image):
if not isinstance(image, pygame.Surface):
image = pygame.imaself.text_intro.visible = True
self.text_result.visible = Falsege.load(image).convert_alpha()
self.background = image
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
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()
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
def get_accept_button(container, rect, text, cb_click=None, cb_over=None, cb_out=None):
background = pygame.image.load("assets/windows/dialog_button.png").convert()
return TextButton2(container, rect, 1, text, 24, pygame.Color("#397b7e"), background, cb_click, cb_over, cb_out)
def change_color(surface, old_color, new_color):
# No funciona en pygame 1.8.0
i = 0
palette = surface.get_palette()
for color in palette:
if color == old_color:
surface.set_palette_at(i, get_color_tuple(new_color))
i += 1
def get_color_tuple(color):
if isinstance(color, tuple):
return color[0:3]
elif isinstance(color, pygame.Color):
return (color.r, color.g, color.b)
else:
color = pygame.Color(color)
return get_color_tuple(color)
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
#### Paths controls ####
def check_directory(directory):
try:
print directory
os.listdir(directory)
return True
except OSError:
return False
def check_image(image_path):
try:
print image_path
pygame.image.load(image_path)
return True
except:
return False
def verify_path(action, game_manager):
if isinstance(action.effect, effects.Effect): # If the action has effects on bars
if action.kid_animation_path: # and has a kid animation path
return check_directory("%s/%s/%s" % (action.kid_animation_path, game_manager.character.sex, game_manager.character.clothes)) # check animation directory (action_path/sex/clothes)
else:
return True
if isinstance(action.effect, effects.ClothesEffect): # If the action has clothes effects
return check_directory("%s/%s/%s" % (game_manager.character.mood.kid_animation_path, game_manager.character.sex, action.effect.clothes_id))
if isinstance(action.effect, effects.LocationEffect): # If the action has location effects
return check_image(game_manager.environments_dictionary[action.effect.place_id + "_" + game_manager.current_weather].background_path)
return True
|