Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity.py
blob: f2d9558cb3e2ff730ea7e3d6074dbe65a50f2a7f (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# -*- coding: UTF-8 -*-
# Copyright 2011 Gonzalo Odiard, Manuel QuiƱones
#
# 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

from gi.repository import Gtk
from gi.repository import GObject
import logging

from gettext import gettext as _

from sugar3.activity import activity
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.radiotoolbutton import RadioToolButton
from sugar3.graphics.toggletoolbutton import ToggleToolButton

from model import GameModel
from resources import CollectResourcesWin
from questions import PrepareQuestionsWin
from editmap import EditMapWin
from mapnav import MapNavView
from game_map import GameMap
from dialogs import ResourceDialog, QuestionDialog

PLAY_MODE = 0
EDIT_MODE = 1

EDIT_RESOURCES_ACTION = 1
EDIT_QUESTIONS_ACTION = 2
EDIT_MAP_ACTION = 3
EDIT_DESCRIPTIONS_ACTION = 4


class IngeniumMachinaActivity(activity.Activity):
    """IngeniumMachinaActivity class as specified in activity.info"""

    def __init__(self, handle):
        self.model = GameModel()

        activity.Activity.__init__(self, handle)

        # we do not have collaboration features
        # make the share option insensitive
        self.max_participants = 1

        self.toolbar_box = ToolbarBox()

        activity_button = ActivityToolbarButton(self)
        self.toolbar_box.toolbar.insert(activity_button, 0)

        self._edit_button = ToggleToolButton('view-source')
        self._edit_button.set_tooltip(_('Edit game'))
        self._edit_button.set_active(False)
        self.toolbar_box.toolbar.insert(self._edit_button, -1)

        self._edit_button.connect('toggled', self.__change_mode_cb)

        tool_group = None

        self._resources_button = self._insert_radio(tool_group, 'action_1',
                _('Collect information'))
        self._resources_button.connect('clicked', self.__resources_button_cb)
        tool_group = self._resources_button

        self._questions_button = self._insert_radio(tool_group, 'action_2',
                _('Prepare questions'))
        self._questions_button.connect('clicked', self.__questions_button_cb)

        self._map_button = self._insert_radio(tool_group, 'action_3',
                _('Construct map'))
        self._map_button.connect('clicked', self.__map_button_cb)

        self._descriptions_button = self._insert_radio(tool_group, 'action_4',
                _('Write descriptions'))
        self._descriptions_button.connect('clicked',
                self.__descriptions_button_cb)

        self.toolbar_box.toolbar.insert(Gtk.SeparatorToolItem(), -1)

        self._add_button = ToolButton('add')
        self._add_button.connect('clicked', self.__add_cb)
        self.toolbar_box.toolbar.insert(self._add_button, -1)

        self._remove_button = ToolButton('remove')
        self._remove_button.connect('clicked', self.__remove_cb)
        self.toolbar_box.toolbar.insert(self._remove_button, -1)

        separator = Gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)
        self.toolbar_box.toolbar.insert(separator, -1)
        separator.show()

        stop_button = StopButton(self)
        self.toolbar_box.toolbar.insert(stop_button, -1)
        stop_button.show()

        self.set_toolbar_box(self.toolbar_box)
        self.toolbar_box.show_all()

        # init edition windows
        self.collect_resources_win = None
        self.prepare_questions_win = None
        self.edit_map_win = None
        self.edit_descriptions_win = None

        # init game
        self.activity_mode = PLAY_MODE
        self.action = EDIT_RESOURCES_ACTION
        self.update_buttons_state()
        self.main_notebook = Gtk.Notebook()

        self.main_notebook.set_show_tabs(False)
        self.main_notebook.show_all()
        self.set_canvas(self.main_notebook)

        if handle.object_id is None:
            self.main_notebook.append_page(self.create_play_view(), None)

    def create_play_view(self):
        if not 'map_data' in self.model.data or \
            self.model.data['map_data'] is None:
            self.game_map = GameMap()
        else:
            self.game_map = GameMap(self.model.data['map_data'])
        self.mapnav_game = MapNavView(self.game_map, self.model)
        self.mapnav_game.show()
        self.mapnav_game.connect('resource-clicked',
                self.__resource_clicked_cb)
        self.mapnav_game.connect('question-clicked',
                self.__question_clicked_cb)
        return self.mapnav_game

    def __change_mode_cb(self, button):
        if button.get_active():
            self.activity_mode = EDIT_MODE
            if self.action == EDIT_RESOURCES_ACTION:
                self.__resources_button_cb(self._resources_button)
            elif self.action == EDIT_QUESTIONS_ACTION:
                self.__questions_button_cb(self._questions_button)
            elif self.action == EDIT_DESCRIPTIONS_ACTION:
                self.__descriptions_button_cb(self._descriptions_button)
            else:
                self.main_notebook.set_current_page(0)

        else:
            self.activity_mode = PLAY_MODE
            self.main_notebook.set_current_page(0)
            self.mapnav_game.clear_cache()
        self.update_buttons_state()

    def __add_cb(self, button):
        if self.action is None:
            return
        if self.action == EDIT_RESOURCES_ACTION:
            self.collect_resources_win.add_resource()
        elif self.action == EDIT_QUESTIONS_ACTION:
            self.prepare_questions_win.add_question()
        elif self.action == EDIT_MAP_ACTION:
            self.edit_map_win.add_selected_object()

    def __remove_cb(self, button):
        if self.action is None:
            return
        if self.action == EDIT_RESOURCES_ACTION:
            self.collect_resources_win.del_resource()
        elif self.action == EDIT_QUESTIONS_ACTION:
            self.prepare_questions_win.del_question()
        elif self.action == EDIT_MAP_ACTION:
            self.edit_map_win.remove_selected_object()

    def update_buttons_state(self):
        edit_mode = self.activity_mode == EDIT_MODE
        self._resources_button.set_sensitive(edit_mode)
        self._questions_button.set_sensitive(edit_mode)
        self._map_button.set_sensitive(edit_mode)
        self._descriptions_button.set_sensitive(edit_mode)
        self._add_button.set_sensitive(edit_mode)
        self._remove_button.set_sensitive(edit_mode)

    def _insert_radio(self, group, icon_name, label):
        button = RadioToolButton()
        button.props.group = group
        button.props.icon_name = icon_name
        button.set_tooltip(label)
        self.toolbar_box.toolbar.insert(button, -1)
        return button

    def __questions_button_cb(self, button):
        if self.prepare_questions_win is None:
            self.prepare_questions_win = PrepareQuestionsWin(self)
            button.page = self.main_notebook.get_n_pages()
            self.main_notebook.append_page(self.prepare_questions_win, None)
            self.prepare_questions_win.connect('question_updated',
                    self.__question_updated_cb)
        self.main_notebook.set_current_page(button.page)
        self.action = EDIT_QUESTIONS_ACTION

    def __resources_button_cb(self, button):
        if self.collect_resources_win is None:
            self.collect_resources_win = CollectResourcesWin(self)
            button.page = self.main_notebook.get_n_pages()
            self.main_notebook.append_page(self.collect_resources_win, None)
            # connect signal to know if the resources are updated
            self.collect_resources_win.connect('resource_updated',
                    self.__resources_updated_cb)
        self.main_notebook.set_current_page(button.page)
        self.action = EDIT_RESOURCES_ACTION

    def __resources_updated_cb(self, origin):
        logging.error('** Resources updated signal')
        if self.edit_map_win is not None:
            self.edit_map_win.load_resources_and_questions()

    def __question_updated_cb(self, origin):
        logging.error('** Questions updated signal')
        if self.edit_map_win is not None:
            self.edit_map_win.load_resources_and_questions()

    def __map_button_cb(self, button):
        if self.edit_map_win is None:
            self.edit_map_win = EditMapWin(self.model)
            button.page = self.main_notebook.get_n_pages()
            self.main_notebook.append_page(self.edit_map_win, None)

            # Try connect with the playing map
            logging.error('Connecting signal map-updated')
            self.edit_map_win.nav_view.connect('map-updated',
                self.mapnav_game.receive_update_wall_info)

        self.main_notebook.set_current_page(button.page)
        self.action = EDIT_MAP_ACTION

    def __descriptions_button_cb(self, button):
        if self.edit_descriptions_win is None:
            self.edit_descriptions_win = Gtk.HBox()

            button.page = self.main_notebook.get_n_pages()
            self.main_notebook.append_page(self.edit_descriptions_win, None)
            # connect signal to know if the resources are updated

        self.main_notebook.set_current_page(button.page)
        self.action = EDIT_DESCRIPTIONS_ACTION

    def __resource_clicked_cb(self, mapnav, id_resource):
        logging.error('** Resource %s clicked', id_resource)
        resource_dialog = ResourceDialog(self.model, id_resource)
        resource_dialog.set_transient_for(self.get_toplevel())
        resource_dialog.show_all()

    def __question_clicked_cb(self, mapnav, id_question):
        logging.error('** Question %s clicked', id_question)
        question_dialog = QuestionDialog(self.model, id_question)
        question_dialog.set_transient_for(self.get_toplevel())
        question_dialog.connect('reply-selected', self.__question_replied_cb)
        question_dialog.show_all()
        self.model.register_displayed_question(id_question)

    def __question_replied_cb(self, dialog, id_question, valid):
        logging.error('** Question %s replied %s', id_question, valid)
        if valid:
            self.model.register_replied_question(id_question)

    def read_file(self, file_path):
        '''Read file from Sugar Journal.'''
        logging.error('READING FILE %s', file_path)
        self.model.read(file_path)
        self.main_notebook.append_page(self.create_play_view(), None)

    def write_file(self, file_path):
        '''Save file on Sugar Journal. '''
        logging.error('WRITING FILE %s', file_path)
        self.metadata['mime_type'] = 'application/x-ingenium-machine'
        self.model.write(file_path)


class CollectInformationWin(Gtk.VBox):

    def __init__(self):
        GObject.GObject.__init__(self)
        self.introduction = Gtk.Label(label=_('Edit your Adventure Game in 4 steps'))
        self.pack_start(self.introduction, False)