comparison frontends/primitivus/chat.py @ 144:80661755ea8d

Primitivus: Tarot card game implementation - quick frontend: card_game added - wix: card_game splitted with quick frontend - tools: new game library - primitivus: new card_game widget (not finished yet) - primitivus: SàT XML UI management: first draft
author Goffi <goffi@goffi.org>
date Mon, 26 Jul 2010 19:43:44 +0800
parents 227394eb080c
children f197b52796ee
comparison
equal deleted inserted replaced
143:119f45746fde 144:80661755ea8d
23 from quick_frontend.quick_contact_list import QuickContactList 23 from quick_frontend.quick_contact_list import QuickContactList
24 from quick_frontend.quick_chat import QuickChat 24 from quick_frontend.quick_chat import QuickChat
25 import custom_widgets 25 import custom_widgets
26 import time 26 import time
27 from tools.jid import JID 27 from tools.jid import JID
28 from card_game import CardGame
28 29
29 30
30 class ChatText(urwid.FlowWidget): 31 class ChatText(urwid.FlowWidget):
31 """Manage the printing of chat message""" 32 """Manage the printing of chat message"""
32 33
77 self.target = target 78 self.target = target
78 QuickChat.__init__(self, target, host, type) 79 QuickChat.__init__(self, target, host, type)
79 self.content = urwid.SimpleListWalker([]) 80 self.content = urwid.SimpleListWalker([])
80 self.text_list = urwid.ListBox(self.content) 81 self.text_list = urwid.ListBox(self.content)
81 self.chat_widget = urwid.Frame(self.text_list) 82 self.chat_widget = urwid.Frame(self.text_list)
82 self.columns = urwid.Columns([('weight', 8, self.chat_widget)]) 83 self.chat_colums = urwid.Columns([('weight', 8, self.chat_widget)])
83 urwid.WidgetWrap.__init__(self, self.__getDecoration(self.columns)) 84 self.chat_colums = urwid.Columns([('weight', 8, self.chat_widget)])
85 self.pile = urwid.Pile([self.chat_colums])
86 urwid.WidgetWrap.__init__(self, self.__getDecoration(self.pile))
84 self.setType(type) 87 self.setType(type)
85 self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00 %Y")) #struct_time of day changing time 88 self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00 %Y")) #struct_time of day changing time
86 self.show_timestamp = True 89 self.show_timestamp = True
87 self.show_short_nick = False 90 self.show_short_nick = False
88 self.show_title = 1 #0: clip title; 1: full title; 2: no title 91 self.show_title = 1 #0: clip title; 1: full title; 2: no title
89 self.subject = None 92 self.subject = None
90 93
91 def keypress(self, size, key): 94 def keypress(self, size, key):
92 if key == "meta p": #user wants to (un)hide the presents panel 95 if key == "meta p": #user wants to (un)hide the presents panel
93 if self.type == 'group': 96 if self.type == 'group':
94 widgets = self.columns.widget_list 97 widgets = self.chat_colums.widget_list
95 if self.present_panel in widgets: 98 if self.present_panel in widgets:
96 self.__removePresentPanel() 99 self.__removePresentPanel()
97 else: 100 else:
98 self.__appendPresentPanel() 101 self.__appendPresentPanel()
99 elif key == "meta t": #user wants to (un)hide timestamp 102 elif key == "meta t": #user wants to (un)hide timestamp
133 def setType(self, type): 136 def setType(self, type):
134 QuickChat.setType(self, type) 137 QuickChat.setType(self, type)
135 if type == 'one2one': 138 if type == 'one2one':
136 self.historyPrint(profile=self.host.profile) 139 self.historyPrint(profile=self.host.profile)
137 elif type == 'group': 140 elif type == 'group':
138 if len(self.columns.widget_list) == 1: 141 if len(self.chat_colums.widget_list) == 1:
139 present_widget = self.__buildPresentList() 142 present_widget = self.__buildPresentList()
140 self.present_panel = custom_widgets.VerticalSeparator(present_widget) 143 self.present_panel = custom_widgets.VerticalSeparator(present_widget)
141 self.__appendPresentPanel() 144 self.__appendPresentPanel()
142 145
143 def __getDecoration(self, widget): 146 def __getDecoration(self, widget):
144 return custom_widgets.LabelLine(widget, custom_widgets.SurroundedText(unicode(self.target))) 147 return custom_widgets.LabelLine(widget, custom_widgets.SurroundedText(unicode(self.target)))
145 148
146 def showDecoration(self, show=True): 149 def showDecoration(self, show=True):
150 """Show/Hide the decoration around the chat window"""
147 if show: 151 if show:
148 main_widget = self.__getDecoration(self.columns) 152 main_widget = self.__getDecoration(self.pile)
149 else: 153 else:
150 main_widget = self.columns 154 main_widget = self.pile
151 self._w = main_widget 155 self._w = main_widget
152 156
153 157
154 def __buildPresentList(self): 158 def __buildPresentList(self):
155 self.present_wid = custom_widgets.GenericList([],option_type = custom_widgets.ClickableText) 159 self.present_wid = custom_widgets.GenericList([],option_type = custom_widgets.ClickableText)
156 return self.present_wid 160 return self.present_wid
157 161
158 def __appendPresentPanel(self): 162 def __appendPresentPanel(self):
159 self.columns.widget_list.append(self.present_panel) 163 self.chat_colums.widget_list.append(self.present_panel)
160 self.columns.column_types.append(('weight', 2)) 164 self.chat_colums.column_types.append(('weight', 2))
161 165
162 def __removePresentPanel(self): 166 def __removePresentPanel(self):
163 self.columns.set_focus(0) #necessary as the focus change to the next object, we can go out of range if we are on the last object of self.columns 167 self.chat_colums.set_focus(0) #necessary as the focus change to the next object, we can go out of range if we are on the last object of self.chat_colums
164 self.columns.widget_list.remove(self.present_panel) 168 self.chat_colums.widget_list.remove(self.present_panel)
165 del self.columns.column_types[-1] 169 del self.chat_colums.column_types[-1]
170
171 def __appendGamePanel(self, widget):
172 assert (len(self.pile.widget_list) == 1)
173 self.pile.widget_list.insert(0,widget)
174 self.pile.item_types.insert(0,('weight', 1))
175 self.pile.widget_list.insert(1,urwid.Filler(urwid.Divider('-')))
176 self.pile.item_types.insert(1,('fixed', 1))
177 self.host.redraw()
178
179 def __removeGamePanel(self):
180 assert (len(self.pile.widget_list) == 3)
181 self.pile.set_focus(0) #necessary as the focus change to the next object, we can go out of range if we are on the last object of self.chat_colums
182 del self.pile.widget_list[0]
183 del self.pile.item_types[0]
184 self.host.redraw()
166 185
167 def setSubject(self, subject, wrap='space'): 186 def setSubject(self, subject, wrap='space'):
168 """Set title for a group chat""" 187 """Set title for a group chat"""
169 QuickChat.setSubject(self, subject) 188 QuickChat.setSubject(self, subject)
170 self.subject = subject 189 self.subject = subject
207 assert (from_jid.__class__ == JID) 226 assert (from_jid.__class__ == JID)
208 my_jid = self.host.profiles[profile]['whoami'] 227 my_jid = self.host.profiles[profile]['whoami']
209 self.content.append(ChatText(self, timestamp or None, my_jid, from_jid, msg)) 228 self.content.append(ChatText(self, timestamp or None, my_jid, from_jid, msg))
210 self.text_list.set_focus(len(self.content)-1) 229 self.text_list.set_focus(len(self.content)-1)
211 self.host.redraw() 230 self.host.redraw()
231
232 def startGame(self, game_type, referee, players):
233 """Configure the chat window to start a game"""
234 if game_type=="Tarot":
235 try:
236 self.tarot_wid = CardGame(self, referee, players, self.nick)
237 self.__appendGamePanel(self.tarot_wid)
238 except e:
239 self.host.debug()
240
241 def getGame(self, game_type):
242 """Return class managing the game type"""
243 #TODO: check that the game is launched, and manage errors
244 if game_type=="Tarot":
245 return self.tarot_wid
212 246
213 #MENU EVENTS# 247 #MENU EVENTS#
214 def onTarotRequest(self, menu): 248 def onTarotRequest(self, menu):
215 if len(self.occupants) != 4: 249 if len(self.occupants) != 4:
216 self.host.debug()
217 self.host.showPopUp(custom_widgets.Alert(_("Can't start game"), _("You need to be exactly 4 peoples in the room to start a Tarot game"), ok_cb=self.host.removePopUp)) 250 self.host.showPopUp(custom_widgets.Alert(_("Can't start game"), _("You need to be exactly 4 peoples in the room to start a Tarot game"), ok_cb=self.host.removePopUp))
218 else: 251 else:
219 self.host.bridge.tarotGameCreate(self.id, list(self.occupants), self.host.profile) 252 self.host.bridge.tarotGameCreate(self.id, list(self.occupants), self.host.profile)