Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mainscreen.py
blob: 6c3735ad0bec7dce721b8440da4bfbd44cfc8406 (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Copyright 2008 by Kate Scheppke and Wade Brainerd.  
# This file is part of Typing Turtle.
#
# Typing Turtle 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.
# 
# Typing Turtle 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 Typing Turtle.  If not, see <http://www.gnu.org/licenses/>.

# Import standard Python modules.
import logging, os, math, time, copy, simplejson, locale, datetime, random, re, glob
from gettext import gettext as _

# Import PyGTK.
import gobject, pygtk, gtk, pango

# Import Sugar UI modules.
import sugar.activity.activity
from sugar.graphics import *

# Import activity modules.
import lessonscreen, medalscreen, balloongame
import titlescene
import keyboard

# Temporary SVGs of medals from Wikimedia Commons.
# See the links below for licensing information.
# http://commons.wikimedia.org/wiki/File:Gold_medal_world_centered.svg
# http://commons.wikimedia.org/wiki/File:Silver_medal_world_centered.svg
# http://commons.wikimedia.org/wiki/File:Bronze_medal_world_centered.svg

class MainScreen(gtk.VBox):
    def __init__(self, activity):
        gtk.VBox.__init__(self)
        
        self.activity = activity
        
        # Build background.
        self.titlescene = titlescene.TitleScene()
        
        # Build lessons list.
        self.lessonbox = gtk.HBox()
        
        #nexticon = sugar.graphics.icon.Icon(icon_name='go-next')
        #self.nextlessonbtn.add(nexticon)
        nextlabel = gtk.Label()
        nextlabel.set_markup("<span size='8000'>" + _('Next') + "</span>")

        self.nextlessonbtn = gtk.Button()
        self.nextlessonbtn.add(nextlabel)
        self.nextlessonbtn.connect('clicked', self.next_lesson_clicked_cb)
        
        #previcon = sugar.graphics.icon.Icon(icon_name='go-previous')
        #self.prevlessonbtn.add(previcon)
        prevlabel = gtk.Label()
        prevlabel.set_markup("<span size='8000'>" + _('Previous') + "</span>")

        self.prevlessonbtn = gtk.Button()
        self.prevlessonbtn.add(prevlabel)
        self.prevlessonbtn.connect('clicked', self.prev_lesson_clicked_cb)
        
        lessonlabel = gtk.Label()
        lessonlabel.set_markup("<span size='12000'>" + _('Start Lesson') + "</span>")
        
        lessonbtn = gtk.Button()
        lessonbtn.add(lessonlabel)
        lessonbtn.connect('clicked', self.lesson_clicked_cb)
        lessonbtn.modify_bg(gtk.STATE_NORMAL, self.get_colormap().alloc_color('#60b060'))
        
        # Load lessons for this language.
        code = locale.getdefaultlocale()[0]
        self.load_lessons('lessons/' + code)

        # Fallback to en_US lessons if none found.
        if not len(self.lessons):
            self.load_lessons('lessons/en_US')

        # We cannot run without lessons/
        if not len(self.lessons):
            sys.exit(1)

        # Sort by the 'order' field.
        self.lessons.sort(lambda x, y: x.get('order', 0) - y.get('order', 0))

        # Load all the keyboard images.
        width = int(gtk.gdk.screen_width())
        height = int(gtk.gdk.screen_height()*0.4)
        self.keyboard_images = keyboard.KeyboardImages(width, height)
        self.keyboard_images.load_images()
        
        navbox = gtk.HBox()
        navbox.set_spacing(10)
        navbox.pack_start(self.prevlessonbtn, True)
        navbox.pack_start(lessonbtn, True)
        navbox.pack_start(self.nextlessonbtn, True)
        
        lessonbox = gtk.VBox()
        lessonbox.set_spacing(10)
        lessonbox.pack_start(navbox, False)
        lessonbox.pack_start(self.lessonbox)
        
        self.pack_start(self.titlescene, False, True, 10)
        self.pack_start(lessonbox, True)
        
        self.show_next_lesson()

    def load_lessons(self, path):
        # Find all .lesson files in ./lessons/en_US/ for example.
        self.lessons = []
        for f in glob.iglob(path + '/*.lesson'):
            fd = open(f, 'r')
            try:
                lesson = simplejson.loads(fd.read())
                self.lessons.append(lesson)
            finally:
                fd.close()

    def get_next_lesson(self):
        """Returns the index of the first lesson without a medal."""
        index = len(self.lessons)-1
        for i in xrange(0, len(self.lessons)):
            if self.lessons[i]['order'] >= 0 and \
               not self.activity.data['medals'].has_key(self.lessons[i]['name']):
                index = min(index, i)
        return index
    
    def show_next_lesson(self):
        """Displays the first lesson which the user can activate that does not yet have a medal."""
        self.show_lesson(self.get_next_lesson())
    
    def show_lesson(self, index):
        # Clear all widgets in the lesson box.
        for w in self.lessonbox:
            self.lessonbox.remove(w)
        
        self.prevlessonbtn.set_sensitive(index > 0)
        self.nextlessonbtn.set_sensitive(index < len(self.lessons)-1)
        
        lesson = self.lessons[index]
        
        self.lesson_index = index
        self.visible_lesson = lesson

        medal_type = 'none'
        if self.activity.data['medals'].has_key(lesson['name']):
            medal_type = self.activity.data['medals'][lesson['name']]['type']
        
        # Create the lesson button.
        namelabel = gtk.Label()
        namelabel.set_alignment(0.5, 0.5)
        namelabel.set_markup("<span size='20000'><b>" + lesson['name'] + "</b></span>")
        desclabel = gtk.Label()
        desclabel.set_alignment(0.5, 0.5)
        desclabel.set_markup("<span size='10000' color='#606060'>" + lesson['description'] + "</span>")
        
        if medal_type != 'none':
            hint = _('You earned a medal in this lesson!  Advance to the next one\nby clicking the Next button.')
        else:
            hint = ''
                
        #hintlabel = gtk.Label()
        #hintlabel.set_alignment(0.0, 0.8)
        #hintlabel.set_markup("<span size='8000' color='#606020'>" + hint + "</span>")
        
        labelbox = gtk.VBox()
        labelbox.set_spacing(10)
        labelbox.set_border_width(20)
        labelbox.pack_start(namelabel, False)
        labelbox.pack_start(desclabel, False)
        #labelbox.pack_start(hintlabel)

        # Create the medal image.
        images = {
            'none':   'images/no-medal.svg',
            'bronze': 'images/bronze-medal.svg',
            'silver': 'images/silver-medal.svg',
            'gold':   'images/gold-medal.svg'
        }
        medalpixbuf = gtk.gdk.pixbuf_new_from_file(images[medal_type])
        medalpixbuf = medalpixbuf.scale_simple(200, 200, gtk.gdk.INTERP_BILINEAR)
        
        medalimage = gtk.Image()
        medalimage.set_from_pixbuf(medalpixbuf)
        
        names = {
            'none':   _('No Medal Yet'),
            'bronze': _('Bronze Medal'),
            'silver': _('Silver Medal'),
            'gold':   _('Gold Medal'),
        }
        medallabel = gtk.Label(names[medal_type])
        
        medalbox = gtk.VBox()
        medalbox.pack_start(medalimage)
        medalbox.pack_start(medallabel)
        
        medalbtn = gtk.Button()
        medalbtn.add(medalbox)
        medalbtn.connect('clicked', self.medal_clicked_cb)
        
        # Hilite the button in the direction of the first unmedaled lesson.
        next_index = self.get_next_lesson()
        if next_index > self.lesson_index:
            self.nextlessonbtn.modify_bg(gtk.STATE_NORMAL, self.get_colormap().alloc_color('#ff8080'))
        else:
            self.nextlessonbtn.modify_bg(gtk.STATE_NORMAL, self.get_colormap().alloc_color('#40a040'))
        if next_index < self.lesson_index:
            self.prevlessonbtn.modify_bg(gtk.STATE_NORMAL, self.get_colormap().alloc_color('#ff8080'))
        else:
            self.prevlessonbtn.modify_bg(gtk.STATE_NORMAL, self.get_colormap().alloc_color('#40a040'))
        
        self.lessonbox.pack_start(labelbox, True)
        if medal_type != 'none':
            self.lessonbox.pack_start(medalbtn, False)

        self.lessonbox.show_all()
    
    def next_lesson_clicked_cb(self, widget):
        self.show_lesson(self.lesson_index+1)
    
    def prev_lesson_clicked_cb(self, widget):
        self.show_lesson(self.lesson_index-1)
    
    def lesson_clicked_cb(self, widget):
        if self.visible_lesson['type'] == 'balloon':
            reload(balloongame)
            self.activity.push_screen(balloongame.BalloonGame(self.visible_lesson, self.activity))
        else:
            reload(lessonscreen)
            self.activity.push_screen(lessonscreen.LessonScreen(self.visible_lesson, self.keyboard_images, self.activity))
    
    def medal_clicked_cb(self, widget):
        if self.activity.data['medals'].has_key(self.visible_lesson['name']):
            medal = self.activity.data['medals'][self.visible_lesson['name']]
            self.activity.push_screen(medalscreen.MedalScreen(medal, self.activity))