Mercurial > libervia-backend
annotate frontends/wix/card_game.py @ 91:39c672544593
Tarot: bidding phase
- quick_app: command line is now parsed, "profile" option allow to select it
- xml_tools: list-single is now managed
- plugin tarot: method and signal to manage contract (contrat): tarotChooseContrat & tarotGameContratChoosed
- wix: Q&D Form hack to manage more generic form (not only registration), used to show contract selection form
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 27 May 2010 19:26:19 +0930 |
parents | 4020931569b8 |
children | 2503de7fb4c7 |
rev | line source |
---|---|
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 pdb | |
27 from logging import debug, info, error | |
28 from tools.jid import JID | |
91 | 29 from form import Form |
81 | 30 |
83 | 31 CARD_WIDTH = 74 |
32 CARD_HEIGHT = 136 | |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
33 MIN_WIDTH = 950 #Minimum size of the panel |
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
34 MIN_HEIGHT = 500 |
83 | 35 |
87 | 36 families_order = ['pique', 'coeur', 'trefle', 'carreau', 'atout'] #I have swith the usual order 'trefle' and 'carreau' because card are more easy to see if couleur change (black, red, black, red) |
37 values_order = map(str,range(1,11))+["valet","cavalier","dame","roi"] | |
38 | |
81 | 39 class Card(): |
40 """This class is used to represent a card, graphically and logically""" | |
41 | |
42 def __init__(self, file): | |
43 """@param file: path of the PNG file""" | |
44 self.bitmap = wx.Image(file).ConvertToBitmap() | |
45 root_name = os.path.splitext(os.path.basename(file))[0] | |
46 self.family,self.value=root_name.split('_') | |
47 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False | |
48 | |
49 print "Carte:",self.family, self.value, self.bout | |
50 | |
87 | 51 def __cmp__(self, other): |
52 if other == None: | |
53 return 1 | |
54 if self.family != other.family: | |
55 idx1 = families_order.index(self.family) | |
56 idx2 = families_order.index(other.family) | |
57 return idx1.__cmp__(idx2) | |
58 if self.family == 'atout': | |
59 if self.value == other.value == 'excuse': | |
60 return 0 | |
61 if self.value == 'excuse': | |
62 return -1 | |
63 if other.value == 'excuse': | |
64 return 1 | |
65 return int(self.value).__cmp__(int(other.value)) | |
66 #at this point we have the same family which is not 'atout' | |
67 idx1 = values_order.index(self.value) | |
68 idx2 = values_order.index(other.value) | |
69 return idx1.__cmp__(idx2) | |
70 | |
71 def __str__(self): | |
72 return "[%s,%s]" % (self.family, self.value) | |
83 | 73 |
81 | 74 def draw(self, dc, x, y): |
75 """Draw the card on the device context | |
76 @param dc: device context | |
77 @param x: abscissa | |
78 @param y: ordinate""" | |
79 dc.DrawBitmap(self.bitmap, x, y, True) | |
80 | |
81 | |
82 class CardPanel(wx.Panel): | |
83 """This class is used to display the cards""" | |
84 | |
90 | 85 def __init__(self, parent, referee, players, user): |
81 | 86 wx.Panel.__init__(self, parent) |
90 | 87 self.parent = parent |
88 self.referee = referee | |
87 | 89 self.players = players |
90 self.user = user | |
91 self.bottom_nick = self.user | |
92 idx = self.players.index(self.user) | |
93 idx = (idx + 1) % len(self.players) | |
94 self.right_nick = self.players[idx] | |
95 idx = (idx + 1) % len(self.players) | |
96 self.top_nick = self.players[idx] | |
97 idx = (idx + 1) % len(self.players) | |
98 self.left_nick = self.players[idx] | |
86
4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
Goffi <goffi@goffi.org>
parents:
83
diff
changeset
|
99 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT)) |
83 | 100 self.load_cards("/home/goffi/dev/divers/images/cards/") |
101 self.selected = None #contain the card to highlight | |
102 self.hand_size = 13 #number of cards in a hand | |
103 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards | |
87 | 104 self.hand = [] |
105 self.my_turn = False | |
81 | 106 self.SetBackgroundColour(wx.GREEN) |
83 | 107 self.Bind(wx.EVT_SIZE, self.onResize) |
81 | 108 self.Bind(wx.EVT_PAINT, self.onPaint) |
83 | 109 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
110 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) | |
90 | 111 self.parent.host.bridge.tarotGameReady(user, referee, profile_key = self.parent.host.profile) |
81 | 112 |
113 def load_cards(self, dir): | |
114 """Load all the cards in memory | |
115 @param dir: directory where the PNG files are""" | |
116 self.cards={} | |
117 self.deck=[] | |
118 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names | |
119 self.cards["pique"]={} #spade | |
120 self.cards["coeur"]={} #heart | |
121 self.cards["carreau"]={} #diamond | |
122 self.cards["trefle"]={} #club | |
123 for file in glob.glob(dir+'/*_*.png'): | |
124 card = Card(file) | |
125 self.cards[card.family, card.value]=card | |
126 self.deck.append(card) | |
127 """for value in map(str,range(1,22))+['excuse']: | |
128 self.idx_cards.append(self.cards["atout",value]) | |
129 for family in ["pique", "coeur", "carreau", "trefle"]: | |
130 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: | |
131 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards ! | |
132 | |
87 | 133 def newGame(self, hand): |
134 """Start a new game, with given hand""" | |
90 | 135 print "gof: new game ici avec",hand |
87 | 136 assert (len(self.hand) == 0) |
137 for family, value in hand: | |
138 self.hand.append(self.cards[family, value]) | |
139 self.hand.sort() | |
140 self.my_turn = True | |
141 | |
91 | 142 def contratSelected(self, data): |
143 """Called when the contrat has been choosed | |
144 @param data: form result""" | |
145 debug (_("Contrat choosed")) | |
146 print "\n\n\n===============>>>> \o/ :) :) :) ", data, "\n\n\n" | |
147 contrat = data[0][1] | |
148 self.parent.host.bridge.tarotGameContratChoosed(self.user, self.referee, contrat or 'Passe', self.parent.host.profile) | |
149 | |
150 def chooseContrat(self, xml_data): | |
151 """Called when the player as to select hist contrat | |
152 @param xml_data: SàT xml representation of the form""" | |
153 misc = {'callback': self.contratSelected} | |
154 form = Form(self.parent.host, xml_data, title = _('Please choose your contrat'), options = ['NO_CANCEL'], misc = misc) | |
155 | |
156 | |
83 | 157 def _is_on_hand(self, pos_x, pos_y): |
158 """Return True if the coordinate are on the hand cards""" | |
159 if pos_x > self.orig_x and pos_y > self.orig_y \ | |
160 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ | |
161 and pos_y < self.end_y: | |
162 return True | |
163 return False | |
164 | |
165 def onResize(self, event): | |
166 self._recalc_ori() | |
167 | |
168 def _recalc_ori(self): | |
169 """Recalculate origines, must be call when size change""" | |
170 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards | |
171 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 | |
172 self.end_y = self.orig_y + CARD_HEIGHT | |
173 | |
81 | 174 def onPaint(self, event): |
175 dc = wx.PaintDC(self) | |
87 | 176 |
177 #We print the names to know who play where TODO: print avatars when available | |
178 max_x, max_y = self.GetSize() | |
179 border = 10 #border between nick and end of panel | |
180 right_y = left_y = 200 | |
181 right_width, right_height = dc.GetTextExtent(self.right_nick) | |
182 right_x = max_x - right_width - border | |
183 left_x = border | |
184 top_width, top_height = dc.GetTextExtent(self.top_nick) | |
185 top_x = (max_x - top_width) / 2 | |
186 top_y = border | |
187 dc.DrawText(self.right_nick, right_x, right_y) | |
188 dc.DrawText(self.top_nick, top_x, top_y) | |
189 dc.DrawText(self.left_nick, left_x, left_y) | |
190 | |
83 | 191 x=self.orig_x |
192 for card in self.hand: | |
87 | 193 card.draw(dc,x,self.orig_y - 30 if self.my_turn and card == self.selected else self.orig_y) |
83 | 194 x+=self.visible_size |
195 | |
196 def onMouseMove(self, event): | |
197 pos_x,pos_y = event.GetPosition() | |
198 if self._is_on_hand(pos_x, pos_y): | |
199 try: | |
200 self.selected = self.hand[(pos_x-self.orig_x)/self.visible_size] | |
201 except IndexError: | |
202 self.selected = self.hand[-1] | |
203 self.Refresh() | |
204 else: | |
205 self.selected = None | |
206 self.Refresh() | |
207 | |
208 def onMouseClick(self, event): | |
209 print "mouse click:",event.GetPosition() | |
210 pos_x,pos_y = event.GetPosition() | |
211 if self._is_on_hand(pos_x, pos_y): | |
212 idx = (pos_x-self.orig_x)/self.visible_size | |
213 if idx == len(self.hand): | |
214 idx-=1 | |
215 if self.hand[idx] == self.selected: | |
216 del self.hand[idx] | |
217 self._recalc_ori() | |
218 self.Refresh() |