Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Saludame.activity/gui/utilities.py
blob: d307cffef6bdaafa7fec2826636ae1adf5a63c27 (plain)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# -*- coding: utf-8 -*-

# Copyright (C) 2011 ceibalJAM! - ceibaljam.org
# This file is part of Saludame.
#
# Saludame is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Saludame is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Saludame. If not, see <http://www.gnu.org/licenses/>.

# Utilitarios: Text, Button (abstract), ImageButton, TextButton

import pygame
from widget import *
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
        
        self.type = type
        
        # 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))           
                    
        # 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):
        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, keep_format=False):
        
        if not isinstance(image, pygame.Surface):
            image = pygame.image.load(image)
            if keep_format:
                self.background = image
            else:
                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.highlight = True   # Highlight it when mouse over
        
        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
        if self.highlight:
            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
        if self.highlight:
            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 and self.highlight:
            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

        self.text_intro = None
        self.text_result = None

        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()

            if self.text_intro:
                self.text_intro.visible = True
        
        if self.text_result:    
            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 TextBlockButton(Button):     
    def __init__(self, container, rect, frame_rate, text, size, color, cb_click=None, cb_over=None, cb_out=None):
        self.text = TextBlock(container, rect.x, rect.y, frame_rate, text, size, color, "normal", False)
        Button.__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.text.switch_color_text(color)
        self.set_dirty()
        
    def draw(self, screen):
        self.text.draw(screen)
        self.parent.set_dirty_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()
        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, anchor_type="topleft"):    
        
        Widget.__init__(self, container, pygame.Rect(x, y, 0, 0), frame_rate)
        
        self.type = type
        self.lines = []
        self.font = get_font(size)
        self.color = color
        self.parse_lines(text)
        self.size = size
        
        self.fill = fill
        self.anchor = (container[0] + x, container[1] + y)
        self.anchor_type = anchor_type
        
        if type == "tooltip":
            self.rect_absolute.bottomleft = (x, y)
        
        self.prepare_text_block()
        
    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 render_lines(self):
        self.rect_absolute.width = 0
        self.rect_absolute.height = 0
        self.rendered_lines = []
        for l in self.lines:          
            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 
            self.rendered_lines.append(r)
            
    def prepare_text_block(self):
        self.render_lines()
            
        if self.type == "tooltip":
            self.rect_absolute.height += 20
            self.rect_absolute.width += 20
        
        if self.anchor_type == "center":
            # Center the rectangle in the given coordinates
            #self.rect_absolute.center = self.rect_absolute.topleft
            self.rect_absolute.center = self.anchor
            
        # 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
            
        self.rect_in_container.size = self.rect_absolute.size
        self.rect_in_container.left = self.rect_absolute.left - self.container.left
        self.rect_in_container.top = self.rect_absolute.top - self.container.top
        
    def draw(self, screen):
        if self.visible:
            if self.fill:
                screen.fill((247, 247, 247), self.rect_absolute)
                
            if self.type == "tooltip":
                top = self.rect_absolute.top + 10
                left = self.rect_absolute.left + 10
            else:
                top = self.rect_absolute.top
                left = self.rect_absolute.left
            
            if self.anchor_type == "center":
                number_of_lines = 0
                for r in self.rendered_lines:
                    x = left + (self.rect_absolute.width - r.get_width())/2
                    y = top + r.get_rect().height * number_of_lines
                    screen.blit(r, (x, y))
                    number_of_lines += 1
            else:
                number_of_lines = 0
                for r in self.rendered_lines:
                    screen.blit(r, (left, top + r.get_rect().height * number_of_lines))
                    number_of_lines += 1
            
            if self.type == "tooltip":
                pygame.draw.rect(screen, pygame.Color("#7fe115"), self.rect_absolute, 2)
                
    def switch_color_text(self, color):
        self.color = color
        self.render_lines()
        self.set_dirty()

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