dabodemo Commit
Revision 418
Date: 2006-10-15 15:06:19 -0700 (Sun, 15 Oct 2006)
Author: ed

Changed:
U   trunk/games/montana.py

Log:
Added preferences to the montana game. Moved several settings that were 
hardcoded in the game to preferences.


Diff:
Modified: trunk/games/montana.py
===================================================================
--- trunk/games/montana.py      2006-10-15 18:59:14 UTC (rev 417)
+++ trunk/games/montana.py      2006-10-15 22:06:19 UTC (rev 418)
@@ -47,25 +47,20 @@
 dabo.ui.loadUI("wx")
 import cardlib
 
-# to go in prefs:
-defaultRedeals = 2
-flashOnlyAces = False
-deckDir = "cards/large"
 
-
 class MontanaDeck(cardlib.PokerDeck):
-       def passHoverEvent(self, evt):
-               self.Parent.onHover(evt)
-       
-       def passEndHoverEvent(self, evt):
-               self.Parent.endHover(evt)
+#      def passHoverEvent(self, evt):
+#              self.board.onHover(evt)
+#      
+#      def passEndHoverEvent(self, evt):
+#              self.board.endHover(evt)
                
                
        def appendCard(self, suit, rank):
                newCard = super(MontanaDeck, self).appendCard(suit, rank)
-               newCard._hover = True
-               newCard.onHover = self.passHoverEvent
-               newCard.endHover = self.passEndHoverEvent
+#              newCard._hover = True
+#              newCard.onHover = self.passHoverEvent
+#              newCard.endHover = self.passEndHoverEvent
                newCard.bindEvent(dabo.dEvents.MouseLeftDown, 
self.board.onCardMDown)
                newCard.bindEvent(dabo.dEvents.MouseLeftUp, 
self.board.onCardMUp)
 
@@ -96,8 +91,8 @@
 
 
 class Board(dabo.ui.dPanel):
-       DeckDirectory = deckDir
        def afterInit(self):
+               self.DeckDirectory = self.Form.getDeckDir()
                self.BackColor = "olivedrab"
                self._priorHandScore = 0
                self._score = 0
@@ -132,8 +127,7 @@
        
        
        def newGame(self):
-               # Change this to use preference setting!
-               self.Redeals = defaultRedeals
+               self.Redeals = self.Form.PreferenceManager.redeals
                self._priorHandScore = 0
                self._score = 0
                
@@ -201,10 +195,14 @@
                                        self.updateStatus()
        
        
-       def onHover(self, evt):
-               self.onCardMDown(evt)
-       def endHover(self, evt):
-               self.onCardMUp(evt)
+#      def onHover(self, evt):
+#              if evt is None:
+#                      return
+#              self.onCardMDown(evt)
+#      def endHover(self, evt):
+#              if evt is None:
+#                      return
+#              self.onCardMUp(evt)
                
        def onCardMDown(self, evt):
                card = evt.EventObject
@@ -226,20 +224,20 @@
                                if rank == 1:
                                        # Another ace; do nothing
                                        return
-               elif flashOnlyAces:
-                       rank, suit = None, None
 
+               
                if rank and suit:
+                       flashAll = aceClicked or not 
self.Form.getOnlyFlashAces()
                        # Flash the card next in sequence
                        target = self.getCard(rank+1, suit)
-                       if target:
+                       if target and flashAll:
                                self.flashCards = [target,]
                        if aceClicked:
                                self.cardTimer.start(1)
                        else:
                                # Add the card before in sequence also
                                target = self.getCard(rank-1, suit)
-                               if target.Rank > 1:
+                               if (target.Rank > 1) and flashAll:
                                        self.flashCards.append(target)
                                # Need to delay the start of the blinking, so 
that the user has enough
                                # time to finish the click to move the card
@@ -248,6 +246,8 @@
                                
        def onCardMUp(self, evt):
                card = evt.EventObject
+               if card is None:
+                       return
                # Stop any flashing:
                self.cardTimer.stop()
                for fc in self.flashCards:
@@ -287,19 +287,19 @@
 
                # update the form
                self.Form.updateScore()
-               
+               deckdir = self.DeckDirectory
                # Mark the dead aces. These are all the aces to the right
                # of Kings.
                sz = self.gridSizer
                for ace in self.deck.aces:
-                       ace.AlternatePicture = "%s/blank" % self.DeckDirectory
+                       ace.AlternatePicture = "%s/blank" % deckdir
                dead = 0
                self.isStuck = False
                for suit in "SHDC":
                        king = self.getCard(13, suit)
                        rtCard = sz.getNeighbor(king, "right")
                        while (rtCard is not None) and (rtCard.Rank == 1):
-                               rtCard.AlternatePicture = "%s/x" % 
self.DeckDirectory
+                               rtCard.AlternatePicture = "%s/x" % deckdir
                                dead += 1
                                rtCard = sz.getNeighbor(rtCard, "right")
                if dead == 4:
@@ -418,7 +418,7 @@
                if hasattr(self, "_redeals"):
                        v = self._redeals
                else:
-                       v = self._redeals = defaultRedeals
+                       v = self._redeals = self.Form.PreferenceManager.redeals
                return v
 
        def _setRedeals(self, val):
@@ -448,13 +448,27 @@
                        
                        
 class MontanaForm(dabo.ui.dForm):
+       def __init__(self, *args, **kwargs):
+               self._basePrefKey = "dabo.demo.games.montana"
+               super(MontanaForm, self).__init__(*args, **kwargs)
+
+
        def afterInit(self):
                self.Centered = True
                self.Caption = "Montana"
+               pfm = self.PreferenceManager
+               if not isinstance(pfm.redeals, (int, long)):
+                       pfm.redeals = 2         
+               if not isinstance(pfm.flashOnlyAces, bool):
+                       pfm.flashOnlyAces = False
+               if not isinstance(pfm.smallDeck, bool):
+                       pfm.smallDeck = False
+
                # Add the board, score display and re-deal button
                self.gameBoard = Board(self)
                self.Sizer.append1x(self.gameBoard)
                self.layout()
+               self.fitToSizer(80, 30)
                self.fillMenu()
                dabo.ui.callAfter(self.startGame)
        
@@ -499,6 +513,17 @@
                btn.Enabled = (self.gameBoard.isStuck and 
self.gameBoard.Redeals > 0)
 
 
+       def getOnlyFlashAces(self):
+               return self.PreferenceManager.flashOnlyAces
+               
+
+       def getDeckDir(self):
+               ret = "cards/large"
+               if self.PreferenceManager.smallDeck:
+                       ret = "cards/small"
+               return ret
+
+
        def onEditUndo(self, evt):
                self.gameBoard.undo()
                
@@ -508,8 +533,32 @@
                
 
        def onEditPreferences(self, evt):
-               print "PREF EDIT"
+               """Create a dialog to edit preferences."""
+               xml = self.getPrefControlXML()
+               class MontanaPrefDialog(dabo.ui.dOkCancelDialog):
+                       def addControls(self):
+                               self.prf = self.Parent.PreferenceManager
+                               ctls = self.addObject(xml)
+                               self.Sizer.append1x(ctls, border=10)
+                               self.Sizer.appendSpacer(20)
+                               self.update()
                
+               prf = self.PreferenceManager
+               # Save the card size. If it changes, we need to create a new 
board
+               small = prf.smallDeck
+               prf.AutoPersist = False
+               dlg = MontanaPrefDialog(self, Caption="Montana Prefs")
+               dlg.show()
+               if dlg.Accepted:
+                       prf.persist()
+                       if prf.smallDeck != small:
+                               msg = """You must quit and restart the game for 
the 
+                                               card size changes to take 
effect""".replace("\t", "")
+                               dabo.ui.info(msg, title="Card Size Changed")
+               else:
+                       prf.flushCache()
+               prf.AutoPersist = True
+               
        
        def onRules(self, evt):
                win = dabo.ui.dForm(self, Caption="Montana Rules", 
Centered=True)
@@ -555,12 +604,24 @@
                self.txtHandScore.Value = self.gameBoard.Score
                self.txtGameScore.Value = self.gameBoard.TotalScore
                self.layout()
+
+
+       def getPrefControlXML(self):
+               return """<?xml version="1.0" encoding="utf-8" standalone="no"?>
+<dPanel classID="408701584" BorderColor="(0, 0, 0)" sizerInfo="{}" 
Name="dPanel" Buffered="False" code-ID="dPanel-top" ForeColor="(0, 0, 0)" 
designerClass="path://montanaPrefs.cdxml" BorderWidth="0" BackColor="(221, 221, 
221)" BorderStyle="Default" ToolTipText="None" BorderLineStyle="Solid" 
savedClass="True">
+       <dGridSizer classID="408701584-408700624" HGap="3" Rows="3" 
designerClass="LayoutGridSizer" VGap="3" MaxDimension="r" Columns="2">
+               <dLabel classID="408701584-408656304" BorderColor="(0, 0, 0)" 
FontBold="False" Name="dLabel" FontFace="Lucida Grande" sizerInfo="{'RowSpan': 
1, 'RowExpand': False, 'ColSpan': 1, 'Proportion': 0, 'HAlign': 'Right', 
'ColExpand': False, 'VAlign': 'Middle', 'Expand': False}" rowColPos="(0, 0)" 
Caption="Number of Redeals:" ForeColor="(0, 0, 0)" designerClass="controlMix" 
BorderWidth="0" BackColor="(221, 221, 221)" FontSize="13" BorderStyle="Default" 
ToolTipText="None" BorderLineStyle="Solid" FontItalic="False" 
FontUnderline="False"></dLabel>
+               <dSpinner classID="408701584-408656560" BorderColor="(0, 0, 0)" 
FontBold="False" Name="dSpinner" FontFace="Lucida Grande" 
sizerInfo="{'RowSpan': 1, 'RowExpand': False, 'ColSpan': 1, 'Proportion': 0, 
'HAlign': 'Left', 'ColExpand': True, 'VAlign': 'Top', 'Expand': False}"  
rowColPos="(0, 1)" FontUnderline="False" ForeColor="(0, 0, 0)" 
DataSource="self.Form.prf" designerClass="controlMix" BorderWidth="0" 
BackColor="(221, 221, 221)" FontSize="13" BorderStyle="Default" 
ToolTipText="None" BorderLineStyle="Solid" FontItalic="False" 
DataField="redeals"></dSpinner>
+               <dLabel classID="408701584-408773008" BorderColor="(0, 0, 0)" 
FontBold="False" Name="dLabel1" FontFace="Lucida Grande" sizerInfo="{'RowSpan': 
1, 'RowExpand': False, 'ColSpan': 1, 'Proportion': 0, 'HAlign': 'Right', 
'ColExpand': False, 'VAlign': 'Middle', 'Expand': False}" rowColPos="(1, 0)" 
Caption="Only flash empty spaces?" ForeColor="(0, 0, 0)" 
designerClass="controlMix" BorderWidth="0" BackColor="(221, 221, 221)" 
FontSize="13" BorderStyle="Default" ToolTipText="None" BorderLineStyle="Solid" 
FontItalic="False" FontUnderline="False"></dLabel>
+               <dCheckBox classID="408701584-408811504" BorderColor="(0, 0, 
0)" FontBold="False" DataField="flashOnlyAces" Name="dCheckBox" 
FontFace="Lucida Grande" sizerInfo="{'RowSpan': 1, 'RowExpand': False, 
'ColSpan': 1, 'Proportion': 0, 'HAlign': 'Left', 'ColExpand': True, 'VAlign': 
'Top', 'Expand': True}" rowColPos="(1, 1)" Caption="" ForeColor="(0, 0, 0)" 
DataSource="self.Form.prf" designerClass="controlMix" BorderWidth="0" 
BackColor="(221, 221, 221)" FontSize="13" BorderStyle="Default" 
ToolTipText="None" BorderLineStyle="Solid" FontItalic="False" 
FontUnderline="False"></dCheckBox>
+               <dCheckBox classID="408701584-430021296" BorderColor="(0, 0, 
0)" FontBold="False" DataField="smallDeck" Name="dCheckBox1" FontFace="Lucida 
Grande" sizerInfo="{'RowSpan': 1, 'RowExpand': False, 'ColSpan': 1, 
'Proportion': 0, 'HAlign': 'Left', 'ColExpand': True, 'VAlign': 'Top', 
'Expand': True}" rowColPos="(2, 1)" Caption="" ForeColor="(0, 0, 0)" 
DataSource="self.Form.prf" designerClass="controlMix" BorderWidth="0" 
BackColor="(221, 221, 221)" FontSize="13" BorderStyle="Default" 
ToolTipText="None" BorderLineStyle="Solid" FontItalic="False" 
FontUnderline="False"></dCheckBox>
+               <dLabel classID="408701584-430460656" BorderColor="(0, 0, 0)" 
FontBold="False" Name="dLabel2" FontFace="Lucida Grande" sizerInfo="{'RowSpan': 
1, 'RowExpand': False, 'ColSpan': 1, 'Proportion': 0, 'HAlign': 'Right', 
'ColExpand': False, 'VAlign': 'Middle', 'Expand': False}" rowColPos="(2, 0)" 
Caption="Use small cards" ForeColor="(0, 0, 0)" designerClass="controlMix" 
BorderWidth="0" BackColor="(221, 221, 221)" FontSize="13" BorderStyle="Default" 
ToolTipText="None" BorderLineStyle="Solid" FontItalic="False" 
FontUnderline="False"></dLabel>
+       </dGridSizer>
+</dPanel>"""
                
 
 if __name__ == "__main__":
        app = dabo.dApp(MainFormClass=MontanaForm)
        app.setAppInfo("appName", "Montana")
-       app.UserSettingProvider.SettingsDirectoryName = "Dabo"
-       app.UserSettingProvider.SettingsFileName = "MontanaDemo.ini"
        app.start()
 




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev

Reply via email to