From bc030a1fbb12e4d50678d4e90c5f11a5fac3cbfa Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 22 Oct 2011 14:50:50 +0000 Subject: added sector mode --- 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: diff --git a/ball.py b/ball.py index 66e6adf..630919a 100644 --- a/ball.py +++ b/ball.py @@ -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] diff --git a/bounce.py b/bounce.py index 26f5f23..615a6b5 100644 --- a/bounce.py +++ b/bounce.py @@ -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 @@ + + + + + + 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 = '