From 50c6bdbbee4c575ac2f781ef4aad9cb3fdc437cc Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 12 Jun 2014 19:36:58 +0000 Subject: enhancements to the bar --- diff --git a/NEWS b/NEWS index d5a1b28..ca9702d 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +22 + +ENHANCEMENTS: +* Make bar wedge-shaped to make it easier to tell which direction is 0 vs 1 + 21 BUG FIX: diff --git a/activity/activity.info b/activity/activity.info index bedbeb1..9a43ed7 100644 --- a/activity/activity.info +++ b/activity/activity.info @@ -1,6 +1,6 @@ [Activity] name = FractionBounce -activity_version = 21 +activity_version = 22 license = GPLv3 bundle_id = org.sugarlabs.FractionBounceActivity exec = sugar-activity FractionBounceActivity.FractionBounceActivity diff --git a/bar.py b/bar.py index 88311f9..7899b34 100644 --- a/bar.py +++ b/bar.py @@ -12,7 +12,8 @@ from sprites import Sprite -from svg_utils import svg_header, svg_footer, svg_rect, svg_str_to_pixbuf +from svg_utils import (svg_header, svg_footer, svg_rect, svg_str_to_pixbuf, + svg_wedge) from gettext import gettext as _ @@ -24,9 +25,11 @@ class Bar(): ''' The Bar class is used to define the bars at the bottom of the screen ''' - def __init__(self, sprites, width, height, scale, size): + def __init__(self, sprites, width, height, scale, size, + colors=['#FFFFFF', '#AAAAAA']): ''' Initialize the 2-segment bar, labels, and mark ''' self.sprites = sprites + self.colors = colors[:] self.bars = {} self.screen_width = width self.screen_height = height @@ -59,7 +62,10 @@ class Bar(): return self.bars[2].get_xy()[0] def bar_y(self): - return self.bars[2].get_xy()[1] + if self.bars[2].get_xy()[1] < 0: + return self.bars[2].get_xy()[1] + 1000 + else: + return self.bars[2].get_xy()[1] def width(self): return self.bars[2].rect[2] @@ -67,10 +73,15 @@ class Bar(): def height(self): return self.bars[2].rect[3] + def show_bar(self, n): + if n in self.bars: + self.bars[n].move([self.bar_x(), self.bar_y()]) + def hide_bars(self): ''' Hide all of the bars ''' for bar in self.bars: - self.bars[bar].set_layer(-1) + if self.bars[bar].get_xy()[1] > 0: + self.bars[bar].move_relative([0, -1000]) def make_labels(self): ''' Label the bar ''' @@ -94,17 +105,49 @@ class Bar(): return self.bars[nsegments] def make_bar(self, nsegments): + return self.make_wedge_bar(nsegments) + + def make_rect_bar(self, nsegments): ''' Create a bar with n segments ''' svg = svg_header(self.screen_width - self.ball_size, BAR_HEIGHT, 1.0) dx = (self.screen_width - self.ball_size) / float(nsegments) for i in range(int(nsegments) / 2): svg += svg_rect(dx, BAR_HEIGHT * self.scale, 0, 0, - i * 2 * dx, 0, '#FFFFFF', '#FFFFFF') + i * 2 * dx, 0, self.colors[0], self.colors[0]) svg += svg_rect(dx, BAR_HEIGHT * self.scale, 0, 0, - (i * 2 + 1) * dx, 0, '#AAAAAA', '#AAAAAA') + (i * 2 + 1) * dx, 0, self.colors[1], self.colors[1]) if int(nsegments) % 2 == 1: # odd svg += svg_rect(dx, BAR_HEIGHT * self.scale, 0, 0, - (i * 2 + 2) * dx, 0, '#FFFFFF', '#FFFFFF') + (i * 2 + 2) * dx, 0, self.colors[0], self.colors[0]) + svg += svg_footer() + + self.bars[nsegments] = Sprite(self.sprites, 0, 0, + svg_str_to_pixbuf(svg)) + self.bars[nsegments].move( + (int(self.ball_size / 2), self.screen_height - \ + int((self.ball_size + self.height()) / 2))) + + def make_wedge_bar(self, nsegments): + ''' Create a wedged-shaped bar with n segments ''' + svg = svg_header(self.screen_width - self.ball_size, BAR_HEIGHT, 1.0) + dx = (self.screen_width - self.ball_size) / float(nsegments) + dy = BAR_HEIGHT * self.scale / float(nsegments) + print 'nsegments', nsegments + for i in range(int(nsegments) / 2): + print i, i * 2 * dy, (i * 2 + 1) * dy, + svg += svg_wedge(dx, BAR_HEIGHT * self.scale, + i * 2 * dx, + i * 2 * dy, (i * 2 + 1) * dy, + self.colors[0], self.colors[0]) + svg += svg_wedge(dx, BAR_HEIGHT * self.scale, + (i * 2 + 1) * dx, + (i * 2 + 1) * dy, (i * 2 + 2) * dy, + self.colors[1], self.colors[1]) + if int(nsegments) % 2 == 1: # odd + svg += svg_wedge(dx, BAR_HEIGHT * self.scale, + (i * 2 + 2) * dx, + (i * 2 + 2) * dy, BAR_HEIGHT * self.scale, + self.colors[0], self.colors[0]) svg += svg_footer() self.bars[nsegments] = Sprite(self.sprites, 0, 0, diff --git a/bounce.py b/bounce.py index 546dc1b..8442f09 100644 --- a/bounce.py +++ b/bounce.py @@ -54,8 +54,8 @@ from random import uniform import os -from svg_utils import svg_header, svg_footer, svg_rect, svg_str_to_pixbuf, \ - svg_from_file +from svg_utils import (svg_header, svg_footer, svg_rect, svg_str_to_pixbuf, + svg_from_file) from play_audio import play_audio_from_file from ball import Ball @@ -67,10 +67,13 @@ import logging _logger = logging.getLogger('fractionbounce-activity') try: + from sugar3 import profile + COLORS = profile.get_color().to_string().split(',') from sugar3.graphics import style GRID_CELL_SIZE = style.GRID_CELL_SIZE -except ImportError: - GRID_CELL_SIZE = 0 +except: + COLORS = ['#FFFFFF', '#AAAAAA'] + GRID_CELL_SIZE = 55 from sprites import Sprites, Sprite @@ -169,7 +172,7 @@ class Bounce(): self.current_frame = 0 self.bar = Bar(self.sprites, self.width, self.height, self.scale, - self.ball.width()) + self.ball.width(), COLORS) self.current_bar = self.bar.get_bar(2) self.ball_y_max = self.bar.bar_y() - self.ball.height() @@ -383,16 +386,15 @@ class Bounce(): self.bar.hide_bars() if self.expert: # Show two-segment bar in expert mode - self.current_bar = self.bar.get_bar(2) + nseg = 2 else: if self.mode == 'percents': nseg = 10 else: 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())) - self.current_bar.set_layer(0) + # generate new bar on demand + self.current_bar = self.bar.get_bar(nseg) + self.bar.show_bar(nseg) def _easter_egg_test(self): ''' Test to see if we show the Easter Egg ''' diff --git a/svg_utils.py b/svg_utils.py index 662b5e8..b662e31 100644 --- a/svg_utils.py +++ b/svg_utils.py @@ -39,7 +39,20 @@ def svg_sector(x, y, r, a, fill, stroke): svg_string = '