[cairo] PyGtk Drag and Drop in a gtk.DrawingArea
Antoni Boucher
bouanto at hotmail.com
Sat Jul 11 14:44:08 PDT 2009
Hello, thanks for your answer. Gerdus van Zyl, I tried your idea and it's much better, but not perfect. The speed is ok, but, I don't know why, there are more than one image which follow the mouse (trails).
Here is a part of my code :
class Echiquier(gtk.DrawingArea):
def __init__(self, largeurCase, positionDepartDonnee, echiquier, fen, blancsAJouer):
super(Echiquier, self).__init__()
[...]
self.connect("expose-event", self.expose, blancsAJouer)
self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.BUTTON1_MOTION_MASK)
self.connect("button-press-event", self.piecePrise)
self.connect("motion-notify-event", self.pieceBouge)
self.connect("button-release-event", self.pieceRelache)
self.echiquier = echiquier
self.FEN = fen
[...]
self.pionBlanc = gtk.gdk.pixbuf_new_from_file_at_size('Images/pionB.svg', self.largeurCase, self.largeurCase)
self.cavalierBlanc = gtk.gdk.pixbuf_new_from_file_at_size('Images/cavalierB.svg', self.largeurCase, self.largeurCase)
[...]
def piecePrise(self, widget, evenement):
if evenement.button == 1:
#We remove the piece from the board and we get its (x, y) when the user click on the board
self.pieceSelectionneeX = int(evenement.y / self.largeurCase)
self.pieceSelectionneeY = int(evenement.x / self.largeurCase)
self.pieceSelectionnee = self.echiquier[self.pieceSelectionneeX][self.pieceSelectionneeY]
self.echiquier[self.pieceSelectionneeX][self.pieceSelectionneeY] = 'VIDE'
def pieceBouge(self, widget, evenement):
if evenement.is_hint:
self.posSourisX, self.posSourisY, state = event.window.get_pointer()
else:
self.posSourisX = evenement.x
self.posSourisY = evenement.y
state = evenement.state
if state & gtk.gdk.BUTTON1_MASK:
#We draw the piece at its new (x, y) when the user moves the mouse
contexte = self.window.cairo_create()
surface = None
if self.pieceSelectionnee == 'PION_B':
surface = self.pionBlanc
elif self.pieceSelectionnee == 'PION_N':
surface = self.pionNoir
[...]
self.posX = self.posSourisX - self.largeurCase / 2
self.posY = self.posSourisY - self.largeurCase / 2
contexte.set_source_pixbuf(surface, self.posX, self.posY)
contexte.paint()
self.queue_draw_area(self.posX - self.largeurCase, self.posY - self.largeurCase, self.posX + self.largeurCase * 2, self.posX + self.largeurCase * 2)
def pieceRelache(self, widget, evenement):
#We put the piece at its begining (x, y) when the user releases the mouse button
self.echiquier[self.pieceSelectionneeX][self.pieceSelectionneeY] = self.pieceSelectionnee
self.pieceSelectionneeX = -1
self.pieceSelectionneeY = -1
self.pieceSelectionnee = 'VIDE'
self.queue_draw_area(0, 0, self.largeurCase * 8, self.largeurCase * 8)
def expose(self, widget, evenement, blancsAJouer):
#We draw again the board
contexte = widget.window.cairo_create()
contexte.set_source_rgb(1, 1, 1)
contexte.rectangle(0, 0, self.largeurCase * 8, self.largeurCase * 8)
contexte.fill()
contexte.set_source_rgb(0.81, 0.81, 0.81)
#We place the piece which follow the mouse
for i in range(8):
for j in range(8):
if (i + j) % 2 == 1:
contexte.rectangle(i * self.largeurCase, j * self.largeurCase, self.largeurCase, self.largeurCase)
contexte.fill()
if self.pieceSelectionnee != 'VIDE':
surface = None
if self.pieceSelectionnee == 'PION_B':
surface = self.pionBlanc
elif self.pieceSelectionnee == 'PION_N':
surface = self.pionNoir
[...]
contexte.set_source_pixbuf(surface, self.posX, self.posY)
contexte.paint()
How can I improve it ? I don't want trails.
Thanks for your answer.
_________________________________________________________________
Créez un personnage à votre image pour votre WL Messenger
http://go.microsoft.com/?linkid=9656622
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cairographics.org/archives/cairo/attachments/20090711/b63c7a6a/attachment.html
More information about the cairo
mailing list