diff options
Diffstat (limited to 'Saludame.activity/window.py')
-rwxr-xr-x | Saludame.activity/window.py | 198 |
1 files changed, 69 insertions, 129 deletions
diff --git a/Saludame.activity/window.py b/Saludame.activity/window.py index 5ac77f2..ceb8341 100755 --- a/Saludame.activity/window.py +++ b/Saludame.activity/window.py @@ -4,112 +4,83 @@ import pygame import os import menu_creator import animation -import utilities import status_bars +from utilities import * + BLACK = pygame.Color("black") BACKGROUND_PATH = os.path.normpath("assets/background/background.png") class Window: - def __init__(self, rect, frame_rate, background_color): + # Una ventana contiene 'n' widgets + + def __init__(self, rect, frame_rate, background, screen, windows_controller): self.rect = rect self.frame_rate = frame_rate + self.background = background self.surface = pygame.Surface((rect.width, rect.height)) - self.background_color = background_color - self.surface.fill(self.background_color) - - def draw(self, screen): - self.surface.fill(self.background_color) - screen.blit(self.surface, self.rect) - return [self.rect] - - -class BlinkWindow(Window): - - def __init__(self, rect, frame_rate, background_color): - Window.__init__(self, rect, frame_rate, background_color) - self.par = True - - def draw(self, screen): - self.par = not self.par - - if self.par: - self.surface.fill(self.background_color) - else: - self.surface.fill(BLACK) + self.screen = screen + self.windows_controller = windows_controller + + self.widgets = [] # Lista de widgets que contiene la ventana + self.windows = [] # Lista de ventanas que contiene la ventana + self.buttons = [] # Lista de botones que contiene la ventana + + self.repaint = True + + def draw(self, screen, frames): + if self.repaint: # Solo actualizamos el fondo de la ventana cuando hagamos un 'repaint' + # De otra forma solo actualizamos los widgets + if ((not isinstance(self.background, pygame.Surface)) and (not isinstance(self.background, tuple)) and (not isinstance(self.background, pygame.Color))): + # Si entramos aca es porque es una imagen que tenemos que convertir + self.surface = pygame.image.load(self.background).convert_alpha() + else: + self.surface.fill(self.background) # En este caso background se corresponde con un color - screen.blit(self.surface, self.rect) - - return [self.rect] - - -class StatusWindow(Window): - - def __init__(self, rect, frame_rate, background_color): - self.rect = rect - self.frame_rate = frame_rate - self.surface = pygame.Surface(rect.size) - self.background_color = background_color + screen.blit(self.surface, self.rect) # Pintamos el "fondo" de la ventana + + self.repaint = False - self.surface.fill(self.background_color) - - self.bars = [] - self.bars.append(IdleStatusBar(pygame.Rect(20, 15, 460, 30), pygame.Color("green"))) - self.bars.append(IdleStatusBar(pygame.Rect(20, 55, 460, 30), pygame.Color("blue"), 35)) - self.bars.append(IdleStatusBar(pygame.Rect(20, 95, 460, 30), pygame.Color("yellow"), 65)) - - def draw(self, screen): - self.surface.fill(self.background_color) + self.widgets.extend(self.buttons) # Agregamos los botones a la lista de widgets para que sean pintados - for bar in self.bars: - bar.draw(self.surface) + for widget in self.widgets: + if (frames % widget.frame_rate == 0): + widget.draw(screen) # Pintamos los widgets que "contiene" la ventana - screen.blit(self.surface, self.rect) + for win in self.windows: + if (frames % win.frame_rate == 0): + win.draw(screen) # Le decimos a cada ventana que se pinte - return [self.rect] - - -class StatusBar: - - def __init__(self, rect, color, value=0): - self.rect = rect - self.color = color - self.surface = pygame.Surface(rect.size) - self.value = value - - -class IdleStatusBar(StatusBar): + return self.rect - def __init__(self, rect, color, value=0): - StatusBar.__init__(self, rect, color, value) - - def draw(self, screen): - self.value = (self.value + 1) % 101 - factor = self.value / 100.0 - - rect = pygame.Rect((0, 0), self.rect.size) - rect.width *= factor - - self.surface.fill(BLACK) - self.surface.fill(self.color, rect) - screen.blit(self.surface, self.rect) + def handle_mouse_down(self, (x, y)): + for button in self.buttons: + if button.contains_point(x, y): + button.on_mouse_click() - return [self.rect] + def handle_mouse_over(self, (x, y)): + for button in self.buttons: + if button.contains_point(x, y): + if(not button.over): + button.on_mouse_over() + button.over = True + else: + # Ineficiente! Por ahora lo dejo asi para PROBAR + # Esta todo el tiempo haciendo esto! Cambiar + button.over = False + button.on_mouse_out() class KidWindow(Window): - def __init__(self, rect, frame_rate): - Window.__init__(self, rect, frame_rate, BLACK) - - self.first = True + def __init__(self, rect, frame_rate, screen, windows_controller): self.background = pygame.image.load(BACKGROUND_PATH).convert() + Window.__init__(self, rect, frame_rate, self.background, screen, windows_controller) kid_rect = pygame.Rect((100, 20), (350, 480)) kid_background = self.background.subsurface(kid_rect) - self.windows = [] self.windows.append(animation.Kid(kid_rect, kid_background, 1)) def draw(self, screen): @@ -135,62 +106,31 @@ class KidWindow(Window): changes.extend(win.draw(screen)) return changes -class MainWindow(): +class MainWindow(Window): - class ChallengesButton(utilities.ImageButton): - - def __init__(self, rect, x, y): - utilities.ImageButton.__init__(self, rect, x, y, "challenges/trophy.png") - self.frame_rate = 1 - - def on_mouse_click(self, windows_controller): - windows_controller.set_active_window("challenges") + def __init__(self, rect, frame_rate, clock, screen, windows_controller): + Window.__init__(self, rect, frame_rate, (0, 0, 0), screen, windows_controller) - class CustomizationButton(utilities.ImageButton): - - def __init__(self, rect, x, y): - utilities.ImageButton.__init__(self, rect, x, y, "customization/palette.png") - self.frame_rate = 1 - - def on_mouse_click(self, windows_controller): - windows_controller.set_active_window("customization") - - def __init__(self, clock): self.name = "main" self.clock = clock - self.rect = pygame.Rect(0, 0, 1200, 780) + self.windows = [] # Lista de ventanas que 'componen' la ventana principal - #self.windows.append(BlinkWindow(pygame.Rect((700, 0), (500, 140)), 5, pygame.Color("red"))) - #self.windows.append(BlinkWindow(pygame.Rect((700, 150), (500, 140)), 5, pygame.Color("blue"))) - #self.windows.append(StatusWindow(pygame.Rect((700, 300), (500, 140)), 2, pygame.Color("gray"))) - self.windows.append(KidWindow(pygame.Rect((0, 0), (600, 500)), 1)) - self.windows.append(animation.Apple(pygame.Rect((150, 600), (150, 172)), 10)) + + self.windows.append(KidWindow(pygame.Rect((0, 0), (600, 500)), 1, screen, windows_controller)) + self.windows.append(animation.Apple(pygame.Rect((150, 550), (150, 172)), 10)) self.windows.append(menu_creator.load_menu()) - self.windows.append(animation.FPS(pygame.Rect((650, 80), (50, 20)), 15, self.clock)) + self.windows.append(animation.FPS(pygame.Rect((650, 80), (50, 20)), 15, self.clock)) + self.windows.append(status_bars.BarsWindow((700, 90), 1, pygame.Color("gray"))) - self.status_bars = status_bars.BarsWindow((700, 90), 1, pygame.Color("gray")) - self.windows.append(self.status_bars) + challengesButton = ImageButton(self.rect, pygame.Rect((40, 550), (80, 80)), 1, "challenges/trophy.png", self._cb_button_click_challenges) + customizationButton = ImageButton(self.rect, pygame.Rect((100, 550), (80, 80)), 1, "customization/palette.png", self._cb_button_click_customization) - self.buttons = [] - self.buttons.append(MainWindow.CustomizationButton(self.rect, 10, 650)) - self.buttons.append(MainWindow.ChallengesButton(self.rect, 70, 650)) + self.buttons.append(challengesButton) + self.buttons.append(customizationButton) - self.windows.extend(self.buttons) # Buttons are drawn too + def _cb_button_click_challenges(self, button): + self.windows_controller.set_active_window("challenges") - def handle_mouse_down(self, (x, y), windows_controller): - # Temporal para probar el manejo de ventanas entre 'challenges' y 'main' - #windows_controller.set_active_window("challenges") - - for button in self.buttons: - if button.contains_point(x, y): - button.on_mouse_click(windows_controller) - - # Temporal para probar BarsWindow - self.status_bars.on_mouse_click((x,y)) - - def handle_mouse_over(self, (x, y)): - None - - def get_windows(self): - return self.windows - + def _cb_button_click_customization(self, button): + #self.windows_controller.set_active_window("customization") + pass |