#!/usr/bin/python

from gtk import *
import GtkExtra

PIXMAP_ROOT = '/usr/share/pixmaps/brainvita/'

def coods(i):
	"Computes x,y coods of square on board from index"
	if i < 6:
		row = i/3
		col = 2 + (i%3)
	elif i < 27:
		row = 2 + (i-6)/7
		col = (i-6)%7
	else:
		row = 5 + (i-27)/3
		col = 2 + (i%3)
	return row,col

def indexof(r,c):
	"Computes index from row and col"
	if r < 2:
		return r*3+c-2
	elif r < 5:
		return 6 + (r-2)*7 + c
	else:
		return 25 + (r-5)*3 + c

class Bsquare:
	"Square on Brainvita board"
	
	def __init__(self,i,fl,wn,tb,bvita):
		self.row, self.col = coods(i)
		self.index = i
		self.filled = fl
		self.pmaps = []
		self.image = 0

		ebox = GtkEventBox()
		box = GtkPixmap(bvita.bpix, bvita.bmsk)
		fixed = GtkFixed()
		tb.attach(ebox,self.col,self.col+1,self.row,self.row+1)
		ebox.add(fixed)
		fixed.put(box,0,0)
		ebox.set_events(GDK.BUTTON_PRESS_MASK)
		ebox.connect("button_press_event",bvita.callback,self)
		
		for j in range(4):
			self.pmaps.append(GtkPixmap(bvita.pix[j],bvita.msk[j]))
			fixed.put(self.pmaps[j],5,5)

		box.show()
		if self.filled:
			self.pmaps[self.image].show()
		fixed.show()
		ebox.show()
	
	def rotate(self):
		self.pmaps[self.image].hide()
		self.image = (self.image+1)%4
		self.pmaps[self.image].show()
	
	def stop(self):
		self.pmaps[self.image].hide()
		self.image = 0
		self.pmaps[0].show()

	def gone_out(self):
		self.pmaps[self.image].hide()
		self.image = 0
		self.filled = 0
	
	def came_in(self):
		self.pmaps[self.image].show()
		self.filled = 1

class Bvita:
	"Game of Brainvita"
	
	mleft = 32				# Marbles left
	sel_square = None		# The marble selected to be moved
	squares = []
	
	def __init__(self):
		win = GtkWindow()
		win.set_title("Brainvita")
		win.set_usize(294,294)
		win.connect("delete_event",mainquit)
		win.connect("destroy",mainquit)

		grid = GtkTable(7,7,TRUE)
		win.add(grid)
		self.get_pixmaps(win)
		self.set_squares(win,grid)

		grid.show()
		win.show()
		timeout_add(120,self.animate)
		
	def get_pixmaps(self,win):
		p1,m1 = create_pixmap_from_xpm(win,None,PIXMAP_ROOT+"red1.xpm")
		p2,m2 = create_pixmap_from_xpm(win,None,PIXMAP_ROOT+"red2.xpm")
		p3,m3 = create_pixmap_from_xpm(win,None,PIXMAP_ROOT+"red3.xpm")
		p4,m4 = create_pixmap_from_xpm(win,None,PIXMAP_ROOT+"red4.xpm")
		self.pix = [p1,p2,p3,p4]
		self.msk = [m1,m2,m3,m4]
		self.bpix, self.bmsk = create_pixmap_from_xpm(win,None,PIXMAP_ROOT+"box.xpm")

	def set_squares(self,w,f):
		for i in range(33):
			filled = (i != 16)
			self.squares.append(Bsquare(i,filled,w,f,self))

	def callback(self,eb,ev,sq):
		if (sq.filled):
			if sq.index == self.sel_square:
				self.sel_square = None
				sq.stop()
			else:
				ind = self.sel_square
				self.sel_square = sq.index
				if (ind != None):
					self.squares[ind].stop()

		elif (self.sel_square != None):
			self.move_to(sq.index)
	
	def animate(self):
		if self.sel_square != None:
			self.squares[self.sel_square].rotate()
		return 1

	def move_to(self,goal):
		curr = self.squares[self.sel_square]
		dest = self.squares[goal]
		
		r1,c1 = curr.row,curr.col
		r2,c2 = dest.row,dest.col
		r,c = (r1+r2)/2, (c1+c2)/2
		ind = indexof(r,c)
		empty = self.squares[ind].filled
		self.sel_square = None

		if (empty and ((r1==r2 and abs(c1-c2)==2) or (c1==c2 and abs(r1-r2) == 2))):
			# Valid move!
			curr.gone_out()
			self.squares[ind].gone_out()
			dest.came_in()
			self.mleft = self.mleft - 1
		else:
			curr.stop()

a = Bvita()
mainloop()
