diff options
author | Walter Bender <walter@walter-laptop.(none)> | 2009-10-07 16:24:43 (GMT) |
---|---|---|
committer | Walter Bender <walter@walter-laptop.(none)> | 2009-10-07 16:24:43 (GMT) |
commit | a10a5ffb402543560a6bf040b9f1149751a48e78 (patch) | |
tree | 91dc2c9565be4c1b5b34fdb1c11caee8110127fc | |
parent | a86ece87b3b15f34be38179abb0e17085d178660 (diff) |
adding rotation sets
-rw-r--r-- | card.py | 12 | ||||
-rwxr-xr-x | cardsort.py | 21 | ||||
-rw-r--r-- | orientation.py | 75 |
3 files changed, 102 insertions, 6 deletions
@@ -39,7 +39,7 @@ class Card: self.east = c[1] self.south = c[2] self.west = c[3] - self.rotate = 0 + self.orientation = 0 # create sprite from svg file self.spr = sprNew(tw, x, y,\ self.load_image(tw.path,i,tw.card_dim*tw.scale)) @@ -57,6 +57,10 @@ class Card: '.svg'), \ int(wh), int(wh)) + def set_orientation(self,r): + while r != self.orientation: + self.rotate_ccw() + def rotate_ccw(self): # print "rotating card " + str(self.spr.label) tmp = self.north @@ -64,9 +68,9 @@ class Card: self.east = self.south self.south = self.west self.west = tmp - self.rotate += 90 - if self.rotate > 359: - self.rotate -= 360 + self.orientation += 90 + if self.orientation > 359: + self.orientation -= 360 tmp = self.spr.image.rotate_simple(90) self.spr.image = tmp diff --git a/cardsort.py b/cardsort.py index 136528d..4218143 100755 --- a/cardsort.py +++ b/cardsort.py @@ -29,17 +29,20 @@ import os.path import window import grid +import card import sprites +from orientation import get_rotation_sets class CardSortMain: def __init__(self): + self.r = 0 self.tw = None # create a new window self.win = gtk.Window(gtk.WINDOW_TOPLEVEL) # for some reason, full screen width/height doesn't work self.win.set_size_request( - int(gtk.gdk.screen_width()*.9), \ - int(gtk.gdk.screen_height()*.9)) + int(gtk.gdk.screen_width()-32), \ + int(gtk.gdk.screen_height()-32)) self.win.set_title(_("CardSort") + ": " + \ _("click to rotate; drag to swap")) self.win.connect("delete_event", lambda w,e: gtk.main_quit()) @@ -48,6 +51,9 @@ class CardSortMain: menu_items = gtk.MenuItem(_("Toggle blank card")) menu.append(menu_items) menu_items.connect("activate", self._toggle_card_cb) + menu_items = gtk.MenuItem(_("Apply rotation sets")) + menu.append(menu_items) + menu_items.connect("activate", self._apply_rotation_sets_cb) menu_items.show() root_menu = gtk.MenuItem("Tools") root_menu.show() @@ -83,6 +89,17 @@ class CardSortMain: self.tw.grid.toggle_blank() sprites.redrawsprites(self.tw) + def _apply_rotation_sets_cb(self, widget): + rotation_sets = get_rotation_sets() + i = self.r + for j in range(9): + self.tw.grid.card_table[self.tw.grid.grid.index(j)]\ + .set_orientation(rotation_sets[i][j]) + sprites.redrawsprites(self.tw) + self.r += 1 + if self.r == 64: + self.r = 0 + def main(): gtk.main() return 0 diff --git a/orientation.py b/orientation.py new file mode 100644 index 0000000..d31579c --- /dev/null +++ b/orientation.py @@ -0,0 +1,75 @@ +# There are a limited number of valid orientation combinations available, +# assuming each card has two neighboring patterns of the same gender, e.g., +# north and east are the same gender, wst and south are the same gender in the +# initial position +# +# Since the gender of a neighboring edge has to be opposite, the following +# constraints apply: +# +ORIENTATION_CONSTRAINTS = [(-1,-1),(0,-1),(1,-1), \ + (-1,0), (3,1), (4,2), \ + (-1,3), (6,4), (7,5)] +# where the pairs are (left dependency, top dependency) +# and -1 indicates an edge, i.e., no dpendency +# +# Thus there are 4x2x2x2x1x1x2x1x1 = 64 possible orientation sets. + +# unconstrained positions +ROTS = (0,90,180,270) + +# constraint tables +LEFT = {0:(0,270),90:(90,180),180:(90,180),270:(0,270)} +TOP = {0:(0,90),90:(0,90),180:(180,270),270:(180,270)} +TOP_LEFT = [{0:0,90:90,180:90,270:0}, \ + {0:0,90:90,180:90,270:0}, \ + {0:270,90:180,180:180,270:270}, \ + {0:270,90:180,180:180,270:270}] + +def get_rotation_sets(): + + # Create a list of rotation sets (assuming CCW rotation) + rot_sets = [] + for i in range(64): + rot_sets.append([]) + + # first tile can be any rotation (4 groups of 16) + for i in range(4): + for j in range(16): + rot_sets[i*16+j].append(ROTS[i]) + + # second tile is constrained to the left only (4 groups of 8x2) + for i in range(4): + for j in range(8): + rot_sets[i*16+j].append(LEFT[rot_sets[i*16+j][0]][0]) + rot_sets[i*16+j+8].append(LEFT[rot_sets[i*16+j+8][0]][1]) + + # third tile is constrained to the left only (8 groups of 4x2) + for i in range(8): + for j in range(4): + rot_sets[i*8+j].append(LEFT[rot_sets[i*8+j][1]][0]) + rot_sets[i*8+j+4].append(LEFT[rot_sets[i*8+j+4][1]][1]) + + # fourth tile is constrained to the top only (16 groups of 2x2 + for i in range(16): + for j in range(2): + rot_sets[i*4+j].append(TOP[rot_sets[i*4+j][1]][0]) + rot_sets[i*4+j+2].append(TOP[rot_sets[i*4+j+2][1]][1]) + + # fifth tile is constrained by top and left + # sixth tile is constrained by top and left + for i in range(64): + rot_sets[i].append(TOP_LEFT[ROTS.index(rot_sets[i][1])][rot_sets[i][3]]) + rot_sets[i].append(TOP_LEFT[ROTS.index(rot_sets[i][2])][rot_sets[i][4]]) + + # seventh tile is constrained to the top only (32 groups of 1x2) + for i in range(32): + rot_sets[i*2].append(TOP[rot_sets[i*2][4]][0]) + rot_sets[i*2+1].append(TOP[rot_sets[i*2+1][4]][1]) + + # eigth tile is constrained by top and left + # ninth tile is constrained by top and left + for i in range(64): + rot_sets[i].append(TOP_LEFT[ROTS.index(rot_sets[i][4])][rot_sets[i][6]]) + rot_sets[i].append(TOP_LEFT[ROTS.index(rot_sets[i][5])][rot_sets[i][7]]) + + return rot_sets |