diff options
author | Walter Bender <walter@sugarlabs.org> | 2010-06-28 19:21:47 (GMT) |
---|---|---|
committer | Walter Bender <walter@sugarlabs.org> | 2010-06-28 19:21:47 (GMT) |
commit | b3b9bb81902c83cfd1d7c60ed49a7ed996e18c22 (patch) | |
tree | a4248efda61188b421feea4aa7db004f07e9d75c | |
parent | 2af2168614d90a75ca4364368148a1816848106c (diff) |
added Cuisenaire-like rods
-rw-r--r-- | AbacusActivity.py | 16 | ||||
-rw-r--r-- | abacus_window.py | 74 |
2 files changed, 80 insertions, 10 deletions
diff --git a/AbacusActivity.py b/AbacusActivity.py index 81bdc17..65bb948 100644 --- a/AbacusActivity.py +++ b/AbacusActivity.py @@ -40,7 +40,7 @@ _logger = logging.getLogger("abacus-activity") from abacus_window import Abacus, Custom, Suanpan, Soroban, Schety,\ Nepohualtzintzin, Binary, Hex, Decimal, Fractions,\ - Caacupe + Caacupe, Cuisenaire def _button_factory(icon_name, tooltip, callback, toolbar): """Factory for making toolbar buttons""" @@ -171,6 +171,8 @@ class AbacusActivity(activity.Activity): self._fraction_cb, _abacus_toolbar) self.caacupe = _button_factory("caacupe-off", _('Caacupé'), self._caacupe_cb, _abacus_toolbar) + self.cuisenaire = _button_factory("cuisenaire-off", _('Rods'), + self._cuisenaire_cb, _abacus_toolbar) self._rods_label = _label_factory(_("Rods:")+" ", _custom_toolbar) self._rods_spin = _spin_factory(15, 1, 20, self._rods_spin_cb, @@ -231,6 +233,8 @@ class AbacusActivity(activity.Activity): self._fraction_cb(None) elif self.metadata['abacus'] == 'caacupe': self._caacupe_cb(None) + elif self.metadata['abacus'] == 'cuisenaire': + self._cuisenaire_cb(None) elif self.metadata['abacus'] == 'decimal': self._decimal_cb(None) elif self.metadata['abacus'] == 'custom': @@ -262,6 +266,7 @@ class AbacusActivity(activity.Activity): self.hex.set_icon("hex-off") self.fraction.set_icon("fraction-off") self.caacupe.set_icon("caacupe-off") + self.caacupe.set_icon("cuisenaire-off") self.decimal.set_icon("decimal-off") if self.abacus.chinese is not None: self.abacus.chinese.hide() @@ -281,6 +286,8 @@ class AbacusActivity(activity.Activity): self.abacus.decimal.hide() if self.abacus.caacupe is not None: self.abacus.caacupe.hide() + if self.abacus.cuisenaire is not None: + self.abacus.cuisenaire.hide() if self.abacus.custom is not None: self.abacus.custom.hide() @@ -383,6 +390,13 @@ class AbacusActivity(activity.Activity): self._select_abacus(self.caacupe, self.abacus.caacupe.name+"-on", self.abacus.caacupe) + def _cuisenaire_cb(self, button): + """ Display Cuisenaire-like rods; hide the others """ + if self.abacus.cuisenaire is None: + self.abacus.cuisenaire = Cuisenaire(self.abacus) + self._select_abacus(self.cuisenaire, self.abacus.cuisenaire.name+"-on", + self.abacus.cuisenaire) + def write_file(self, file_path): """ Write the bead positions to the Journal """ _logger.debug("Saving current abacus to Journal: %s " % ( diff --git a/abacus_window.py b/abacus_window.py index f1676eb..bb5e152 100644 --- a/abacus_window.py +++ b/abacus_window.py @@ -111,18 +111,18 @@ def _svg_indicator(): svg_string += _svg_style("fill:#ff0000;stroke:#ff0000;stroke-width:3.0;") return svg_string -def _svg_bead(fill, stroke): - """ Returns a bead-shaped SVG object """ - svg_string = "%s %s %s %s" % ("<path d=\"m1.5 15 A 15 13.5 90 0 1", - "15 1.5 L 25 1.5 A 15 13.5 90 0 1 38.5", - "15 A 15 13.5 90 0 1 25 28.5 L 15", - "28.5 A 15 13.5 90 0 1 1.5 15 z\"\n") +def _svg_bead(fill, stroke, scale=1.0): + """ Returns a bead-shaped SVG object; scale is used to elongate """ + _h = 15+30*(scale-1.0) + _h2 = 30*scale-1.5 + svg_string = "<path d=\"m 1.5 15 A 15 13.5 90 0 1 15 1.5 L 25 1.5 A 15 13.5 90 0 1 38.5 15 L 38.5 %f A 15 13.5 90 0 1 25 %f L 15 %f A 15 13.5 90 0 1 1.5 %f L 1.5 15 z\"\n" %\ + (_h, _h2, _h2, _h) svg_string += _svg_style("fill:%s;stroke:%s;stroke-width:1.5" %\ (fill, stroke)) return svg_string -def _svg_header(w, h, scale): - """ Returns SVG header """ +def _svg_header(w, h, scale, hscale=1.0): + """ Returns SVG header; some beads are elongated (hscale) """ svg_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"" svg_string += " standalone=\"no\"?>\n" svg_string += "<!-- Created with Python -->\n" @@ -131,7 +131,7 @@ def _svg_header(w, h, scale): svg_string += " xmlns=\"http://www.w3.org/2000/svg\"\n" svg_string += " version=\"1.0\"\n" svg_string += "%s%f%s" % (" width=\"", w*scale, "\"\n") - svg_string += "%s%f%s" % (" height=\"", h*scale, "\">\n") + svg_string += "%s%f%s" % (" height=\"", h*scale*hscale, "\">\n") svg_string += "%s%f%s%f%s" % ("<g\n transform=\"matrix(", scale, ",0,0,", scale, ",0,0)\">\n") @@ -295,6 +295,7 @@ class Abacus(): self.decimal = None self.fraction = None self.caacupe = None + self.cuisenaire = None self.custom = None self.chinese.show() @@ -1258,3 +1259,58 @@ class Caacupe(Fractions): for i in range(-value): self.beads[bead_index+self.bead_count[rod]-i-1].move_down() +class Cuisenaire(Caacupe): + """ Inherit from Caacupe abacus. """ + + def set_parameters(self): + """ Create an abacus with fractions: 10 by 10 (with 1/1, 1/2, 1/3. 1/4, + 1/5, 1/6, 1/7, 1/8, 1/9, 1/10). """ + self.bead_count = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) + self.name = "cuisenaire" + self.num_rods = 10 + self.top_beads = 0 + self.bot_beads = 10 + self.base = 10 + self.top_factor = 5 + return + + def draw_rods_and_beads(self, x, y): + """ Override default in order to center beads vertically; beads + are scaled vertically to match their value """ + self.bead_pixbuf = [] + for i in range(self.num_rods): + _bead = _svg_header(BWIDTH, BHEIGHT, self.abacus.scale, + 10.0/(i+1)) +\ + _svg_bead("#FFFFFF", "#000000", + 10.0/(i+1)) +\ + _svg_footer() + self.bead_pixbuf.append(_svg_str_to_pixbuf(_bead)) + + dx = (BWIDTH+BOFFSET)*self.abacus.scale + + self.beads = [] + self.rods = [] + bo = (BWIDTH-BOFFSET)*self.abacus.scale/4 + ro = (BWIDTH+5)*self.abacus.scale/2 + for i in range(self.num_rods): + _rod = _svg_header(10, self.frame_height-(FSTROKE*2), + self.abacus.scale) +\ + _svg_rect(10, self.frame_height-(FSTROKE*2), 0, 0, 0, 0, + ROD_COLORS[(i+9)%len(ROD_COLORS)], "#404040") +\ + _svg_footer() + self.rods.append(Sprite(self.abacus.sprites, x+i*dx+ro, y, + _svg_str_to_pixbuf(_rod))) + + for b in range(self.bead_count[i]): + self.beads.append(Bead( + Sprite(self.abacus.sprites, + x+i*dx+bo, + y+(1+b*10.0/(i+1))*BHEIGHT*self.abacus.scale, + self.bead_pixbuf[i]), + BHEIGHT*self.abacus.scale, + 1.0/(i+1), 0, True)) + self.beads[-1].set_label_color("#000000") + + for r in self.rods: + r.type = "frame" + return |