diff options
author | Samus_ <mail2samus@gmail.com> | 2010-03-21 12:27:43 (GMT) |
---|---|---|
committer | Samus_ <mail2samus@gmail.com> | 2010-03-21 12:27:43 (GMT) |
commit | c48cffc27abda68dfb6296c2049caeea35a8fed0 (patch) | |
tree | fe0cc006d7857efbd8f6ed2a99a8ddccff2bc797 | |
parent | 5be7ccacf36a085e48a02f69acf4bb8e00367ffb (diff) |
refactor
-rwxr-xr-x | fracciones.activity/gtkcake.py | 58 |
1 files changed, 30 insertions, 28 deletions
diff --git a/fracciones.activity/gtkcake.py b/fracciones.activity/gtkcake.py index c81d5ec..aa52216 100755 --- a/fracciones.activity/gtkcake.py +++ b/fracciones.activity/gtkcake.py @@ -27,11 +27,11 @@ class Cake(gtk.DrawingArea): gtk.DrawingArea.__init__(self, *args, **kwargs) # seƱales self.connect("expose_event", self.expose) - self.connect("button_press_event", self.button_press) + self.connect("button_press_event", self.select) # Los eventos del raton no estan activados para el DrawingArea self.add_events(gtk.gdk.BUTTON_PRESS_MASK) + # variables de estado de la torta - self.subdivisions = subdivisions self.selected_list = subdivisions * [False] # Carga imagenes @@ -48,11 +48,32 @@ class Cake(gtk.DrawingArea): self._draw(context) - def button_press(self, widget, event): + def select(self, widget, event): """Manejador del evento button_press_event""" x, y = event.get_coords() if self._select(x, y): self._draw(widget.window.cairo_create()) + + + def _select(self, ux, uy): + """Selecciona un trozo de la torta. Devuelve True si un objeto fue + seleccionado, False en caso contrario.""" + # Transformamos las coordenadas del usuario a coordenadas reales (0-1) + rect = self.get_allocation() + wx = float(ux - rect.x) / float(rect.width) + wy = float(uy - rect.y) / float(rect.height) + # Centramos + wx -= 0.5 + wy -= 0.5 + if math.pow(wx, 2) + math.pow(wy, 2) > math.pow(WRADIUS, 2): + return False + angle = math.atan2(wy, wx) + if angle < 0: + angle += 2 * math.pi + sector = angle * len(self.selected_list) / (2 * math.pi) + index = int(math.floor(sector)) + self.selected_list[index] = not self.selected_list[index] + return True def _draw(self, context): @@ -63,8 +84,9 @@ class Cake(gtk.DrawingArea): context.set_source_rgb(0, 0, 0) context.arc(WIDTH/2, HEIGHT/2, RADIUS, 0, 2 * math.pi) context.stroke() - for i in range(self.subdivisions): - angle = 2 * math.pi * i / self.subdivisions + subdivisions = len(self.selected_list) + for i in range(subdivisions): + angle = 2 * math.pi * i / subdivisions context.move_to(WIDTH/2, HEIGHT/2) context.line_to( WIDTH/2 + RADIUS*math.cos(angle), @@ -86,10 +108,11 @@ class Cake(gtk.DrawingArea): image_ctx.paint() # el metodo de enmascaramiento es dibujar en modo "borrar" ya que mask no quiso andar image_ctx.set_operator(cairo.OPERATOR_CLEAR) + subdivisions = len(self.selected_list) for index, selected in enumerate(self.selected_list): if selected: - angle_start = 2 * math.pi * index / self.subdivisions - angle_end = 2 * math.pi * (index + 1) / self.subdivisions + angle_start = 2 * math.pi * index / subdivisions + angle_end = 2 * math.pi * (index + 1) / subdivisions image_ctx.move_to(WIDTH/2, HEIGHT/2) image_ctx.line_to( WIDTH/2 + RADIUS*math.cos(angle_start), @@ -123,27 +146,6 @@ class Cake(gtk.DrawingArea): draw_grid() - def _select(self, ux, uy): - """Selecciona un trozo de la torta. Devuelve True si un objeto fue - seleccionado, False en caso contrario.""" - # Transformamos las coordenadas del usuario a coordenadas reales (0-1) - rect = self.get_allocation() - wx = float(ux - rect.x) / float(rect.width) - wy = float(uy - rect.y) / float(rect.height) - # Centramos - wx -= 0.5 - wy -= 0.5 - if math.pow(wx, 2) + math.pow(wy, 2) > math.pow(WRADIUS, 2): - return False - angle = math.atan2(wy, wx) - if angle < 0: - angle += 2 * math.pi - sector = angle * self.subdivisions / (2 * math.pi) - index = int(math.floor(sector)) - self.selected_list[index] = not self.selected_list[index] - return True - - if __name__ == "__main__": # Probamos el nuevo widget Cake window = gtk.Window() |