81
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 wix: a SAT frontend |
|
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) |
|
7 |
|
8 This program is free software: you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation, either version 3 of the License, or |
|
11 (at your option) any later version. |
|
12 |
|
13 This program is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 """ |
|
21 |
|
22 |
|
23 |
|
24 import wx |
|
25 import os.path, glob |
|
26 import random |
|
27 import pdb |
|
28 from logging import debug, info, error |
|
29 from tools.jid import JID |
|
30 from quick_frontend.quick_chat import QuickChat |
|
31 from contact_list import ContactList |
|
32 |
83
|
33 CARD_WIDTH = 74 |
|
34 CARD_HEIGHT = 136 |
|
35 |
81
|
36 class Card(): |
|
37 """This class is used to represent a card, graphically and logically""" |
|
38 |
|
39 def __init__(self, file): |
|
40 """@param file: path of the PNG file""" |
|
41 self.bitmap = wx.Image(file).ConvertToBitmap() |
|
42 root_name = os.path.splitext(os.path.basename(file))[0] |
|
43 self.family,self.value=root_name.split('_') |
|
44 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False |
|
45 |
|
46 print "Carte:",self.family, self.value, self.bout |
|
47 |
83
|
48 |
81
|
49 def draw(self, dc, x, y): |
|
50 """Draw the card on the device context |
|
51 @param dc: device context |
|
52 @param x: abscissa |
|
53 @param y: ordinate""" |
|
54 dc.DrawBitmap(self.bitmap, x, y, True) |
|
55 |
|
56 |
|
57 class CardPanel(wx.Panel): |
|
58 """This class is used to display the cards""" |
|
59 |
|
60 def __init__(self, parent): |
|
61 wx.Panel.__init__(self, parent) |
83
|
62 self.load_cards("/home/goffi/dev/divers/images/cards/") |
|
63 self.selected = None #contain the card to highlight |
|
64 self.hand_size = 13 #number of cards in a hand |
|
65 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards |
|
66 self.hand = random.sample(self.deck, self.hand_size) |
81
|
67 self.SetBackgroundColour(wx.GREEN) |
83
|
68 self.Bind(wx.EVT_SIZE, self.onResize) |
81
|
69 self.Bind(wx.EVT_PAINT, self.onPaint) |
83
|
70 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
|
71 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) |
81
|
72 |
|
73 def load_cards(self, dir): |
|
74 """Load all the cards in memory |
|
75 @param dir: directory where the PNG files are""" |
|
76 self.cards={} |
|
77 self.deck=[] |
|
78 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names |
|
79 self.cards["pique"]={} #spade |
|
80 self.cards["coeur"]={} #heart |
|
81 self.cards["carreau"]={} #diamond |
|
82 self.cards["trefle"]={} #club |
|
83 for file in glob.glob(dir+'/*_*.png'): |
|
84 card = Card(file) |
|
85 self.cards[card.family, card.value]=card |
|
86 self.deck.append(card) |
|
87 """for value in map(str,range(1,22))+['excuse']: |
|
88 self.idx_cards.append(self.cards["atout",value]) |
|
89 for family in ["pique", "coeur", "carreau", "trefle"]: |
|
90 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: |
|
91 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards ! |
|
92 |
83
|
93 def _is_on_hand(self, pos_x, pos_y): |
|
94 """Return True if the coordinate are on the hand cards""" |
|
95 if pos_x > self.orig_x and pos_y > self.orig_y \ |
|
96 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ |
|
97 and pos_y < self.end_y: |
|
98 return True |
|
99 return False |
|
100 |
|
101 def onResize(self, event): |
|
102 self._recalc_ori() |
|
103 |
|
104 def _recalc_ori(self): |
|
105 """Recalculate origines, must be call when size change""" |
|
106 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards |
|
107 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 |
|
108 self.end_y = self.orig_y + CARD_HEIGHT |
|
109 |
81
|
110 def onPaint(self, event): |
|
111 dc = wx.PaintDC(self) |
83
|
112 x=self.orig_x |
|
113 for card in self.hand: |
|
114 card.draw(dc,x,self.orig_y - 30 if card == self.selected else self.orig_y) |
|
115 x+=self.visible_size |
|
116 |
|
117 def onMouseMove(self, event): |
|
118 pos_x,pos_y = event.GetPosition() |
|
119 if self._is_on_hand(pos_x, pos_y): |
|
120 try: |
|
121 self.selected = self.hand[(pos_x-self.orig_x)/self.visible_size] |
|
122 except IndexError: |
|
123 self.selected = self.hand[-1] |
|
124 self.Refresh() |
|
125 else: |
|
126 self.selected = None |
|
127 self.Refresh() |
|
128 |
|
129 def onMouseClick(self, event): |
|
130 print "mouse click:",event.GetPosition() |
|
131 pos_x,pos_y = event.GetPosition() |
|
132 if self._is_on_hand(pos_x, pos_y): |
|
133 idx = (pos_x-self.orig_x)/self.visible_size |
|
134 if idx == len(self.hand): |
|
135 idx-=1 |
|
136 if self.hand[idx] == self.selected: |
|
137 del self.hand[idx] |
|
138 self._recalc_ori() |
|
139 self.Refresh() |
81
|
140 |
|
141 class CardGame(wx.Frame, QuickChat): |
83
|
142 """The window used to play all kind of card games""" |
81
|
143 |
|
144 def __init__(self, host): |
83
|
145 wx.Frame.__init__(self, None, pos=(0,0), size=(950,500)) |
81
|
146 |
|
147 self.host = host |
|
148 |
|
149 self.sizer = wx.BoxSizer(wx.VERTICAL) |
|
150 self.panel = CardPanel(self) |
|
151 self.sizer.Add(self.panel, 1, flag=wx.EXPAND) |
|
152 self.SetSizer(self.sizer) |
|
153 self.SetAutoLayout(True) |
|
154 |
|
155 |
|
156 #events |
|
157 |