Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/abacus_window.py
diff options
context:
space:
mode:
authorWalter Bender <walter@sugarlabs.org>2010-04-05 18:22:46 (GMT)
committer Walter Bender <walter@sugarlabs.org>2010-04-05 18:22:46 (GMT)
commit6e1cb0b29746921c0b02dfd64fcaa3c132939bf9 (patch)
treeb1121186bb75a1b4d29adde02fea7729e31119f5 /abacus_window.py
parent643c94c93418a2ff1d1867b6ccd9ab7158e7dbc7 (diff)
save/restore abacus state
Diffstat (limited to 'abacus_window.py')
-rw-r--r--abacus_window.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/abacus_window.py b/abacus_window.py
index f0695ce..eef6cf1 100644
--- a/abacus_window.py
+++ b/abacus_window.py
@@ -197,6 +197,55 @@ class AbacusGeneric():
i.set_layer(101)
self.bar.set_layer(102)
+ def set_value(self, string):
+ """ Set abacus to value in string """
+ v = []
+ for r in range(self.num_rods):
+ v.append(0)
+
+ # Convert string to column values.
+ o = len(string) - self.num_rods
+ for i in range(self.num_rods):
+ v[self.num_rods-i-1] = int(string[o+self.num_rods-i-1])
+ if o > 0:
+ v[0] += int(string[0])*10
+
+ # Move the beads to correspond to column values.
+ for r in range(self.num_rods):
+ self.set_rod_value(r, v[r])
+
+ def set_rod_value(self, r, v):
+ """ Move beads on rod r to represent value v """
+ bot = v%5
+ top = (v-bot)/5
+ top_bead_index = r*(self.top_beads+self.bot_beads)
+ bot_bead_index = r*(self.top_beads+self.bot_beads)+self.top_beads
+
+ print v, top, bot, r, top_bead_index, bot_bead_index
+
+ # Clear the top.
+ for i in range(self.top_beads):
+ if self.beads[top_bead_index+i].state:
+ self.beads[top_bead_index+i].move((0,
+ -2*BHEIGHT*self.abacus.scale))
+ # Clear the bottom.
+ for i in range(self.bot_beads):
+ if self.beads[bot_bead_index+i].state:
+ self.beads[bot_bead_index+i].move((0,
+ 2*BHEIGHT*self.abacus.scale))
+
+ # Set the top.
+ for i in range(top):
+ self.beads[top_bead_index+self.top_beads-i-1].move_relative((0,
+ 2*BHEIGHT*self.abacus.scale))
+ self.beads[top_bead_index+self.top_beads-i-1].state = 1
+
+ # Set the bottom
+ for i in range(bot):
+ self.beads[bot_bead_index+i].move_relative((0,
+ -2*BHEIGHT*self.abacus.scale))
+ self.beads[bot_bead_index+i].state = 1
+
def value(self):
""" Return a string representing the value of each rod. """
string = ''