Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjmpc <jumapico@gmail.com>2009-10-27 21:03:20 (GMT)
committer jmpc <jumapico@gmail.com>2009-10-27 21:03:20 (GMT)
commitcbfcad25c0d47b7e3f1279308ab0a599ab7a134d (patch)
tree21ffda2bd669ee886ca812e35ed5718570a09275
parente2360380e8d9504e1f55012bc0755b3a11f94173 (diff)
Agregamos las imagenes de primer plano y de fondo. Empezamos a probar las mascaras para la imagen de primer plano.
-rwxr-xr-xcake.py79
1 files changed, 73 insertions, 6 deletions
diff --git a/cake.py b/cake.py
index 0c18288..b68db41 100755
--- a/cake.py
+++ b/cake.py
@@ -1,14 +1,47 @@
#!/usr/bin/env python
# -*- encoding: utf8 -*-
+"""
+La torta es una imagen redonda en un archivo de 500x500 pixeles.
+Tiene un borde de 30 pixeles.
+
+Pasado a coordenadas mundiales el radio es: (500-2*30)/(2*500) = 0.44
+
+"""
import math
import gtk
import cairo
+WRADIUS = 0.44
+
+
+def draw_grid(context, N):
+ context.set_source_rgb(0, 0, 0)
+ context.arc(250, 250, 500*WRADIUS, 0, 2 * math.pi)
+ context.stroke()
+ for i in xrange(N):
+ angle = 2 * math.pi * i / N
+ context.move_to(250, 250)
+ context.line_to(
+ 250 + 500*WRADIUS*math.cos(angle),
+ 250 + 500*WRADIUS*math.sin(angle)
+ )
+ context.stroke()
+
+
+def mask_image(context, selected, image):
+ context.set_source_surface(image, 0, 0)
+ context.paint()
+
+
class Cake(gtk.DrawingArea):
- def __init__(self):
+ def __init__(self, N):
gtk.DrawingArea.__init__(self)
self.connect("expose_event", self.expose)
+ #self.N = N
+ #self.selected = N * [0]
+ self.N = 5
+ self.selected = [0, 0, 1, 1, 1]
def expose(self, widget, event):
@@ -33,21 +66,55 @@ class Cake(gtk.DrawingArea):
def draw_image(self, context):
+ # Load images
+ image_bg = cairo.ImageSurface.create_from_png("bg.png")
+ image_fg = cairo.ImageSurface.create_from_png("fg.png")
+
+ # Scale the surface
rect = self.get_allocation()
- image = cairo.ImageSurface.create_from_png("fg.png")
context.save()
context.scale(
- float(rect.width) / image.get_width(),
- float(rect.height) / image.get_height()
+ #float(rect.width) / float(image.get_width()),
+ #float(rect.height) / float(image.get_height())
+ rect.width / 500.0,
+ rect.height / 500.0,
)
- context.set_source_surface(image, 0, 0)
+
+ # Draw the background
+ context.set_source_surface(image_bg, 0, 0)
context.paint()
+
+ # Draw the foreground
+ mask_image(context, self.selected, image_fg)
+
+ # Draw the grid
+ draw_grid(context, self.N)
context.restore()
+ def select(self, ux, uy):
+ """
+ Si x, y esta dentro de la torta cambiamos el sector.
+
+ """
+ # Transformamos las coordenadas del usuario al rango 0.0, 1.0.
+ rect = self.get_allocation()
+ wx = float(ux - rect.x) / float(rect.width)
+ wy = float(uy - rect.y) / float(rect.height)
+ 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.N / (2 * math.pi)
+ index = int(math.floor(sector))
+ self.selected[index] = 1 - self.selected[index]
+ return True
+
+
def main():
window = gtk.Window()
- cake = Cake()
+ cake = Cake(6)
window.add(cake)
window.connect("destroy", gtk.main_quit)