-
Notifications
You must be signed in to change notification settings - Fork 8
/
PukllanapacActivity.py
191 lines (164 loc) · 6.94 KB
/
PukllanapacActivity.py
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
#Copyright (c) 2010 Walter Bender
# 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.
#
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the
# Free Software Foundation, 51 Franklin Street, Suite 500 Boston, MA
# 02110-1335 USA
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
import sugar3
from sugar3.activity import activity
try:
from sugar3.graphics.toolbarbox import ToolbarBox
_have_toolbox = True
except ImportError:
_have_toolbox = False
if _have_toolbox:
from sugar3.bundle.activitybundle import ActivityBundle
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.graphics.toolbarbox import ToolbarButton
from sugar3.datastore import datastore
from gettext import gettext as _
import locale
import os.path
from toolbar_utils import radio_factory, label_factory, separator_factory, \
button_factory
from window import Game
SERVICE = 'org.sugarlabs.PukllanapacActivity'
IFACE = SERVICE
PATH = '/org/augarlabs/PukllanapacActivity'
LEVEL_ICONS = ['level1', 'level2', 'level3']
GAME_ICONS = ['rectangle', 'hexagon', 'hexagon2']
class PukllanapacActivity(activity.Activity):
""" Sliding puzzle game """
def __init__(self, handle):
""" Initialize the toolbars and the game board """
super(PukllanapacActivity,self).__init__(handle)
self._play_level = 0
self._play_mode = 0
self._setup_toolbars(_have_toolbox)
# Create a canvas
canvas = Gtk.DrawingArea()
canvas.set_size_request(Gdk.Screen.width(), \
Gdk.Screen.height())
self.set_canvas(canvas)
canvas.show()
self.show_all()
self.win = Game(canvas, os.path.join(activity.get_bundle_path(),
'images'), self)
# Restore game state from Journal or start new game
if 'play_level' in self.metadata:
self.change_play_level_cb(play_level=int(
self.metadata['play_level']))
if 'play_mode' in self.metadata:
self.change_play_mode_cb(play_mode=int(
self.metadata['play_mode']))
grid = []
for i in range(24):
if 'card' + str(i) in self.metadata:
grid.append(int(self.metadata['card' + str(i)]))
self.win.grid.restore_grid(grid, self.win.mode)
for i in range(24):
if 'rotate' + str(i) in self.metadata:
self.win.grid.card_table[grid[i]].set_orientation(
int(self.metadata['rotate' + str(i)]))
self.win.mask(self._play_level)
def write_file(self, file_path):
""" Write the grid status to the Journal """
self.metadata['play_mode'] = self._play_mode
self.metadata['play_level'] = self._play_level
for i in range(24):
self.metadata['card' + str(i)] = str(self.win.grid.grid[i])
self.metadata['rotate' + str(i)] = str(
self.win.grid.card_table[self.win.grid.grid[i]].orientation)
def _setup_toolbars(self, have_toolbox):
""" Setup the toolbars.. """
if have_toolbox:
toolbox = ToolbarBox()
# Activity toolbar
activity_button = ActivityToolbarButton(self)
toolbox.toolbar.insert(activity_button, 0)
activity_button.show()
self.set_toolbar_box(toolbox)
toolbox.show()
toolbar = toolbox.toolbar
else:
# Use pre-0.86 toolbar design
games_toolbar = Gtk.Toolbar()
toolbox = activity.ActivityToolbox(self)
self.set_toolbox(toolbox)
toolbox.add_toolbar(_('Game'), games_toolbar)
toolbox.show()
toolbox.set_current_toolbar(1)
toolbar = games_toolbar
# Add the buttons and labels to the toolbars
self.level_button = button_factory(
LEVEL_ICONS[self._play_level], toolbar, self.change_play_level_cb,
tooltip=_('Set difficulty level.'))
mode = self._play_mode
mode += 1
if mode == len(GAME_ICONS):
mode = 0
self.game_buttons = []
for i in range(len(GAME_ICONS)):
if i==0:
self.game_buttons.append(radio_factory(
GAME_ICONS[0], toolbar, self.change_play_mode_cb,
cb_arg=0, tooltip=_('Select game.'), group=None))
else:
self.game_buttons.append(radio_factory(
GAME_ICONS[i], toolbar, self.change_play_mode_cb,
cb_arg=i, tooltip=_('Select game.'),
group=self.game_buttons[0]))
self.game_buttons[mode].set_active(True)
separator_factory(toolbar, False, True)
self.status_label = label_factory(toolbar, _("drag to swap"), width=85)
if _have_toolbox:
separator_factory(toolbox.toolbar, True, False)
stop_button = StopButton(self)
stop_button.props.accelerator = '<Ctrl>q'
toolbox.toolbar.insert(stop_button, -1)
stop_button.show()
def change_play_level_cb(self, button=None, play_level=None):
""" Cycle between levels """
if self._play_mode > 0:
return
if play_level is None:
self._play_level += 1
if self._play_level == len(LEVEL_ICONS):
self._play_level = 0
else:
self._play_level = play_level
self.level_button.set_icon_name(LEVEL_ICONS[self._play_level])
self.win.grid.reset(GAME_ICONS[self._play_mode])
self.win.mask(self._play_level)
def change_play_mode_cb(self, button=None, play_mode=None):
""" Cycle between game modes """
if play_mode is None:
self._play_mode += 1
if self._play_mode == len(GAME_ICONS):
self._play_mode = 0
else:
self._play_mode = play_mode
mode = self._play_mode
mode += 1
if mode == len(GAME_ICONS):
mode = 0
if hasattr(self, 'win'):
self.win.mode = GAME_ICONS[self._play_mode]
self.win.grid.initialize_cards(self.win.sprites, self.win.path,
self.win.card_dim, self.win.scale,
GAME_ICONS[self._play_mode])
if self._play_mode > 0:
self._play_level = len(LEVEL_ICONS) - 1
self.level_button.set_icon_name(LEVEL_ICONS[self._play_level])
self.win.mask(self._play_level)
self.win.grid.reset(GAME_ICONS[self._play_mode])