Mercurial > libervia-backend
annotate frontends/wix/card_game.py @ 86:4b5f2d55b6ac
wix: Tarot panel now appear on top of groupchat window when a Tarot game is started
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 11 May 2010 13:06:05 +0930 |
parents | 1ac759ea28c3 |
children | 66d784082930 |
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 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 | |
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
|
35 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
|
36 MIN_HEIGHT = 500 |
83 | 37 |
81 | 38 class Card(): |
39 """This class is used to represent a card, graphically and logically""" | |
40 | |
41 def __init__(self, file): | |
42 """@param file: path of the PNG file""" | |
43 self.bitmap = wx.Image(file).ConvertToBitmap() | |
44 root_name = os.path.splitext(os.path.basename(file))[0] | |
45 self.family,self.value=root_name.split('_') | |
46 self.bout = True if self.family=="atout" and self.value in ["1","21","excuse"] else False | |
47 | |
48 print "Carte:",self.family, self.value, self.bout | |
49 | |
83 | 50 |
81 | 51 def draw(self, dc, x, y): |
52 """Draw the card on the device context | |
53 @param dc: device context | |
54 @param x: abscissa | |
55 @param y: ordinate""" | |
56 dc.DrawBitmap(self.bitmap, x, y, True) | |
57 | |
58 | |
59 class CardPanel(wx.Panel): | |
60 """This class is used to display the cards""" | |
61 | |
62 def __init__(self, parent): | |
63 wx.Panel.__init__(self, parent) | |
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
|
64 self.SetMinSize(wx.Size(MIN_WIDTH, MIN_HEIGHT)) |
83 | 65 self.load_cards("/home/goffi/dev/divers/images/cards/") |
66 self.selected = None #contain the card to highlight | |
67 self.hand_size = 13 #number of cards in a hand | |
68 self.visible_size = CARD_WIDTH/2 #number of pixels visible for cards | |
69 self.hand = random.sample(self.deck, self.hand_size) | |
81 | 70 self.SetBackgroundColour(wx.GREEN) |
83 | 71 self.Bind(wx.EVT_SIZE, self.onResize) |
81 | 72 self.Bind(wx.EVT_PAINT, self.onPaint) |
83 | 73 self.Bind(wx.EVT_MOTION, self.onMouseMove) |
74 self.Bind(wx.EVT_LEFT_UP, self.onMouseClick) | |
81 | 75 |
76 def load_cards(self, dir): | |
77 """Load all the cards in memory | |
78 @param dir: directory where the PNG files are""" | |
79 self.cards={} | |
80 self.deck=[] | |
81 self.cards["atout"]={} #As Tarot is a french game, it's more handy & logical to keep french names | |
82 self.cards["pique"]={} #spade | |
83 self.cards["coeur"]={} #heart | |
84 self.cards["carreau"]={} #diamond | |
85 self.cards["trefle"]={} #club | |
86 for file in glob.glob(dir+'/*_*.png'): | |
87 card = Card(file) | |
88 self.cards[card.family, card.value]=card | |
89 self.deck.append(card) | |
90 """for value in map(str,range(1,22))+['excuse']: | |
91 self.idx_cards.append(self.cards["atout",value]) | |
92 for family in ["pique", "coeur", "carreau", "trefle"]: | |
93 for value in map(str,range(1,11))+["valet","cavalier","dame","roi"]: | |
94 self.idx_cards.append(self.cards[family, value])""" #XXX: no need to sort the cards ! | |
95 | |
83 | 96 def _is_on_hand(self, pos_x, pos_y): |
97 """Return True if the coordinate are on the hand cards""" | |
98 if pos_x > self.orig_x and pos_y > self.orig_y \ | |
99 and pos_x < self.orig_x + (len(self.hand)+1) * self.visible_size \ | |
100 and pos_y < self.end_y: | |
101 return True | |
102 return False | |
103 | |
104 def onResize(self, event): | |
105 self._recalc_ori() | |
106 | |
107 def _recalc_ori(self): | |
108 """Recalculate origines, must be call when size change""" | |
109 self.orig_x = (self.GetSizeTuple()[0]-(len(self.hand)+1)*self.visible_size)/2 #where we start to draw cards | |
110 self.orig_y = self.GetSizeTuple()[1] - CARD_HEIGHT - 20 | |
111 self.end_y = self.orig_y + CARD_HEIGHT | |
112 | |
81 | 113 def onPaint(self, event): |
114 dc = wx.PaintDC(self) | |
83 | 115 x=self.orig_x |
116 for card in self.hand: | |
117 card.draw(dc,x,self.orig_y - 30 if card == self.selected else self.orig_y) | |
118 x+=self.visible_size | |
119 | |
120 def onMouseMove(self, event): | |
121 pos_x,pos_y = event.GetPosition() | |
122 if self._is_on_hand(pos_x, pos_y): | |
123 try: | |
124 self.selected = self.hand[(pos_x-self.orig_x)/self.visible_size] | |
125 except IndexError: | |
126 self.selected = self.hand[-1] | |
127 self.Refresh() | |
128 else: | |
129 self.selected = None | |
130 self.Refresh() | |
131 | |
132 def onMouseClick(self, event): | |
133 print "mouse click:",event.GetPosition() | |
134 pos_x,pos_y = event.GetPosition() | |
135 if self._is_on_hand(pos_x, pos_y): | |
136 idx = (pos_x-self.orig_x)/self.visible_size | |
137 if idx == len(self.hand): | |
138 idx-=1 | |
139 if self.hand[idx] == self.selected: | |
140 del self.hand[idx] | |
141 self._recalc_ori() | |
142 self.Refresh() | |
81 | 143 |
144 class CardGame(wx.Frame, QuickChat): | |
83 | 145 """The window used to play all kind of card games""" |
81 | 146 |
147 def __init__(self, host): | |
83 | 148 wx.Frame.__init__(self, None, pos=(0,0), size=(950,500)) |
81 | 149 |
150 self.host = host | |
151 | |
152 self.sizer = wx.BoxSizer(wx.VERTICAL) | |
153 self.panel = CardPanel(self) | |
154 self.sizer.Add(self.panel, 1, flag=wx.EXPAND) | |
155 self.SetSizer(self.sizer) | |
156 self.SetAutoLayout(True) | |
157 | |
158 | |
159 #events | |
160 |