Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordmazzone <mazzone.diego@gmail.com>2010-10-01 13:37:51 (GMT)
committer dmazzone <mazzone.diego@gmail.com>2010-10-01 13:37:51 (GMT)
commit9e19bfdeaa98f596253ad85d89f4b7afb8111b61 (patch)
treecddc290d3093bd25b161768e650d0a3e3b123914
parent15e50e2b081aea4334febd5e532c3366524a2abe (diff)
Versión 1: Control de ventamas, widgets, event listeners, jerarquías.
No del todo funcional pero para que vayan viendo el código. Cuando tenga la versión estable y funcional con todo andando subo más comentarios sobre las modificaciones.
-rwxr-xr-xSaludame.activity/challenges.py144
-rw-r--r--Saludame.activity/customization.py15
-rwxr-xr-xSaludame.activity/game.py20
-rwxr-xr-xSaludame.activity/menu_creator.py2
-rwxr-xr-xSaludame.activity/utilities.py134
-rw-r--r--Saludame.activity/widget.py29
-rwxr-xr-xSaludame.activity/window.py198
-rwxr-xr-xSaludame.activity/windows_controller.py (renamed from Saludame.activity/windowsController.py)14
8 files changed, 243 insertions, 313 deletions
diff --git a/Saludame.activity/challenges.py b/Saludame.activity/challenges.py
index 8d460a5..5f5799e 100755
--- a/Saludame.activity/challenges.py
+++ b/Saludame.activity/challenges.py
@@ -5,6 +5,7 @@
import pygame
import os
from utilities import *
+from window import *
S_CORRECT_PATH = os.path.normpath("assets/sound/correct.ogg")
S_OVER_PATH = os.path.normpath("assets/sound/over.ogg")
@@ -13,106 +14,69 @@ I_FRANCIA_PATH = os.path.normpath("assets/challenges/francia.jpg")
FIN_MC = False # Toma el valor True cuando finaliza el juego de multiple choice
-class MultipleChoice:
+class MultipleChoice(Window):
- def __init__(self, rect, frame_rate):
- self.rect = rect
- self.frame_rate = frame_rate
- self.background = pygame.image.load(I_FRANCIA_PATH).convert()
+ def __init__(self, rect, frame_rate, background, screen, windows_controller):
+ Window.__init__(self, rect, frame_rate, background, screen, windows_controller)
- self.question = Text(self.rect.left + 10, self.rect.top + 5, "Cual es la capital de Francia?", 20)
+ ###### Images ######
+ self.image = pygame.image.load(I_FRANCIA_PATH).convert()
+ ####################
- # Boton cerrar
- self.btn_close = CloseButton(self.rect, 770, 5, 30, 30, "X")
-
- self.choices = []
- self.choices += [Choice(self.rect, 20, 40, 200, 20, "Buenos Aires")]
- self.choices += [Choice(self.rect, 20, 70, 200, 20, "Oslo")]
- self.choices += [Choice(self.rect, 20, 100, 200, 20, "Roma")]
- self.choices += [Choice(self.rect, 20, 130, 200, 20, "Paris")]
- self.choices += [Choice(self.rect, 20, 160, 200, 20, "Moscu")]
-
- self.btn_view_answer = ViewAnswer(self.rect, 20, 200, 200, 20, "Me doy por vencido! :(...", self.choices[3])
- self.choices += [self.btn_view_answer]
-
- def draw(self, screen):
- screen.fill((150, 150, 255), self.rect)
- screen.blit(self.background, (self.rect.right - 230, self.rect.top + 30))
- self.question.draw(screen)
- self.btn_close.draw(screen)
- self.btn_view_answer.draw(screen)
- for button in self.choices:
- button.draw(screen)
- return [self.rect]
-
- def handle_mouse_down(self, (x, y), windows_controller):
- global FIN_MC
-
- if (self.btn_close.contains_point(x, y)):
- self.btn_close.on_mouse_click(windows_controller)
-
- else:
- for choice in self.choices:
- if (choice.contains_point(x, y) and not FIN_MC):
- fin = choice.on_mouse_click() # Fin representa el usuario ya contesto bien o se dio por vencido
- if(fin):
- FIN_MC = fin
- self.choices = [self.choices[3], self.choices[5]]
- break # No tiene sentido seguir iterando sobre los botones si ya sabemos cual apreto
-
- def handle_mouse_over(self, (x, y)):
- for choice in self.choices:
- if (choice.contains_point(x, y)):
- if(not choice.over):
- choice.on_mouse_over()
- choice.over = True
- else:
- choice.over = False
- choice.on_mouse_out()
-
- def get_windows(self):
- return [self]
-
-class Choice(Button):
- def __init__(self, rect, x, y, w, h, text):
- Button.__init__(self, rect, x, y, w, h, text)
+ ###### Sounds ######
self.s_correct = pygame.mixer.Sound(S_CORRECT_PATH)
self.s_over = pygame.mixer.Sound(S_OVER_PATH)
self.s_incorrect = pygame.mixer.Sound(S_INCORRECT_PATH)
- self.y = y
- """
- Temporal para simular una respuesta correcta basandonos en la coordenada
- 'y' del rectangulo que la contiene.
- """
+ ####################
- def on_mouse_click(self):
- if(self.y == 130):
- self.s_correct.play()
- return True # Damos por finalizada la pregunta
- else:
- self.s_incorrect.play()
+ # Question
+ self.question = Text(self.rect, 5, 5, 1, "Cual es la capital de Francia?", 40, (0, 255, 0))
+ self.widgets.append(self.question)
- def on_mouse_over(self):
- self.s_over.play()
- self.set_background_color((0, 255, 0))
+ # Close Button
+ self.btn_close = TextButton(self.rect, pygame.Rect((770, 5), (30, 30)), 1, "X", 30, (0, 0, 0), self._cb_button_click_close)
+ self.buttons += [self.btn_close]
- def on_mouse_out(self):
- self.set_background_color((255, 0, 0))
+ ###### Choices ######
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 40), (200, 20)), 1, "Buenos Aires", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 70), (200, 20)), 1, "Oslo", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 100), (200, 20)), 1, "Roma", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 130), (200, 20)), 1, "Paris", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
+ self.buttons += [TextButton(self.rect, pygame.Rect((20, 160), (200, 20)), 1, "Moscu", 30, (255, 255, 255), self._cb_button_click_choice, self._cb_button_over_choice, self._cb_button_out_choice)]
-class ViewAnswer(Button):
- def __init__(self, rect, x, y, w, h, text, resp):
- Button.__init__(self, rect, x, y, w, h, text)
- self.s_over = pygame.mixer.Sound(S_OVER_PATH)
- self.resp = resp
- self.set_background_color((20, 100, 45))
+ # Answer Button
+ self.btn_view_answer = TextButton(self.rect, pygame.Rect(20, 200, 20, 20), 1, "Me doy por vencido! :(...", 30, (255, 20, 20), self._cb_button_click_answer, self._cb_button_over_answer, self._cb_button_out_answer)
+ self.buttons += [self.btn_view_answer]
+
+ france_image = Image(self.rect, pygame.Rect(500, 40, 20, 20), 1, self.image)
+ self.widgets.append(france_image)
+
+ def _cb_button_click_choice(self, button):
+ global FIN_MC
+ if(button.rect.top == 130):
+ self.s_correct.play()
+ FIN_MC = True # Damos por finalizada la pregunta
+ else:
+ self.s_incorrect.play()
- def on_mouse_click(self):
- self.resp.set_background_color((0, 255, 255))
- return True # Damos por finalizada la pregunta
+ def _cb_button_over_choice(self, button):
+ button.switch_color_text((255, 0, 0))
+ button.force_update(self.screen)
+ self.s_over.play()
+
+ def _cb_button_out_choice(self, button):
+ button.switch_color_text((255, 255, 255))
+ button.force_update(self.screen)
+
+ def _cb_button_click_answer(self, button):
+ global FIN_MC
+ FIN_MC = True
- def on_mouse_over(self):
+ def _cb_button_over_answer(self, button):
self.s_over.play()
- self.set_background_color((45, 255, 100))
-
- def on_mouse_out(self):
- self.set_background_color((20, 100, 45))
+
+ def _cb_button_out_answer(self, button):
+ pass
+
+ def _cb_button_click_close(self, button):
+ self.windows_controller.close_active_window()
diff --git a/Saludame.activity/customization.py b/Saludame.activity/customization.py
index 2569969..3505133 100644
--- a/Saludame.activity/customization.py
+++ b/Saludame.activity/customization.py
@@ -8,12 +8,13 @@ from gettext import gettext as _
class CustomizationWindow(window.Window):
- def __init__(self, rect, frame_rate):
- window.Window.__init__(self, rect, frame_rate, pygame.Color("Gray"))
+ def __init__(self, rect, frame_rate, background, screen):
+ window.Window.__init__(self, rect, frame_rate, background, screen)
+ self.screen = screen
self.windows = []
kid_rect = self.rect.move(50, 40)
- self.kid = CustomizatedKid(kid_rect, 1)
+ self.kid = CustomizatedKid(kid_rect, 1, pygame.Color("Black"), screen)
self.windows.append(self.kid)
self.btn_close = utilities.CloseButton(self.rect, 770, 5, 30, 30, "X")
@@ -57,11 +58,11 @@ class CustomizatedKid(window.Window):
"skin": pygame.Color("#803300")
}
- def __init__(self, rect, frame_rate):
- window.Window.__init__(self, rect, frame_rate, pygame.Color("Black"))
+ def __init__(self, rect, frame_rate, background, screen):
+ window.Window.__init__(self, rect, frame_rate, background, screen)
- self.mappings = CustomizatedKid.COLOR_MAP.copy()
- self.set_gender("male")
+ self.mappings = CustomizatedKid.COLOR_MAP.copy()
+ self.set_gender("male")
def draw(self, screen):
screen.blit(self.sprite, self.rect)
diff --git a/Saludame.activity/game.py b/Saludame.activity/game.py
index 8680f4e..5fcf037 100755
--- a/Saludame.activity/game.py
+++ b/Saludame.activity/game.py
@@ -4,7 +4,9 @@ import pygame
import logging
from gettext import gettext as _
-from windowsController import *
+from utilities import *
+
+from windows_controller import *
import window
import challenges
import customization
@@ -51,26 +53,30 @@ class Main():
# This clock is used to keep the game at the desired FPS.
clock = pygame.time.Clock()
+
# Challenges Window
- challenges_window = challenges.MultipleChoice(pygame.Rect((200, 150), (800, 400)), 1)
+ challenges_window = challenges.MultipleChoice(pygame.Rect((200, 150), (800, 400)), 1, (100,45,255), screen, self.windows_controller)
self.windows_controller.add_new_window(challenges_window, "challenges")
-
+
+ """
# Customization Window
- customization_window = customization.CustomizationWindow(pygame.Rect((200, 100), (800, 500)), 1)
+ customization_window = customization.CustomizationWindow(pygame.Rect((200, 100), (800, 500)), 1, pygame.Color("Gray"), screen)
self.windows_controller.add_new_window(customization_window, "customization")
+ """
# Main Window
- main_window = (window.MainWindow(clock))
+ main_window = (window.MainWindow(screen.get_rect(), 1, clock, screen, self.windows_controller))
self.windows_controller.add_new_window(main_window, "main")
# Activamos ventana principal
- self.windows_controller.set_active_window("main")
-
+ self.windows_controller.set_active_window("main")
+
frames = 0
# Main loop
update = True # The first time the screen need to be updated
running = True
+
while running:
if fromSugar:
diff --git a/Saludame.activity/menu_creator.py b/Saludame.activity/menu_creator.py
index 428a9db..d305fb7 100755
--- a/Saludame.activity/menu_creator.py
+++ b/Saludame.activity/menu_creator.py
@@ -11,7 +11,7 @@ example = [
("sport", "assets/icons/icon.png", "Do sports...", [
("run", "assets/icons/icon.png", "Run", None),
("jump rope", "assets/icons/icon.png", "Jump the rope", None),
- ("footbal", "assets/icons/icon.png", "Play footbal", None)
+ ("football", "assets/icons/icon.png", "Play footbal", None)
]),
("sleep", "assets/icons/icon.png", "Go to sleep", None),
("talk", "assets/icons/icon3.png", "talk with a friend", None),
diff --git a/Saludame.activity/utilities.py b/Saludame.activity/utilities.py
index 97d3bdd..294e173 100755
--- a/Saludame.activity/utilities.py
+++ b/Saludame.activity/utilities.py
@@ -1,106 +1,98 @@
# -*- coding: utf-8 -*-
-# Utilitarios
+# Utilitarios: Text, Button (abstract), ImageButton, TextButton
-import pygame
+from widget import *
-class Text:
- def __init__(self, x, y, text, size):
+class Text(Widget):
+ def __init__(self, container, x, y, frame_rate, text, size, color):
self.font = pygame.font.SysFont(None, size)
- self.ren = self.font.render(text, 1, (0, 0, 100))
- self.x = x
- self.y = y
+ self.surface = self.font.render(text, True, color)
+
+ self.text = text
+ Widget.__init__(self, container, self.surface.get_rect(topleft=(x, y)), frame_rate, self.surface, None)
+
+ def switch_color_text(self, color):
+ self.surface = self.font.render(self.text, True, color)
+ return (self)
- def draw(self, screen):
- screen.blit(self.ren, (self.x, self.y))
+class Image(Widget):
+ def __init__(self, container, rect, frame_rate, image):
+
+ if not isinstance(image, pygame.Surface):
+ self.surface = pygame.image.load(image).convert_alpha()
+ else:
+ self.surface = image
+ Widget.__init__(self, container, rect, frame_rate, self.surface)
+
-class Button:
+class Button(Widget):
# Clase abstracta que representa un boton
- def __init__(self, rect, x, y, w, h, text):
- # Agregamos los botones con coordenadas "relativas" a la ventana que los cotiene
- self.rect = pygame.Rect(rect.left + x, rect.top + y, w, h)
- self.text = text
+ 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.font = pygame.font.SysFont(None, 16)
+ self.function_on_mouse_click = cb_click
+ self.function_on_mouse_over = cb_over
+ self.function_on_mouse_out = cb_out
- self.background_color = (255,0,0)
self.over = False
-
+
def contains_point(self, x, y):
- return self.rect.collidepoint(x, y)
-
- def draw(self, surface):
- ren = self.font.render(self.text, 1, (0, 0, 100))
- surface.fill(self.background_color, self.rect)
- surface.blit(ren, (self.rect.left + 5, self.rect.top + 5))
-
- def set_background_color(self, color):
- self.background_color = color
+ return self.rect_in_container.collidepoint(x, y)
- # Eventos sobre el boton... seran sobreescritos por los hijos
-
def on_mouse_click(self):
- None
+ if self.function_on_mouse_click: # if there's a callback setted makes the call
+ self.function_on_mouse_click(self)
def on_mouse_over(self):
- None
+ if self.function_on_mouse_over: # if there's a callback setted makes the call
+ self.function_on_mouse_over(self)
def on_mouse_out(self):
- None
+ if self.function_on_mouse_out: # 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
-class ImageButton:
+ def set_on_mouse_out(self, fn):
+ self.function_on_mouse_out = fn
- # Clase abstracta que representa un boton con una imagen de fondo
+
+class ImageButton(Button):
- def __init__(self, rect, x, y, image):
+ 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):
+ self.image = pygame.image.load(image).convert_alpha()
+
+ 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.image.load(image).convert_alpha()
+ self.surface = image
- self.image = image
-
- # Agregamos los botones con coordenadas "relativas" a la ventana que los cotiene
- self.rect = pygame.Rect(rect.left + x, rect.top + y, image.get_width(), image.get_height())
-
- self.background_color = (0, 0, 0)
- self.over = False
+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(rect, 5, 5, frame_rate, text, size, color)
+ ImageButton.__init__(self, container, rect, frame_rate, self.text.surface, cb_click, cb_over, cb_out)
- def contains_point(self, x, y):
- return self.rect.collidepoint(x, y)
-
- def draw(self, surface):
- surface.blit(self.image, self.rect.topleft)
- return [self.rect]
-
- def set_background_color(self, color):
- self.background_color = color
+ def switch_color_text(self, color):
+ self.surface = self.text.switch_color_text(color).surface
- # Eventos sobre el boton... seran sobreescritos por los hijos
- def on_mouse_click(self):
- None
-
- def on_mouse_over(self):
- None
-
- def on_mouse_out(self):
- None
-
-class CloseButton(Button):
- def __init__(self, rect, x, y, w, h, text):
- Button.__init__(self, rect, x, y, w, h, text)
- self.font = pygame.font.SysFont(None, 30) # Modificamos atributo heredado
- self.background_color = (150, 150, 255) # Modificamos atributo heredado
-
- def on_mouse_click(self, windows_controller):
- windows_controller.close_active_window()
-
def change_color(surface, old_color, new_color):
# No funciona en pygame 1.8.0
#image_pixel_array = pygame.PixelArray(self.sprite)
#image_pixel_array.replace(old_color, new_color)
mapped_int = surface.map_rgb(old_color)
- surface.set_palette_at(mapped_int, new_color[0:3])
-
+ surface.set_palette_at(mapped_int, new_color[0:3])
diff --git a/Saludame.activity/widget.py b/Saludame.activity/widget.py
new file mode 100644
index 0000000..3a542b3
--- /dev/null
+++ b/Saludame.activity/widget.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+
+import pygame
+
+class Widget:
+
+ # Un widget representa cualquier cosa "pintable"
+
+ def __init__(self, container, rect, frame_rate, surface, tooltip=None):
+ self.container = container # Ventana (Rect) que "contiene" al widget
+ self.rect = rect # Rect del widget (relativo al container)
+ self.rect_in_container = pygame.Rect((self.container.left + self.rect.left,
+ self.container.top + self.rect.top),
+ (self.rect.size)) # Rect del widget (absoluto al screen)
+ self.frame_rate = frame_rate
+ self.surface = surface
+
+ # El widget puede (opcionalmente) tener un tooltip
+ self.tooltip = tooltip
+
+ def draw(self, screen):
+ screen.blit(self.surface, self.rect_in_container)
+ return self.rect_in_container
+
+ def force_update(self, screen): # Forzamos la actualizacion del widget independientemente del frame_rate
+ screen.blit(self.surface, self.rect_in_container)
+ pygame.display.update(self.rect_in_container)
+
+
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
diff --git a/Saludame.activity/windowsController.py b/Saludame.activity/windows_controller.py
index 3657022..f9dc09e 100755
--- a/Saludame.activity/windowsController.py
+++ b/Saludame.activity/windows_controller.py
@@ -12,9 +12,10 @@ class WindowsController:
self.reload_main = True
def close_active_window(self):
+ self.windows_stack[-1].repaint = True
# Solo puede ser llamado por la ventana activa e implica
# hacer un pop del stack
- self.windows_stack.pop()
+ self.windows_stack.pop()
if (self.windows_stack[-1].name == "main"):
self.reload_main = True
@@ -25,8 +26,7 @@ class WindowsController:
self.windows[window_key] = window
def handle_mouse_down(self, (x, y)):
- self.windows_stack[-1].handle_mouse_down((x, y), self)
- # Se pasa el controlador a si mismo por si el evento es cerrar la ventana
+ self.windows_stack[-1].handle_mouse_down((x, y))
def handle_mouse_over(self, (x, y)):
self.windows_stack[-1].handle_mouse_over((x, y))
@@ -41,11 +41,9 @@ class WindowsController:
pygame.display.flip() # Actualizamos el screen para hacer visibles los efectos
self.reload_main = False
- changes = []
- # Una ventana puede estar compuesta de varias ventanas a un mismo nivel
- for win in self.windows_stack[-1].get_windows():
- if frames % win.frame_rate == 0:
- changes.extend(win.draw(screen))
+ changes = []
+ if frames % self.windows_stack[-1].frame_rate == 0:
+ changes.extend(self.windows_stack[-1].draw(screen, frames))
if changes:
pygame.display.update(changes)