Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/cardtable.py
diff options
context:
space:
mode:
authorSimon Schampijer <simon@schampijer.de>2007-07-12 20:45:24 (GMT)
committer Simon Schampijer <simon@schampijer.de>2007-07-12 20:45:24 (GMT)
commit8a2defe196f4f1d1edd7845ca871d58a57c717e1 (patch)
tree9244672f0969a47ab7dc509bac0427c51df5dfe4 /cardtable.py
parent1c2728aad09cf28fd194f20c01138fc53e0f4b68 (diff)
Started to merge the memosono game and the meorize game into one activity.
Diffstat (limited to 'cardtable.py')
-rwxr-xr-xcardtable.py192
1 files changed, 192 insertions, 0 deletions
diff --git a/cardtable.py b/cardtable.py
new file mode 100755
index 0000000..b9f96b5
--- /dev/null
+++ b/cardtable.py
@@ -0,0 +1,192 @@
+#! /usr/bin/env python
+#
+# Copyright (C) 2007, One Laptop Per Child
+#
+# Muriel de Souza Godoi - muriel@laptop.org
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+
+import gtk, pygtk
+import svgcard
+import os
+import time
+import gobject
+import math
+import gc
+
+class CardTable(gtk.EventBox):
+
+ __gsignals__ = {
+ 'card-flipped': (gobject.SIGNAL_RUN_FIRST, None, [int, gobject.TYPE_PYOBJECT]),
+ 'card-highlighted': (gobject.SIGNAL_RUN_FIRST, None, [int, gobject.TYPE_PYOBJECT]),
+ }
+
+ def __init__(self):
+ gtk.EventBox.__init__(self)
+
+ # Set table settings
+ self.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#000000'))
+ self.table = gtk.Table()
+ self.table.grab_focus()
+ self.table.set_flags(gtk.CAN_FOCUS)
+ self.table.set_flags(gtk.CAN_DEFAULT)
+ self.table.set_row_spacings(11)
+ self.table.set_col_spacings(11)
+ self.table.set_border_width(11)
+ self.table.set_resize_mode(gtk.RESIZE_IMMEDIATE)
+ self.set_property('child', self.table)
+ self.fist_load = True
+
+ def load_game(self, widget, data, grid):
+ self.data = data
+ self.cards_data = grid
+ self.size = int(math.ceil(math.sqrt(len(grid))))
+ self.table.resize(self.size, self.size)
+ self.card_size = self.get_card_size(self.size)
+ self.cards = {}
+ self.cd2id = {}
+ self.id2cd = {}
+ self.dict = {}
+ self.selected_card = [0, 0]
+ self.flipped_card = -1
+ self.table_positions = {}
+
+ # Build the table
+ if data['divided']=='True':
+ text1 = str(self.data['face1'])
+ text2 = str(self.data['face2'])
+ else:
+ text1 = str(self.data['face'])
+ text2 = str(self.data['face'])
+ buffer_card_1 = svgcard.SvgCard(-1, {'front_border':{'opacity':'0'}, 'front_h_border':{'opacity':'0.5'}, 'back_text':{'card_text':text1}}, {}, None, self.card_size)
+ buffer_card_2 = svgcard.SvgCard(-1, {'front_border':{'opacity':'0'}, 'front_h_border':{'opacity':'0.5'}, 'back_text':{'card_text':text2}}, {}, None, self.card_size)
+
+ self.game_dir = os.path.join(os.path.dirname(__file__), 'games')
+ x = 0
+ y = 0
+ id = 0
+ for card in self.cards_data:
+ if card[1] <> '':
+ jpg = os.path.join(self.game_dir, self.data['game_name']+'/images/'+str(card[1]))
+ else:
+ jpg = None
+ props = {}
+ props['front_border'] = {'opacity':'1'}
+ props['front_h_border'] ={'opacity':'1'}
+ props['front_text']= {'card_text':card[3], 'card_line1':card[4], 'card_line2':card[5], 'card_line3':card[6], 'card_line4':card[7]}
+
+ if card[0]== '1':
+ buffer_card = buffer_card_1
+ else:
+ buffer_card = buffer_card_2
+
+ card = svgcard.SvgCard(id, props, buffer_card.get_cache(), jpg, self.card_size)
+ card.connect('enter-notify-event', self.mouse_event, [x, y])
+ card.connect("button-press-event", self.flip_card_mouse, id)
+ self.table_positions[(x, y)]=1
+ self.cd2id[card] = id
+ self.id2cd[id] = card
+ self.cards[(x, y)] = card
+ self.dict[id] = (x, y)
+ self.table.attach(card, x, x+1, y, y+1, gtk.SHRINK, gtk.SHRINK)
+ #button = gtk.Button('button')
+ #button.show()
+ #self.table.attach(button, x, x+1, y, y+1, gtk.SHRINK, gtk.SHRINK)
+ x += 1
+ if x == self.size:
+ x = 0
+ y +=1
+ id += 1
+ self.fist_load = False
+ gc.collect()
+
+ def change_game(self, widget, data, grid):
+ if not self.fist_load:
+ for card in self.cards.values():
+ self.table.remove(card)
+ del card
+ self.load_game(None, data, grid)
+
+ def get_card_size(self, size_table):
+ x = (780 - (11*size_table))/size_table
+ return x
+
+ def mouse_event(self, widget, event, coord):
+ #self.table.grab_focus()
+ card = self.cards[coord[0], coord[1]]
+ id = self.cd2id.get(card)
+ self.emit('card-highlighted', id, True)
+ self.selected_card = (coord[0], coord[1])
+
+ def key_press_event(self, widget, event):
+ #self.table.grab_focus()
+ x= self.selected_card[0]
+ y= self.selected_card[1]
+
+ if event.keyval in (gtk.keysyms.Left, gtk.keysyms.KP_Left,gtk.keysyms.a):
+ if self.table_positions.has_key((x-1, y)):
+ card = self.cards[x-1, y]
+ id = self.cd2id.get(card)
+ self.emit('card-highlighted', id, False)
+
+ elif event.keyval in (gtk.keysyms.Right, gtk.keysyms.KP_Right,gtk.keysyms.d):
+ if self.table_positions.has_key((x+1, y)):
+ card = self.cards[x+1, y]
+ id = self.cd2id.get(card)
+ self.emit('card-highlighted', id, False)
+
+ elif event.keyval in (gtk.keysyms.Up, gtk.keysyms.KP_Up,gtk.keysyms.w):
+ if self.table_positions.has_key((x, y-1)):
+ card = self.cards[x, y-1]
+ id = self.cd2id.get(card)
+ self.emit('card-highlighted', id, False)
+
+ elif event.keyval in (gtk.keysyms.Down, gtk.keysyms.KP_Down,gtk.keysyms.s):
+ if self.table_positions.has_key((x, y+1)):
+ card = self.cards[x, y+1]
+ id = self.cd2id.get(card)
+ self.emit('card-highlighted', id, False)
+
+ elif event.keyval in (gtk.keysyms.space,gtk.keysyms.KP_Page_Down):
+ card = self.cards[x, y]
+ self.card_flipped(card)
+
+ def flip_card_mouse(self, widget, event, id):
+ position = self.dict[id]
+ card = self.cards[position]
+ self.card_flipped(card)
+
+ def card_flipped(self, card):
+ if not card.is_flipped():
+ id = self.cd2id[card]
+ self.emit('card-flipped', id, False)
+
+ def set_border(self, widget, id, stroke_color, fill_color):
+ self.id2cd[id].set_border(stroke_color, fill_color)
+
+ def flop_card(self, widget, id):
+ self.id2cd.get(id).flop()
+
+ def flip_card(self, widget, id):
+ self.id2cd.get(id).flip()
+
+ def highlight_card(self, widget, id, status):
+ self.selected_card = self.dict.get(id)
+ self.id2cd.get(id).set_highlight(status)
+
+ def reset(self, widget):
+ for id in self.id2cd.keys():
+ self.id2cd[id].reset()