Back to index

gcompris  8.2.2
oscar_and_friends.py
Go to the documentation of this file.
00001 # Oscar_and_friends Board module
00002 import gnomecanvas
00003 import gcompris
00004 import gcompris.utils
00005 import gcompris.skin
00006 import gcompris.admin
00007 import gtk
00008 import gtk.gdk
00009 import random
00010 
00011 from gcompris import gcompris_gettext as _
00012 
00013 class Gcompris_oscar_and_friends:
00014   """Testing gcompris python class"""
00015 
00016 
00017   def __init__(self, gcomprisBoard):
00018     self.gcomprisBoard = gcomprisBoard
00019     self.next_item = None
00020     self.back_item = None
00021     self.text_item = None
00022 
00023     self.current_texts = None
00024     self.current_texts_index = 0
00025 
00026     #initialisation to default values. Some of them will be replaced by
00027     #the configured values.
00028 
00029   def start(self):
00030     print("Gcompris_oscar_and_friends start.")
00031 
00032     gcompris.bar_set(0)
00033     # Create our rootitem. We put each canvas item in it so at the end we
00034     # only have to kill it. The canvas deletes all the items it contains automaticaly.
00035     self.rootitem = self.gcomprisBoard.canvas.root().add(
00036       gnomecanvas.CanvasGroup,
00037       x=0.0,
00038       y=0.0
00039       )
00040 
00041     self.display_book_control()
00042     self.display_scene_1_0()
00043 
00044 
00045   def end(self):
00046     # Remove the root item removes all the others inside it
00047     self.rootitem.destroy()
00048 
00049   def ok(self):
00050     print("Gcompris_oscar_and_friends ok.")
00051 
00052 
00053   def repeat(self):
00054     print("Gcompris_oscar_and_friends repeat.")
00055 
00056 
00057   def config(self):
00058     print("Gcompris_oscar_and_friends config.")
00059 
00060 
00061   def key_press(self, keyval, commit_str, preedit_str):
00062     print("Gcompris_oscar_and_friends key press. %i %s" % (keyval, str))
00063 
00064     # Return  True  if you did process a key
00065     # Return  False if you did not processed a key
00066     #         (gtk need to send it to next widget)
00067     return True
00068 
00069   def pause(self, pause):
00070     print("Gcompris_oscar_and_friends pause. %i" % pause)
00071 
00072 
00073   def set_level(self, level):
00074     print("Gcompris_oscar_and_friends set level. %i" % level)
00075 
00076   # ---- End of Initialisation
00077 
00078   def display_book_control(self):
00079     self.back_item = self.rootitem.add(
00080       gnomecanvas.CanvasPixbuf,
00081       pixbuf = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin("button_backward.png")),
00082       x = gcompris.BOARD_WIDTH - 100,
00083       y = gcompris.BOARD_HEIGHT - 40
00084       )
00085     self.back_item.hide()
00086     self.back_item.connect("event", gcompris.utils.item_event_focus)
00087     self.back_item.connect("event", self.back_item_event)
00088 
00089     self.next_item = self.rootitem.add(
00090       gnomecanvas.CanvasPixbuf,
00091       pixbuf = gcompris.utils.load_pixmap(gcompris.skin.image_to_skin("button_forward.png")),
00092       x = gcompris.BOARD_WIDTH - 50,
00093       y = gcompris.BOARD_HEIGHT - 40
00094       )
00095     self.next_item.hide()
00096     self.next_item.connect("event", gcompris.utils.item_event_focus)
00097     self.next_item.connect("event", self.next_item_event)
00098 
00099     self.text_item = self.rootitem.add(
00100       gnomecanvas.CanvasText,
00101       x=gcompris.BOARD_WIDTH * 0.45,
00102       y=gcompris.BOARD_HEIGHT - 40.0,
00103       text="",
00104       font=gcompris.skin.get_font("gcompris/content"),
00105       fill_color="white",
00106       justification=gtk.JUSTIFY_CENTER
00107       )
00108     self.text_item.hide()
00109 
00110   def display_scene_1_0(self):
00111       gcompris.set_background(self.gcomprisBoard.canvas.root(),
00112                               "oscar_and_friends/scene1_0.png")
00113 
00114       self.display_texts_init(
00115         [
00116         _("Below the surface, Oscar the octopus and his friends sat on a rock,\nwatching the bad weather above them."),
00117         _("They were all happy that the storms never reached all the way down to them."),
00118         _("No matter how bad it was up there, the bottom was always calm and quiet."),
00119         _("Oscar's best friends were Charlie the crab and Sandy the sea-star.")
00120         ]
00121         )
00122 
00123 
00124   def display_texts_init(self, texts):
00125     self.current_texts = texts
00126     self.current_texts_index = 0
00127     self.text_item.set(text=self.current_texts[self.current_texts_index])
00128     self.display_back_previous_buttons()
00129     self.text_item.show()
00130 
00131 
00132   def display_next_text(self):
00133     self.current_texts_index += 1
00134     if(self.current_texts_index < len(self.current_texts)):
00135       self.text_item.set(text=self.current_texts[self.current_texts_index])
00136       self.display_back_previous_buttons()
00137 
00138   def display_previous_text(self):
00139     self.current_texts_index -= 1
00140     if(self.current_texts_index >= 0):
00141       self.text_item.set(text=self.current_texts[self.current_texts_index])
00142       self.display_back_previous_buttons()
00143 
00144   def display_back_previous_buttons(self):
00145     if(self.current_texts_index>0):
00146       self.back_item.show()
00147     else:
00148       self.back_item.hide()
00149 
00150     if(self.current_texts_index<len(self.current_texts)-1):
00151       self.next_item.show()
00152     else:
00153       self.next_item.hide()
00154 
00155   def back_item_event(self, widget, event=None):
00156     if event.type == gtk.gdk.BUTTON_PRESS:
00157       if event.button == 1:
00158         self.display_previous_text()
00159         return True
00160     return False
00161 
00162   def next_item_event(self, widget, event=None):
00163     if event.type == gtk.gdk.BUTTON_PRESS:
00164       if event.button == 1:
00165         self.display_next_text()
00166         return True
00167     return False
00168