Hello:
I don't know if there are a better place for this question.
I am trying to port a simple application from PyCairo to HippoCanvas, i can
create the objects and add to a Box, but i can't show them.
I have tried with do_paint_background and another methods, but without
success.
Anybody can help me?
Thanks
Gonzalo Odiard
PD: Attach the cairo and the hippo version
#!/usr/bin/env python
# Tareas pendientes:
# Menues y dialogos para elegir imagen y cantidad de piezas
# Eventos de click y dnd para mover las piezas
# Deteccion de encuentro de piezas
# Deteccion de armado final
# Rotar piezas
import gtk , cairo,gobject
import math, random
# inicializacion
print "Leo imagen"
image = cairo.ImageSurface.create_from_png ("images/ardilla.png")
CANT_PIEZAS = 25
print "calculo SIZE"
SIZE = int(math.sqrt(image.get_width() * image.get_height() / CANT_PIEZAS ))
print image.get_width(), image.get_height(), SIZE
ALETA = SIZE/5
SPACE = 30
piezas = []
print "cant de piezas"
CANT_X = CANT_PIEZAS * SIZE / image.get_height()
CANT_Y = CANT_PIEZAS * SIZE / image.get_width()
print CANT_X, CANT_Y
class Pieza:
x = 0
y = 0
deltaX = 0
deltaY = 0
signoX = 0
signoY = 0
xImage = 0
yImage = 0
class Game:
piezaSeleccionadaN = -1
piezaSeleccionadaP = -1
deltaSeleccionX = 0
deltaSeleccionY = 0
dibujar = True
game = Game()
def makePuzzle(piezas):
for n in range(0,CANT_X):
piezas.append([])
for p in range(0,CANT_Y):
#creo pieza
pieza = Pieza()
pieza.deltaX = (random.random()-.5)*(SIZE/4)
pieza.deltaY = (random.random()-.5)*(SIZE/4)
if random.random() > .5:
pieza.signoX = 1
else:
pieza.signoX = -1
if random.random() > .5:
pieza.signoY = 1
else:
pieza.signoY = -1
pieza.x =(SIZE/2) + n * (SIZE + SPACE)
pieza.y =(SIZE/2) + p * (SIZE + SPACE)
piezas[n].append(pieza)
pieza.xImage = n * SIZE
pieza.yImage = p * SIZE
def drawPieza(ctx,posX,posY,pieza):
#print posX,posY
ctx.translate(pieza.x,pieza.y)
ctx.move_to(0, 0)
# cara superior
if posY == 0:
ctx.rel_line_to(SIZE, 0)
else:
ctx.rel_line_to((SIZE/2)-piezas[posX][posY-1].deltaX-ALETA/2,0)
x,y = (SIZE/2)-piezas[posX][posY-1].deltaX-ALETA/2,0
SIGNO = piezas[posX][posY-1].signoX
ctx.curve_to (x+ALETA/2,y,x-ALETA/2,y+ALETA*SIGNO,x+ALETA/2,y+ALETA*SIGNO)
ctx.curve_to (x+ALETA*1.5, y+ALETA * SIGNO, x+ALETA/2, y, x+ALETA, y)
ctx.rel_line_to((SIZE/2)+piezas[posX][posY-1].deltaX-ALETA/2,0)
# cara derecha
if posX < CANT_X-1:
ctx.rel_line_to(0,(SIZE/2)-pieza.deltaY-ALETA/2)
x,y,SIGNO = SIZE,(SIZE/2)-pieza.deltaY-ALETA/2,pieza.signoY
ctx.curve_to (x,y+ALETA/2,x+ALETA*SIGNO,y-ALETA/2,x+ALETA*SIGNO,y+ALETA/2)
ctx.curve_to (x+ALETA * SIGNO, y+ALETA*1.5, x, y+ALETA/2, x, y+ALETA)
ctx.rel_line_to(0,(SIZE/2)+pieza.deltaY-ALETA/2)
else:
ctx.rel_line_to(0, SIZE)
# cara inferior
if posY < CANT_Y-1:
ctx.rel_line_to(-(SIZE/2)-pieza.deltaX+ALETA/2,0)
x,y,SIGNO = (SIZE/2)-pieza.deltaX+ALETA/2,SIZE,pieza.signoX
ctx.curve_to (x-ALETA/2,y,x+ALETA/2,y+ALETA*SIGNO,x-ALETA/2,y+ALETA*SIGNO)
ctx.curve_to (x-ALETA*1.5, y+ALETA * SIGNO, x-ALETA/2, y, x-ALETA, y)
ctx.rel_line_to(-(SIZE/2)+pieza.deltaX+ALETA/2,0)
else:
ctx.rel_line_to(-SIZE, 0)
# cara izquierda
if posX == 0:
ctx.rel_line_to(0, -SIZE)
else:
ctx.rel_line_to(0,-(SIZE/2)-piezas[posX-1][posY].deltaY+ALETA/2)
x,y = 0, (SIZE/2)-piezas[posX-1][posY].deltaY+ALETA/2
SIGNO = piezas[posX-1][posY].signoY
ctx.curve_to (x,y-ALETA/2,x+ALETA*SIGNO,y+ALETA/2,x+ALETA*SIGNO,y-ALETA/2)
ctx.curve_to (x+ALETA * SIGNO, y-ALETA*1.5, x, y-ALETA/2, x, y-ALETA)
ctx.rel_line_to(0,-(SIZE/2)+piezas[posX-1][posY].deltaY+ALETA/2)
ctx.close_path()
def expose (da, event):
ctx = da.window.cairo_create()
x, y, width, height = da.allocation
# set a clip region for the expose event
#ctx.rectangle(event.area.x, event.area.y,
# event.area.width, event.area.height)
#ctx.set_source_rgb (1, 0, 0)
#ctx.stroke()
#ctx.clip()
#draw(ctx,x, y, width, height)
draw(ctx,event.area.x, event.area.y,
event.area.width, event.area.height)
return False
def draw (ctx,x,y,width,height):
#x, y, width, height = da.allocation
print "draw", x, y , width, height
#ctx = da.window.cairo_create()
print " "
ctx.set_line_width(1)
for n in range(0,CANT_X):
for p in range(0,CANT_Y):
# verifico si hay que dibujar la pieza o no
if ((game.piezaSeleccionadaN != n) and (game.piezaSeleccionadaP != p)):
pieza = piezas[n][p]
if (not ((pieza.x > x + width) or
(pieza.x + SIZE < x ) or
(pieza.y > y + height) or
(pieza.y+SIZE < y ))):
print "Dibujando",n,p
ctx.save()
drawPieza(ctx,n,p,pieza)
ctx.set_source_surface(image,-pieza.xImage,-pieza.yImage)
ctx.fill_preserve()
ctx.set_source_rgb (0, 0, 0)
ctx.stroke()
ctx.restore()
# Dibujo ultima la pieza seleccionada
if ((game.piezaSeleccionadaN != -1) and (game.piezaSeleccionadaP != -1)):
pieza = piezas[game.piezaSeleccionadaN][game.piezaSeleccionadaP]
ctx.save()
drawPieza(ctx,game.piezaSeleccionadaN,game.piezaSeleccionadaP,pieza)
ctx.set_source_surface(image,-pieza.xImage,-pieza.yImage)
ctx.fill_preserve()
ctx.set_source_rgb (0, 0, 1)
ctx.stroke()
ctx.restore()
# http://www.pygtk.org/pygtk2tutorial-es/sec-EventHandling.html#simplescribblefig
# http://www.pygtk.org/pygtk2tutorial-es/examples/scribblesimple.py
def button_press_event(widget, event):
if event.button == 1: # and pixmap != None:
x, y, state = event.window.get_pointer()
#print "Click!", x , y
# Recorro las piezas para ver si estoy en alguna
for n in range(0,CANT_X):
for p in range(0,CANT_Y):
pieza = piezas[n][p]
if ((pieza.x <= x) and (pieza.x + SIZE >= x) and
(pieza.y <= y) and (pieza.y + SIZE >= y)):
print "Encuentro" , n,p
game.piezaSeleccionadaN = n
game.piezaSeleccionadaP = p
game.deltaSeleccionX = x - pieza.x
game.deltaSeleccionY = y - pieza.y
return True
def timer_redibujo(drawingarea):
#print "evento timer",game.dibujar
if (game.dibujar == True):
drawingarea.queue_draw_area(0,0,600,600)
game.dibujar = False
return 1
def motion_notify_event(widget, event):
if event.is_hint:
x, y, state = event.window.get_pointer()
else:
x = event.x
y = event.y
state = event.state
if state & gtk.gdk.BUTTON1_MASK: # and pixmap != None:
#print game.piezaSeleccionadaN, game.piezaSeleccionadaP
pieza = piezas[game.piezaSeleccionadaN][game.piezaSeleccionadaP]
#print "llamada", pieza.x - SIZE,pieza.y - SIZE, pieza.x + SIZE, pieza.y + SIZE
xori = pieza.x
yori = pieza.y
pieza.x = x - game.deltaSeleccionX
pieza.y = y - game.deltaSeleccionY
deltaXsup,deltaYsup,deltaXinf,deltaYinf = 0,0,0,0
if (game.deltaSeleccionX > 0):
deltaXsup = - game.deltaSeleccionX
else:
deltaXinf = game.deltaSeleccionX
if (game.deltaSeleccionY > 0):
deltaYsup = - game.deltaSeleccionY
else:
deltaYinf = game.deltaSeleccionY
widget.queue_draw_area(pieza.x - ALETA - 1 ,#+ deltaXsup,
pieza.y - ALETA - 1 ,#+ deltaYsup,
SIZE + ALETA * 2 + 1 ,#+ deltaXinf,
SIZE + ALETA * 2 + 1 )#+ deltaYinf)
#widget.queue_draw_area(xori - ALETA -1,yori-ALETA -1, xori + SIZE+ALETA+1, yori + SIZE + ALETA +1)
#game.dibujar = True
#widget.queue_draw_area(x-ALETA -1 ,y-ALETA -1, x + SIZE+ALETA+1, y + SIZE+ALETA+1)
#print "Movimiento"
return True
def main():
win = gtk.Window()
makePuzzle(piezas)
win.connect('destroy', gtk.main_quit)
win.set_default_size(600, 600)
drawingarea = gtk.DrawingArea()
win.add(drawingarea)
drawingarea.connect('expose_event', expose)
drawingarea.connect("motion_notify_event", motion_notify_event)
drawingarea.connect("button_press_event", button_press_event)
drawingarea.set_events(gtk.gdk.EXPOSURE_MASK
| gtk.gdk.LEAVE_NOTIFY_MASK
| gtk.gdk.BUTTON_PRESS_MASK
| gtk.gdk.POINTER_MOTION_MASK
| gtk.gdk.POINTER_MOTION_HINT_MASK)
#source_id = gobject.timeout_add(100, timer_redibujo,drawingarea)
win.show_all()
gtk.main()
if __name__ == '__main__':
main()
import logging
import hippo
import gtk,os
from gettext import gettext as _
from sugar.activity import activity
from sugar.graphics import font
from sugar.graphics import color
from sugar.graphics import units
from sugar.graphics.toolbar import Toolbar
from sugar.graphics.iconbutton import IconButton
from sugar.graphics.entry import Entry
from sugar.graphics.optionmenu import OptionMenu
from sugar.graphics.menu import MenuItem
# Tareas pendientes:
# Menues y dialogos para elegir imagen y cantidad de piezas
# Deteccion de encuentro de piezas
# Deteccion de armado final
# Rotar piezas
# ver activitiesdonut.py
import gtk , cairo,gobject
import math, random
# inicializacion
print "Leo imagen"
path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"images")
pxImage = cairo.ImageSurface.create_from_png(os.path.join(path,"ardilla.png"))
height = pxImage.get_height()
width = pxImage.get_width()
CANT_PIEZAS = 25
print "calculo SIZE"
SIZE = int(math.sqrt(width * height / CANT_PIEZAS ))
print width, height, SIZE
ALETA = SIZE/5
SPACE = 30
piezas = []
print "cant de piezas"
CANT_X = CANT_PIEZAS * SIZE / height
CANT_Y = CANT_PIEZAS * SIZE / width
print CANT_X, CANT_Y
class Pieza(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'HeadBreakPieza'
x = 0
y = 0
deltaX = 0
deltaY = 0
signoX = 0
signoY = 0
posX = 0
posY = 0
def __init__(self,n,p,**kwargs):
print "creando pieza",n,p
hippo.CanvasBox.__init__(self,**kwargs)
self.deltaX = (random.random()-.5)*(SIZE/4)
self.deltaY = (random.random()-.5)*(SIZE/4)
if random.random() > .5:
self.signoX = 1
else:
self.signoX = -1
if random.random() > .5:
self.signoY = 1
else:
self.signoY = -1
self.x =(SIZE/2) + n * (SIZE)
self.y =(SIZE/2) + p * (SIZE)
posX = n
posY = p
#def paint(self, ctx):
def do_paint_background(self, ctx, damaged_box):
print "dibujando pieza",n,p
ctx.save()
ctx.translate(self.x,self.y)
ctx.move_to(0, 0)
# cara superior
if posY == 0:
ctx.rel_line_to(SIZE, 0)
else:
ctx.rel_line_to((SIZE/2)-piezas[self.posX][self.posY-1].deltaX-ALETA/2,0)
x,y = self.x+(SIZE/2)-piezas[self.posX][self.posY-1].deltaX-ALETA/2,self.y
SIGNO = piezas[self.posX][self.posY-1].signoX
ctx.curve_to (x+ALETA/2,y,x-ALETA/2,y+ALETA*SIGNO,x+ALETA/2,y+ALETA*SIGNO)
ctx.curve_to (x+ALETA*1.5, y+ALETA * SIGNO, x+ALETA/2, y, x+ALETA, y)
ctx.rel_line_to((SIZE/2)+piezas[self.posX][self.posY-1].deltaX-ALETA/2,0)
# cara derecha
if posX < CANT_X-1:
ctx.rel_line_to(0,(SIZE/2)-self.deltaY-ALETA/2)
x,y,SIGNO = self.x+SIZE,self.y+(SIZE/2)-self.deltaY-ALETA/2,self.signoY
ctx.curve_to (x,y+ALETA/2,x+ALETA*SIGNO,y-ALETA/2,x+ALETA*SIGNO,y+ALETA/2)
ctx.curve_to (x+ALETA * SIGNO, y+ALETA*1.5, x, y+ALETA/2, x, y+ALETA)
ctx.rel_line_to(0,(SIZE/2)+self.deltaY-ALETA/2)
else:
ctx.rel_line_to(0, SIZE)
# cara inferior
if posY < CANT_Y-1:
ctx.rel_line_to(-(SIZE/2)-self.deltaX+ALETA/2,0)
x,y,SIGNO = self.x+(SIZE/2)-self.deltaX+ALETA/2,self.y+ SIZE,self.signoX
ctx.curve_to (x-ALETA/2,y,x+ALETA/2,y+ALETA*SIGNO,x-ALETA/2,y+ALETA*SIGNO)
ctx.curve_to (x-ALETA*1.5, y+ALETA * SIGNO, x-ALETA/2, y, x-ALETA, y)
ctx.rel_line_to(-(SIZE/2)+self.deltaX+ALETA/2,0)
else:
ctx.rel_line_to(-SIZE, 0)
# cara izquierda
if posX == 0:
ctx.rel_line_to(0, -SIZE)
else:
ctx.rel_line_to(0,-(SIZE/2)-piezas[self.posX-1][self.posY].deltaY+ALETA/2)
x = self.x
y = self.y+(SIZE/2)-piezas[self.posX-1][self.posY].deltaY+ALETA/2
SIGNO = piezas[self.posX-1][self.posY].signoY
ctx.curve_to (x,y-ALETA/2,x+ALETA*SIGNO,y+ALETA/2,x+ALETA*SIGNO,y-ALETA/2)
ctx.curve_to (x+ALETA * SIGNO, y-ALETA*1.5, x, y-ALETA/2, x, y-ALETA)
ctx.rel_line_to(0,-(SIZE/2)+piezas[self.posX-1][self.posY].deltaY+ALETA/2)
ctx.close_path()
ctx.set_source_surface(image,-pieza.xImage,-pieza.yImage)
ctx.fill_preserve()
ctx.set_source_rgb (0, 0, 0)
ctx.stroke()
ctx.restore()
# def do_allocate(self, width, height, origin_changed):
# hippo.CanvasBox.do_allocate(self, width, height, origin_changed)
class Game:
piezaSeleccionadaN = -1
piezaSeleccionadaP = -1
deltaSeleccionX = 0
deltaSeleccionY = 0
game = Game()
def makePuzzle(piezas,vbox):
for n in range(0,CANT_X):
piezas.append([])
for p in range(0,CANT_Y):
#creo pieza
pieza = Pieza(n,p)
piezas[n].append(pieza)
vbox.append(pieza)
# http://www.w3.org/TR/SVG11/paths.html
#def scramble():
# for n in range(0,CANT_X):
# for p in range(0,CANT_Y):
# pieza = piezas[n][p]
# deltaX = (random.random()-.5)*(SIZE)
# deltaY = (random.random()-.5)*(SIZE)
# pieza.group.translate(deltaX,deltaY)
class HeadBreak(activity.Activity):
def __init__(self, handle):
activity.Activity.__init__(self, handle)
vbox = hippo.CanvasBox()
self.set_root(vbox)
toolbar = Toolbar()
vbox.append(toolbar)
button = IconButton(icon_name='theme:stock-close')
button.connect("activated", self._button_activated_cb)
toolbar.append(button)
makePuzzle(piezas,vbox)
#canvas = hippo.Canvas()
#canvas.connect("item-view-created", on_item_view_created)
#vbox.append(canvas)
#create_canvas_model(self)
#scramble()
def _button_activated_cb(self, button):
logging.debug('FooActivity._button_activated_cb')
_______________________________________________
Sugar mailing list
[email protected]
http://mailman.laptop.org/mailman/listinfo/sugar