Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/memorizetoolbar.py
blob: 0c9df062bfdc69e1288d9c69e48d617c556948c7 (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
#    Copyright (C) 2006, 2007, 2008 One Laptop Per Child
#
#    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 2 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, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import gobject
from os.path import join, dirname

from gettext import gettext as _
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.toolcombobox import ToolComboBox
from sugar.graphics.alert import Alert
from sugar.graphics.icon import Icon
from sugar.activity.widgets import RadioMenuButton
from sugar.graphics.menuitem import MenuItem

import logging
from gobject import SIGNAL_RUN_FIRST, TYPE_PYOBJECT

#ARIEL
import gtk
from sugar.activity.activity import get_bundle_path
import os
import datetime

_logger = logging.getLogger('memorize-activity')


class MemorizeToolbarBuilder(gobject.GObject):

    __gtype_name__ = 'MemoryToolbarBuilder'

    standard_game_names = ['addition',
                           'letters',
                           'sounds'
                           ]
    translated_game_names = [_('Addition'),
                             _('Letters'),
                             _('Sounds')
                            ]

    __gsignals__ = {
    'game_changed': (SIGNAL_RUN_FIRST, None, 5 * [TYPE_PYOBJECT]),
    'game_saved': (SIGNAL_RUN_FIRST, None, (gobject.TYPE_STRING,)),
    'game_selected': (SIGNAL_RUN_FIRST, None, (gobject.TYPE_STRING,))
    }

    def __init__(self, activity):
        gobject.GObject.__init__(self)
        self.activity = activity
        self.toolbar = self.activity.get_toolbar_box().toolbar
        self.jobject = None

        # Change demo games button
        self._demo_games = RadioMenuButton(icon_name='memorize-collection')
        #ARIEL self._demo_games.props.tooltip = _('Load demo games')
        self._demo_games.props.tooltip = _('Load/Save games')

        for i, game in enumerate(self.translated_game_names):
            menu_item = MenuItem(game)
            menu_item.connect('activate', self.__activate_game_cb, i)
            self._demo_games.props.palette.menu.append(menu_item)
            menu_item.show()

        #ARIEL list saved games
        datadir = get_bundle_path() + "/data"
        for filename in os.listdir(datadir):
            if not filename.endswith(".json"):
                continue

            menu_item = MenuItem(filename)
            menu_item.connect('activate', self.__activate_saved_game_cb, filename)
            self._demo_games.props.palette.menu.append(menu_item)
            menu_item.show()


        #ARIEL Add Save game menu item
        separator = gtk.SeparatorMenuItem()
        self._demo_games.props.palette.menu.append(separator)
        separator.show()
        menu_item = MenuItem("Save current")
        menu_item.connect('activate', self.__save_game_cb, i+1)
        self._demo_games.props.palette.menu.append(menu_item)
        menu_item.show()

        self.toolbar.insert(self._demo_games, -1)

        # Change size combobox
        self._size_combo = ToolComboBox()
        self._sizes = ['4 X 4', '5 X 5', '6 X 6']
        for i, f in enumerate(self._sizes):
            self._size_combo.combo.append_item(i, f)
        self.size_handle_id = self._size_combo.combo.connect( \
                'changed', self._game_size_cb)
        self.toolbar.insert(self._size_combo, -1)
        self._size_combo.combo.set_active(0)

        # Reset Button
        self._restart_button = ToolButton('game-new')
        self._restart_button.connect('clicked', self._game_reset_cb)
        self._restart_button.set_tooltip(_('Restart Game'))
        self.toolbar.insert(self._restart_button, -1)
        self._restart_button.show()

    def _game_reset_cb(self, widget):
        self.activity.game.model.count = 0
        self.emit('game_changed', None, None, 'reset', None, None)

    def update_controls(self, active):
        self._size_combo.set_sensitive(active)
        self._demo_games.set_sensitive(active)
        self._restart_button.set_sensitive(active)

    def _game_size_cb(self, widget):
        game_size = int(self._sizes[self._size_combo.combo.get_active()][0])
        self.emit('game_changed', None, game_size, 'size', None, None)

    def __save_game_cb(self, menu, i):
        """ARIEL Save current game
        """
        # Create data dir if not exists
        datadir = get_bundle_path() + "/data"
        if not os.path.exists(datadir):
            os.mkdir(datadir)

        # Build file name based on datetime
        filename = "memorize_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ".json"
        filepath = datadir + "/" + filename

        # Get game matrix
        self.emit('game_saved',filepath)

        #~ # Write the file
        #~ f = open ( filepath, "w" )
        #~ f.close()

    #ARIEL click in the menu item handler
    def __activate_saved_game_cb(self, menu, filename):
        self.emit('game_selected',filename)

    def __activate_game_cb(self, menu, i):
        self._game_selected_index = i
        if self.activity.game.model.is_demo:
            self._change_game()
        else:
            alert = Alert()
            alert.props.title = _('Discard your modified game?')
            icon = Icon(icon_name='dialog-ok')
            alert.add_button(1, _('Discard'), icon)
            icon = Icon(icon_name='dialog-cancel')
            alert.add_button(0, _('Do not discard'), icon)
            alert.connect('response', self._change_game_alert_cb)
            self.activity.add_alert(alert)

    def _change_game_alert_cb(self, alert, response_id):
        if alert is not None:
            self.activity.remove_alert(alert)
        if response_id == 1:
            self._change_game()

    def _change_game(self):
        title = self.translated_game_names[self._game_selected_index]
        game_size = int(self._sizes[self._size_combo.combo.get_active()][0])

        game_name = self.standard_game_names[self._game_selected_index]

        game_file = join(dirname(__file__), 'demos', game_name + '.zip')
        self.emit('game_changed', game_file, game_size, 'demo', title, None)

    def update_toolbar(self, widget, data, grid):
        size = data.get('size')
        self._size_combo.combo.handler_block(self.size_handle_id)
        size_index = self._sizes.index(size + ' X ' + size)
        self._size_combo.combo.set_active(int(size_index))
        self._size_combo.combo.handler_unblock(self.size_handle_id)