diff options
author | Walter Bender <walter@sugarlabs.org> | 2010-04-05 18:22:46 (GMT) |
---|---|---|
committer | Walter Bender <walter@sugarlabs.org> | 2010-04-05 18:22:46 (GMT) |
commit | 6e1cb0b29746921c0b02dfd64fcaa3c132939bf9 (patch) | |
tree | b1121186bb75a1b4d29adde02fea7729e31119f5 | |
parent | 643c94c93418a2ff1d1867b6ccd9ab7158e7dbc7 (diff) |
save/restore abacus state
-rw-r--r-- | AbacusActivity.py | 7 | ||||
-rw-r--r-- | abacus_window.py | 49 |
2 files changed, 54 insertions, 2 deletions
diff --git a/AbacusActivity.py b/AbacusActivity.py index cd32fdc..a5dfc35 100644 --- a/AbacusActivity.py +++ b/AbacusActivity.py @@ -130,7 +130,6 @@ class AbacusActivity(activity.Activity): 'images/'), self) # Read the current mode from the Journal - # TODO: read/restore bead positions try: if self.metadata['abacus'] == 'suanpan': self._chinese_cb(None) @@ -142,6 +141,10 @@ class AbacusActivity(activity.Activity): self._mayan_cb(None) except: pass + try: + self.abacus.mode.set_value(self.metadata['value']) + except: + pass def _chinese_cb(self, button): """ Display the suanpan; hide the others """ @@ -200,7 +203,7 @@ class AbacusActivity(activity.Activity): _logger.debug("Saving current abacus to Journal: %s " % ( self.abacus.mode.name)) self.metadata['abacus'] = self.abacus.mode.name - # TODO: Write bead positions + self.metadata['value'] = self.abacus.mode.value() # # Project toolbar for pre-0.86 toolbars 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 = '' |