Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Bender <walter@sugarlabs.org>2010-10-25 22:15:19 (GMT)
committer Walter Bender <walter@sugarlabs.org>2010-10-25 22:15:19 (GMT)
commit44e95e30d320349cf3fa6d3e8b14647ee82f916a (patch)
tree4e7a15e874c4940676eb6d40dcd9f469863e8f6e
parent1b7c082f39dc6763899cc2bbd6c41ccd5432c006 (diff)
improved workflow for loading custom images
-rw-r--r--VisualMatchActivity.py184
-rw-r--r--constants.py45
-rw-r--r--deck.py18
-rw-r--r--game.py196
-rw-r--r--grid.py3
5 files changed, 279 insertions, 167 deletions
diff --git a/VisualMatchActivity.py b/VisualMatchActivity.py
index 4474427..81f7f96 100644
--- a/VisualMatchActivity.py
+++ b/VisualMatchActivity.py
@@ -24,9 +24,8 @@ try:
except ImportError:
_new_sugar_system = False
from sugar.graphics.toolbutton import ToolButton
-from sugar.graphics.objectchooser import ObjectChooser
+
from sugar.datastore import datastore
-from sugar import mime
import telepathy
from dbus.service import signal
@@ -123,36 +122,6 @@ def _separator_factory(toolbar, visible=True, expand=False):
_separator.show()
-def _find_the_number_in_the_name(name):
- """ Find which element in an array (journal entry title) is a number """
- parts = name.split('.')
- before = ''
- after = ''
- for i in range(len(parts)):
- ii = len(parts) - i - 1
- try:
- int(parts[ii])
- for j in range(ii):
- before += (parts[j] + '.')
- for j in range(ii + 1, len(parts)):
- after += ('.' + parts[j])
- return before, after, ii
- except ValueError:
- pass
- return '', '', -1
-
-
-def _construct_a_name(before, i, after):
- """ Make a numbered file name from parts """
- name = ''
- if before != '':
- name += before
- name += str(i)
- if after != '':
- name += after
- return name
-
-
class VisualMatchActivity(activity.Activity):
""" Dimension matching game """
def __init__(self, handle):
@@ -161,7 +130,7 @@ class VisualMatchActivity(activity.Activity):
self._read_journal_data()
self._old_sugar_system = _old_sugar_system
- datapath = self._find_datapath()
+ datapath = self._find_datapath(activity)
self._setup_toolbars(_new_sugar_system)
canvas = self._setup_canvas(datapath)
self._setup_presence_service()
@@ -169,18 +138,23 @@ class VisualMatchActivity(activity.Activity):
if not hasattr(self, '_saved_state'):
self._saved_state = None
self.vmw.new_game(self._saved_state, self._deck_index)
+
if self._editing_word_list:
self.vmw.editing_word_list = True
self.vmw.edit_word_list()
+ elif self._editing_custom_cards:
+ self.vmw.editing_custom_cards = True
+ self.vmw.edit_custom_card()
def _select_game_cb(self, button, card_type):
""" Choose which game we are playing. """
if self.vmw.joiner(): # joiner cannot change level
return
self.vmw.card_type = card_type
- if card_type == 'custom' and len(self.vmw.custom_paths) < 9:
- self._import_cb()
- self.vmw.new_game()
+ if card_type == 'custom' and self.vmw.custom_paths[0] is None:
+ self.image_import_cb()
+ else:
+ self.vmw.new_game()
def _robot_cb(self, button):
""" Toggle robot assist on/off """
@@ -219,76 +193,12 @@ class VisualMatchActivity(activity.Activity):
int(low_score[play_level] / 60),
int(low_score[play_level] % 60))
- def _import_cb(self, button=None):
+ def image_import_cb(self, button=None):
""" Import custom cards from the Journal """
- chooser = None
- name = None
- if hasattr(mime, 'GENERIC_TYPE_IMAGE'):
- # See #2398
- if 'image/svg+xml' not in \
- mime.get_generic_type(mime.GENERIC_TYPE_IMAGE).mime_types:
- mime.get_generic_type(
- mime.GENERIC_TYPE_IMAGE).mime_types.append('image/svg+xml')
- chooser = ObjectChooser(parent=self,
- what_filter=mime.GENERIC_TYPE_IMAGE)
- else:
- try:
- chooser = ObjectChooser(parent=self, what_filter=None)
- except TypeError:
- chooser = ObjectChooser(None, self,
- gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
-
- if chooser is not None:
- try:
- result = chooser.run()
- if result == gtk.RESPONSE_ACCEPT:
- jobject = chooser.get_selected_object()
- if jobject and jobject.file_path:
- name = jobject.metadata['title']
- mime_type = jobject.metadata['mime_type']
- _logger.debug('result of choose: %s (%s)' % \
- (name, str(mime_type)))
- finally:
- chooser.destroy()
- del chooser
-
- if name is not None:
- self._find_custom_paths(name, mime_type)
-
- def _find_custom_paths(self, name, mime_type):
- basename, suffix, i = _find_the_number_in_the_name(name)
- if i < 0: # not part of a numbered sequence
- self.vmw.card_type = 'pattern'
- return
- dsobjects, nobjects = datastore.find({'mime_type': [str(mime_type)]})
- _logger.debug('%d' % nobjects)
- self.vmw.custom_paths = []
- if nobjects > 0:
- for j in range(DECKSIZE):
- for i in range(nobjects):
- if dsobjects[i].metadata['title'] == \
- _construct_a_name(basename, j + 1, suffix):
- _logger.debug('result of find: %s' %\
- dsobjects[i].metadata['title'])
- # self.vmw.custom_paths.append(dsobjects[i].file_path)
- self.vmw.custom_paths.append(dsobjects[i])
- break
- self.vmw.card_type = 'custom'
- if len(self.vmw.custom_paths) < 9:
- self.vmw.card_type = 'pattern'
- elif len(self.vmw.custom_paths) < 27:
- self.vmw.level = DIFFICULTY_LEVEL.index(LOW)
- elif len(self.vmw.custom_paths) < 81:
- self.vmw.level = DIFFICULTY_LEVEL.index(MEDIUM)
- else:
- self.vmw.level = DIFFICULTY_LEVEL.index(HIGH)
- if self.vmw.card_type == 'custom':
- self.button_custom.set_icon('new-custom-game')
- self.button_custom.set_tooltip(_('New custom game'))
- self.metadata['custom_name'] = name
- self.metadata['custom_mime_type'] = mime_type
- self.set_level_label()
- return
+ self.vmw.editing_custom_cards = True
+ self.vmw.edit_custom_card()
+ if self.vmw.robot:
+ self._robot_cb(button)
def _number_card_O_cb(self, button, numberO):
""" Choose between O-card list for numbers game. """
@@ -385,17 +295,28 @@ class VisualMatchActivity(activity.Activity):
int(self.metadata['low_score_beginner'])]
else:
self._low_score = [-1, -1, -1]
+ if 'editing_custom_cards' in self.metadata:
+ self._editing_custom_cards = bool(int(
+ self.metadata['editing_custom_cards']))
+ else:
+ self._editing_custom_cards = False
if self._card_type == 'custom':
- if 'custom_name' in self.metadata:
- self._basename = self.metadata['custom_name']
- self._mime_type = self.metadata['custom_mime_type']
+ if 'custom_object' in self.metadata:
+ self._custom_object = self.metadata['custom_object']
else:
+ self._custom_object = None
self._card_type = 'pattern'
+ self._custom_jobject = []
+ for i in range(9):
+ if 'custom_' + str(i) in self.metadata:
+ self._custom_jobject.append(self.metadata['custom_' + str(i)])
+ else:
+ self._custom_jobject.append(None)
- def _find_datapath(self):
+ def _find_datapath(self, activity):
""" Find the datapath for saving card files. """
- if hasattr(self, 'get_activity_root'):
- return os.path.join(self.get_activity_root(), 'data')
+ if hasattr(activity, 'get_activity_root'):
+ return os.path.join(activity.get_activity_root(), 'data')
else:
return os.path.join(os.environ['HOME'], ".sugar", "default",
SERVICE, 'data')
@@ -457,21 +378,15 @@ class VisualMatchActivity(activity.Activity):
toolbox.set_current_toolbar(1)
self.button_pattern = _button_factory('new-pattern-game',
- _('New pattern game'),
- self._select_game_cb, games_toolbar,
- 'pattern')
+ _('New pattern game'), self._select_game_cb, games_toolbar,
+ 'pattern')
self.button_number = _button_factory('new-number-game',
- _('New number game'),
- self._select_game_cb, games_toolbar,
- 'number')
- self.button_word = _button_factory('new-word-game',
- _('New word game'),
- self._select_game_cb, games_toolbar,
- 'word')
- self.button_custom = _button_factory('insert-image',
- _('Import custom cards'),
- self._select_game_cb, games_toolbar,
- 'custom')
+ _('New number game'), self._select_game_cb, games_toolbar, 'number')
+ self.button_word = _button_factory('new-word-game', _('New word game'),
+ self._select_game_cb, games_toolbar, 'word')
+ self.button_custom = _button_factory('no-custom-game',
+ _('Import custom cards'), self._select_game_cb, games_toolbar,
+ 'custom')
if new_sugar_system:
self._set_extras(games_toolbar, games_toolbar=True)
@@ -484,7 +399,7 @@ class VisualMatchActivity(activity.Activity):
self._edit_words_cb,
tools_toolbar)
self.import_button = _button_factory('insert-image',
- _('Import custom cards'), self._import_cb, tools_toolbar)
+ _('Import custom cards'), self.image_import_cb, tools_toolbar)
self.product_button = _button_factory('product', _('product'),
self._number_card_O_cb,
numbers_toolbar,
@@ -584,8 +499,15 @@ class VisualMatchActivity(activity.Activity):
self.vmw.buddies = []
self.vmw.word_lists = self._word_lists
self.vmw.editing_word_list = self._editing_word_list
- if self.vmw.card_type == 'custom':
- self._find_custom_paths(self._basename, self._mime_type)
+ if hasattr(self, '_custom_object') and self._custom_object is not None:
+ _logger.debug('restoring %s' % (self._custom_object))
+ self.vmw._find_custom_paths(datastore.get(self._custom_object))
+ for i in range(9):
+ if hasattr(self, '_custom_jobject') and \
+ self._custom_jobject[i] is not None:
+ self.vmw.custom_paths[i] = datastore.get(
+ self._custom_jobject[i])
+ _logger.debug('restoring %s' % (self._custom_jobject[i]))
return canvas
def write_file(self, file_path):
@@ -630,7 +552,8 @@ class VisualMatchActivity(activity.Activity):
else:
data.append(i.index)
for i in self.vmw.clicked:
- if i is None or self.vmw.editing_word_list:
+ if i is None or self.vmw.deck.spr_to_card(i) is None or \
+ self.vmw.editing_word_list:
data.append(None)
else:
data.append(self.vmw.deck.spr_to_card(i).index)
@@ -640,7 +563,8 @@ class VisualMatchActivity(activity.Activity):
else:
data.append(i.index)
for i in self.vmw.match_list:
- data.append(self.vmw.deck.spr_to_card(i).index)
+ if self.vmw.deck.spr_to_card(i) is not None:
+ data.append(self.vmw.deck.spr_to_card(i).index)
for i in self.vmw.word_lists:
for j in i:
data.append(j)
diff --git a/constants.py b/constants.py
index dc17970..a201e85 100644
--- a/constants.py
+++ b/constants.py
@@ -46,25 +46,25 @@ MEDIUM = 'medium'
HIGH = 'high'
DIFFICULTY_LEVEL = [MEDIUM, HIGH, LOW]
-RED_STROKE = "#FF6040"
-RED_FILL = "#FFC4B8"
-DARK_RED = "#C03020"
-BLUE_STROKE = "#0060C8"
-BLUE_FILL = "#ACC8E4"
-DARK_BLUE = "#003064"
-GREEN_STROKE = "#00B418"
-GREEN_FILL = "#AFE8A8"
-DARK_GREEN = "#005A0C"
-BLACK = "#000000"
-WHITE = "#FFFFFF"
-YELLOW = "#FFFF00"
-GRAY = "#C0C0C0"
+RED_STROKE = '#FF6040'
+RED_FILL = '#FFC4B8'
+DARK_RED = '#802014'
+BLUE_STROKE = '#0060C8'
+BLUE_FILL = '#ACC8E4'
+DARK_BLUE = '#003064'
+GREEN_STROKE = '#00B418'
+GREEN_FILL = '#AFE8A8'
+DARK_GREEN = '#005A0C'
+BLACK = '#000000'
+WHITE = '#FFFFFF'
+YELLOW = '#FFFF00'
+GRAY = '#C0C0C0'
DARK_COLOR = [DARK_RED, DARK_GREEN, DARK_BLUE]
COLOR_PAIRS = ([RED_STROKE, RED_FILL],
[GREEN_STROKE, GREEN_FILL],
[BLUE_STROKE, BLUE_FILL])
-FILL_STYLES = ["solid", "none", "gradient"]
-CARD_TYPES = ["X", "O", "C"]
+FILL_STYLES = ['solid', 'none', 'gradient']
+CARD_TYPES = ['X', 'O', 'C']
ROMAN_NUMERALS = {5: 'V', 7: 'VII', 10: 'X', 11: 'XI', 14: 'XIV', 15: 'XV',
21: 'XXI', 22: 'XXII', 33: 'XXXIII'}
NUMBER_NAMES = {5: _('five'), 7: _('seven'), 11: _('eleven'), 10: _('ten'),
@@ -75,11 +75,18 @@ NUMBER_PRODUCTS = {5: '1×5', 7: '1×7', 11: '1×11', 10: '2×5',
21: '3×7', 33: '3×11'}
CHINESE_NUMERALS = {5: '五', 7: '七', 10: '十', 11: '十一', 14: '十四',
15: '十五', 21: '廿一', 22: '廿二', 33: '卅三'}
-WORD_STYLES = ["font-weight: bold", "", "font-style: italic"]
-WORD_CARD_INDICIES = [1, 4, 7, 37, 40, 43, 73, 76, 79, None, None, None, None,
+WORD_STYLES = ['font-weight: bold', '', 'font-style: italic']
+WORD_CARD_INDICIES = [0, 4, 8, 36, 40, 44, 72, 76, 80, None, None, None, None,
None, None]
-WORD_CARD_MAP = {1: (0, 0), 4: (0, 1), 7: (0, 2), 37: (1, 0), 40: (1, 1),
- 43: (1, 2), 73: (2, 0), 76: (2, 1), 79: (2, 2)}
+WORD_CARD_MAP = {WORD_CARD_INDICIES[0]: (0, 0), WORD_CARD_INDICIES[1]: (0, 1),
+ WORD_CARD_INDICIES[2]: (0, 2), WORD_CARD_INDICIES[3]: (1, 0),
+ WORD_CARD_INDICIES[4]: (1, 1), WORD_CARD_INDICIES[5]: (1, 2),
+ WORD_CARD_INDICIES[6]: (2, 0), WORD_CARD_INDICIES[7]: (2, 1),
+ WORD_CARD_INDICIES[8]: (2, 2)}
+CUSTOM_CARD_INDICIES = [0, 4, 8, 36, 40, 44, 72, 76, 80, None, None, None, None,
+ None, None]
+# CUSTOM_CARD_INDICIES = [0, 1, 2, 36, 37, 38, 72, 73, 74, None, None, None, None, None, None]
+
# 'dead key' Unicode dictionaries
DEAD_KEYS = ['grave', 'acute', 'circumflex', 'tilde', 'diaeresis', 'abovering']
diff --git a/deck.py b/deck.py
index 4cae052..d423d8f 100644
--- a/deck.py
+++ b/deck.py
@@ -43,9 +43,9 @@ class Deck:
# Initialize the deck of cards by looping through all the patterns
i = 0
for shape in range(0, shape_range):
- for color in range(0, color_range):
- for num in range(0, number_range):
- for fill in range(0, fill_range):
+ for fill in range(0, fill_range):
+ for color in range(0, color_range):
+ for num in range(0, number_range):
i = self._make(sprites, card_type, numbers_type, i,
lists, scale, shape, color, num, fill)
@@ -61,9 +61,13 @@ class Deck:
self.cards.append(Card(sprites, generate_number_card(shape, color,
num, fill, numbers_type, scale), [shape, color, num, fill]))
elif card_type == 'custom':
- self.cards.append(Card(sprites, generate_word_card(color, shape,
- num, fill, scale), [shape, color, num, fill],
- file_path=lists[i], scale=scale))
+ if len(lists) == 9:
+ index = shape * 3 + num
+ else:
+ index = i
+ self.cards.append(Card(sprites, generate_word_card(shape, color,
+ num, fill, scale), [shape, color, num, fill],
+ file_path=lists[index], scale=scale))
i += 1
else:
self.cards.append(Card(sprites, generate_word_card(shape, color,
@@ -127,7 +131,7 @@ class Deck:
def index_to_card(self, i):
""" Given a card index, find the corresponding card in the deck. """
for c in self.cards:
- if c.index == i:
+ if c is not None and c.index == i:
return c
return None
diff --git a/game.py b/game.py
index 53fe20f..25494b2 100644
--- a/game.py
+++ b/game.py
@@ -20,6 +20,10 @@ import gobject
from gettext import gettext as _
+from sugar.graphics.objectchooser import ObjectChooser
+from sugar.datastore import datastore
+from sugar import mime
+
import logging
_logger = logging.getLogger('visualmatch-activity')
@@ -32,7 +36,7 @@ except ImportError:
from constants import LOW, MEDIUM, HIGH, SELECTMASK, MATCHMASK, ROW, COL, \
WORD_CARD_INDICIES, MATCH_POSITION, DEAD_DICTS, DEAD_KEYS, WHITE_SPACE, \
NOISE_KEYS, WORD_CARD_MAP, KEYMAP, CARD_HEIGHT, CARD_WIDTH, DEAL, \
- DIFFICULTY_LEVEL, BACKGROUNDMASK
+ DIFFICULTY_LEVEL, BACKGROUNDMASK, DECKSIZE, CUSTOM_CARD_INDICIES
from grid import Grid
from deck import Deck
@@ -42,6 +46,36 @@ from gencards import generate_selected_card, generate_match_card, \
generate_smiley
+def _find_the_number_in_the_name(name):
+ """ Find which element in an array (journal entry title) is a number """
+ parts = name.split('.')
+ before = ''
+ after = ''
+ for i in range(len(parts)):
+ ii = len(parts) - i - 1
+ try:
+ int(parts[ii])
+ for j in range(ii):
+ before += (parts[j] + '.')
+ for j in range(ii + 1, len(parts)):
+ after += ('.' + parts[j])
+ return before, after, ii
+ except ValueError:
+ pass
+ return '', '', -1
+
+
+def _construct_a_name(before, i, after):
+ """ Make a numbered file name from parts """
+ name = ''
+ if before != '':
+ name += before
+ name += str(i)
+ if after != '':
+ name += after
+ return name
+
+
class Game():
""" The game play -- called from within Sugar or GNOME """
@@ -70,13 +104,15 @@ class Game():
self.scale = 0.8 * self.height / (CARD_HEIGHT * 5.5)
self.card_width = CARD_WIDTH * self.scale
self.card_height = CARD_HEIGHT * self.scale
- self.custom_paths = []
+ self.custom_paths = [None, None, None, None, None, None, None, None,
+ None]
self.sprites = Sprites(self.canvas)
self.selected = []
self.match_display_area = []
self.smiley = None
self.clicked = [None, None, None]
self.editing_word_list = False
+ self.editing_custom_cards = False
self.edit_card = None
self.dead_key = None
@@ -84,6 +120,7 @@ class Game():
""" Start a new game """
# If we were editing the word list, time to stop
self.editing_word_list = False
+ self.editing_custom_cards = False
self.edit_card = None
# If there is already a deck, hide it.
@@ -127,21 +164,20 @@ class Game():
self.deck.index = deck_index
_deck_start = ROW * COL + 3
_deck_stop = _deck_start + self.deck.count()
- self._restore_word_list(
- saved_state[_deck_stop + 3 * self.matches:])
+ self._restore_word_list(saved_state[_deck_stop + \
+ 3 * self.matches:])
self.deck.restore(saved_state[_deck_start: _deck_stop])
self.grid.restore(self.deck, saved_state[0: ROW * COL])
self._restore_selected(saved_state[ROW * COL: ROW * COL + 3])
- self._restore_matches(
- saved_state[_deck_stop: _deck_stop + 3\
- * self.matches])
+ self._restore_matches(saved_state[_deck_stop: _deck_stop + \
+ 3 * self.matches])
elif not self.joiner():
_logger.debug("Starting new game.")
if self.card_type == 'custom':
self.deck = Deck(self.sprites, self.card_type,
[self.numberO, self.numberC],
- self.custom_paths,
- self.scale, DIFFICULTY_LEVEL[self.level])
+ self.custom_paths, self.scale,
+ DIFFICULTY_LEVEL[self.level])
else:
self.deck = Deck(self.sprites, self.card_type,
[self.numberO, self.numberC], self.word_lists,
@@ -191,6 +227,38 @@ class Game():
return True
return False
+ def edit_custom_card(self):
+ """ Update the custom cards from the Journal """
+ if not self.editing_custom_cards:
+ return
+
+ # Set the card type to custom, and generate a new deck.
+ self.deck.hide()
+ self.card_type = 'custom'
+ if len(self.custom_paths) < 3:
+ for i in range(len(self.custom_paths), 81):
+ self.custom_paths.append(None)
+ self.deck = Deck(self.sprites, self.card_type,
+ [self.numberO, self.numberC],
+ self.custom_paths,
+ self.scale, DIFFICULTY_LEVEL.index(HIGH))
+ self.deck.hide()
+ self._unselect()
+ self.matches = 0
+ self.robot_matches = 0
+ self.match_list = []
+ self.total_time = 0
+ self.edit_card = None
+ self.dead_key = None
+ if hasattr(self, 'timeout_id') and self.timeout_id is not None:
+ gobject.source_remove(self.timeout_id)
+ # Fill the grid with custom cards.
+ self.grid.restore(self.deck, CUSTOM_CARD_INDICIES)
+ self.set_label("deck", "")
+ self.set_label("match", "")
+ self.set_label("clock", "")
+ self.set_label("status", _('Edit the custom cards.'))
+
def edit_word_list(self):
""" Update the word cards """
if not self.editing_word_list:
@@ -201,7 +269,7 @@ class Game():
self.card_type = 'word'
self.deck = Deck(self.sprites, self.card_type,
[self.numberO, self.numberC], self.word_lists,
- self.scale, DIFFICULTY_LEVEL[1])
+ self.scale, DIFFICULTY_LEVEL.index(HIGH))
self.deck.hide()
self._unselect()
self.matches = 0
@@ -272,7 +340,9 @@ class Game():
self.selected[i].show_card()
break
- if self.editing_word_list == True:
+ _logger.debug("selecting card %d" % (
+ self.deck.cards.index(self.deck.spr_to_card(spr))))
+ if self.editing_word_list:
# Only edit one card at a time, so unselect other cards
for a in self.clicked:
if a is not None and a is not spr:
@@ -281,6 +351,22 @@ class Game():
self.selected[i].hide_card()
# Edit card label
self.edit_card = self.deck.spr_to_card(spr)
+ elif self.editing_custom_cards:
+ # Only edit one card at a time, so unselect other cards
+ for a in self.clicked:
+ if a is not None and a is not spr:
+ i = self.clicked.index(a)
+ self.clicked[i] = None
+ self.selected[i].hide_card()
+ # Choose an image from the Journal for a card
+ self.edit_card = self.deck.spr_to_card(spr)
+ self._choose_custom_card()
+ # Regenerate the deck with the new card definitions
+ self.deck = Deck(self.sprites, self.card_type,
+ [self.numberO, self.numberC],
+ self.custom_paths, self.scale, DIFFICULTY_LEVEL[1])
+ self.deck.hide()
+ self.grid.restore(self.deck, CUSTOM_CARD_INDICIES)
elif None not in self.clicked:
# If we have three cards selected, test for a match.
self._test_for_a_match()
@@ -539,6 +625,94 @@ class Game():
return False
return True
+ def _choose_custom_card(self):
+ """ Select a custom card from the Journal """
+ chooser = None
+ name = None
+ if hasattr(mime, 'GENERIC_TYPE_IMAGE'):
+ # See #2398
+ if 'image/svg+xml' not in \
+ mime.get_generic_type(mime.GENERIC_TYPE_IMAGE).mime_types:
+ mime.get_generic_type(
+ mime.GENERIC_TYPE_IMAGE).mime_types.append('image/svg+xml')
+ chooser = ObjectChooser(parent=self.activity,
+ what_filter=mime.GENERIC_TYPE_IMAGE)
+ else:
+ try:
+ chooser = ObjectChooser(parent=self, what_filter=None)
+ except TypeError:
+ chooser = ObjectChooser(None, self.activity,
+ gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
+
+ if chooser is not None:
+ try:
+ result = chooser.run()
+ if result == gtk.RESPONSE_ACCEPT:
+ jobject = chooser.get_selected_object()
+ if jobject and jobject.file_path:
+ name = jobject.metadata['title']
+ mime_type = jobject.metadata['mime_type']
+ _logger.debug('result of choose: %s (%s)' % \
+ (name, str(mime_type)))
+ finally:
+ chooser.destroy()
+ del chooser
+
+ if name is not None:
+ self._find_custom_paths(jobject)
+
+ def _find_custom_paths(self, jobject):
+ """ Associate a Journal object with a card """
+ found_a_sequence = False
+ if self.custom_paths[0] is None:
+ basename, suffix, i = _find_the_number_in_the_name(
+ jobject.metadata['title'])
+ """ If this is the first card, try to find paths for other custom
+ cards based on the name; else just load the card. """
+ if i >= 0:
+ dsobjects, nobjects = datastore.find(
+ {'mime_type': [str(jobject.metadata['mime_type'])]})
+ _logger.debug('%d' % nobjects)
+ self.custom_paths = []
+ if nobjects > 0:
+ for j in range(DECKSIZE):
+ for i in range(nobjects):
+ if dsobjects[i].metadata['title'] == \
+ _construct_a_name(basename, j + 1, suffix):
+ _logger.debug('result of find: %s' % \
+ dsobjects[i].metadata['title'])
+ self.custom_paths.append(dsobjects[i])
+ break
+
+ if len(self.custom_paths) < 9:
+ for i in range(3, 81):
+ self.custom_paths.append(
+ self.custom_paths[int(i / 27)])
+ elif len(self.custom_paths) < 27:
+ for i in range(9, 81):
+ self.custom_paths.append(
+ self.custom_paths[int(i / 9)])
+ elif len(self.custom_paths) < 81:
+ for i in range(9, 81):
+ self.custom_paths.append(
+ self.custom_paths[int(i / 3)])
+ found_a_sequence = True
+ self.activity.metadata['custom_object'] = jobject.object_id
+ self.activity.metadata['custom_mime_type'] = \
+ jobject.metadata['mime_type']
+
+ if not found_a_sequence:
+ grid_index = self.grid.spr_to_grid(self.edit_card.spr)
+ _logger.debug('editing card %d' % (grid_index))
+ self.custom_paths[grid_index] = jobject
+ self.activity.metadata['custom_' + str(grid_index)] = \
+ jobject.object_id
+
+ self.card_type = 'custom'
+ self.activity.button_custom.set_icon('new-custom-game')
+ self.activity.button_custom.set_tooltip(_('New custom game'))
+ return
+
class Permutation:
"""Permutaion class for checking for all possible matches on the grid """
diff --git a/grid.py b/grid.py
index 0bcde49..79acd9e 100644
--- a/grid.py
+++ b/grid.py
@@ -17,6 +17,9 @@ import random
from constants import ROW, COL, MATCH_POSITION, DEAL
+import logging
+_logger = logging.getLogger('visualmatch-activity')
+
class Grid:
""" Class for managing ROWxCOL matrix of cards """