diff options
author | Samus_ <mail2samus@gmail.com> | 2009-10-16 15:22:06 (GMT) |
---|---|---|
committer | Samus_ <mail2samus@gmail.com> | 2009-10-16 15:22:06 (GMT) |
commit | dca36e62b1079c9ac9c6524cfd9044a1e82b9e86 (patch) | |
tree | 90a4846fad9c22e28f19feb4474cebcb4d94cff9 | |
parent | 1514282ac094a808c77260bfd435d460049c1ea9 (diff) |
changed change_state approach due to cyclic-references
-rw-r--r-- | application.py | 15 | ||||
-rw-r--r-- | estados.py | 122 | ||||
-rw-r--r-- | estados/__init__.py | 5 | ||||
-rw-r--r-- | estados/basestate.py | 22 | ||||
-rw-r--r-- | estados/fin.py | 31 | ||||
-rw-r--r-- | estados/inicio.py | 23 | ||||
-rw-r--r-- | estados/juego.py | 38 | ||||
-rw-r--r-- | estados/start.py | 32 |
8 files changed, 162 insertions, 126 deletions
diff --git a/application.py b/application.py index bba92b7..463a4fc 100644 --- a/application.py +++ b/application.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import gtk, pygtk from util import reparent -from estados import BaseState, Inicio +from estados import * pygtk.require("2.0") @@ -13,6 +13,13 @@ class ApplicationManager(object): -Provee el metodo 'change_state' utilizado para cargar un nuevo bloque interior dentro del marco. -Provee atributo 'state_info' (diccionario compartido que almacena los datos de cada contenido cargado). """ + estados_dict = { + "Inicio": Inicio, + "Start": Start, + "Juego": Juego, + "Fin": Fin, + } + def __init__(self, window): super(ApplicationManager, self).__init__() @@ -23,14 +30,14 @@ class ApplicationManager(object): window.set_title("Fracciones") self.state_info = dict() - self.change_state(Inicio(self)) + self.change_state("Inicio") window.show() def change_state(self, new_state): - assert(isinstance(new_state, BaseState)) - reparent(new_state, new_state.template, self.inner_container) + new_state_instance = self.estados_dict[new_state](self) + reparent(new_state_instance, new_state_instance.template, self.inner_container) def gtk_main_quit(self, userdata=None): diff --git a/estados.py b/estados.py deleted file mode 100644 index bf8bd73..0000000 --- a/estados.py +++ /dev/null @@ -1,122 +0,0 @@ -# -*- coding: utf-8 -*- -import logging -import gtk -import cairo - - -log = logging.getLogger(__name__) - - -class BaseState(object): - """ tareas comunes a todos los estados """ - - def __init__(self, appmanager): - super(BaseState, self).__init__() - - self._appmanager = appmanager - # cuando BaseState se instancia desde una clase heredada self.__class__ es esa clase y no BaseState - # esto nos permite usar la clase misma como llave en el diccionario - self.state_info = appmanager.state_info[self.__class__] if self.__class__ in appmanager.state_info else {} - - - def change_state(self, new_state): - self._appmanager.state_info[self.__class__] = self.state_info - self._appmanager.change_state(new_state) - - -class Inicio(BaseState): - """ - Clase de inicio. - - """ - template = "inicio.glade" - - - def __init__(self, appmanager): - super(Inicio, self).__init__(appmanager) - - - def on_buttonjugar_clicked(self, widget): - log.debug("-> cambiar estado a Start") - self.change_state(Start(self._appmanager)) - - -class Start(BaseState): - """ - Clase para probar las transiciones. - - """ - template = "win.glade" - - - def __init__(self, appmanager): - super(Start, self).__init__(appmanager) - self.image = cairo.ImageSurface.create_from_png("data/start.png") - - - def on_drawingarea_button_press_event(self, widget, event): - #log.debug("-> cambiar estado a Fin") - #self.change_state(Fin(self._appmanager)) - self.change_state(Juego(self._appmanager)) - - - def on_drawingarea_expose_event(self, widget, event): - cr = widget.window.cairo_create() - cr.set_source_surface(self.image, 0, 0) - cr.paint() - - -class Juego(BaseState): - """ - Clase que dibuja la torta. - - """ - template = "juego.glade" - - - def __init__(self, appmanager): - super(Juego, self).__init__(appmanager) - # Aqui tenemos que cargar en la segunda posicion del contenedor vbox - # una clase torta en este caso. - # Si fuera otro tipo de juego estariamos cargando un grid que - # contendria X clases torta. - - - def on_drawingarea_button_press_event(self, widget, event): - log.debug("-> cambiar estado a Fin") - self.change_state(Fin(self._appmanager)) - - - def on_drawingarea_expose_event(self, widget, event): - cr = widget.window.cairo_create() - cr.set_source_surface(self.image, 0, 0) - cr.paint() - - - def on_button_clicked(self, widget): - log.debug("-> cambiar estado a Start") - self.change_state(Fin(self._appmanager)) - - -class Fin(BaseState): - """ - Clase para probar las transiciones. - - """ - template = "win.glade" - - - def __init__(self, appmanager): - super(Fin, self).__init__(appmanager) - self.image = cairo.ImageSurface.create_from_png("data/win.png") - - - def on_drawingarea_button_press_event(self, widget, event): - log.debug("-> cambiar estado a Start") - self.change_state(Start(self._appmanager)) - - - def on_drawingarea_expose_event(self, widget, event): - cr = widget.window.cairo_create() - cr.set_source_surface(self.image, 0, 0) - cr.paint() diff --git a/estados/__init__.py b/estados/__init__.py new file mode 100644 index 0000000..fbb1089 --- /dev/null +++ b/estados/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +from estados.inicio import Inicio +from estados.juego import Juego +from estados.start import Start +from estados.fin import Fin diff --git a/estados/basestate.py b/estados/basestate.py new file mode 100644 index 0000000..1ca9d16 --- /dev/null +++ b/estados/basestate.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +import logging + + +log = logging.getLogger(__name__) + + +class BaseState(object): + """ tareas comunes a todos los estados """ + + def __init__(self, appmanager): + super(BaseState, self).__init__() + + self._appmanager = appmanager + # cuando BaseState se instancia desde una clase heredada self.__class__ es esa clase y no BaseState + # esto nos permite usar la clase misma como llave en el diccionario + self.state_info = appmanager.state_info[self.__class__] if self.__class__ in appmanager.state_info else {} + + + def change_state(self, new_state): + self._appmanager.state_info[self.__class__] = self.state_info + self._appmanager.change_state(new_state) diff --git a/estados/fin.py b/estados/fin.py new file mode 100644 index 0000000..2234890 --- /dev/null +++ b/estados/fin.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +import logging +import cairo +from estados.basestate import BaseState + + +log = logging.getLogger(__name__) + + +class Fin(BaseState): + """ + Clase para probar las transiciones. + + """ + template = "win.glade" + + + def __init__(self, appmanager): + super(Fin, self).__init__(appmanager) + self.image = cairo.ImageSurface.create_from_png("data/win.png") + + + def on_drawingarea_button_press_event(self, widget, event): + log.debug("-> cambiar estado a Start") + self.change_state("Start") + + + def on_drawingarea_expose_event(self, widget, event): + cr = widget.window.cairo_create() + cr.set_source_surface(self.image, 0, 0) + cr.paint() diff --git a/estados/inicio.py b/estados/inicio.py new file mode 100644 index 0000000..14b8ef0 --- /dev/null +++ b/estados/inicio.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +import logging +from estados.basestate import BaseState + + +log = logging.getLogger(__name__) + + +class Inicio(BaseState): + """ + Clase de inicio. + + """ + template = "inicio.glade" + + + def __init__(self, appmanager): + super(Inicio, self).__init__(appmanager) + + + def on_buttonjugar_clicked(self, widget): + log.debug("-> cambiar estado a Start") + self.change_state("Start") diff --git a/estados/juego.py b/estados/juego.py new file mode 100644 index 0000000..9ab0693 --- /dev/null +++ b/estados/juego.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +import logging +from estados.basestate import BaseState + + +log = logging.getLogger(__name__) + + +class Juego(BaseState): + """ + Clase que dibuja la torta. + + """ + template = "juego.glade" + + + def __init__(self, appmanager): + super(Juego, self).__init__(appmanager) + # Aqui tenemos que cargar en la segunda posicion del contenedor vbox + # una clase torta en este caso. + # Si fuera otro tipo de juego estariamos cargando un grid que + # contendria X clases torta. + + + def on_drawingarea_button_press_event(self, widget, event): + log.debug("-> cambiar estado a Fin") + self.change_state("Fin") + + + def on_drawingarea_expose_event(self, widget, event): + cr = widget.window.cairo_create() + cr.set_source_surface(self.image, 0, 0) + cr.paint() + + + def on_button_clicked(self, widget): + log.debug("-> cambiar estado a Start") + self.change_state("Fin") diff --git a/estados/start.py b/estados/start.py new file mode 100644 index 0000000..3236d7c --- /dev/null +++ b/estados/start.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +import logging +import cairo +from estados.basestate import BaseState + + +log = logging.getLogger(__name__) + + +class Start(BaseState): + """ + Clase para probar las transiciones. + + """ + template = "win.glade" + + + def __init__(self, appmanager): + super(Start, self).__init__(appmanager) + self.image = cairo.ImageSurface.create_from_png("data/start.png") + + + def on_drawingarea_button_press_event(self, widget, event): + #log.debug("-> cambiar estado a Fin") + #self.change_state("Fin") + self.change_state("Juego") + + + def on_drawingarea_expose_event(self, widget, event): + cr = widget.window.cairo_create() + cr.set_source_surface(self.image, 0, 0) + cr.paint() |