diff 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
line wrap: on
line diff
--- a/frontends/primitivus/chat.py	Thu Jul 22 22:47:29 2010 +0800
+++ b/frontends/primitivus/chat.py	Mon Jul 26 19:43:44 2010 +0800
@@ -25,6 +25,7 @@
 import custom_widgets
 import time
 from tools.jid  import JID
+from card_game import CardGame
 
 
 class ChatText(urwid.FlowWidget):
@@ -79,8 +80,10 @@
         self.content = urwid.SimpleListWalker([])
         self.text_list = urwid.ListBox(self.content)
         self.chat_widget = urwid.Frame(self.text_list)
-        self.columns = urwid.Columns([('weight', 8, self.chat_widget)])
-        urwid.WidgetWrap.__init__(self, self.__getDecoration(self.columns))
+        self.chat_colums = urwid.Columns([('weight', 8, self.chat_widget)])
+        self.chat_colums = urwid.Columns([('weight', 8, self.chat_widget)])
+        self.pile = urwid.Pile([self.chat_colums])
+        urwid.WidgetWrap.__init__(self, self.__getDecoration(self.pile))
         self.setType(type)
         self.day_change = time.strptime(time.strftime("%a %b %d 00:00:00  %Y")) #struct_time of day changing time
         self.show_timestamp = True
@@ -91,7 +94,7 @@
     def keypress(self, size, key):
         if key == "meta p": #user wants to (un)hide the presents panel
             if self.type == 'group':
-                widgets = self.columns.widget_list
+                widgets = self.chat_colums.widget_list
                 if self.present_panel in widgets:
                     self.__removePresentPanel()
                 else:
@@ -135,7 +138,7 @@
         if type == 'one2one':
             self.historyPrint(profile=self.host.profile)
         elif type == 'group':
-            if len(self.columns.widget_list) == 1:
+            if len(self.chat_colums.widget_list) == 1:
                 present_widget = self.__buildPresentList()
                 self.present_panel = custom_widgets.VerticalSeparator(present_widget)
                 self.__appendPresentPanel()
@@ -144,10 +147,11 @@
         return custom_widgets.LabelLine(widget, custom_widgets.SurroundedText(unicode(self.target)))
 
     def showDecoration(self, show=True):
+        """Show/Hide the decoration around the chat window"""
         if show:
-            main_widget = self.__getDecoration(self.columns)
+            main_widget = self.__getDecoration(self.pile)
         else:
-            main_widget = self.columns
+            main_widget = self.pile
         self._w = main_widget
 
 
@@ -156,13 +160,28 @@
         return self.present_wid
    
     def __appendPresentPanel(self):
-        self.columns.widget_list.append(self.present_panel) 
-        self.columns.column_types.append(('weight', 2))
+        self.chat_colums.widget_list.append(self.present_panel) 
+        self.chat_colums.column_types.append(('weight', 2))
 
     def __removePresentPanel(self):
-        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
-        self.columns.widget_list.remove(self.present_panel)
-        del self.columns.column_types[-1]
+        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
+        self.chat_colums.widget_list.remove(self.present_panel)
+        del self.chat_colums.column_types[-1]
+    
+    def __appendGamePanel(self, widget):
+        assert (len(self.pile.widget_list) == 1)
+        self.pile.widget_list.insert(0,widget)
+        self.pile.item_types.insert(0,('weight', 1))
+        self.pile.widget_list.insert(1,urwid.Filler(urwid.Divider('-')))
+        self.pile.item_types.insert(1,('fixed', 1))
+        self.host.redraw()
+
+    def __removeGamePanel(self):
+        assert (len(self.pile.widget_list) == 3)
+        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
+        del self.pile.widget_list[0]
+        del self.pile.item_types[0]
+        self.host.redraw()
 
     def setSubject(self, subject, wrap='space'):
         """Set title for a group chat"""
@@ -209,11 +228,25 @@
         self.content.append(ChatText(self, timestamp or None, my_jid, from_jid, msg))
         self.text_list.set_focus(len(self.content)-1)
         self.host.redraw()
+    
+    def startGame(self, game_type, referee, players):
+        """Configure the chat window to start a game"""
+        if game_type=="Tarot":
+            try:
+                self.tarot_wid = CardGame(self, referee, players, self.nick)
+                self.__appendGamePanel(self.tarot_wid)
+            except e:
+                self.host.debug()
+    
+    def getGame(self, game_type):
+        """Return class managing the game type"""
+        #TODO: check that the game is launched, and manage errors
+        if game_type=="Tarot":
+            return self.tarot_wid 
 
     #MENU EVENTS#
     def onTarotRequest(self, menu):
         if len(self.occupants) != 4:
-            self.host.debug()
             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)) 
         else:
             self.host.bridge.tarotGameCreate(self.id, list(self.occupants), self.host.profile)