louis_braille.py 11.3 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
#  gcompris - louis_braille.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/>.
#
# louis_braille activity.
import gtk
import gtk.gdk
import gcompris
import gcompris.utils
import gcompris.skin
24
import gcompris.bonus
25 26
import goocanvas
import pango
27
import random
28 29 30 31 32
import ConfigParser
from BrailleChar import *

from gcompris import gcompris_gettext as _

33 34 35
COLOR_ON = 0XFFFFFFFFL
COLOR_OFF= 0X000000FFL
CIRCLE_FILL = "white"
36 37 38
CELL_WIDTH = 30

LOUIS_BRAILLE_NAME = ['L','O','U','I','S','B','R','A','I','L','L','E']
39 40
NUMBERS = [12,13,14,15,16,17,18,19,20,21,22]
SEQUENCE = ['a','b','c','d','e','f','g','h','i','j','k']
41 42
NUMBER_SEQUENCE = [[1,'a'],[2,'b'],[3,'c'],[4,'d'],[5,'e'],[6,'f'],
                   [7,'g'],[8,'h'],[9,'i'],[10,'j'],[11,'k']]
43 44
random.shuffle(NUMBERS)
random.shuffle(NUMBER_SEQUENCE)
45 46 47 48 49 50 51 52 53 54 55 56

class Gcompris_louis_braille:
  """Empty gcompris python class"""


  def __init__(self, gcomprisBoard):
    # Save the gcomprisBoard, it defines everything we need
    # to know from the core
    self.gcomprisBoard = gcomprisBoard
    self.gcomprisBoard.level=1
    self.gcomprisBoard.sublevel=1
    self.gcomprisBoard.number_of_sublevel=1
57
    self.gcomprisBoard.maxlevel = 12
58 59 60 61 62 63 64 65 66 67

    # Needed to get key_press
    gcomprisBoard.disable_im_context = True

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

    # Set a background image
    gcompris.set_background(self.gcomprisBoard.canvas.get_root_item(),
68
                            "louis_braille/background.svgz")
69

70 71 72 73
    #Initialize variables
    self.won = 0
    self.counter = 0
    self.gamewon = 0
74 75
    self.offset_x = self.offset_y = 0
    self.init_coord = {}
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

    # 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.read_data()
    self.display_game(self.gcomprisBoard.level)

  def read_data(self):
    '''Load the activity data'''
    config = ConfigParser.RawConfigParser()
    p = gcompris.get_properties()
    filename = gcompris.DATA_DIR + '/' + self.gcomprisBoard.name + '/activity.desktop'
    try:
      gotit = config.read(filename)
      if not gotit:
         gcompris.utils.dialog(_("Cannot find the file '{filename}'").format(filename=filename),None)
         return False

    except ConfigParser.Error as error:
      gcompris.utils.dialog(_("Failed to parse data set '{filename}' with error:\n{error}").
                              format(filename=filename, error=error), None)
      return False

    self.dataset = config
    return True

  def display_game(self, level):
109
      if(level == 12):
110 111 112
          gcompris.bar_location(gcompris.BOARD_WIDTH - 120, -1, 0.8)

          for index in range(11):
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
              group_item = goocanvas.Group(parent = self.rootitem)

              # Create Rounded Rectangles for each story
              goocanvas.Rect(parent = group_item,
                             x = 100,
                             y = (index + 0.5) * 43,
                             width = 550,
                             height = 40,
                             radius_x = 17,
                             radius_y = 17,
                             stroke_color = "orange",
                             fill_color = "white",
                             line_width = 2.0)

              # Displaying the STORY
              goocanvas.Text(parent = group_item,
                             x = 370.0,
                             y = (index + 1) * 43,
                             text = str(self.dataset.get(str(NUMBER_SEQUENCE[index][0]),"_story")),
                             fill_color = "black",
                             anchor = gtk.ANCHOR_CENTER,
                             alignment = pango.ALIGN_CENTER,
                             font = 'SANS 9',
                             width = 500,
                             )
              # It is hard to manage focus when we move the item
              #gcompris.utils.item_focus_init(self.dragText, self.dragRect)
              group_item.connect("button_press_event", self.component_drag)
              group_item.connect("motion_notify_event", self.component_drag)
              group_item.connect("button_release_event", self.component_drag)
143 144


145 146 147
          ok = goocanvas.Svg(parent = self.rootitem,
                         svg_handle = gcompris.skin.svg_get(),
                         svg_id = "#OK",
148
                         tooltip = _("Click to confirm your sequence")
149
                         )
150
          ok.translate(200,-70)
151 152 153 154 155 156 157
          ok.connect("button_press_event", self.ok_event)
          gcompris.utils.item_focus_init(ok, None)


      else :
          gcompris.bar_location(gcompris.BOARD_WIDTH - 140, 350, 0.8)

158 159 160 161 162 163 164 165 166 167 168 169
          goocanvas.Rect( parent = self.rootitem,
                          x = 50,
                          y = 20,
                          width = 700,
                          height = 95,
                          radius_x = 10,
                          radius_y = 10,
                          stroke_color_rgba = 0x666666FFL,
                          fill_color_rgba = 0x33333333L,
                          line_width = 2.0)


170 171 172 173
          #Previous Button
          ok = goocanvas.Svg(parent = self.rootitem,
                         svg_handle = gcompris.skin.svg_get(),
                         svg_id = "#PREVIOUS",
174
                         tooltip = _("Click to move to previous story page")
175 176 177 178 179 180 181 182 183
                         )
          ok.translate(-300,50)
          ok.connect("button_press_event", self.previous_event, self.gcomprisBoard.level)
          gcompris.utils.item_focus_init(ok, None)

          #Next Button
          ok = goocanvas.Svg(parent = self.rootitem,
                         svg_handle = gcompris.skin.svg_get(),
                         svg_id = "#NEXT",
184
                         tooltip = _("Click to move to next story page")
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
                         )
          ok.translate(75,-58)
          ok.connect("button_press_event", self.next_event, self.gcomprisBoard.level)
          gcompris.utils.item_focus_init(ok, None)

          #Display name of louis braille
          for index in range(5):
              BrailleChar(self.rootitem,(index+1) *(CELL_WIDTH + 20)+30,
                             20, 50, LOUIS_BRAILLE_NAME[index] ,COLOR_ON, COLOR_OFF,
                             CIRCLE_FILL, CIRCLE_FILL, True,False ,False, None)
          for index in range(5,12):
              BrailleChar(self.rootitem,(index+1) *(CELL_WIDTH + 20)+65,
                             20, 50, LOUIS_BRAILLE_NAME[index] ,COLOR_ON, COLOR_OFF,
                             CIRCLE_FILL, CIRCLE_FILL,True,False ,False, None)

200
          story = self.dataset.get(str(level),"_story")
201

202 203 204 205 206 207 208 209 210 211
          #Rectangle for YEAR
          goocanvas.Rect(parent=self.rootitem,
                          x=330,
                          y=382,
                          width=180,
                          height=25,
                          stroke_color="orange",
                          fill_color="white",
                          line_width=2.0)

212 213 214
          #Displaying the YEAR
          goocanvas.Text(parent = self.rootitem,
                   x=420.0,
215
                   y=395.0,
216
                   text=str(self.dataset.get(str(level),"_year")),
217 218 219 220 221 222
                   fill_color="black",
                   anchor = gtk.ANCHOR_CENTER,
                   alignment = pango.ALIGN_CENTER,
                   font = 'SANS 17'
                   )

223 224 225 226 227 228 229 230 231 232
          #Rectangle for STORY
          goocanvas.Rect(parent=self.rootitem,
                          x=50,
                          y=415,
                          width=720,
                          height=80,
                          stroke_color="orange",
                          fill_color="white",
                          line_width=2.0)

233 234
          #Displaying the STORY
          goocanvas.Text(parent = self.rootitem,
235
                   x=400.0,
236
                   y=455.0,
237 238 239 240
                   text=str(story),
                   fill_color="black",
                   anchor = gtk.ANCHOR_CENTER,
                   alignment = pango.ALIGN_CENTER,
241
                   width = 700,
242 243 244 245 246 247 248 249 250 251
                   font = 'SANS 17'
                   )

          #Displaying the IMAGE
          goocanvas.Image(parent = self.rootitem,
                                 pixbuf = gcompris.utils.load_pixmap
                                 (str(self.dataset.get(str(level),"image"))),
                                 x = 300,
                                 y = 120,
                                 )
252 253

  def component_drag(self, widget, target, event):
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      groupitem = target.get_parent()
      groupitem.raise_(None)

      if event.type == gtk.gdk.BUTTON_PRESS:
        bounds = groupitem.get_bounds()
        self.offset_y = event.y

      elif ( event.type == gtk.gdk.MOTION_NOTIFY
             and event.state & gtk.gdk.BUTTON1_MASK ):
          groupitem.translate(0, event.y - self.offset_y)

      elif event.type == gtk.gdk.BUTTON_RELEASE:
        pass
        # Must find the closer stop to drop this item

      return True
270

271 272 273 274 275 276 277 278 279 280 281 282
  def enter_callback(self, event, widget, index):
      print self.widget_array[index].get_text()

  def ok_event(self, event ,target ,item):
      for index in range(11):
          if(self.widget_array[index].get_text() == NUMBER_SEQUENCE[index][1]):
              self.won +=1
      if (self.won == 11):
          gcompris.bonus.display(gcompris.bonus.WIN,gcompris.bonus.TUX)
          self.gamewon = 1
      else :
          gcompris.bonus.display(gcompris.bonus.LOOSE,gcompris.bonus.TUX)
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

  def previous_event(self, event, target,item, level):
      if (self.gcomprisBoard.level == 1):
          self.gcomprisBoard.level = self.gcomprisBoard.maxlevel
      else :
          self.gcomprisBoard.level = level - 1
      self.end()
      self.start()

  def next_event(self, event, target, item, level):
      if (self.gcomprisBoard.level == self.gcomprisBoard.maxlevel):
          self.gcomprisBoard.level = 1
      else :
          self.gcomprisBoard.level = level + 1
      self.end()
      self.start()

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

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


  def repeat(self):
    print("louis_braille repeat.")


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


  def key_press(self, keyval, commit_str, preedit_str):
    utf8char = gtk.gdk.keyval_to_unicode(keyval)
    strn = u'%c' % utf8char

  def pause(self, pause):
    print("louis_braille pause. %i" % pause)
322 323 324 325
    if(self.gamewon == 1):
        self.gcomprisBoard.level = 1
        self.end()
        self.start()
326 327

  def set_level(self, level):
328
    print("louis_braille set level. %i" % level)