diff options
author | Walter Bender <walter@walter-laptop.(none)> | 2009-10-04 16:38:16 (GMT) |
---|---|---|
committer | Walter Bender <walter@walter-laptop.(none)> | 2009-10-04 16:38:16 (GMT) |
commit | 141e6e82047a2f9a1a4b449fadaf69ce1b1424a4 (patch) | |
tree | c21704eb3c8c1395e5bde0e45a7846d0cfbbc4c5 | |
parent | 1674b5be6c03d570ead998329569d206dfdcc4be (diff) |
refactoring for cmdline version
-rw-r--r-- | CardSortActivity.py | 323 | ||||
-rw-r--r-- | NEWS | 8 | ||||
-rw-r--r-- | card.py | 78 | ||||
-rwxr-xr-x | cardsort.py | 76 | ||||
-rw-r--r-- | grid.py | 96 | ||||
-rw-r--r-- | images/card0.svg | 21 | ||||
-rw-r--r-- | images/card1.svg | 21 | ||||
-rw-r--r-- | images/card2.svg | 16 | ||||
-rw-r--r-- | images/card3.svg | 21 | ||||
-rw-r--r-- | images/card4.svg | 21 | ||||
-rw-r--r-- | images/card5.svg | 21 | ||||
-rw-r--r-- | images/card6.svg | 21 | ||||
-rw-r--r-- | images/card7.svg | 21 | ||||
-rw-r--r-- | images/card8.svg | 21 | ||||
-rw-r--r-- | sprites.py | 8 | ||||
-rw-r--r-- | window.py | 155 |
16 files changed, 492 insertions, 436 deletions
diff --git a/CardSortActivity.py b/CardSortActivity.py index 0522e82..29d85e9 100644 --- a/CardSortActivity.py +++ b/CardSortActivity.py @@ -23,137 +23,37 @@ pygtk.require('2.0') import gtk import gobject -import sugar -from sugar.activity import activity -from sugar.bundle.activitybundle import ActivityBundle -from sugar.activity.widgets import ActivityToolbarButton -from sugar.activity.widgets import StopButton -from sugar.graphics.toolbarbox import ToolbarBox -from sugar.graphics.toolbarbox import ToolbarButton -from sugar.graphics.toolbutton import ToolButton -from sugar.graphics.menuitem import MenuItem -from sugar.graphics.icon import Icon -from sugar.graphics import style -from sugar.datastore import datastore +try: + import sugar + + from sugar.activity import activity + from sugar.bundle.activitybundle import ActivityBundle + from sugar.activity.widgets import ActivityToolbarButton + from sugar.activity.widgets import StopButton + from sugar.graphics.toolbarbox import ToolbarBox + from sugar.graphics.toolbarbox import ToolbarButton + from sugar.graphics.toolbutton import ToolButton + from sugar.graphics.menuitem import MenuItem + from sugar.graphics.icon import Icon + from sugar.datastore import datastore + + from sugar import profile +except: + class activity: + Activity = None + -from sugar import profile from gettext import gettext as _ import locale import os.path -from sprites import * -from math import sqrt -class taWindow: pass +from sprites import * +import window SERVICE = 'org.sugarlabs.CardSortActivity' IFACE = SERVICE PATH = '/org/augarlabs/CardSortActivity' -CARD_DIM = 135 -CARD_DEFS = ((1,3,-2,-3),(2,3,-3,-2),(2,3,-4,-4),\ - (2,1,-1,-4),(3,4,-4,-3),(4,2,-1,-2),\ - (1,1,-2,-4),(4,2,-3,-4),(1,3,-1,-2)) - - -# -# class for defining 3x3 matrix of cards -# -class Grid: - # 123 - # 456 - # 789 - def __init__(self,tw): - self.grid = [1,2,3,4,5,6,7,8,9] - self.card_table = {} - # Initialize the cards - i = 0 - x = (tw.width-(CARD_DIM*3))/2 - y = (tw.height-(CARD_DIM*3))/2 - for c in CARD_DEFS: - self.card_table[i] = Card(tw,c,i,x,y) - self.card_table[i].draw_card() - x += CARD_DIM - if x > (tw.width+(CARD_DIM*2))/2: - x = (tw.width-(CARD_DIM*3))/2 - y += CARD_DIM - i += 1 - - def swap(self,a,b): - # swap grid elements and x,y positions of sprites - print "swapping cards " + str(a) + " and " + str(b) - tmp = self.grid[a] - x = self.card_table[a].spr.x - y = self.card_table[a].spr.y - self.grid[a] = self.grid[b] - self.card_table[a].spr.x = self.card_table[b].spr.x - self.card_table[a].spr.y = self.card_table[b].spr.y - self.grid[b] = tmp - self.card_table[b].spr.x = x - self.card_table[b].spr.y = y - - def print_grid(self): - print self.grid - return - - def test(self): - for i in (0,1,3,4,6,7): - if self.card_table[self.grid[i]].east + \ - self.card_table[self.grid[i+1]].west != 0: - return False - for i in (0,1,2,3,4,5): - if self.card_table[self.grid[i]].south + \ - self.card_table[self.grid[i+3]].north != 0: - return False - return True - - -# -# class for defining individual cards -# -class Card: - # Spade = 1,-1 - # Heart = 2,-2 - # Club = 3,-3 - # Diamond = 4,-4 - def __init__(self,tw,c,i,x,y): - self.north = c[0] - self.east = c[1] - self.south = c[2] - self.west = c[3] - self.rotate = 0 - # create sprite from svg file - self.spr = sprNew(tw,x,y,self.load_image(tw.path,i)) - self.spr.label = i - - def draw_card(self): - setlayer(self.spr,2000) - draw(self.spr) - - def load_image(self, file, i): - print "loading " + os.path.join(file + str(i) + '.svg') - return gtk.gdk.pixbuf_new_from_file(os.path.join(file + \ - str(i) + "x" + \ - '.svg')) - - def rotate_ccw(self): - # print "rotating card " + str(self.spr.label) - tmp = self.north - self.north = self.east - self.east = self.south - self.south = self.west - self.west = tmp - self.rotate += 90 - if self.rotate > 359: - self.rotate -= 360 - tmp = self.spr.image.rotate_simple(90) - self.spr.image = tmp - - def print_card(self): - print "(" + str(self.north) + "," + str(self.east) + \ - "," + str(self.south) + "," + str(self.west) + \ - ") " + str(self.rotate) + "ccw" + \ - " x:" + str(self.spr.x) + " y:" + str(self.spr.y) - # # Sugar activity # @@ -162,157 +62,86 @@ class CardSortActivity(activity.Activity): def __init__(self, handle): super(CardSortActivity,self).__init__(handle) - # Use 0.86 toolbar design - toolbar_box = ToolbarBox() - - # Buttons added to the Activity toolbar - activity_button = ActivityToolbarButton(self) - toolbar_box.toolbar.insert(activity_button, 0) - activity_button.show() - - # Solver button - self.solve_puzzle = ToolButton( "solve-off" ) - self.solve_puzzle.set_tooltip(_('Solve it')) - self.solve_puzzle.props.sensitive = True - self.solve_puzzle.connect('clicked', self._solver_cb) - toolbar_box.toolbar.insert(self.solve_puzzle, -1) - self.solve_puzzle.show() - - separator = gtk.SeparatorToolItem() - separator.show() - toolbar_box.toolbar.insert(separator, -1) + try: + # Use 0.86 toolbar design + toolbar_box = ToolbarBox() + + # Buttons added to the Activity toolbar + activity_button = ActivityToolbarButton(self) + toolbar_box.toolbar.insert(activity_button, 0) + activity_button.show() + + # Solver button + self.solve_puzzle = ToolButton( "solve-off" ) + self.solve_puzzle.set_tooltip(_('Solve it')) + self.solve_puzzle.props.sensitive = True + self.solve_puzzle.connect('clicked', self._solver_cb) + toolbar_box.toolbar.insert(self.solve_puzzle, -1) + self.solve_puzzle.show() + + separator = gtk.SeparatorToolItem() + separator.show() + toolbar_box.toolbar.insert(separator, -1) + + # Label for showing status + self.results_label = gtk.Label(_("click to rotate; drag to swap")) + self.results_label.show() + results_toolitem = gtk.ToolItem() + results_toolitem.add(self.results_label) + toolbar_box.toolbar.insert(results_toolitem,-1) - # Label for showing status - self.results_label = gtk.Label(_("click to rotate; drag to swap")) - self.results_label.show() - results_toolitem = gtk.ToolItem() - results_toolitem.add(self.results_label) - toolbar_box.toolbar.insert(results_toolitem,-1) + separator = gtk.SeparatorToolItem() + separator.props.draw = False + separator.set_expand(True) + separator.show() + toolbar_box.toolbar.insert(separator, -1) - separator = gtk.SeparatorToolItem() - separator.props.draw = False - separator.set_expand(True) - separator.show() - toolbar_box.toolbar.insert(separator, -1) + # The ever-present Stop Button + stop_button = StopButton(self) + stop_button.props.accelerator = '<Ctrl>Q' + toolbar_box.toolbar.insert(stop_button, -1) + stop_button.show() - # The ever-present Stop Button - stop_button = StopButton(self) - stop_button.props.accelerator = '<Ctrl>Q' - toolbar_box.toolbar.insert(stop_button, -1) - stop_button.show() + self.set_toolbar_box(toolbar_box) + toolbar_box.show() - self.set_toolbar_box(toolbar_box) - toolbar_box.show() + except: + pass # Create a canvas canvas = gtk.DrawingArea() canvas.set_size_request(gtk.gdk.screen_width(), \ gtk.gdk.screen_height()) self.set_canvas(canvas) + canvas.show() self.show_all() # Initialize the canvas - self.tw = taWindow() - self.tw.window = canvas - canvas.set_flags(gtk.CAN_FOCUS) - canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK) - canvas.add_events(gtk.gdk.BUTTON_RELEASE_MASK) - canvas.connect("expose-event", self._expose_cb, self.tw) - canvas.connect("button-press-event", self._button_press_cb, self.tw) - canvas.connect("button-release-event", self._button_release_cb, self.tw) - self.tw.width = gtk.gdk.screen_width() - self.tw.height = gtk.gdk.screen_height()-style.GRID_CELL_SIZE - self.tw.area = canvas.window - self.tw.gc = self.tw.area.new_gc() - self.tw.scale = 3 - self.tw.cm = self.tw.gc.get_colormap() - self.tw.msgcolor = self.tw.cm.alloc_color('black') - self.tw.sprites = [] - - # Initialize the grid - self.tw.path = os.path.join(activity.get_bundle_path(),'images/card') - self.tw.grid = Grid(self.tw) + self.tw = window.new_window(canvas, \ + os.path.join(activity.get_bundle_path(), \ + 'images/card'), \ + self) - # Start solving the puzzle - self.tw.press = -1 - self.tw.release = -1 - self.tw.start_drag = [0,0] # # Solver # def _solver_cb(self, button): self.solve_puzzle.set_icon("solve-on") + """ We need to write this code """ - self.results_label.set_text(_("I don't know how to solve it.")) - self.results_label.show() - self.solve_puzzle.set_icon("solve-off") - return True - - # - # Repaint - # - def _expose_cb(self, win, event, tw): - redrawsprites(tw) - return True - # - # Button press - # - def _button_press_cb(self, win, event, tw): - win.grab_focus() - x, y = map(int, event.get_coords()) - tw.start_drag = [x,y] - spr = findsprite(tw,(x,y)) - if spr is None: - tw.press = -1 - tw.release = -1 - return True - # take note of card under button press - tw.press = spr.label - return True - - # - # Button release - # - def _button_release_cb(self, win, event, tw): - win.grab_focus() - x, y = map(int, event.get_coords()) - spr = findsprite(tw,(x,y)) - if spr is None: - tw.press = -1 - tw.release = -1 - return True - # take note of card under button release - tw.release = spr.label - # if the same card (click) then rotate - if tw.press == tw.release: - # check to see if it was an aborted move - if self.distance(tw.start_drag,[x,y]) < 20: - tw.grid.card_table[tw.press].rotate_ccw() - # tw.grid.card_table[tw.press].print_card() - # if different card (drag) then swap - else: - tw.grid.swap(tw.press,tw.release) - # tw.grid.print_grid() - inval(tw.grid.card_table[tw.press].spr) - inval(tw.grid.card_table[tw.release].spr) - redrawsprites(tw) - tw.press = -1 - tw.release = -1 - if tw.grid.test() == True: - self.results_label.set_text(_("You solved the puzzle.")) - self.results_label.show() - else: - self.results_label.set_text(_("Keep trying.")) - self.results_label.show() + """ + instead, swap in/out blank tile + """ + self.tw.grid.toggle_blank() + self.results_label.set_text(_("toggling in/out blank tile")) + redrawsprites(self.tw) + self.results_label.show() + self.solve_puzzle.set_icon("solve-off") return True - def distance(self,start,stop): - dx = start[0]-stop[0] - dy = start[1]-stop[1] - return sqrt(dx*dx+dy*dy) @@ -1,3 +1,9 @@ +2 + +* scaling to different size displays +* fixed index bug in test +* added blank card for "easy" mode + 1 -Card Sorting puzzle project +* Card Sorting puzzle project @@ -0,0 +1,78 @@ +#Copyright (c) 2009, Walter Bender + +#Permission is hereby granted, free of charge, to any person obtaining a copy +#of this software and associated documentation files (the "Software"), to deal +#in the Software without restriction, including without limitation the rights +#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +#copies of the Software, and to permit persons to whom the Software is +#furnished to do so, subject to the following conditions: + +#The above copyright notice and this permission notice shall be included in +#all copies or substantial portions of the Software. + +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +#THE SOFTWARE. + +import pygtk +pygtk.require('2.0') +import gtk +import gobject +import os.path + +from sprites import * + +# +# class for defining individual cards +# +class Card: + # Spade = 1,-1 + # Heart = 2,-2 + # Club = 3,-3 + # Diamond = 4,-4 + def __init__(self,tw,c,i,x,y): + self.north = c[0] + self.east = c[1] + self.south = c[2] + self.west = c[3] + self.rotate = 0 + # create sprite from svg file + self.spr = sprNew(tw, x, y,\ + self.load_image(tw.path,i,tw.card_dim*tw.scale)) + self.spr.label = i + + def draw_card(self): + setlayer(self.spr,2000) + draw(self.spr) + + def load_image(self, file, i, wh): + # print "loading " + os.path.join(file + str(i) + '.svg') + \ + # " scale: " + str(wh) + return gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(file + \ + str(i) + "x" + \ + '.svg'), \ + int(wh), int(wh)) + + def rotate_ccw(self): + # print "rotating card " + str(self.spr.label) + tmp = self.north + self.north = self.east + self.east = self.south + self.south = self.west + self.west = tmp + self.rotate += 90 + if self.rotate > 359: + self.rotate -= 360 + tmp = self.spr.image.rotate_simple(90) + self.spr.image = tmp + + def print_card(self): + print "(" + str(self.north) + "," + str(self.east) + \ + "," + str(self.south) + "," + str(self.west) + \ + ") " + str(self.rotate) + "ccw" + \ + " x:" + str(self.spr.x) + " y:" + str(self.spr.y) + diff --git a/cardsort.py b/cardsort.py new file mode 100755 index 0000000..d93fcc6 --- /dev/null +++ b/cardsort.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python + +import pygtk +pygtk.require('2.0') +import gtk +import os.path + +import window + +class HelloWorld: + + # This is a callback function. The data arguments are ignored + # in this example. More on callbacks below. + def hello(self, widget, data=None): + print "Hello World" + + def delete_event(self, widget, event, data=None): + print "delete event occurred" + return False + + # Another callback + def destroy(self, widget, data=None): + gtk.main_quit() + + def __init__(self): + # create a new window + self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) + self.window.connect("delete_event", self.delete_event) + self.window.connect("destroy", self.destroy) + + # Sets the border width of the window. + self.window.set_border_width(10) + + # Creates a new button with the label "Hello World". + self.button = gtk.Button("Hello World") + + # When the button receives the "clicked" signal, it will call the + # function hello() passing it None as its argument. The hello() + # function is defined above. + self.button.connect("clicked", self.hello, None) + + # This will cause the window to be destroyed by calling + # gtk_widget_destroy(window) when "clicked". Again, the destroy + # signal could come from here, or the window manager. + self.button.connect_object("clicked", gtk.Widget.destroy, self.window) + + # This packs the button into the window (a GTK container). + self.window.add(self.button) + + # The final step is to display this newly created widget. + self.button.show() + + # and the window + self.window.show() + + def main(self): + # All PyGTK applications must have a gtk.main(). Control ends here + # and waits for an event to occur (like a key press or mouse event). + win = gtk.Window(gtk.WINDOW_TOPLEVEL) + # win = gtk.Window() + win.set_has_frame(True) + win.set_decorated(True) + tw = window.new_window(win, \ + os.path.join(os.path.abspath('.'), \ + 'images/card')) + tw.width = gtk.gdk.screen_width() + tw.height = gtk.gdk.screen_height() + win.connect("destroy", lambda w: gtk.main_quit()) + gtk.main() + +# If the program is run directly or passed as an argument to the python +# interpreter then create a HelloWorld instance and show it +if __name__ == "__main__": + hello = HelloWorld() + hello.main() + @@ -0,0 +1,96 @@ +#Copyright (c) 2009, Walter Bender + +#Permission is hereby granted, free of charge, to any person obtaining a copy +#of this software and associated documentation files (the "Software"), to deal +#in the Software without restriction, including without limitation the rights +#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +#copies of the Software, and to permit persons to whom the Software is +#furnished to do so, subject to the following conditions: + +#The above copyright notice and this permission notice shall be included in +#all copies or substantial portions of the Software. + +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +#THE SOFTWARE. + +import pygtk +pygtk.require('2.0') +import gtk +import gobject + +from sprites import * +from card import * + +CARD_DEFS = ((1,3,-2,-3),(2,3,-3,-2),(2,3,-4,-4),\ + (2,1,-1,-4),(3,4,-4,-3),(4,2,-1,-2),\ + (1,1,-2,-4),(4,2,-3,-4),(1,3,-1,-2),\ + (0,0,0,0)) + + +# +# class for defining 3x3 matrix of cards +# +class Grid: + # 123 + # 456 + # 789 + def __init__(self,tw): + self.grid = [0,1,2,3,4,5,6,7,8,9] + self.card_table = {} + # Initialize the cards + i = 0 + x = int((tw.width-(tw.card_dim*3*tw.scale))/2) + y = int((tw.height-(tw.card_dim*3*tw.scale))/2) + for c in CARD_DEFS: + self.card_table[i] = Card(tw,c,i,x,y) + self.card_table[i].draw_card() + x += int(tw.card_dim*tw.scale) + if x > (tw.width+(tw.card_dim*2*tw.scale))/2: + x = int((tw.width-(tw.card_dim*3*tw.scale))/2) + y += int(tw.card_dim*tw.scale) + i += 1 + if i == 9: # put the extra (blank) card off the screen + y = tw.height + + def toggle_blank(self): + self.swap(6,9) + + def swap(self,a,b): + # swap grid elements and x,y positions of sprites + # print "swapping cards " + str(a) + " and " + str(b) + ai = self.grid.index(a) + bi = self.grid.index(b) + self.grid[bi] = a + self.grid[ai] = b + x = self.card_table[a].spr.x + y = self.card_table[a].spr.y + self.card_table[a].spr.x = self.card_table[b].spr.x + self.card_table[a].spr.y = self.card_table[b].spr.y + self.card_table[b].spr.x = x + self.card_table[b].spr.y = y + + def print_grid(self): + print self.grid + return + + def test(self): + # self.print_grid() + for i in (0,1,3,4,6,7): + if self.card_table[self.grid[i]].east != 0 and \ + self.card_table[self.grid[i+1]].west != 0 and \ + self.card_table[self.grid[i]].east + \ + self.card_table[self.grid[i+1]].west != 0: + return False + for i in (0,1,2,3,4,5): + if self.card_table[self.grid[i]].south != 0 and \ + self.card_table[self.grid[i+3]].north != 0 and \ + self.card_table[self.grid[i]].south + \ + self.card_table[self.grid[i+3]].north != 0: + return False + return True + diff --git a/images/card0.svg b/images/card0.svg deleted file mode 100644 index e3640a8..0000000 --- a/images/card0.svg +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135.00011" - height="135.00002" - id="svg5406"> - <defs - id="defs5408" /> - <g - transform="translate(-263.64643,-426.24546)" - id="layer1"> - <path - d="M 50,-0.90625 C 50.024832,-0.88981002 34.87376,12.57703 35,19.09375 C 35.126229,25.610489 42.15031,24.51668 45,24.09375 L 45,34.09375 L 0.5,34.09375 L 0.5,79.59375 L 15.5,79.59375 C 15.5,79.59375 12.01332,69.589779 20.5,69.59375 C 28.98668,69.59772 25.5,79.593751 25.5,79.59375 C 25.5,79.59375 35.462309,76.196128 35.5,84.59375 C 35.53769,92.99137 25.5,89.593752 25.5,89.59375 C 25.5,89.59375 28.09181,99.743416 20.5,99.59375 C 12.9082,99.44408 15.5,89.593752 15.5,89.59375 L 0.5,89.59375 L 0.5,134.09375 L 45.09375,134.09375 L 45.09375,124.09375 C 45.09375,124.09375 36.45921,115.23238 35.25,111.09375 C 33.84065,106.27011 38.252031,99.202102 42.625,99.09375 C 46.99798,98.98543 49.999999,104.09375 50,104.09375 C 50,104.09375 52.811292,99.100122 57.40625,99.09375 C 62.00121,99.08738 66.055583,106.23141 64.78125,111.09375 C 63.67038,115.33239 54.9375,124.09375 54.9375,124.09375 L 54.9375,134.09375 L 100.5,134.09375 L 100.5,89.59375 L 115.5,89.59375 C 115.5,89.593752 112.9082,99.44408 120.5,99.59375 C 128.09181,99.743416 125.5,89.59375 125.5,89.59375 C 125.5,89.593752 135.53769,92.99137 135.5,84.59375 C 135.4623,76.196128 125.5,79.59375 125.5,79.59375 C 125.5,79.593751 128.98668,69.59772 120.5,69.59375 C 112.01332,69.589779 115.5,79.59375 115.5,79.59375 L 100.5,79.59375 L 100.5,34.09375 L 55,34.09375 L 55,24.09375 C 55.000002,24.093749 64.89639,26.32091 65,19.09375 C 65.103599,11.866599 49.97517,-0.92268 50,-0.90625 z" - transform="translate(263.14643,427.15173)" - id="path2392" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:0.99997878px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> - </g> -</svg> diff --git a/images/card1.svg b/images/card1.svg deleted file mode 100644 index 4aa754c..0000000 --- a/images/card1.svg +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135.00011" - height="135.00169" - id="svg2388"> - <defs - id="defs2390" /> - <g - transform="translate(-226.50357,-360.53)" - id="layer1"> - <path - d="M 42.625,1.71875 C 38.252031,1.8271001 33.84065,8.89511 35.25,13.71875 C 36.45921,17.85738 45.09375,26.71875 45.09375,26.71875 L 45.09375,36.71875 L 0.5,36.71875 L 0.5,82.3125 L 10.5,82.3125 C 10.5,82.3125 19.36137,73.677957 23.5,72.46875 C 28.32364,71.0594 35.391649,75.470778 35.5,79.84375 C 35.60832,84.21673 30.5,87.218754 30.5,87.21875 C 30.5,87.21875 35.493629,89.998789 35.5,94.59375 C 35.50637,99.18871 28.36234,103.24308 23.5,101.96875 C 19.26136,100.85788 10.5,92.124997 10.5,92.125 L 0.5,92.125 L 0.5,136.71875 L 45,136.71875 L 45,121.71875 C 45,121.71875 34.996031,125.20543 35,116.71875 C 35.00397,108.23207 44.999999,111.71875 45,111.71875 C 45,111.71875 41.60238,101.75644 50,101.71875 C 58.39762,101.68106 55.000001,111.71875 55,111.71875 C 55,111.71875 65.149673,109.12694 65,116.71875 C 64.85033,124.31055 55.000001,121.71875 55,121.71875 L 55,136.71875 L 100.5,136.71875 L 100.5,92.21875 L 115.5,92.21875 C 115.5,92.218748 112.9082,102.06908 120.5,102.21875 C 128.0918,102.36842 125.5,92.21875 125.5,92.21875 C 125.5,92.218748 135.5377,95.61637 135.5,87.21875 C 135.46231,78.821131 125.5,82.21875 125.5,82.21875 C 125.5,82.218747 128.9866,72.22272 120.5,72.21875 C 112.0133,72.214783 115.5,82.21875 115.5,82.21875 L 100.5,82.21875 L 100.5,36.71875 L 54.9375,36.71875 L 54.9375,26.71875 C 54.9375,26.71875 63.67038,17.95739 64.78125,13.71875 C 66.055583,8.8564104 62.00121,1.71238 57.40625,1.71875 C 52.811292,1.7251201 50,6.71875 50,6.71875 C 49.999999,6.7187502 46.99798,1.61043 42.625,1.71875 z" - transform="translate(226.00357,358.81295)" - id="path2394" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:0.991781px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> - </g> -</svg> diff --git a/images/card2.svg b/images/card2.svg deleted file mode 100644 index f1489d0..0000000 --- a/images/card2.svg +++ /dev/null @@ -1,16 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135.00011" - height="135.00169" - id="svg3350"> - <defs - id="defs3352" /> - <path - d="M 42.125,0.0033886085 C 37.752031,0.11173722 33.34065,7.1796584 34.75,12.003238 C 35.95921,16.141816 44.59375,25.003075 44.59375,25.003075 L 44.59375,35.002949 L 0,35.002949 L 0,80.502378 L 10,80.502378 L 20,70.502504 L 35,85.502315 L 20,100.50212 L 10,90.502253 L 0,90.502253 L 0,135.00169 L 44.5,135.00169 L 44.5,125.00182 L 34.5,115.00194 L 49.5,100.00213 L 64.5,115.00194 L 54.5,125.00182 L 54.5,135.00169 L 100,135.00169 L 100,90.502253 L 115,90.502253 C 115,90.502251 112.4082,100.35246 120,100.50212 C 127.59181,100.65179 125,90.502253 125,90.502253 C 125,90.502251 135.03769,93.89983 135,85.502315 C 134.9623,77.104802 125,80.502378 125,80.502378 C 125,80.502382 128.48668,70.506474 120,70.502504 C 111.51332,70.498536 115,80.502378 115,80.502378 L 100,80.502378 L 100,35.002949 L 54.4375,35.002949 L 54.4375,25.003075 C 54.4375,25.003076 63.17038,16.241825 64.28125,12.003238 C 65.555579,7.1409589 61.50121,-0.0029813116 56.90625,0.0033886085 C 52.311288,0.0097584985 49.5,5.0033258 49.5,5.0033258 C 49.499999,5.003326 46.49798,-0.10493003 42.125,0.0033886085 z" - id="path2397" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> -</svg> diff --git a/images/card3.svg b/images/card3.svg deleted file mode 100644 index 9a86603..0000000 --- a/images/card3.svg +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135.00002" - height="135.00169" - id="svg3392"> - <defs - id="defs3394" /> - <g - transform="translate(-257.93214,-423.38165)" - id="layer1"> - <path - d="M 42.25,0.8125 C 37.877001,0.92079997 33.4656,7.9889 34.875,12.8125 C 36.084199,16.9511 44.6875,25.8125 44.6875,25.8125 L 44.6875,35.8125 L 0.5,35.8125 L 0.5,81.3125 L 10.5,81.3125 L 20.5,71.3125 L 35.5,86.3125 L 20.5,101.3125 L 10.5,91.3125 L 0.5,91.3125 L 0.5,135.8125 L 45,135.8125 L 45,125.8125 C 42.1503,126.2354 35.126199,127.3292 35,120.8125 C 34.8738,114.2957 50.024901,100.8289 50,100.8125 C 49.9752,100.796 65.103596,113.5853 65,120.8125 C 64.8964,128.0396 55,125.8125 55,125.8125 L 55,135.8125 L 100.5,135.8125 L 100.5,91.3125 L 110.5,91.3125 C 110.5,91.312503 108.2729,101.2089 115.5,101.3125 C 122.7272,101.4161 135.5165,86.2877 135.5,86.3125 C 135.48361,86.337397 122.0168,71.1863 115.5,71.3125 C 108.9833,71.438703 110.0771,78.4628 110.5,81.3125 L 100.5,81.3125 L 100.5,35.8125 L 54.53125,35.8125 L 54.53125,25.8125 C 54.531249,25.812501 63.2641,17.0511 64.375,12.8125 C 65.649304,7.9502 61.595,0.8061 57,0.8125 C 52.4051,0.81879997 49.625,5.8125 49.625,5.8125 C 49.625001,5.8124999 46.623,0.7041 42.25,0.8125 z" - transform="translate(257.43214,422.57085)" - id="path2387" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> - </g> -</svg> diff --git a/images/card4.svg b/images/card4.svg deleted file mode 100644 index 0d597ae..0000000 --- a/images/card4.svg +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135" - height="135.00011" - id="svg3200"> - <defs - id="defs3202" /> - <g - transform="translate(-29.360716,-778.80672)" - id="layer1"> - <path - d="M 50,62.6875 C 41.602378,62.725191 45,72.6875 45,72.6875 C 45.000001,72.687501 35.00397,69.20082 35,77.6875 C 34.996029,86.174183 45,82.6875 45,82.6875 L 45,97.6875 L 0.5,97.6875 L 0.5,143.1875 L 15.5,143.1875 C 15.5,143.1875 12.01332,133.18352 20.5,133.1875 C 28.98668,133.19147 25.500001,143.1875 25.5,143.1875 C 25.5,143.1875 35.462311,139.78989 35.5,148.1875 C 35.53769,156.58512 25.500001,153.1875 25.5,153.1875 C 25.5,153.1875 28.09181,163.33718 20.5,163.1875 C 12.9082,163.03783 15.5,153.1875 15.5,153.1875 L 0.5,153.1875 L 0.5,197.6875 L 45,197.6875 L 45,187.6875 L 35,177.6875 L 50,162.6875 L 65,177.6875 L 55,187.6875 L 55,197.6875 L 100.5,197.6875 L 100.5,153.1875 L 110.5,153.1875 L 120.5,163.1875 L 135.5,148.1875 L 120.5,133.1875 L 110.5,143.1875 L 100.5,143.1875 L 100.5,97.6875 L 55,97.6875 L 55,82.6875 C 54.999999,82.687503 64.85033,85.2793 65,77.6875 C 65.149671,70.095693 55,72.6875 55,72.6875 C 54.999999,72.687501 58.39762,62.64981 50,62.6875 z" - transform="translate(28.860716,716.11933)" - id="path2387" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> - </g> -</svg> diff --git a/images/card5.svg b/images/card5.svg deleted file mode 100644 index a632650..0000000 --- a/images/card5.svg +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135.00169" - height="135" - id="svg3209"> - <defs - id="defs3211" /> - <g - transform="translate(-295.075,-306.24719)" - id="layer1"> - <path - d="M 50,3.0625 L 35,18.0625 L 45,28.0625 L 45,38.0625 L 0.5,38.0625 L 0.5,83.65625 L 10.5,83.65625 C 10.5,83.65625 19.36137,75.021713 23.5,73.8125 C 28.32364,72.40315 35.391649,76.814534 35.5,81.1875 C 35.60832,85.56048 30.5,88.562501 30.5,88.5625 C 30.5,88.5625 35.493629,91.342537 35.5,95.9375 C 35.50637,100.53246 28.36234,104.58683 23.5,103.3125 C 19.26136,102.20163 10.5,93.468753 10.5,93.46875 L 0.5,93.46875 L 0.5,138.0625 L 45,138.0625 L 45,128.0625 C 42.15031,128.48543 35.126229,129.57924 35,123.0625 C 34.87376,116.54578 50.024832,103.07894 50,103.0625 C 49.97517,103.04607 65.103599,115.83535 65,123.0625 C 64.89639,130.28966 55.000002,128.0625 55,128.0625 L 55,138.0625 L 100.5,138.0625 L 100.5,93.46875 L 110.5,93.46875 C 110.5,93.468753 119.26136,102.20163 123.5,103.3125 C 128.36234,104.58683 135.50637,100.53246 135.5,95.9375 C 135.49363,91.342537 130.5,88.5625 130.5,88.5625 C 130.5,88.562501 135.60832,85.56048 135.5,81.1875 C 135.39165,76.814534 128.32364,72.40315 123.5,73.8125 C 119.36137,75.021713 110.5,83.65625 110.5,83.65625 L 100.5,83.65625 L 100.5,38.0625 L 55,38.0625 L 55,28.0625 L 65,18.0625 L 50,3.0625 z" - transform="translate(294.575,303.18469)" - id="path2387" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> - </g> -</svg> diff --git a/images/card6.svg b/images/card6.svg deleted file mode 100644 index 26888a3..0000000 --- a/images/card6.svg +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135.00002" - height="135.00002" - id="svg2397"> - <defs - id="defs2399" /> - <g - transform="translate(-259.92143,-373.34456)" - id="layer1"> - <path - d="M 50.5,0.6875 C 50.524832,0.70394001 35.37376,14.17078 35.5,20.6875 C 35.626229,27.204239 42.65031,26.11043 45.5,25.6875 L 45.5,35.6875 L 0.5,35.6875 L 0.5,80.6875 L 10.5,80.6875 L 20.5,70.6875 L 35.5,85.6875 L 20.5,100.6875 L 10.5,90.6875 L 0.5,90.6875 L 0.5,135.6875 L 45.59375,135.6875 L 45.59375,125.6875 C 45.59375,125.6875 36.959209,116.82613 35.75,112.6875 C 34.34065,107.86386 38.752029,100.79585 43.125,100.6875 C 47.49798,100.57918 50.500001,105.6875 50.5,105.6875 C 50.5,105.6875 53.31129,100.69387 57.90625,100.6875 C 62.50121,100.68113 66.555577,107.82516 65.28125,112.6875 C 64.17038,116.92614 55.437499,125.6875 55.4375,125.6875 L 55.4375,135.6875 L 100.5,135.6875 L 100.5,90.6875 L 110.5,90.6875 C 110.5,90.687503 108.27284,100.58389 115.5,100.6875 C 122.72715,100.7911 135.51643,85.66267 135.5,85.6875 C 135.48355,85.712329 122.01672,70.56126 115.5,70.6875 C 108.98326,70.813731 110.07707,77.83781 110.5,80.6875 L 100.5,80.6875 L 100.5,35.6875 L 55.5,35.6875 L 55.5,25.6875 C 55.500002,25.687499 65.39639,27.91466 65.5,20.6875 C 65.603599,13.460349 50.47517,0.67107 50.5,0.6875 z" - transform="translate(259.42143,372.65708)" - id="path2387" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> - </g> -</svg> diff --git a/images/card7.svg b/images/card7.svg deleted file mode 100644 index 815e4a2..0000000 --- a/images/card7.svg +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135.00169" - height="135" - id="svg4304"> - <defs - id="defs4306" /> - <g - transform="translate(-172.21786,-626.23612)" - id="layer1"> - <path - d="M 50,1.25 L 35,16.25 L 45,26.25 L 45,36.25 L 0.5,36.25 L 0.5,81.75 L 10.5,81.75 L 20.5,71.75 L 35.5,86.75 L 20.5,101.75 L 10.5,91.75 L 0.5,91.75 L 0.5,136.25 L 45,136.25 L 45,121.25 C 45,121.25 34.996031,124.7367 35,116.25 C 35.00397,107.7633 44.999999,111.25 45,111.25 C 45,111.25 41.60238,101.2877 50,101.25 C 58.39762,101.2123 55.000001,111.25 55,111.25 C 55,111.25 65.149673,108.6582 65,116.25 C 64.85033,123.8418 55.000001,121.25 55,121.25 L 55,136.25 L 100.5,136.25 L 100.5,91.6875 L 110.5,91.6875 C 110.5,91.6875 119.2613,100.38913 123.5,101.5 C 128.3623,102.77433 135.5064,98.71996 135.5,94.125 C 135.49359,89.530042 130.5,86.75 130.5,86.75 C 130.49999,86.749999 135.6083,83.74798 135.5,79.375 C 135.39159,75.002031 128.3236,70.59065 123.5,72 C 119.3613,73.20921 110.5,81.84375 110.5,81.84375 L 100.5,81.84375 L 100.5,36.25 L 55,36.25 L 55,26.25 L 65,16.25 L 50,1.25 z" - transform="translate(171.71786,624.98612)" - id="path2387" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> - </g> -</svg> diff --git a/images/card8.svg b/images/card8.svg deleted file mode 100644 index 6664656..0000000 --- a/images/card8.svg +++ /dev/null @@ -1,21 +0,0 @@ -<?xml version="1.0" encoding="UTF-8" standalone="no"?> -<!-- Created with Inkscape (http://www.inkscape.org/) --> -<svg - xmlns:svg="http://www.w3.org/2000/svg" - xmlns="http://www.w3.org/2000/svg" - version="1.0" - width="135.00011" - height="135.00002" - id="svg4855"> - <defs - id="defs4857" /> - <g - transform="translate(-206.50358,-294.80223)" - id="layer1"> - <path - d="M 50,1.78125 C 50.024832,1.79769 34.87376,15.26453 35,21.78125 C 35.126229,28.29799 42.15031,27.20418 45,26.78125 L 45,36.78125 L 0.5,36.78125 L 0.5,82.375 L 10.5,82.375 C 10.5,82.375 19.36137,73.77171 23.5,72.5625 C 28.32364,71.15315 35.391649,75.564531 35.5,79.9375 C 35.60832,84.31048 30.5,87.312499 30.5,87.3125 C 30.5,87.3125 35.493629,90.092542 35.5,94.6875 C 35.50637,99.28246 28.36234,103.33683 23.5,102.0625 C 19.26136,100.95163 10.5,92.21875 10.5,92.21875 L 0.5,92.21875 L 0.5,136.78125 L 48.75,136.78125 L 48.75,126.78125 C 45.90031,127.20418 38.87623,128.29799 38.75,121.78125 C 38.62376,115.26453 53.774828,101.79769 53.75,101.78125 C 53.72517,101.76482 68.853599,114.5541 68.75,121.78125 C 68.64639,129.00841 58.749998,126.78125 58.75,126.78125 L 58.75,136.78125 L 100.5,136.78125 L 100.5,92.28125 L 115.5,92.28125 C 115.5,92.281251 112.9082,102.13158 120.5,102.28125 C 128.09181,102.43092 125.5,92.28125 125.5,92.28125 C 125.5,92.281251 135.53769,95.67887 135.5,87.28125 C 135.4623,78.883626 125.5,82.28125 125.5,82.28125 C 125.5,82.281249 128.98668,72.28522 120.5,72.28125 C 112.01332,72.277278 115.5,82.28125 115.5,82.28125 L 100.5,82.28125 L 100.5,36.78125 L 55,36.78125 L 55,26.78125 C 55.000002,26.78125 64.89639,29.00841 65,21.78125 C 65.103599,14.5541 49.97517,1.76482 50,1.78125 z" - transform="translate(206.00358,293.021)" - id="path2392" - style="fill:#ff0000;fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:0.99997878px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" /> - </g> -</svg> @@ -133,9 +133,9 @@ def draw_label(spr, label, myscale, center_flag, truncate_flag): mylabel = label.replace("\0"," ") l = len(mylabel) if truncate_flag and l > 8: - pl = spr.tw.window.create_pango_layout("..."+mylabel[l-8:]) + pl = spr.tw.canvas.create_pango_layout("..."+mylabel[l-8:]) else: - pl = spr.tw.window.create_pango_layout(mylabel) + pl = spr.tw.canvas.create_pango_layout(mylabel) pl.set_font_description(fd) if center_flag: swidth = pl.get_size()[0]/pango.SCALE @@ -153,9 +153,9 @@ def draw_label(spr, label, myscale, center_flag, truncate_flag): # used for most things def draw_label1(spr, label): - draw_label(spr, label, 7, True, True) + draw_label(spr, label, 12, True, True) # used for status blocks def draw_label2(spr, label): - draw_label(spr, str(label), 9, False, False) + draw_label(spr, str(label), 14, False, False) diff --git a/window.py b/window.py new file mode 100644 index 0000000..ea3bb25 --- /dev/null +++ b/window.py @@ -0,0 +1,155 @@ +#Copyright (c) 2009, Walter Bender + +#Permission is hereby granted, free of charge, to any person obtaining a copy +#of this software and associated documentation files (the "Software"), to deal +#in the Software without restriction, including without limitation the rights +#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +#copies of the Software, and to permit persons to whom the Software is +#furnished to do so, subject to the following conditions: + +#The above copyright notice and this permission notice shall be included in +#all copies or substantial portions of the Software. + +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +#THE SOFTWARE. + +import pygtk +pygtk.require('2.0') +import gtk +from gettext import gettext as _ + +try: + from sugar.graphics import style + GRID_CELL_SIZE = style.GRID_CELL_SIZE +except: + GRID_CELL_SIZE = 0 + +from grid import * +from card import * + +from math import sqrt + +CARD_DIM = 135 + +class taWindow: pass + +# +# handle launch from both within and without of Sugar environment +# +def new_window(canvas, path, parent=None): + tw = taWindow() + tw.canvas = canvas + tw.path = path + tw.activity = parent + # starting from command line + if parent is None: + tw.sugar = False + tw.canvas.set_size_request(gtk.gdk.screen_width(), \ + gtk.gdk.screen_height()) + tw.canvas.show_all() + # starting from Sugar + else: + tw.sugar = True + parent.show_all() + + tw.canvas.set_flags(gtk.CAN_FOCUS) + tw.canvas.add_events(gtk.gdk.BUTTON_PRESS_MASK) + tw.canvas.add_events(gtk.gdk.BUTTON_RELEASE_MASK) + tw.canvas.connect("expose-event", _expose_cb, tw) + tw.canvas.connect("button-press-event", _button_press_cb, tw) + tw.canvas.connect("button-release-event", _button_release_cb, tw) + tw.width = gtk.gdk.screen_width() + tw.height = gtk.gdk.screen_height()-GRID_CELL_SIZE + # scale to fill 80% of screen height + tw.card_dim = CARD_DIM + tw.scale = 0.8 * tw.height/(tw.card_dim*3) + tw.area = tw.canvas.window + tw.gc = tw.area.new_gc() + tw.cm = tw.gc.get_colormap() + tw.msgcolor = tw.cm.alloc_color('black') + tw.sprites = [] + + # Initialize the grid + tw.grid = Grid(tw) + + # Start solving the puzzle + tw.press = -1 + tw.release = -1 + tw.start_drag = [0,0] + + return tw + + +# +# Button press +# +def _button_press_cb(win, event, tw): + win.grab_focus() + x, y = map(int, event.get_coords()) + tw.start_drag = [x,y] + spr = findsprite(tw,(x,y)) + if spr is None: + tw.press = -1 + tw.release = -1 + return True + # take note of card under button press + tw.press = spr.label + return True + +# +# Button release +# +def _button_release_cb(win, event, tw): + win.grab_focus() + x, y = map(int, event.get_coords()) + spr = findsprite(tw,(x,y)) + if spr is None: + tw.press = -1 + tw.release = -1 + return True + # take note of card under button release + tw.release = spr.label + # if the same card (click) then rotate + if tw.press == tw.release: + # check to see if it was an aborted move + if distance(tw.start_drag,[x,y]) < 20: + tw.grid.card_table[tw.press].rotate_ccw() + # tw.grid.card_table[tw.press].print_card() + # if different card (drag) then swap + else: + tw.grid.swap(tw.press,tw.release) + # tw.grid.print_grid() + inval(tw.grid.card_table[tw.press].spr) + inval(tw.grid.card_table[tw.release].spr) + redrawsprites(tw) + tw.press = -1 + tw.release = -1 + if tw.sugar is True: + if tw.grid.test() == True: + tw.activity.results_label.set_text(_("You solved the puzzle.")) + tw.activity.results_label.show() + else: + tw.activity.results_label.set_text(_("Keep trying.")) + tw.activity.results_label.show() + return True + +# +# Measure length of drag between button press and button release +# +def distance(start,stop): + dx = start[0]-stop[0] + dy = start[1]-stop[1] + return sqrt(dx*dx+dy*dy) + + +# +# Repaint +# +def _expose_cb(win, event, tw): + redrawsprites(tw) + return True |