forked from sugarlabs/fractionbounce
/
bar.py
197 lines (167 loc) · 7.58 KB
/
bar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
#Copyright (c) 2011, Walter Bender, Paulina Clares, Chris Rowe
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
from gi.repository import Gdk
from sugar3.graphics import style
from sprites import Sprite
from svg_utils import (svg_header, svg_footer, svg_rect, svg_str_to_pixbuf,
svg_wedge)
BAR_HEIGHT = style.GRID_CELL_SIZE
class Bar():
''' The Bar class is used to define the bars at the bottom of the
screen '''
def __init__(self, sprites, ball_size, colors=['#FFFFFF', '#AAAAAA']):
''' Initialize the 2-segment bar, labels, and mark '''
self._sprites = sprites
self._colors = colors[:]
self.bars = {}
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height() - style.GRID_CELL_SIZE
self._scale = Gdk.Screen.height() / 900.0
self._ball_size = ball_size
self.make_bar(2)
self._make_wedge_mark()
def resize_all(self):
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height() - style.GRID_CELL_SIZE
self._scale = Gdk.Screen.height() / 900.0
for bar in self.bars.keys():
self.bars[bar].hide()
self.mark.hide()
for bar in self.bars.keys():
self.make_bar(bar)
self._make_wedge_mark()
def _make_wedge_mark(self):
''' Make a mark to show the fraction position on the bar. '''
dx = self._ball_size / 2.
n = (self._width - self._ball_size) / dx
dy = (BAR_HEIGHT * self._scale) / n
s = 3.5
i = int(n / 2) - 1
mark = svg_header(self._ball_size,
BAR_HEIGHT * self._scale + s, 1.0)
mark += svg_wedge(dx, BAR_HEIGHT * self._scale + s,
s,
i * 2 * dy + s, (i * 2 + 1) * dy + s,
'#FF0000', '#FFFFFF')
mark += svg_wedge(dx, BAR_HEIGHT * self._scale + s,
dx + s,
(i * 2 + 1) * dy + s, (i * 2 + 2) * dy + s,
'#FF0000', '#FFFFFF')
mark += svg_footer()
self.mark = Sprite(self._sprites, 0,
self._height, # hide off bottom of screen
svg_str_to_pixbuf(mark))
self.mark.set_layer(1)
def _make_mark(self):
''' Make a mark to show the fraction position on the bar. '''
mark = svg_header(self._ball_size / 2.,
BAR_HEIGHT * self._scale + 4, 1.0)
mark += svg_rect(self._ball_size / 2.,
BAR_HEIGHT * self._scale + 4, 0, 0, 0, 0,
'#FF0000', '#FF0000')
mark += svg_rect(1, BAR_HEIGHT * self._scale + 4, 0, 0,
self._ball_size / 4., 0, '#000000', '#000000')
mark += svg_footer()
self.mark = Sprite(self._sprites, 0,
self._height, # hide off bottom of screen
svg_str_to_pixbuf(mark))
self.mark.set_layer(1)
def mark_width(self):
return self.mark.rect[2]
def bar_x(self):
return self.bars[2].get_xy()[0]
def bar_y(self):
if self.bars[2].get_xy()[1] < 0:
return self.bars[2].get_xy()[1] + 3000
else:
return self.bars[2].get_xy()[1]
def width(self):
return self.bars[2].rect[2]
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 bump_bars(self, direction='up'):
''' when the toolbars expand and contract, we need to move the bar '''
if direction == 'up':
dy = -style.GRID_CELL_SIZE
else:
dy = style.GRID_CELL_SIZE
for bar in self.bars:
self.bars[bar].move_relative([0, dy])
self.mark.move_relative([0, dy])
def hide_bars(self):
''' Hide all of the bars '''
for bar in self.bars:
if self.bars[bar].get_xy()[1] > 0:
self.bars[bar].move_relative([0, -3000])
def get_bar(self, nsegments):
''' Return a bar with n segments '''
if nsegments not in self.bars:
self.make_bar(nsegments)
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._width - self._ball_size,
BAR_HEIGHT * self._scale, 1.0)
dx = self._width / float(nsegments)
for i in range(int(nsegments) / 2):
svg += svg_rect(dx, BAR_HEIGHT * self._scale, 0, 0,
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,
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, 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(
(0, self._height -
int((self._ball_size + self._height()) / 2)))
def _make_wedge_bar(self, nsegments):
''' Create a wedged-shaped bar with n segments '''
s = 3.5 # add provision for stroke width
svg = svg_header(self._width, BAR_HEIGHT * self._scale + s, 1.0)
dx = self._width / float(nsegments)
dy = (BAR_HEIGHT * self._scale) / float(nsegments)
for i in range(int(nsegments) / 2):
svg += svg_wedge(dx, BAR_HEIGHT * self._scale + s,
i * 2 * dx + s,
i * 2 * dy + s, (i * 2 + 1) * dy + s,
'#000000', '#FFFFFF')
svg += svg_wedge(dx, BAR_HEIGHT * self._scale + s,
(i * 2 + 1) * dx + s,
(i * 2 + 1) * dy + s, (i * 2 + 2) * dy + s,
'#000000', '#FFFFFF')
if int(nsegments) % 2 == 1: # odd
svg += svg_wedge(dx, BAR_HEIGHT * self._scale + s,
(i * 2 + 2) * dx + s,
(i * 2 + 2) * dy + s,
BAR_HEIGHT * self._scale + s,
'#000000', '#FFFFFF')
svg += svg_footer()
self.bars[nsegments] = Sprite(self._sprites, 0, 0,
svg_str_to_pixbuf(svg))
self.bars[nsegments].set_layer(2)
self.bars[nsegments].set_label_attributes(18, horiz_align="left", i=0)
self.bars[nsegments].set_label_attributes(18, horiz_align="right", i=1)
self.bars[nsegments].set_label_color('black', i=0)
self.bars[nsegments].set_label_color('white', i=1)
self.bars[nsegments].set_label(' 0', i=0)
self.bars[nsegments].set_label('1 ', i=1)
self.bars[nsegments].move(
(0, self._height - BAR_HEIGHT * self._scale))