diff options
-rw-r--r-- | FractionBounceActivity.py | 22 | ||||
-rw-r--r-- | ball.py | 13 | ||||
-rw-r--r-- | bounce.py | 24 | ||||
-rw-r--r-- | icons/sector.svg | 19 | ||||
-rw-r--r-- | svg_utils.py | 14 |
5 files changed, 75 insertions, 17 deletions
diff --git a/FractionBounceActivity.py b/FractionBounceActivity.py index dfa30d7..be1b387 100644 --- a/FractionBounceActivity.py +++ b/FractionBounceActivity.py @@ -133,6 +133,10 @@ class FractionBounceActivity(activity.Activity): self._fraction_cb, tooltip=_('fractions'), group=None) + self.sector_button = radio_factory('sector', toolbar, + self._sector_cb, + tooltip=_('sectors'), + group=self.fraction_button) self.percent_button = radio_factory('percent', toolbar, self._percent_cb, tooltip=_('percents'), @@ -156,6 +160,7 @@ class FractionBounceActivity(activity.Activity): tooltip=_('add new fraction'), accelerator='Return') separator_factory(toolbar, expand=False, visible=True) + separator_factory(toolbar, expand=False, visible=False) self._ball_selector = combo_factory(BALLS, toolbar, self._combo_cb, default=_('basketball'), tooltip=_('choose a ball')) @@ -183,18 +188,35 @@ class FractionBounceActivity(activity.Activity): else: chooser(self, '', self._new_ball_from_journal) + def _reset_ball(self): + ''' If we switch back from sector mode, we need to restore the ball ''' + if self.bounce_window.mode != 'sectors': + return + if BALLS[self._ball_selector.get_active()] == _('soccer ball'): + self.bounce_window.ball.new_ball(os.path.join( + activity.get_bundle_path(), 'soccer.svg')) + else: + self.bounce_window.ball.new_ball(os.path.join( + activity.get_bundle_path(), 'basketball.svg')) + def _new_ball_from_journal(self, dsobject): ''' Load an image from the Journal. ''' self.bounce_window.ball.new_ball_from_image(dsobject.file_path) def _fraction_cb(self, arg=None): ''' Set fraction mode ''' + self._reset_ball() self.bounce_window.mode = 'fractions' def _percent_cb(self, arg=None): ''' Set percent mode ''' + self._reset_ball() self.bounce_window.mode = 'percents' + def _sector_cb(self, arg=None): + ''' Set sector mode ''' + self.bounce_window.mode = 'sectors' + def _add_fraction_cb(self, arg=None): ''' Read entries and add a fraction to the list ''' try: @@ -12,10 +12,11 @@ # Boston, MA 02111-1307, USA. import gtk +from math import pi from sprites import Sprite from svg_utils import svg_header, svg_footer, svg_str_to_pixbuf, \ - extract_svg_payload, svg_from_file + extract_svg_payload, svg_from_file, svg_sector import logging _logger = logging.getLogger('fractionbounce-activity') @@ -104,7 +105,6 @@ class Ball(): def new_ball(self, filename): ''' Create a ball object and Easter Egg animation from an SVG file. ''' self.ball.images[0] = svg_str_to_pixbuf(svg_from_file(filename)) - ball = extract_svg_payload(file(filename, 'r')) for i in range(8): self.frames[i].images[0] = svg_str_to_pixbuf( @@ -122,6 +122,15 @@ class Ball(): except: _logger.debug('Could not load image from %s.', filename) + def new_ball_from_fraction(self, fraction): + ''' Create a ball with a section of size fraction. ''' + r = SIZE / 2.0 + self.ball.images[0] = svg_str_to_pixbuf( + svg_header(SIZE, SIZE, 1.0) + \ + svg_sector(r, r, r - 1, 1.999 * pi, '#A0A0A0', '#ff0000') + \ + svg_sector(r, r, r - 1, fraction * 2 * pi, '#ffff00', '#ff0000') + \ + svg_footer()) + def ball_x(self): return self.ball.get_xy()[0] @@ -359,29 +359,23 @@ class Bounce(): if not self.we_are_sharing(): self.n = int(uniform(0, len(self.challenges))) fstr = self.challenges[self.n][0] - saw_a_fraction = False if '/' in fstr: # fraction numden = fstr.split('/', 2) self.fraction = float(numden[0].strip()) / float(numden[1].strip()) - saw_a_fraction = True elif '%' in fstr: # percentage self.fraction = float(fstr.strip().strip('%').strip()) / 100. else: # To do: add support for decimals (using locale) _logger.debug('Could not parse challenge (%s)', fstr) fstr = '1/2' self.fraction = 0.5 - saw_a_fraction = True - if self.mode == 'fractions': - if saw_a_fraction: - self.label = fstr - else: - self.label = fstr.strip().strip('%').strip() + '/100' + if self.mode == 'percents': + self.label = str(int(self.fraction * 100 + 0.5)) + '%' else: # percentage - if not saw_a_fraction: - self.label = fstr - else: - self.label = str(int(self.fraction * 100 + 0.5)) + '%' + self.label = fstr + if self.mode == 'sectors': + self.ball.new_ball_from_fraction(self.fraction) + self.activity.reset_label(self.label) self.ball.ball.set_label(self.label) @@ -389,10 +383,10 @@ class Bounce(): if self.expert: # Show two-segment bar in expert mode self.current_bar = self.bar.get_bar(2) else: - if self.mode == 'fractions': - nseg = self.challenges[self.n][1] + if self.mode == 'percents': + nseg = 10 else: - nseg = 10 # percentages + nseg = self.challenges[self.n][1] # generate new bar on demand self.current_bar = self.bar.get_bar(nseg) self.current_bar.move((self.bar.bar_x(), self.bar.bar_y())) diff --git a/icons/sector.svg b/icons/sector.svg new file mode 100644 index 0000000..c0bbd95 --- /dev/null +++ b/icons/sector.svg @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> + +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + version="1.0" + width="55" + height="55" + id="svg2"> + <path + d="M 47.142859,25.588802 A 19.855213,18.581081 0 1 1 27.35948,7.0078426 l -0.07184,18.5809594 z" + transform="matrix(0,-1.0906182,-1.1654035,0,57.32128,57.260403)" + id="path2984" + style="fill:#c0c0c0;fill-opacity:1;stroke:#ffffff;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none" /> +</svg> diff --git a/svg_utils.py b/svg_utils.py index 0d828c6..2e02b5e 100644 --- a/svg_utils.py +++ b/svg_utils.py @@ -12,6 +12,7 @@ # Boston, MA 02111-1307, USA. import gtk +from math import sin, cos, pi def generate_xo_svg(scale=1.0, colors=["#C0C0C0", "#282828"]): @@ -30,6 +31,19 @@ def svg_str_to_pixbuf(svg_string): return pixbuf +def svg_sector(x, y, r, a, fill, stroke): + ''' Returns an SVG sector ''' + if a < pi: + big_arc = 0 + else: + big_arc = 1 + svg_string = ' <path d="M%f,%f v%f a%f,%f 0 %d,0 %f,%f z"\n' % ( + x, y, -r, r, r, big_arc, -sin(a) * r, r - cos(a) * r) + svg_string += _svg_style('fill:%s;stroke:%s;' % (fill, stroke)) + print svg_string + return svg_string + + def svg_rect(w, h, rx, ry, x, y, fill, stroke): ''' Returns an SVG rectangle ''' svg_string = ' <rect\n' |