From da36d9ce3c9ba0e478af0fa50960431be170ce4f Mon Sep 17 00:00:00 2001 From: Pablo Moleri Date: Wed, 29 Dec 2010 22:24:14 +0000 Subject: Package gui created with all standard gui components --- diff --git a/Saludame.activity/animation.py b/Saludame.activity/animation.py index f0ef907..3165e1d 100755 --- a/Saludame.activity/animation.py +++ b/Saludame.activity/animation.py @@ -3,7 +3,7 @@ import pygame import os import utilities -from window import * +import gui COLORS_HAIR = (pygame.Color("#00ffff"), pygame.Color("#009f9f")) COLORS_SKIN = (pygame.Color("#ffccc7"), pygame.Color("#cba5a0")) @@ -17,10 +17,10 @@ BLACK = pygame.Color("black") BLUE = pygame.Color("blue") WHITE = pygame.Color("white") -class Kid(Window): +class Kid(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller, game_man, mood): - Window.__init__(self, container, rect, frame_rate, windows_controller, "character_window") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "character_window") self.character = game_man.character @@ -98,12 +98,12 @@ class Kid(Window): index += 1 -class ActionAnimation(Widget): +class ActionAnimation(gui.Widget): """ An action animation to show at panel """ def __init__(self, container, rect, frame_rate, animation_path, sound_path): - Widget.__init__(self, container, rect, frame_rate) + gui.Widget.__init__(self, container, rect, frame_rate) self.path = animation_path self.frame_rate = frame_rate diff --git a/Saludame.activity/challenges.py b/Saludame.activity/challenges.py index 2cf1dba..7553e11 100755 --- a/Saludame.activity/challenges.py +++ b/Saludame.activity/challenges.py @@ -6,8 +6,8 @@ Challenges module import pygame import os +import gui import utilities -from window import * from gettext import gettext as _ S_CORRECT_PATH = os.path.normpath("assets/sound/correct.ogg") @@ -24,10 +24,10 @@ TEXT_FONT_SIZE = 18 ANSWER_COLOR = pygame.Color("blue") MOUSE_OVER_COLOR = pygame.Color("green") -class MultipleChoice(Window): +class MultipleChoice(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller, challenges_creator, register_id, bg_color=(0, 0, 0)): - Window.__init__(self, container, rect, frame_rate, windows_controller, register_id, bg_color) + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, register_id, bg_color) self.set_bg_image("assets/windows/window_1.png") @@ -51,17 +51,17 @@ class MultipleChoice(Window): self.lose_points = 0 # Close Button - self.btn_close = TextButton(self.rect, pygame.Rect((910, 0), (30, 30)), 1, "X", 32, (0, 0, 0), self._cb_button_click_close) + self.btn_close = gui.TextButton(self.rect, pygame.Rect((910, 0), (30, 30)), 1, "X", 32, (0, 0, 0), self._cb_button_click_close) self.add_button(self.btn_close) self.wait = 0 ####### Set attributes ####### def set_question(self, question): - if (self.question): + if self.question: self.erase() - - self.question = TextBlock(self.rect, 30, 90, 1, question, TITLE_FONT_SIZE, (0, 0, 0), False) + + self.question = gui.TextBlock(self.rect, 30, 90, 1, question, TITLE_FONT_SIZE, (0, 0, 0), False) self.add_child(self.question) def set_correct_answer(self, a): @@ -75,7 +75,7 @@ class MultipleChoice(Window): for ans in answers: y += 30 - b = TextButton(self.rect, pygame.Rect((x, y), (1, 1)), 1, ans, TEXT_FONT_SIZE, ANSWER_COLOR, self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice) + b = gui.TextButton(self.rect, pygame.Rect((x, y), (1, 1)), 1, ans, TEXT_FONT_SIZE, ANSWER_COLOR, self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice) self.choices.append(b) self.buttons.append(b) self.add_child(b) @@ -83,7 +83,7 @@ class MultipleChoice(Window): def set_image(self, image): if (not isinstance(image, pygame.Surface)): image = pygame.image.load(image) - image = Image(self.rect, pygame.Rect(500, 40, 20, 20), 1, image) + image = gui.Image(self.rect, pygame.Rect(500, 40, 20, 20), 1, image) self.add_child(image) def set_win_points(self, points): @@ -107,7 +107,7 @@ class MultipleChoice(Window): self.windows_controller.game_man.add_points(-self.lose_points) self.s_incorrect.play() self.windows_controller.close_active_window() - + if (self.opportinities == 0): self.windows_controller.windows["info_challenge_window"].update_content(u"Perdiste", u"Qué lástima, no era correcta, \nperdiste %s puntos en tu barra de %s. \nLee la biblioteca o pregunta al maestro/a." % (self.lose_points, self.challenges_creator.game_man.get_lowest_bar().label)) self.windows_controller.set_active_window("info_challenge_window") @@ -262,9 +262,9 @@ class TrueOrFalse(MultipleChoice): self.answers = ["waiting", "waiting", "waiting", "waiting", "waiting"] self.perdio = False -class InfoChallenge(Window): +class InfoChallenge(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller, challenges_creator, text_intro, text_result_good, text_result_bad, bg_color=(0, 0, 0)): - Window.__init__(self, container, rect, frame_rate, windows_controller, "info_challenge_window", bg_color) + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "info_challenge_window", bg_color) self.set_bg_image("assets/windows/window_2.png") self.challenges_creator = challenges_creator @@ -273,9 +273,9 @@ class InfoChallenge(Window): self.add_button(self.btn_continue) # Texts - self.title = utilities.Text(rect, 40, 40, 1, "Verdadero o Falso", 45, pygame.Color("blue")) - self.text = utilities.TextBlock(rect, 40, 120, 1, "Texto de prueba", 35, pygame.Color("black")) - self.image = utilities.Image(rect, pygame.Rect(640, 240, 80, 80), 1, "challenges/ninio_normal.png") + self.title = gui.Text(rect, 40, 40, 1, "Verdadero o Falso", 45, pygame.Color("blue")) + self.text = gui.TextBlock(rect, 40, 120, 1, "Texto de prueba", 35, pygame.Color("black")) + self.image = gui.Image(rect, pygame.Rect(640, 240, 80, 80), 1, "challenges/ninio_normal.png") self.add_child(self.title) self.add_child(self.text) @@ -290,23 +290,23 @@ class InfoChallenge(Window): def _cb_button_click_continue(self, button): self.windows_controller.close_active_window() -class Cooking(Window): +class Cooking(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller, register_id, bg_color=(0, 0, 0)): - Window.__init__(self, container, rect, frame_rate, windows_controller, register_id, bg_color) + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, register_id, bg_color) self.set_bg_image("assets/windows/window_1.png") # Close Button - self.btn_close = TextButton(self.rect, pygame.Rect((910, 0), (30, 30)), 1, "X", 32, (0, 0, 0), self._cb_button_click_close) + self.btn_close = gui.TextButton(self.rect, pygame.Rect((910, 0), (30, 30)), 1, "X", 32, (0, 0, 0), self._cb_button_click_close) self.add_button(self.btn_close) # Some widgets to test DnD self.dnd = [] - self.dnd.append(Image(rect, pygame.Rect(50, 100, 100, 100), 1, "assets/events/ill.jpg")) - self.dnd.append(Image(rect, pygame.Rect(50, 250, 100, 100), 1, "assets/events/caries.jpg")) - self.dnd.append(Image(rect, pygame.Rect(50, 400, 100, 100), 1, "assets/events/unkown.png")) + self.dnd.append(gui.Image(rect, pygame.Rect(50, 100, 100, 100), 1, "assets/events/ill.jpg")) + self.dnd.append(gui.Image(rect, pygame.Rect(50, 250, 100, 100), 1, "assets/events/caries.jpg")) + self.dnd.append(gui.Image(rect, pygame.Rect(50, 400, 100, 100), 1, "assets/events/unkown.png")) - self.trash = Image(rect, pygame.Rect(500, 200, 200, 200), 1, "assets/challenges/trash.png") + self.trash = gui.Image(rect, pygame.Rect(500, 200, 200, 200), 1, "assets/challenges/trash.png") for w in self.dnd: self.add_child(w) @@ -316,11 +316,11 @@ class Cooking(Window): self.mouse_mode = 0 def handle_mouse_down(self, (x, y)): - Window.handle_mouse_down(self, (x, y)) + gui.Window.handle_mouse_down(self, (x, y)) self.mouse_mode = 1 def handle_mouse_up(self, pos): - Window.handle_mouse_up(self, pos) + gui.Window.handle_mouse_up(self, pos) for widget in self.dnd: if self.trash.contains_point(widget.rect_absolute.centerx, widget.rect_absolute.centery): self.remove_child(widget) diff --git a/Saludame.activity/customization.py b/Saludame.activity/customization.py index 35e1b9b..329d762 100755 --- a/Saludame.activity/customization.py +++ b/Saludame.activity/customization.py @@ -2,10 +2,9 @@ import os import pygame -import window -import widget import utilities import animation +import gui from gettext import gettext as _ @@ -50,24 +49,24 @@ TANGO_PALETTE = [ COLORS_SOCKS = TANGO_PALETTE COLORS_SHOES = TANGO_PALETTE -class CustomizationWindow(window.Window): +class CustomizationWindow(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller, character): - window.Window.__init__(self, container, rect, frame_rate, windows_controller, "customization_window") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "customization_window") self.set_bg_image("assets/windows/window_2.png") kid_rect = pygame.Rect((20, 20), (1, 1)) self.kid = CustomizatedKid(self.rect, kid_rect, 1, character) self.add_child(self.kid) - #self.btn_close = utilities.TextButton(self.rect, pygame.Rect((910, 2), (30, 30)), 1, "X", 30, (0, 0, 0), self._cb_button_click_close) + #self.btn_close = gui.TextButton(self.rect, pygame.Rect((910, 2), (30, 30)), 1, "X", 30, (0, 0, 0), self._cb_button_click_close) self.btn_close = utilities.get_accept_button(self.rect, pygame.Rect((400, 500), (1, 1)), _("Continue"), self._cb_button_click_close) button_back = pygame.image.load("customization/customization_button.png").convert() - self.btn_hair = utilities.TextButton2(self.rect, pygame.Rect((500, 120), (70, 30)), 1, _("Hair"), 30, (255, 255, 255), button_back, self._cb_button_hair) - self.btn_skin = utilities.TextButton2(self.rect, pygame.Rect((500, 200), (70, 30)), 1, _("Skin"), 30, (255, 255, 255), button_back, self._cb_button_skin) - self.btn_socks = utilities.TextButton2(self.rect, pygame.Rect((500, 280), (70, 30)), 1, _("Socks"), 30, (255, 255, 255), button_back, self._cb_button_socks) - self.btn_shoes = utilities.TextButton2(self.rect, pygame.Rect((500, 360), (70, 30)), 1, _("Shoes"), 30, (255, 255, 255), button_back, self._cb_button_shoes) + self.btn_hair = gui.TextButton2(self.rect, pygame.Rect((500, 120), (70, 30)), 1, _("Hair"), 30, (255, 255, 255), button_back, self._cb_button_hair) + self.btn_skin = gui.TextButton2(self.rect, pygame.Rect((500, 200), (70, 30)), 1, _("Skin"), 30, (255, 255, 255), button_back, self._cb_button_skin) + self.btn_socks = gui.TextButton2(self.rect, pygame.Rect((500, 280), (70, 30)), 1, _("Socks"), 30, (255, 255, 255), button_back, self._cb_button_socks) + self.btn_shoes = gui.TextButton2(self.rect, pygame.Rect((500, 360), (70, 30)), 1, _("Shoes"), 30, (255, 255, 255), button_back, self._cb_button_shoes) self.buttons += [self.btn_close, self.btn_hair, self.btn_skin, self.btn_socks, self.btn_shoes] self.widgets += [self.btn_close, self.btn_hair, self.btn_skin, self.btn_socks, self.btn_shoes] @@ -109,10 +108,10 @@ class CustomizationWindow(window.Window): MALE_PATH = os.path.normpath("customization/boy.png") FEMALE_PATH = os.path.normpath("customization/girl.png") -class CustomizatedKid(widget.Widget): +class CustomizatedKid(gui.Widget): def __init__(self, container, rect, frame_rate, character): - widget.Widget.__init__(self, container, rect, frame_rate) + gui.Widget.__init__(self, container, rect, frame_rate) self.character = character self.set_gender("male") # Sets the correct picture and applies color mappings @@ -154,4 +153,4 @@ class CustomizatedKid(widget.Widget): if self.dirty: self.dirty = False self.apply_mappings() - return widget.Widget.draw(self, screen) + return gui.Widget.draw(self, screen) diff --git a/Saludame.activity/game.py b/Saludame.activity/game.py index 54ddbeb..a91aa14 100755 --- a/Saludame.activity/game.py +++ b/Saludame.activity/game.py @@ -54,7 +54,7 @@ class Main(): import challenges_creator import customization import sound_manager - import windows_controller + import saludame_windows_controller # Optimizes sound quality and buffer for quick loading pygame.mixer.quit() # When executting from sugar pygame it's already initialized @@ -78,7 +78,7 @@ class Main(): clock = pygame.time.Clock() # windows_controller asociado al screen - self.windows_controller = windows_controller.WindowsController(screen, None) + self.windows_controller = saludame_windows_controller.SaludameWindowsController(screen, None) # Initialize sound_manager, game_manager, character, actions and menu. sound_manager.SoundManager() diff --git a/Saludame.activity/gui/__init__.py b/Saludame.activity/gui/__init__.py new file mode 100644 index 0000000..33a38da --- /dev/null +++ b/Saludame.activity/gui/__init__.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- + +import gui.widget, window, windows_controller, utilities + +Widget = widget.Widget +Window = window.Window +WindowsController = windows_controller.WindowsController +Text = utilities.Text +Image = utilities.Image +Button = utilities.Button +ImageButton = utilities.ImageButton +TextButton = utilities.TextButton +TextButton2 = utilities.TextButton2 +TextBlock = utilities.TextBlock +get_font = utilities.get_font diff --git a/Saludame.activity/gui/utilities.py b/Saludame.activity/gui/utilities.py new file mode 100644 index 0000000..fa149e0 --- /dev/null +++ b/Saludame.activity/gui/utilities.py @@ -0,0 +1,228 @@ +# -*- 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) + + 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 + +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 diff --git a/Saludame.activity/widget.py b/Saludame.activity/gui/widget.py index 3f5b582..3f5b582 100755 --- a/Saludame.activity/widget.py +++ b/Saludame.activity/gui/widget.py diff --git a/Saludame.activity/window.py b/Saludame.activity/gui/window.py index 53d126d..1c725c4 100755 --- a/Saludame.activity/window.py +++ b/Saludame.activity/gui/window.py @@ -2,10 +2,6 @@ import pygame -from utilities import * - -BLACK = pygame.Color("black") - class Window: # Una ventana contiene 'n' widgets diff --git a/Saludame.activity/windows_controller.py b/Saludame.activity/gui/windows_controller.py index 7d5e6c1..e6a3adf 100755 --- a/Saludame.activity/windows_controller.py +++ b/Saludame.activity/gui/windows_controller.py @@ -2,65 +2,35 @@ import pygame from window import Window -import sys from utilities import Text, TextBlock -import challenges_creator -import customization -import main_window -import challenges - """ Clase encargada del control de ventanas """ class WindowsController: - def __init__(self, screen, game_manager): - - #game manager - self.game_man = game_manager - + def __init__(self, screen): internal_size = 1200, 780 # The game is meant to run in this resolution self.scaled_game = ScaledGame(screen, internal_size) self.screen = self.scaled_game.get_internal_screen() - self.windows = {} # Diccionario de ventanas. Aca se encuentran todas las ventanas del programa + self.windows = {} # Diccionario de ventanas. Aca se encuentran todas las ventanas del programa self.windows_stack = [] # Stack de ventanas para el control de venta activa. Aca se enceuntra el stack de ventanas abiertas self.reload_main = True - self.next_update_list = [] + self.next_update_list = [] # Keeps track of updates done to the screen # Tooltips self.showing_tooltip = False self.active_tooltip_bg = None self.active_tooltip = None - - def create_windows_and_activate_main(self, app_loader, clock, bars_loader): - """ - Creates windows and set the main_window as active window - """ - # Challenges - cha_creator = challenges_creator.ChallengesCreator(self.screen.get_rect(), pygame.Rect((250, 30), (934, 567)), 1, self, self.game_man, (40, 40, 200)) - cha_creator.create_challenges() - self.game_man.challenges_creator = cha_creator - - info_master_challenge = challenges.InfoChallenge(self.screen.get_rect(), pygame.Rect((250, 30), (934, 567)), 1, self, cha_creator, u"¡Felicitaciones! \nHas completado el nivel actual. Para pasar de nivel \ndebes contestar bien la siguiente pregunta. \n\n¡¡Suerte!!", u"Felicitaciones, has pasado de nivel. \nSe han desbloqueado nuevas acciones, \n¿te animás a encontrarlas?", u"Contestaste incorrectamente, \ntendrás que intentar pasar de nivel más adelante") - - # Customization Window - customization_window = customization.CustomizationWindow(self.screen.get_rect(), pygame.Rect((250, 30), (934, 567)), 1, self, app_loader.get_character()) - - # Main Window - main_win = main_window.MainWindow(self.screen.get_rect(), self.screen.get_rect(), 1, clock, self, cha_creator, bars_loader, self.game_man) - - # Activate Main window - self.set_active_window("main_window") - self.update(0) - - # Activate Customization over main window - #self.set_active_window("customization_window") - ##### Windows ##### + def get_screen(self): + return self.screen + + + # Windows def close_active_window(self): self.windows_stack[-1].repaint = True # Solo puede ser llamado por la ventana activa e implica @@ -75,9 +45,6 @@ class WindowsController: for win in self.windows_stack[-1].windows: if isinstance(win, Window): win.enable_repaint() - - def get_screen(self): - return self.screen def set_active_window(self, window_key): if window_key <> "main_window": @@ -90,67 +57,20 @@ class WindowsController: self.windows[id] = window def unregister_window(self, window): - del self.windows[window.register_id] + try: + del self.windows[window.register_id] + except: + pass def show_window_hierarchy(self, window): - sys.stdout.write(window.get_register_id()) + print window.get_register_id() W = [] for win in window.windows: W.append(win.register_id) print(" (%s)" % (W)) - - ##### BACKGROUND ##### - def set_environment(self, environment): - self.windows["kid"].set_environment(environment) - - ##### CLOTHES ##### - - def update_clothes(self): - self.windows["kid"].update_clothes() - ##### Actions ##### - def show_action_animation(self, action): - """ - Display an action animation at panel and kid window - """ - self.windows["panel_window"].play_action_animation(action) - self.windows["kid"].play_action_animation(action) - - def stop_actual_action_animation(self): - self.windows["panel_window"].stop_action_animation() - self.windows["kid"].stop_action_animation() - - ##### Events ##### - def add_personal_event(self, event): - self.windows["panel_window"].add_personal_event(event) - - if event.kid_message: - self.show_kid_message(event.kid_message, event.message_time_span) - - def remove_personal_event(self, event): - self.windows["panel_window"].remove_personal_event(event) - - def add_social_event(self, event): - self.windows["panel_window"].add_social_event(event) - - if event.person_path: - self.windows["kid"].add_social_event(event) - - def remove_social_event(self, event): - self.windows["panel_window"].remove_social_event(event) - - ##### Messages at ballon ##### - def show_kid_message(self, message, message_time_span): - print "mostrar mensaje: ", message, " durante: ", message_time_span - self.windows["kid"].show_kid_balloon(message, message_time_span) - - ##### Moods ##### - def set_mood(self, mood): - if self.windows: - self.windows["kid"].set_mood(mood) - - #### Events handlers #### + # Events handlers def handle_mouse_down(self, (x, y)): x, y = self.scaled_game.scale_coordinates((x, y)) self.windows_stack[-1].handle_mouse_down((x, y)) @@ -165,9 +85,9 @@ class WindowsController: def handle_mouse_motion(self, (x, y)): x, y = self.scaled_game.scale_coordinates((x, y)) self.windows_stack[-1].handle_mouse_motion((x, y)) - ########################## - #### Tooltips ##### + + # Tooltips def show_tooltip(self, tooltip): x, y = self.scaled_game.scale_coordinates(pygame.mouse.get_pos()) self.active_tooltip = Text(self.screen.get_rect(), x, y, 1, tooltip, 18, pygame.Color('red'), "tooltip") @@ -191,8 +111,9 @@ class WindowsController: # Lo guardamos en la lista de las proximas actualizaciones self.next_update(self.active_tooltip_bg[1]) self.showing_tooltip = False - ################### + + # Updates to the screen def next_update(self, rect): """ Add a rect that must be updated at next update @@ -284,4 +205,3 @@ class ScaledGame: x = int(display_coordinates[0] / self.scale_factor[0]) y = int(display_coordinates[1] / self.scale_factor[0]) return x, y - diff --git a/Saludame.activity/kid_window.py b/Saludame.activity/kid_window.py index 90cdc40..669e92d 100644 --- a/Saludame.activity/kid_window.py +++ b/Saludame.activity/kid_window.py @@ -2,18 +2,17 @@ import os import pygame -from window import Window -import utilities +import gui import menu_creator import animation BACKGROUND_PATH = os.path.normpath("assets/background/schoolyard_sunny.png") -class KidWindow(Window): +class KidWindow(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller, game_man): - Window.__init__(self, container, rect, frame_rate, windows_controller, "kid") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "kid") self.set_bg_image(pygame.image.load(BACKGROUND_PATH).convert()) self.kid_rect = pygame.Rect((280, 70), (350, 480)) @@ -98,7 +97,7 @@ class KidWindow(Window): self.last_repaint = True self.repaint = True - changes += Window.draw(self, screen, frames) + changes += gui.Window.draw(self, screen, frames) if self.balloon: if not self.balloon.visible: @@ -110,13 +109,13 @@ class KidWindow(Window): return changes -class ExternalCharacter(Window): +class ExternalCharacter(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller, event): self.character = pygame.image.load(event.person_path).convert() rect.size = self.character.get_size() - Window.__init__(self, container, rect, frame_rate, windows_controller, "external_character") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "external_character") self.set_bg_image(self.character) @@ -141,7 +140,7 @@ class ExternalCharacter(Window): changes = [] self.time_span -= 1 self.repaint = True - changes += Window.draw(self, screen, frames) + changes += gui.Window.draw(self, screen, frames) changes += self.message_balloon.draw(screen, frames) return changes else: @@ -150,14 +149,14 @@ class ExternalCharacter(Window): self.dispose() return [self.rect, self.message_balloon.rect] -class MessageBalloon(Window): +class MessageBalloon(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller): background = pygame.image.load("assets/events/balloon.png").convert() rect.size = background.get_size() - Window.__init__(self, container, rect, frame_rate, windows_controller, "balloon") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "balloon") self.bg = (self.windows_controller.screen.subsurface(self.rect).copy()) @@ -172,7 +171,7 @@ class MessageBalloon(Window): self.visible = False def set_text(self, text): - self.text = utilities.TextBlock(self.rect, 35, 40, 1, text, 18, pygame.Color("black")) + self.text = gui.TextBlock(self.rect, 35, 40, 1, text, 18, pygame.Color("black")) self.add_child(self.text) def set_time_span(self, time_span): @@ -184,9 +183,8 @@ class MessageBalloon(Window): if (self.visible): self.time_span -= 1 self.repaint = True - return Window.draw(self, screen, frames) + return gui.Window.draw(self, screen, frames) else: screen.blit(self.bg, self.rect) self.dispose() return [self.rect] - \ No newline at end of file diff --git a/Saludame.activity/main_window.py b/Saludame.activity/main_window.py index ef67e89..264f187 100755 --- a/Saludame.activity/main_window.py +++ b/Saludame.activity/main_window.py @@ -3,17 +3,17 @@ import pygame from gettext import gettext as _ -from window import * +import gui from panel_window import PanelWindow from kid_window import KidWindow import status_bars import animation import utilities -class MainWindow(Window): +class MainWindow(gui.Window): def __init__(self, container, rect, frame_rate, clock, windows_controller, cha_loader, bars_loader, game_man): - Window.__init__(self, container, rect, frame_rate, windows_controller, "main_window") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "main_window") self.clock = clock self.cha_loader = cha_loader @@ -27,41 +27,37 @@ class MainWindow(Window): self.kidW = KidWindow(container, pygame.Rect((227, 0), (973, 609)), 1, windows_controller, game_man) self.windows.append(self.kidW) - #self.windows.append(animation.Apple(pygame.Rect((700, 90), (150, 172)), 10)) self.windows.append(animation.FPS(container, pygame.Rect((1150, 0), (50, 20)), 15, self.clock)) self.windows.append(status_bars.BarsWindow(container, pygame.Rect(0, 0, 227, 590), 5, windows_controller, bars_loader)) self.add_child(Clock(container, pygame.Rect(0, 528, 1, 1), 1, game_man)) - # Challenges - - challenges_button = ImageButton(self.rect, pygame.Rect((1120, 400), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_mc_challenges) + # Challenges + challenges_button = gui.ImageButton(self.rect, pygame.Rect((1120, 400), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_mc_challenges) challenges_button.set_tooltip(_("Multiple choice")) self.add_button(challenges_button) - challenges_button2 = ImageButton(self.rect, pygame.Rect((1120, 500), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_tf_challenges) + challenges_button2 = gui.ImageButton(self.rect, pygame.Rect((1120, 500), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_tf_challenges) challenges_button2.set_tooltip(_("True or false")) self.add_button(challenges_button2) - challenges_button3 = ImageButton(self.rect, pygame.Rect((1120, 300), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_master_challenge) + challenges_button3 = gui.ImageButton(self.rect, pygame.Rect((1120, 300), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_master_challenge) challenges_button3.set_tooltip(_("Master challenge")) self.add_button(challenges_button3) - challenges_button4 = ImageButton(self.rect, pygame.Rect((1120, 200), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_cooking_challenge) + challenges_button4 = gui.ImageButton(self.rect, pygame.Rect((1120, 200), (60, 60)), 1, "challenges/trophy.png", self._cb_button_click_cooking_challenge) challenges_button4.set_tooltip(_("Cooking")) self.add_button(challenges_button4) button_back = pygame.image.load("customization/customization_button.png").convert() - btn_reset = utilities.TextButton2(self.rect, pygame.Rect((1000, 20), (70, 30)), 1, _("Reset"), 30, (255, 255, 255), button_back, self._cb_reset_game) + btn_reset = gui.TextButton2(self.rect, pygame.Rect((1000, 20), (70, 30)), 1, _("Reset"), 30, (255, 255, 255), button_back, self._cb_reset_game) self.add_button(btn_reset) - #stop_animation_button = TextButton(self.rect, pygame.Rect((800, 550), (30, 30)), 1, "Stop animation", 38, (255, 0, 0), self._cb_button_click_stop_animation) - #self.add_button(stop_animation_button) - #btn_change_mood = ImageButton(self.rect, pygame.Rect((1120, 500), (60, 60)), 1, "assets/icons/change.png", self._cb_button_click_change_mood) + #btn_change_mood = gui.ImageButton(self.rect, pygame.Rect((1120, 500), (60, 60)), 1, "assets/icons/change.png", self._cb_button_click_change_mood) #self.add_button(btn_change_mood) - #### Callbacks #### + # Callbacks def _cb_button_click_mc_challenges(self, button): self.cha_loader.get_challenge("mc") self.windows_controller.set_active_window("mc_challenge_window") @@ -92,12 +88,12 @@ class MainWindow(Window): def _cb_reset_game(self, button): self.game_manager.reset_game() -class Clock(Widget): +class Clock(gui.Widget): def __init__(self, container, rect_in_container, frame_rate, game_manager): background = pygame.image.load("assets/layout/clock_background.png").convert() rect_in_container.size = background.get_size() - Widget.__init__(self, container, rect_in_container, frame_rate) + gui.Widget.__init__(self, container, rect_in_container, frame_rate) self.game_manager = game_manager self.background = background @@ -115,7 +111,7 @@ class Clock(Widget): self.frame = pygame.image.load(self.frame_paths[self.frame_index]).convert() def draw(self, screen): - change = Widget.draw(self, screen) + change = gui.Widget.draw(self, screen) if self.game_manager.hour <> self.frame_index: self.frame_index = self.game_manager.hour @@ -126,5 +122,3 @@ class Clock(Widget): screen.blit(self.frame, rect) return change - - diff --git a/Saludame.activity/menu.py b/Saludame.activity/menu.py index e69e0a7..4c88223 100755 --- a/Saludame.activity/menu.py +++ b/Saludame.activity/menu.py @@ -9,8 +9,7 @@ import pygame import os import math -from widget import Widget -from window import Window +import gui import utilities import effects import random @@ -32,7 +31,7 @@ HELP_BUTTON = "assets/menu/menu_help.png" CLOSE_MENU = "close_menu" BACK_MENU = "back_menu" -class Menu(Window): +class Menu(gui.Window): def __init__(self, frame_rate, container, windows_controller, item_list, center, radius, game_manager, font): @@ -40,7 +39,7 @@ class Menu(Window): rect.center = center self.windows_controller = windows_controller self.game_manager = game_manager - Window.__init__(self, container, rect, frame_rate, windows_controller, "menu_window") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "menu_window") self.depth = 0 #it means we are in the root of the menu, mayor values means we are not. @@ -303,7 +302,7 @@ class Menu(Window): item.set_rect_in_container(rect) # Recalculates the absolute coordinates -class Item(Widget): +class Item(gui.Widget): """ Entity that represent an item """ @@ -337,7 +336,7 @@ class Item(Widget): self.rect = pygame.Rect((0, 0), size_and_surface[0]) - Widget.__init__(self, container, self.rect, frame_rate, size_and_surface[1]) + gui.Widget.__init__(self, container, self.rect, frame_rate, size_and_surface[1]) def get_surface(self, font_size, text, bg_image, help_image): font = utilities.get_font(font_size) @@ -385,5 +384,3 @@ class Item(Widget): self.menu.send_action(self.action_id) else: self.menu.send_action(CLOSE_MENU) # if the item have not children and have not an action_id, close the menu - - diff --git a/Saludame.activity/menu_creator.py b/Saludame.activity/menu_creator.py index a027f3e..1c96cb4 100755 --- a/Saludame.activity/menu_creator.py +++ b/Saludame.activity/menu_creator.py @@ -4,6 +4,7 @@ import menu import pygame from gettext import gettext as _ import utilities +import gui items = [ # ("display_name", "path_to_icon.png", "action_id", [Children]), @@ -101,7 +102,7 @@ items = [ MENU_FRAME_RATE = 1 def load_menu(game_manager, center, container, windows_controller): - font = utilities.get_font(20) + font = gui.get_font(20) m = menu.Menu(1, container, windows_controller, [], center, 90, game_manager, font) for item in items: an_item = create_item(item, m, container, font) diff --git a/Saludame.activity/panel_window.py b/Saludame.activity/panel_window.py index 1559b02..b48e7c7 100644 --- a/Saludame.activity/panel_window.py +++ b/Saludame.activity/panel_window.py @@ -5,9 +5,7 @@ import os from gettext import gettext as _ -from window import Window -from widget import Widget -from utilities import * +import gui import animation import customization @@ -19,12 +17,12 @@ WHITE = pygame.Color("white") BAR_BACK_COLOR = pygame.Color("#106168") BAR_FILL_COLOR = pygame.Color("#a742bd") -class PanelWindow(Window): +class PanelWindow(gui.Window): def __init__(self, container, rect, frame_rate, windows_controller): self.timing = 1 # la idea de timing es llevar una cuenta adentro, de los frames que fueron pasando - Window.__init__(self, container, rect, frame_rate, windows_controller, "panel_window") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "panel_window") self.set_bg_image(PANEL_BG_PATH) @@ -43,13 +41,13 @@ class PanelWindow(Window): self.active_personal_events = [] # tuple (event, button) self.index_personal_event = 0 - personal_next = ImageButton(self.rect_personal, pygame.Rect(105, 80, 30, 30), 1, "assets/events/go-next.png", self._cb_button_click_personal_next) - personal_back = ImageButton(self.rect_personal, pygame.Rect(0, 80, 30, 30), 1, "assets/events/go-back.png", self._cb_button_click_personal_back) + personal_next = gui.ImageButton(self.rect_personal, pygame.Rect(105, 80, 30, 30), 1, "assets/events/go-next.png", self._cb_button_click_personal_next) + personal_back = gui.ImageButton(self.rect_personal, pygame.Rect(0, 80, 30, 30), 1, "assets/events/go-back.png", self._cb_button_click_personal_back) self.add_button(personal_next) self.add_button(personal_back) - self.count_personal_events = Text(self.rect_personal, 50, 82, 1, "%s/%s" % (self.index_personal_event, len(self.active_personal_events)), 20, pygame.Color("black")) + self.count_personal_events = gui.Text(self.rect_personal, 50, 82, 1, "%s/%s" % (self.index_personal_event, len(self.active_personal_events)), 20, pygame.Color("black")) self.add_child(self.count_personal_events) self.b_event_personal = None # Visible event at panel @@ -60,24 +58,24 @@ class PanelWindow(Window): self.active_social_events = [] # tuple (event, button) self.index_social_event = 0 - social_next = ImageButton(self.rect_social, pygame.Rect(105, 80, 30, 30), 1, "assets/events/go-next.png", self._cb_button_click_social_next) - social_back = ImageButton(self.rect_social, pygame.Rect(0, 80, 30, 30), 1, "assets/events/go-back.png", self._cb_button_click_social_back) + social_next = gui.ImageButton(self.rect_social, pygame.Rect(105, 80, 30, 30), 1, "assets/events/go-next.png", self._cb_button_click_social_next) + social_back = gui.ImageButton(self.rect_social, pygame.Rect(0, 80, 30, 30), 1, "assets/events/go-back.png", self._cb_button_click_social_back) self.add_button(social_next) self.add_button(social_back) - self.count_social_events = Text(self.rect_social, 50, 82, 1, "%s/%s" % (self.index_social_event, len(self.active_social_events)), 20, pygame.Color("black")) + self.count_social_events = gui.Text(self.rect_social, 50, 82, 1, "%s/%s" % (self.index_social_event, len(self.active_social_events)), 20, pygame.Color("black")) self.add_child(self.count_social_events) self.b_event_social = None # Visible event at panel # Customization - customization_button = ImageButton(self.rect, pygame.Rect(885, 0, 1, 1), 1, "assets/layout/customization.png", self._cb_button_click_customization) + customization_button = gui.ImageButton(self.rect, pygame.Rect(885, 0, 1, 1), 1, "assets/layout/customization.png", self._cb_button_click_customization) customization_button.set_tooltip(_("Customization module")) self.add_button(customization_button) # Info - info_button = ImageButton(self.rect, pygame.Rect(953, 0, 1, 1), 1, "assets/layout/info.png", self._cb_button_click_stop_action) + info_button = gui.ImageButton(self.rect, pygame.Rect(953, 0, 1, 1), 1, "assets/layout/info.png", self._cb_button_click_stop_action) self.add_button(info_button) # Environment @@ -91,10 +89,10 @@ class PanelWindow(Window): weather = self.windows_controller.game_man.current_weather file_path = "assets/events/weather/" + weather + ".png" - self.weather_button = ImageButton(self.rect, pygame.Rect(51, 34, 1, 1), 1, file_path) + self.weather_button = gui.ImageButton(self.rect, pygame.Rect(51, 34, 1, 1), 1, file_path) self.add_button(self.weather_button) - ########## Actions ########## + # Actions def set_active_action(self, action): self.actual_action = action if action.window_animation_path: @@ -123,10 +121,10 @@ class PanelWindow(Window): self.action_progress_bar = None self.repaint = True - ########## Events ########## + # Events def add_personal_event(self, event): if not event in self.active_personal_events: - b_event_personal = ImageButton(self.rect_personal, pygame.Rect(23, 3, 100, 100), 1, pygame.image.load("assets/events/%s" % (event.picture)).convert()) + b_event_personal = gui.ImageButton(self.rect_personal, pygame.Rect(23, 3, 100, 100), 1, pygame.image.load("assets/events/%s" % (event.picture)).convert()) event_info = "%s \n" % (event.description) @@ -171,7 +169,7 @@ class PanelWindow(Window): def add_social_event(self, event): if not event in self.active_social_events: - b_event_social = ImageButton(self.rect_social, pygame.Rect(23, 3, 100, 100), 1, pygame.image.load("assets/events/%s" % (event.picture)).convert()) + b_event_social = gui.ImageButton(self.rect_social, pygame.Rect(23, 3, 100, 100), 1, pygame.image.load("assets/events/%s" % (event.picture)).convert()) event_info = "%s \n" % (event.description) @@ -236,12 +234,12 @@ class PanelWindow(Window): self.timing += 1 - #### Actions #### + # Actions if self.on_animation and self.actual_animation and self.timing % self.actual_animation.frame_rate == 0: if self.timing > 12: self.timing = 0 - #### Events #### + # Events self.surf_personal.fill(WHITE) self.surf_social.fill(WHITE) @@ -251,8 +249,7 @@ class PanelWindow(Window): return [self.rect] - ########### Buttons Callbacks ########### - + # Buttons Callbacks def _cb_button_click_personal(self, button): if game.set_library_function: game.set_library_function("99-Eventos.html") #diarrhea") @@ -299,7 +296,7 @@ class PanelWindow(Window): def _cb_button_click_stop_action(self, nutton): self.stop_action_animation() -class ActionProgressBar(Widget): +class ActionProgressBar(gui.Widget): """ Shows the progress of the active action """ @@ -308,7 +305,7 @@ class ActionProgressBar(Widget): self.action = action surface = pygame.image.load("assets/layout/main_bar_back.png").convert_alpha() - Widget.__init__(self, container, rect_in_container, frame_rate) + gui.Widget.__init__(self, container, rect_in_container, frame_rate) self.background = surface # Borders of the bar self.surface = surface.copy() # Actual surface to blit in the screen, _prepare_surface diff --git a/Saludame.activity/saludame_windows_controller.py b/Saludame.activity/saludame_windows_controller.py new file mode 100644 index 0000000..9ff9577 --- /dev/null +++ b/Saludame.activity/saludame_windows_controller.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- + +import pygame + +import main_window +import challenges_creator +import customization +import challenges +import gui + +""" +Clase encargada del control de ventanas +""" +class SaludameWindowsController(gui.WindowsController): + + def __init__(self, screen, game_manager): + gui.WindowsController.__init__(self, screen) + self.game_man = game_manager + + def create_windows_and_activate_main(self, app_loader, clock, bars_loader): + """ + Creates windows and set the main_window as active window + """ + # Challenges + cha_creator = challenges_creator.ChallengesCreator(self.screen.get_rect(), pygame.Rect((250, 30), (934, 567)), 1, self, self.game_man, (40, 40, 200)) + cha_creator.create_challenges() + self.game_man.challenges_creator = cha_creator + + info_master_challenge = challenges.InfoChallenge(self.screen.get_rect(), pygame.Rect((250, 30), (934, 567)), 1, self, cha_creator, u"¡Felicitaciones! \nHas completado el nivel actual. Para pasar de nivel \ndebes contestar bien la siguiente pregunta. \n\n¡¡Suerte!!", u"Felicitaciones, has pasado de nivel. \nSe han desbloqueado nuevas acciones, \n¿te animás a encontrarlas?", u"Contestaste incorrectamente, \ntendrás que intentar pasar de nivel más adelante") + + # Customization Window + customization_window = customization.CustomizationWindow(self.screen.get_rect(), pygame.Rect((250, 30), (934, 567)), 1, self, app_loader.get_character()) + + # Main Window + main_win = main_window.MainWindow(self.screen.get_rect(), self.screen.get_rect(), 1, clock, self, cha_creator, bars_loader, self.game_man) + + # Activate Main window + self.set_active_window("main_window") + self.update(0) + + # Activate Customization over main window + self.set_active_window("customization_window") + + # BACKGROUND + def set_environment(self, environment): + self.windows["kid"].set_environment(environment) + + + # CLOTHES + def update_clothes(self): + self.windows["kid"].update_clothes() + + + # Actions + def show_action_animation(self, action): + """ + Display an action animation at panel and kid window + """ + self.windows["panel_window"].play_action_animation(action) + self.windows["kid"].play_action_animation(action) + + def stop_actual_action_animation(self): + self.windows["panel_window"].stop_action_animation() + self.windows["kid"].stop_action_animation() + + + # Events + def add_personal_event(self, event): + self.windows["panel_window"].add_personal_event(event) + + if event.kid_message: + self.show_kid_message(event.kid_message, event.message_time_span) + + def remove_personal_event(self, event): + self.windows["panel_window"].remove_personal_event(event) + + def add_social_event(self, event): + self.windows["panel_window"].add_social_event(event) + + if event.person_path: + self.windows["kid"].add_social_event(event) + + def remove_social_event(self, event): + self.windows["panel_window"].remove_social_event(event) + + + # Messages at ballon + def show_kid_message(self, message, message_time_span): + print "mostrar mensaje: ", message, " durante: ", message_time_span + self.windows["kid"].show_kid_balloon(message, message_time_span) + + # Moods + def set_mood(self, mood): + if self.windows: + self.windows["kid"].set_mood(mood) + \ No newline at end of file diff --git a/Saludame.activity/status_bars.py b/Saludame.activity/status_bars.py index 4db7dd6..4ac8a2a 100755 --- a/Saludame.activity/status_bars.py +++ b/Saludame.activity/status_bars.py @@ -5,7 +5,7 @@ from gettext import gettext as _ import status_bars_creator import utilities -from window import * +import gui import game_manager DEFAULT_BARS_VALUES = 50.0 @@ -38,12 +38,12 @@ SUB_BAR_TEXT_COLOR = "#ffffff" # ****************** VISUALES ****************** -class BarsWindow(Window): +class BarsWindow(gui.Window): """ Clase que representa la ventana de las barras de estado del juego. """ def __init__(self, container, rect, frame_rate, windows_controller, bars_loader): - Window.__init__(self, container, rect, frame_rate, windows_controller, "bars_window") + gui.Window.__init__(self, container, rect, frame_rate, windows_controller, "bars_window") # rect and surface: self.rect.size = (227, 590) @@ -134,7 +134,7 @@ class Accordeon: section.move_up() section.compress() -class BarSection(Window): +class BarSection(gui.Window): """ Clase que contiene un conjunto de BarDisplay, y las muestra por pantalla @@ -143,7 +143,7 @@ class BarSection(Window): def __init__(self, windows_controller, container, name, root_bar, children_bar, size, position, icon_path): rect = pygame.Rect(position, size) - Window.__init__(self, container, rect, 1, windows_controller, "bar_section_window") + gui.Window.__init__(self, container, rect, 1, windows_controller, "bar_section_window") # section attributes self.name = name @@ -156,7 +156,7 @@ class BarSection(Window): #label_rect = pygame.Rect((0,0), label_render.get_size()) #label_rect.right = self.rect.right - 8 pos = self.rect.right - 8, 0 - label_widget = utilities.Text(self.rect, pos[0], pos[1], 1, self.name, 16, pygame.Color(TEXT_COLOR), utilities.Text.ALIGN_RIGHT, True, True) + label_widget = gui.Text(self.rect, pos[0], pos[1], 1, self.name, 16, pygame.Color(TEXT_COLOR), gui.Text.ALIGN_RIGHT, True, True) # visuals self.root_bar_display = BarDisplay(BAR_HEIGHT, (size[0] - 2), (BAR_OFFSET_X, SECTION_TOP_PADDING), self.root_bar, ROOT_BAR_PARTITIONS) @@ -168,7 +168,7 @@ class BarSection(Window): if icon_path: icon = pygame.image.load(icon_path).convert() - self.icon = Widget(self.rect, pygame.Rect((0, 0), icon.get_size()), 1, icon) + self.icon = gui.Widget(self.rect, pygame.Rect((0, 0), icon.get_size()), 1, icon) self.fixed_widgets.append(self.icon) else: self.icon = None @@ -251,7 +251,7 @@ class BarSection(Window): display_list.append(display) return display_list -class BarDisplay(Widget): +class BarDisplay(gui.Widget): """ Clase que se encarga de representar visualmente a una barra, manteniéndose actualizada según los incrementos o decrementos de la barra representada. @@ -260,7 +260,7 @@ class BarDisplay(Widget): def __init__(self, height, width, position, status_bar, color_partitions): rect = pygame.Rect(position, (width, height)) - Widget.__init__(self, pygame.Rect(0, 0, width, height), rect, 1) + gui.Widget.__init__(self, pygame.Rect(0, 0, width, height), rect, 1) # attributes self.status_bar = status_bar @@ -313,13 +313,13 @@ class BarDisplay(Widget): return color return sorted(self.color_partitions.values())[-1] -class ScoreSection(Widget): +class ScoreSection(gui.Widget): """ Sección que muestra la barra de puntaje principal. """ def __init__(self, bar, container, size, position, level): rect = pygame.Rect(position, size) - Widget.__init__(self, container, rect, 1) + gui.Widget.__init__(self, container, rect, 1) # attributes self.name = "score section" diff --git a/Saludame.activity/utilities.py b/Saludame.activity/utilities.py index 782bdb3..c355246 100755 --- a/Saludame.activity/utilities.py +++ b/Saludame.activity/utilities.py @@ -1,165 +1,13 @@ # -*- coding: utf-8 -*- -# Utilitarios: Text, Button (abstract), ImageButton, TextButton - -from widget import * +import gui 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 +import effects 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) + return gui.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 @@ -178,79 +26,11 @@ def get_color_tuple(color): 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 +# Fonts - creates an alias for the get_font function +get_font = gui.get_font -#### Paths controls #### +# Paths controls def check_directory(directory): try: print directory -- cgit v0.9.1