From 0326e8f208b263ae315df0922326c57d3f037a5c Mon Sep 17 00:00:00 2001 From: skomoroh Date: Sun, 22 Oct 2006 21:21:38 +0000 Subject: [PATCH] * misc. bugs fixes and improvements git-svn-id: file:///home/shlomif/Backup/svn-dumps/PySolFC/svnsync-repos/pysolfc/PySolFC/trunk@82 efabe8c0-fbe8-4139-b769-b5e6d273206e --- pysollib/games/ultra/matrix.py | 41 ++++++------ pysollib/main.py | 114 ++------------------------------- pysollib/pysolgtk/tkwrap.py | 14 +++- pysollib/tile/tkwrap.py | 74 +++++++++++++++++++-- pysollib/tk/tkwrap.py | 88 ++++++++++++++++++++++++- 5 files changed, 189 insertions(+), 142 deletions(-) diff --git a/pysollib/games/ultra/matrix.py b/pysollib/games/ultra/matrix.py index b36ce233..70e833c3 100644 --- a/pysollib/games/ultra/matrix.py +++ b/pysollib/games/ultra/matrix.py @@ -80,9 +80,6 @@ class Matrix_RowStack(OpenStack): base_rank=ANY_RANK) apply(OpenStack.__init__, (self, x, y, game), cap) - def acceptsCards(self, from_stack, cards): - return OpenStack.acceptsCards(self, from_stack, cards) - def canFlipCard(self): return 0 @@ -315,23 +312,22 @@ class Matrix3(Game): # Game extras # - def shuffle(self): - cards = list(self.cards)[:] + def _shuffleHook(self, cards): + # create solved game + ncards = len(cards)-1 + for c in cards: + if c.rank == ncards: + cards.remove(c) + break + n = 0 + for i in range(ncards-1): + for j in range(i+1, ncards): + if cards[i].rank > cards[j].rank: + n += 1 cards.reverse() - for card in cards: - self.s.talon.addCard(card, update=0) - card.showBack(unhide=0) - - def scramble(self): - if self.gstats.restarted: - self.random.reset() - ncards, randint = self.gameinfo.ncards, self.random.randint - r = self.s.rows[ncards - int(math.sqrt(ncards))] - rc, stackmap = 1, r.blockMap() - for i in range(randint(max(200, ncards * 4), max(300, ncards * 5))): - r.clickHandler(r) - r = self.s.rows[stackmap[rc][randint(0, len(stackmap[0]) - 1)]] - rc, stackmap = (rc + 1) % 2, r.blockMap() + if n%2: + cards[0], cards[1] = cards[1], cards[0] + return [c]+cards # # Game over rides @@ -339,10 +335,9 @@ class Matrix3(Game): def startGame(self): assert len(self.s.talon.cards) == self.gameinfo.ncards - self.s.talon.dealRow(rows=self.s.rows[:self.gameinfo.ncards - 1], - flip=1, frames=3) - self.scramble() self.startDealSample() + self.s.talon.dealRow(rows=self.s.rows[:self.gameinfo.ncards - 1], + frames=3) def isGameWon(self): if self.busy: @@ -352,7 +347,7 @@ class Matrix3(Game): for r in s[:l]: if not r.cards or not r.cards[0].rank == r.id: return 0 - self.s.talon.dealRow(rows=s[l:], flip=1, frames=3) + self.s.talon.dealRow(rows=s[l:], frames=3) return 1 def shallHighlightMatch(self, stack1, card1, stack2, card2): diff --git a/pysollib/main.py b/pysollib/main.py index ccb8b807..e69f5dc6 100644 --- a/pysollib/main.py +++ b/pysollib/main.py @@ -88,7 +88,7 @@ def parse_option(argv): "fg=", "foreground=", "bg=", "background=", "fn=", "font=", - "tile-theme=", + "theme=", "french-only", "noplugins", "nosound", @@ -105,7 +105,7 @@ def parse_option(argv): "fg": None, "bg": None, "fn": None, - "tile-theme": None, + "theme": None, "french-only": False, "noplugins": False, "nosound": False, @@ -125,8 +125,8 @@ def parse_option(argv): opts["bg"] = i[1] elif i[0] in ("--fn", "--font"): opts["fn"] = i[1] - elif i[0] == "--tile-theme": - opts["tile-theme"] = i[1] + elif i[0] == "--theme": + opts["theme"] = i[1] elif i[0] == "--french-only": opts["french-only"] = True elif i[0] == "--noplugins": @@ -194,13 +194,6 @@ def pysol_init(app, args): return 1 sys.exit(1) opts, filename = opts - wm_command = "" - prog = sys.executable - if prog and os.path.isfile(prog): - argv0 = os.path.normpath(args[0]) - prog = os.path.abspath(prog) - if os.path.isfile(argv0): - wm_command = prog + " " + os.path.abspath(argv0) if filename: app.commandline.loadgame = filename app.commandline.game = opts['game'] @@ -281,104 +274,7 @@ def pysol_init(app, args): app.opt.sound_mode = 0 # init toolkit 2) - sw, sh, sd = top.winfo_screenwidth(), top.winfo_screenheight(), top.winfo_screendepth() - top.wm_group(top) - top.wm_title(PACKAGE + " " + VERSION) - top.wm_iconname(PACKAGE + " " + VERSION) - if sw < 640 or sh < 480: - top.wm_minsize(400, 300) - else: - top.wm_minsize(520, 360) - ##self.top.wm_maxsize(9999, 9999) # unlimited - top.wm_protocol("WM_DELETE_WINDOW", top.wmDeleteWindow) - if wm_command: - top.wm_command(wm_command) - if 1: - # set expected window size to assist the layout of the window manager - top.config(width=min(800,sw-64), height=min(600,sh-64)) - try: - wm_set_icon(top, app.dataloader.findIcon()) - except: pass - - # set global color scheme - if not opts["fg"] and not opts["bg"]: - if os.name == "posix": # Unix/X11 - pass - if os.name == "mac": - color, priority = "#d9d9d9", "60" - classes = ( - "Button", "Canvas", "Checkbutton", "Entry", - "Frame", "Label", "Listbox", "Menubutton", ### "Menu", - "Message", "Radiobutton", "Scale", "Scrollbar", "Text", - ) - for c in classes: - top.option_add("*" + c + "*background", color, priority) - top.option_add("*" + c + "*activeBackground", color, priority) - else: - bg, fg = opts["bg"], opts["fg"] - if bg: - top.tk_setPalette(bg) - app.top_palette[1] = bg - app.top_bg = bg - if fg: - top.option_add("*foreground", fg) - app.top_palette[0] = fg - - # - if os.name == "posix": # Unix/X11 - top.option_add('*Entry.background', 'white', 60) - top.option_add('*Entry.foreground', 'black', 60) - top.option_add('*Listbox.background', 'white', 60) - top.option_add('*Listbox.foreground', 'black', 60) - ##top.option_add('*borderWidth', '1', 50) - ##top.option_add('*Button.borderWidth', '1', 50) - top.option_add('*Scrollbar.elementBorderWidth', '1', 60) - top.option_add('*Scrollbar.borderWidth', '1', 60) - top.option_add('*Menu.borderWidth', '1', 60) - #top.option_add('*Button.HighlightBackground', '#595d59') - #top.option_add('*Button.HighlightThickness', '1') - - # font - if opts["fn"]: - font = opts["fn"] - top.option_add("*font", font) - elif os.name == 'posix': - top.option_add("*font", "Helvetica 12", 50) - font = top.option_get('font', '') - else: - font = None - if TOOLKIT == 'tk': - from tkFont import Font - try: - f = Font(top, font) - except: - print >> sys.stderr, "invalid font name:", font - pass - else: - if font: - fa = f.actual() - app.opt.fonts["default"] = (fa["family"], - fa["size"], - fa["slant"], - fa["weight"]) - else: - app.opt.fonts["default"] = None - - if USE_TILE: # for tile - from pysoltk import load_theme - import settings - if opts['tile-theme']: - settings.TILE_THEME = opts['tile-theme'] - try: - load_theme(app, top, settings.TILE_THEME) - except Exception, err: - print >> sys.stderr, 'ERROR: set theme:', err - ##top.option_add('*Toolbar.relief', 'groove') - ##top.option_add('*Toolbar.relief', 'raised') - ##top.option_add('*Toolbar.borderWidth', 1) - ##top.option_add('*Toolbar.Button.Pad', 2) - ##top.option_add('*Toolbar.Button.default', 'disabled') - ##top.option_add('*Toolbar*takeFocus', 0) + top.initToolkit(app, opts['fg'], opts['bg'], opts['fn'], opts['theme']) # check games if len(app.gdb.getGamesIdSortedByName()) == 0: diff --git a/pysollib/pysolgtk/tkwrap.py b/pysollib/pysolgtk/tkwrap.py index 3b8ef444..e5e0ab3e 100644 --- a/pysollib/pysolgtk/tkwrap.py +++ b/pysollib/pysolgtk/tkwrap.py @@ -38,6 +38,7 @@ from gtk import gdk # PySol imports ## from pysollib.images import Images +from pysollib.settings import PACKAGE, VERSION # Toolkit imports from tkutil import makeToplevel, loadImage @@ -164,8 +165,6 @@ class _MfxToplevel(gtk.Window): ##w, h = newGeometry ##self.resize(w, h) - - def wm_group(self, pathName=None): # FIXME pass @@ -238,6 +237,17 @@ class MfxRoot(_MfxToplevel): def connectApp(self, app): self.app = app + def initToolkit(self, app, fg=None, bg=None, font=None, theme=None): + sw, sh, sd = self.winfo_screenwidth(), self.winfo_screenheight(), self.winfo_screendepth() + ##self.wm_group(self) + self.wm_title(PACKAGE + ' ' + VERSION) + ##self.wm_iconname(PACKAGE + ' ' + VERSION) + if sw < 640 or sh < 480: + self.wm_minsize(400, 300) + else: + self.wm_minsize(520, 360) + ##self.self.wm_maxsize(9999, 9999) # unlimited + # sometimes an update() is needed under Windows, whereas # under Unix an update_idletask() would be enough... def busyUpdate(self): diff --git a/pysollib/tile/tkwrap.py b/pysollib/tile/tkwrap.py index 1ffdfc19..b4668d21 100644 --- a/pysollib/tile/tkwrap.py +++ b/pysollib/tile/tkwrap.py @@ -43,10 +43,12 @@ __all__ = ['TclError', import os, sys, time, types from Tkinter import TclError import Tile as Tkinter +from tkFont import Font # PySol imports from pysollib.mfxutil import destruct, Struct -from tkutil import after_idle +from pysollib.settings import PACKAGE, VERSION +from tkutil import after_idle, load_theme, wm_set_icon from tkconst import EVENT_HANDLED, EVENT_PROPAGATE # /*********************************************************************** @@ -85,12 +87,6 @@ StringVar = Tkinter.StringVar class MfxRoot(Tkinter.Tk): def __init__(self, **kw): apply(Tkinter.Tk.__init__, (self,), kw) -## self.tk.call("package", "require", "tile") -## from pysollib.settings import TILE_THEME -## if TILE_THEME: -## ##self.tk.call('style', 'theme', 'use', TILE_THEME) -## style = Tkinter.Style(self) -## style.theme_use(TILE_THEME) self.app = None # for interruptible sleep #self.sleep_var = Tkinter.IntVar(self) @@ -102,6 +98,70 @@ class MfxRoot(Tkinter.Tk): def connectApp(self, app): self.app = app + def initToolkit(self, app, fg=None, bg=None, font=None, theme=None): + sw, sh, sd = self.winfo_screenwidth(), self.winfo_screenheight(), self.winfo_screendepth() + self.wm_group(self) + self.wm_title(PACKAGE + ' ' + VERSION) + self.wm_iconname(PACKAGE + ' ' + VERSION) + if sw < 640 or sh < 480: + self.wm_minsize(400, 300) + else: + self.wm_minsize(520, 360) + ##self.self.wm_maxsize(9999, 9999) # unlimited + self.wm_protocol('WM_DELETE_WINDOW', self.wmDeleteWindow) + prog = sys.executable + if prog and os.path.isfile(prog): + argv0 = os.path.normpath(sys.argv[0]) + prog = os.path.abspath(prog) + if os.path.isfile(argv0): + wm_command = prog + " " + os.path.abspath(argv0) + self.wm_command(wm_command) + if 1: + # set expected window size to assist the layout of the window manager + self.config(width=min(800,sw-64), height=min(600,sh-64)) + try: + wm_set_icon(self, app.dataloader.findIcon()) + except: pass + + # font + if font: + self.option_add('*font', font) + elif os.name == 'posix': + self.option_add('*font', 'Helvetica 12', 50) + font = self.option_get('font', '') + try: + f = Font(self, font) + except: + print >> sys.stderr, 'invalid font name:', font + pass + else: + if font: + fa = f.actual() + app.opt.fonts['default'] = (fa['family'], + fa['size'], + fa['slant'], + fa['weight']) + else: + app.opt.fonts['default'] = None + + # theme + import pysollib.settings + if theme: + pysollib.settings.TILE_THEME = theme + try: + load_theme(app, self, pysollib.settings.TILE_THEME) + except Exception, err: + print >> sys.stderr, 'ERROR: set theme:', err + ##self.option_add('*Toolbar.relief', 'groove') + ##self.option_add('*Toolbar.relief', 'raised') + ##self.option_add('*Toolbar.borderWidth', 1) + ##self.option_add('*Toolbar.Button.Pad', 2) + ##self.option_add('*Toolbar.Button.default', 'disabled') + ##self.option_add('*Toolbar*takeFocus', 0) + + + + # sometimes an update() is needed under Windows, whereas # under Unix an update_idletasks() would be enough... def busyUpdate(self): diff --git a/pysollib/tk/tkwrap.py b/pysollib/tk/tkwrap.py index 74b26631..10b7c4ef 100644 --- a/pysollib/tk/tkwrap.py +++ b/pysollib/tk/tkwrap.py @@ -43,10 +43,12 @@ __all__ = ['TclError', import os, sys, time, types import Tkinter from Tkinter import TclError +from tkFont import Font # PySol imports from pysollib.mfxutil import destruct, Struct -from tkutil import after_idle +from pysollib.settings import PACKAGE, VERSION +from tkutil import after_idle, wm_set_icon from tkconst import EVENT_HANDLED, EVENT_PROPAGATE # /*********************************************************************** @@ -96,6 +98,90 @@ class MfxRoot(Tkinter.Tk): def connectApp(self, app): self.app = app + def initToolkit(self, app, fg=None, bg=None, font=None, theme=None): + sw, sh, sd = self.winfo_screenwidth(), self.winfo_screenheight(), self.winfo_screendepth() + self.wm_group(self) + self.wm_title(PACKAGE + ' ' + VERSION) + self.wm_iconname(PACKAGE + ' ' + VERSION) + if sw < 640 or sh < 480: + self.wm_minsize(400, 300) + else: + self.wm_minsize(520, 360) + ##self.self.wm_maxsize(9999, 9999) # unlimited + self.wm_protocol('WM_DELETE_WINDOW', self.wmDeleteWindow) + prog = sys.executable + if prog and os.path.isfile(prog): + argv0 = os.path.normpath(sys.argv[0]) + prog = os.path.abspath(prog) + if os.path.isfile(argv0): + wm_command = prog + " " + os.path.abspath(argv0) + self.wm_command(wm_command) + if 1: + # set expected window size to assist the layout of the window manager + self.config(width=min(800,sw-64), height=min(600,sh-64)) + try: + wm_set_icon(self, app.dataloader.findIcon()) + except: pass + + # set global color scheme + if not fg and not bg: + if os.name == 'posix': # Unix/X11 + pass + if os.name == 'mac': + color, priority = '#d9d9d9', '60' + classes = ( + 'Button', 'Canvas', 'Checkbutton', 'Entry', + 'Frame', 'Label', 'Listbox', 'Menubutton', ### 'Menu', + 'Message', 'Radiobutton', 'Scale', 'Scrollbar', 'Text', + ) + for c in classes: + self.option_add('*' + c + '*background', color, priority) + self.option_add('*' + c + '*activeBackground', color, priority) + else: + if bg: + self.tk_setPalette(bg) + app.top_palette[1] = bg + app.top_bg = bg + if fg: + self.option_add('*foreground', fg) + app.top_palette[0] = fg + + # + if os.name == 'posix': # Unix/X11 + self.option_add('*Entry.background', 'white', 60) + self.option_add('*Entry.foreground', 'black', 60) + self.option_add('*Listbox.background', 'white', 60) + self.option_add('*Listbox.foreground', 'black', 60) + ##self.option_add('*borderWidth', '1', 50) + ##self.option_add('*Button.borderWidth', '1', 50) + self.option_add('*Scrollbar.elementBorderWidth', '1', 60) + self.option_add('*Scrollbar.borderWidth', '1', 60) + self.option_add('*Menu.borderWidth', '1', 60) + #self.option_add('*Button.HighlightBackground', '#595d59') + #self.option_add('*Button.HighlightThickness', '1') + + # font + if font: + self.option_add('*font', font) + elif os.name == 'posix': + self.option_add('*font', 'Helvetica 12', 50) + font = self.option_get('font', '') + try: + f = Font(self, font) + except: + print >> sys.stderr, 'invalid font name:', font + pass + else: + if font: + fa = f.actual() + app.opt.fonts['default'] = (fa['family'], + fa['size'], + fa['slant'], + fa['weight']) + else: + app.opt.fonts['default'] = None + + # sometimes an update() is needed under Windows, whereas # under Unix an update_idletasks() would be enough... def busyUpdate(self):