braille_alphabets.py 12.2 KB
Newer Older
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
#  gcompris - braille_alphabets.py
#
# Copyright (C) 2003, 2008 Bruno Coudoin
#
#   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.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, see <http://www.gnu.org/licenses/>.
#
# braille_alphabets activity.
import gtk
import gtk.gdk
import gcompris
import gcompris.utils
import gcompris.skin
import gcompris.bonus
import goocanvas
import pango
import gcompris.sound
import string
import random
from gcompris import gcompris_gettext as _
from BrailleChar import *
32
from BrailleMap import *
33

34 35 36 37 38
CELL_WIDTH = 30
COLOR_ON = 0xFF0000FFL
COLOR_OFF = 0X00000000L
CIRCLE_FILL = "#DfDfDf"
CIRCLE_STROKE = "blue"
39 40
MAP_STROKE = "black"
MAP_FILL = "light blue"
41

42 43 44
braille_intro = _("A system of writing for the blinds that uses characters made of raised dots.")
braille_desc = _("The braille cell is composed of 6 dot cells organized in form of two vertical columns"
                 " with 3 dots {1,2,3} side by side on left and 3 dots side by side on right {4,5,6}")
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

#Array Declaration
letter_arr_one = ['A','B','C','D','E','F','G']
random.shuffle(letter_arr_one)
letter_arr_two = ['H','I','J','K','L','M','N']
random.shuffle(letter_arr_two)
letter_arr_three = ['O','P','Q','R','S','T','U']
random.shuffle(letter_arr_three)
letter_arr_four = ['V','W','V','X','Y','Z']
random.shuffle(letter_arr_four)
letter_arr_five = [0,1,2,3,4,5,6,7,8,9]
random.shuffle(letter_arr_five)

class Gcompris_braille_alphabets:
  """Empty gcompris python class"""


  def __init__(self, gcomprisBoard):
    # Save the gcomprisBoard, it defines everything we need
    # to know from the core
    #defining the number of levels in activity
    self.counter = 0
    self.gcomprisBoard = gcomprisBoard
    self.gcomprisBoard.level = 1
    self.gcomprisBoard.maxlevel=6
    self.gcomprisBoard.sublevel=1
    self.gcomprisBoard.number_of_sublevel=1

    #Boolean variable decaration
    self.mapActive = False

    # Needed to get key_press
    gcomprisBoard.disable_im_context = True

  def start(self):
    # Set the buttons we want in the bar
    gcompris.bar_set(gcompris.BAR_LEVEL)
    gcompris.bar_set_level(self.gcomprisBoard)

    pixmap = gcompris.utils.load_svg("braille_alphabets/target.svg")
    gcompris.bar_set_repeat_icon(pixmap)
    gcompris.bar_set(gcompris.BAR_LEVEL|gcompris.BAR_REPEAT_ICON)
    gcompris.bar_location(300,-1,0.6)

    # Create our rootitem. We put each canvas item in it so at the end we
    # only have to kill it. The canvas deletes all the items it contains
    # automaticaly.

    self.rootitem = goocanvas.Group(parent=
                                   self.gcomprisBoard.canvas.get_root_item())
    self.board_upper(self.gcomprisBoard.level)

  def end(self):
    # Remove the root item removes all the others inside it
    self.rootitem.remove()

  def ok(self):
    print("learnbraille ok.")

  def repeat(self):
      if(self.mapActive):
          self.end()
          self.start()
          self.mapActive = False
      else :
          self.rootitem.props.visibility = goocanvas.ITEM_INVISIBLE
          self.rootitem = goocanvas.Group(parent=
                                   self.gcomprisBoard.canvas.get_root_item())
          gcompris.set_default_background(self.gcomprisBoard.canvas.get_root_item())

115
          map_obj = BrailleMap(self.rootitem, COLOR_ON, COLOR_OFF,MAP_FILL, MAP_STROKE)
116 117 118 119 120
          #Move back item
          self.backitem = goocanvas.Image(parent = self.rootitem,
                    pixbuf = gcompris.utils.load_pixmap("braille_alphabets/back.png"),
                    x = 600,
                    y = 450,
121
                    tooltip = _("Move Back")
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
                    )
          self.backitem.connect("button_press_event", self.move_back)
          gcompris.utils.item_focus_init(self.backitem, None)

          self.mapActive = True


  def move_back(self,event,target,item):
      self.end()
      self.start()

  def config(self):
    print("learnbraille config.")

  def key_press(self, keyval, commit_str, preedit_str):
    utf8char = gtk.gdk.keyval_to_unicode(keyval)
    strn = u'%c' % utf8char
    print("Gcompris_learnbraille key press keyval=%i %s" % (keyval, strn))

  def pause(self,pause):
    if(pause == 0):
        self.counter +=1
        if (self.counter == self.sublevel):
            self.increment_level()
        self.end()
        self.start()

  def set_level(self,level):
    gcompris.sound.play_ogg("sounds/receive.wav")
    self.gcomprisBoard.level = level
    self.gcomprisBoard.sublevel = 1
    gcompris.bar_set_level(self.gcomprisBoard)
    self.end()
    self.start()

  def increment_level(self):
    self.counter = 0
    gcompris.sound.play_ogg("sounds/bleep.wav")
    self.gcomprisBoard.sublevel += 1

    if(self.gcomprisBoard.sublevel>self.gcomprisBoard.number_of_sublevel):
        self.gcomprisBoard.sublevel=1
        self.gcomprisBoard.level += 1
        if(self.gcomprisBoard.level > self.gcomprisBoard.maxlevel):
            self.gcomprisBoard.level = 1


  def board_upper(self,level):
    if(level == 1):
        gcompris.set_background(self.gcomprisBoard.canvas.get_root_item(),
                            "braille_alphabets/braille_tux.svgz")
        goocanvas.Text(parent=self.rootitem,
                                 x=390,
                                 y=100,
                                 fill_color="dark blue",
                                 font="Sans 15",
                                 anchor=gtk.ANCHOR_CENTER,
179
                                 text=_("Braille : Unlocking the Code"))
180 181 182 183 184 185 186 187 188
        #Braille Intro
        goocanvas.Text(parent=self.rootitem,
                                 x=520,
                                 y=200,
                                 fill_color="dark blue",
                                 font="Sans 15",
                                 width = 400,
                                 anchor=gtk.ANCHOR_CENTER,
                                 text=braille_intro)
189 190 191
        #Braille Description
        goocanvas.Text(parent=self.rootitem,
                                 x=520,
192
                                 y=300,
193 194
                                 fill_color="dark blue",
                                 font="Sans 15",
195
                                 width = 400,
196
                                 anchor=gtk.ANCHOR_CENTER,
197
                                 text=braille_desc)
198

199

200 201 202 203 204 205
        #TUX svghandle
        svghandle = gcompris.utils.load_svg("braille_alphabets/braille_tux.svgz")
        self.tuxitem = goocanvas.Svg(
                                     parent = self.rootitem,
                                     svg_handle = svghandle,
                                     svg_id = "#TUX-5",
206
                                     tooltip = _("I am braille TUX")
207 208 209 210 211 212 213 214 215 216
                                     )
        self.tuxitem.connect("button_press_event", self.next_level)
        gcompris.utils.item_focus_init(self.tuxitem, None)

        goocanvas.Text(parent = self.rootitem,
                        x = 410,
                        y= 430,
                        fill_color ="black",
                        font = "Sans 10",
                        anchor= gtk.ANCHOR_CENTER,
217 218 219
                        width = 300,
                        text = _("Finished reading braille ! Now click"
                        "me and try reproducing braille characters"))
220 221 222 223 224 225
    elif(level ==2):
        range_lower= 0
        range_upper= 7
        self.sublevel = range_upper - range_lower
        self.board_tile(range_lower,range_upper)
        self.random_letter = letter_arr_one[self.counter]
226
        self.braille_cell(level)
227 228 229 230 231 232 233

    elif(level == 3) :
        range_lower= 7
        range_upper= 14
        self.sublevel = range_upper - range_lower
        self.board_tile(range_lower,range_upper)
        self.random_letter = letter_arr_two[self.counter]
234
        self.braille_cell(level)
235 236 237 238 239 240 241

    elif(level == 4):
        range_lower= 14
        range_upper= 21
        self.sublevel = range_upper - range_lower
        self.board_tile(range_lower,range_upper)
        self.random_letter = letter_arr_three[self.counter]
242
        self.braille_cell(level)
243 244 245 246 247 248 249

    elif(level == 5):
        range_lower= 21
        range_upper= 26
        self.sublevel = range_upper - range_lower
        self.board_tile(range_lower,range_upper)
        self.random_letter = letter_arr_four[self.counter]
250
        self.braille_cell(level)
251 252 253 254 255 256 257

    elif(level == 6):
        range_lower= 0
        range_upper= 10
        self.sublevel = range_upper - range_lower
        self.board_number(range_lower,range_upper)
        self.random_letter = letter_arr_five[self.counter]
258 259
        self.braille_letter = "number"
        self.braille_cell(level)
260 261 262 263 264 265 266 267 268


  def next_level(self,event,target,item):
      self.increment_level()
      self.end()
      self.start()

  def board_tile(self,range_x,range_y):
      for i, letter in enumerate(string.ascii_uppercase[range_x:range_y]):
269 270 271
          tile = BrailleChar(self.rootitem, i*(CELL_WIDTH+60)+60,
                              80, 50, letter ,COLOR_ON ,COLOR_OFF ,CIRCLE_FILL,
                              CIRCLE_STROKE, True ,False ,True, None)
272 273
  def board_number(self,num_1,num_2):
      for letter in range(num_1,num_2):
274 275 276
          tile = BrailleChar(self.rootitem,letter *(CELL_WIDTH+30)+60,
                             80, 50, letter ,COLOR_ON ,COLOR_OFF ,CIRCLE_FILL,
                             CIRCLE_STROKE, True,False ,True, None)
277 278 279 280 281 282 283 284 285 286

  def display_letter(self,letter):
      goocanvas.Text(parent=self.rootitem,
                                 x=600,
                                 y=370,
                                 fill_color="blue",
                                 font="Sans 78",
                                 anchor=gtk.ANCHOR_CENTER,
                                 text=str(letter))

287 288 289 290 291 292
  def braille_cell(self, level):
      if (level == 6):
          self.letter = "number"
      else :
          self.letter = "alphabet"

293 294 295
      gcompris.set_background(self.gcomprisBoard.canvas.get_root_item(),
                            "braille_alphabets/mosaic.svgz")

296
      #Translators : Do not translate the token {letter}
297 298 299
      goocanvas.Text(parent = self.rootitem,
                     x = 100,
                     y = 200,
300
                     text=_("Click on the dots in braille cell area to produce letter {letter}").format(letter = self.random_letter),
301 302 303 304 305 306 307 308
                     fill_color="blue",
                     font='SANS 15')

      goocanvas.Text(parent=self.rootitem,
                      x=160.0, y=250.0,
                     text=_("Braille Cell"),
                     fill_color="blue",
                     font='Sans BOLD')
309
      BrailleChar(self.rootitem, 150, 270, 120, '',COLOR_ON ,COLOR_OFF,CIRCLE_FILL,CIRCLE_STROKE,
310
                   False,True,False,callback = self.letter_change , braille_letter = self.letter)
311 312 313 314 315 316 317 318 319 320 321 322 323
      for i in range(2):
          for j in range(3):
                  goocanvas.Text(parent=self.rootitem,
                                 text=(str(j + 1 + i * 3)),
                                 font='Sans 20',
                                 fill_color="blue",
                                 x=i * 120 + 140,
                                 y=j * 45 + 290)

      #OK Button
      ok = goocanvas.Svg(parent = self.rootitem,
                         svg_handle = gcompris.skin.svg_get(),
                         svg_id = "#OK",
324
                         tooltip = _("Click to confirm your selection of dots")
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
                         )
      ok.translate(30,-185)

      ok.connect("button_press_event", self.ok_event)
      gcompris.utils.item_focus_init(ok, None)

  def ok_event(self,item,target,event):
      if(self.random_letter == self.correct_letter):
          self.display_letter(self.correct_letter)
          gcompris.bonus.display(gcompris.bonus.WIN,gcompris.bonus.SMILEY)
      else :
          gcompris.bonus.display(gcompris.bonus.LOOSE,gcompris.bonus.SMILEY)


  def letter_change(self,letter):
SrishAkaTux's avatar
SrishAkaTux committed
340
      self.correct_letter = letter