Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/GNUChessActivity.py
diff options
context:
space:
mode:
Diffstat (limited to 'GNUChessActivity.py')
-rw-r--r--GNUChessActivity.py286
1 files changed, 111 insertions, 175 deletions
diff --git a/GNUChessActivity.py b/GNUChessActivity.py
index e0bc23e..38ead14 100644
--- a/GNUChessActivity.py
+++ b/GNUChessActivity.py
@@ -14,20 +14,22 @@
from gi.repository import Gtk, Gdk, GObject
from sugar3.activity import activity
-from sugar3 import profile
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.graphics.toolbarbox import ToolbarButton
+from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.objectchooser import ObjectChooser
-from sugar3.datastore import datastore
-from sugar3 import mime
+from sugar3.graphics.menuitem import MenuItem
from sugar3.graphics.alert import ConfirmationAlert, NotifyAlert
from sugar3.graphics.icon import Icon
from sugar3.graphics.xocolor import XoColor
+from sugar3.datastore import datastore
+from sugar3 import mime
+from sugar3 import profile
from toolbar_utils import button_factory, label_factory, separator_factory, \
- radio_factory, entry_factory, combo_factory
+ radio_factory, entry_factory
from utils import json_load, json_dump, get_hardware, \
pixbuf_to_base64, base64_to_pixbuf
@@ -51,11 +53,17 @@ SERVICE = 'org.sugarlabs.GNUChessActivity'
IFACE = SERVICE
PATH = '/org/augarlabs/GNUChessActivity'
+PIECES = {'pawn': {'white': _('White Pawn'), 'black': _('Black Pawn')},
+ 'rook': {'white': _('White Rook'), 'black': _('Black Rook')},
+ 'knight': {'white': _('White Knight'), 'black': _('Black Knight')},
+ 'bishop': {'white': _('White Bishop'), 'black': _('Black Bishop')},
+ 'queen': {'white': _('White Queen'), 'black': _('Black Queen')},
+ 'king': {'white': _('White King'), 'black': _('Black King')}}
+
+
class GNUChessActivity(activity.Activity):
''' Gnuchess interface from Sugar '''
- # FIXME: Disable most buttons when joiner
-
def __init__(self, handle):
''' Initialize the toolbars and the gnuchess '''
try:
@@ -84,8 +92,7 @@ class GNUChessActivity(activity.Activity):
# Create a canvas
canvas = Gtk.DrawingArea()
- canvas.set_size_request(Gdk.Screen.width(), \
- Gdk.Screen.height())
+ canvas.set_size_request(Gdk.Screen.width(), Gdk.Screen.height())
self.set_canvas(canvas)
canvas.show()
self.show_all()
@@ -97,13 +104,16 @@ class GNUChessActivity(activity.Activity):
path=activity.get_bundle_path(),
colors=self.colors)
-
if self.shared_activity:
# We're joining
if not self.get_shared():
xocolors = XoColor(profile.get_color().to_string())
share_icon = Icon(icon_name='zoom-neighborhood',
xo_color=xocolors)
+
+ def _alert_cancel_cb(self, alert, response_id):
+ self.remove_alert(alert)
+
self._joined_alert = NotifyAlert()
self._joined_alert.props.icon = share_icon
self._joined_alert.props.title = _('Please wait')
@@ -118,6 +128,7 @@ class GNUChessActivity(activity.Activity):
self.stopwatch_running = False
self.time_interval = None
+ self.timer_panel_visible = False
if self.game_data is not None: # 'saved_game' in self.metadata:
self._restore()
@@ -125,9 +136,6 @@ class GNUChessActivity(activity.Activity):
self._gnuchess.new_game()
self._restoring = False
- def _alert_cancel_cb(self, alert, response_id):
- self.remove_alert(alert)
-
def restore_cursor(self):
''' No longer thinking, so restore standard cursor. '''
self.get_window().set_cursor(self.old_cursor)
@@ -307,26 +315,13 @@ class GNUChessActivity(activity.Activity):
separator_factory(self.adjust_toolbar, False, True)
- self.time_list = [_('Disabled'),
- #TRANS: Lightning chess 30 seconds between moves
- _('Lightning: %d seconds') % (30),
- #TRANS: Blitz chess 3 minutes between moves
- _('Blitz: %d minutes') % (3),
- #TRANS: Tournament chess 10 minutes between moves
- _('Tournament: %d minutes') % (10)]
- self.timer = Gtk.ComboBoxText()
- for t in self.time_list:
- self.timer.append_text(t)
- self.timer.set_tooltip_text(_('Timer'))
- self.timer.set_active(0)
- self.timer.connect("changed", self.set_timer_cb)
-
- toolitem = Gtk.ToolItem()
- toolitem.add(self.timer)
- self.adjust_toolbar.insert(toolitem, -1)
- toolitem.show()
- self.timer.show()
- self.timer.set_sensitive(True)
+ self.timer_button = ToolButton('timer-0')
+ self.timer_button.set_tooltip(_('Timer'))
+ self.timer_button.connect('clicked', self._timer_button_cb)
+ self.toolbar.insert(self.timer_button, -1)
+ self._setup_timer_palette()
+ self.timer_button.show()
+ self.timer_button.set_sensitive(True)
self.robot_button.set_active(True)
@@ -355,119 +350,31 @@ class GNUChessActivity(activity.Activity):
toolbox.toolbar.insert(stop_button, -1)
stop_button.show()
- button_factory('white-pawn',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='white_pawn',
- tooltip=_('White Pawn'))
-
- button_factory('black-pawn',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='black_pawn',
- tooltip=_('Black Pawn'))
-
- button_factory('white-rook',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='white_rook',
- tooltip=_('White Rook'))
-
- button_factory('black-rook',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='black_rook',
- tooltip=_('Black Rook'))
-
- button_factory('white-knight',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='white_knight',
- tooltip=_('White Knight'))
-
- button_factory('black-knight',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='black_knight',
- tooltip=_('Black Knight'))
-
- button_factory('white-bishop',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='white_bishop',
- tooltip=_('White Bishop'))
-
- button_factory('black-bishop',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='black_bishop',
- tooltip=_('Black Bishop'))
-
- button_factory('white-queen',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='white_queen',
- tooltip=_('White Queen'))
-
- button_factory('black-queen',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='black_queen',
- tooltip=_('Black Queen'))
-
- button_factory('white-king',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='white_king',
- tooltip=_('White King'))
-
- button_factory('black-king',
- self.custom_toolbar,
- self._reskin_cb,
- cb_arg='black_king',
- tooltip=_('Black King'))
+ for piece in PIECES.keys():
+ for color in ['white', 'black']:
+ button_factory('%s-%s' % (color, piece),
+ self.custom_toolbar,
+ self._reskin_cb,
+ cb_arg='%s_%s' % (color, piece),
+ tooltip=PIECES[piece][color])
def do_default_skin_cb(self, button=None):
- self._gnuchess.reskin_from_file('black_king',
- '%s/icons/black-king.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('black_queen',
- '%s/icons/black-queen.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('black_bishop',
- '%s/icons/black-bishop.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('black_knight',
- '%s/icons/black-knight.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('black_rook',
- '%s/icons/black-rook.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('black_pawn',
- '%s/icons/black-pawn.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('white_king',
- '%s/icons/white-king.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('white_queen',
- '%s/icons/white-queen.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('white_bishop',
- '%s/icons/white-bishop.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('white_knight',
- '%s/icons/white-knight.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('white_rook',
- '%s/icons/white-rook.svg' % (activity.get_bundle_path()))
- self._gnuchess.reskin_from_file('white_pawn',
- '%s/icons/white-pawn.svg' % (activity.get_bundle_path()))
+ for piece in PIECES.keys():
+ for color in ['white', 'black']:
+ self._gnuchess.reskin_from_file(
+ '%s_%s' % (color, piece),
+ '%s/icons/%s-%s.svg' % (activity.get_bundle_path(),
+ color, piece))
def _black_pieces(self, colors):
- self._gnuchess.reskin_from_svg('black_king', colors, bw='#000000')
- self._gnuchess.reskin_from_svg('black_queen', colors, bw='#000000')
- self._gnuchess.reskin_from_svg('black_bishop', colors, bw='#000000')
- self._gnuchess.reskin_from_svg('black_knight', colors, bw='#000000')
- self._gnuchess.reskin_from_svg('black_rook', colors, bw='#000000')
- self._gnuchess.reskin_from_svg('black_pawn', colors, bw='#000000')
+ for piece in PIECES.keys():
+ self._gnuchess.reskin_from_svg('black_%s' % piece, colors,
+ bw='#000000')
def _white_pieces(self, colors):
- self._gnuchess.reskin_from_svg('white_king', colors, bw='#ffffff')
- self._gnuchess.reskin_from_svg('white_queen', colors, bw='#ffffff')
- self._gnuchess.reskin_from_svg('white_bishop', colors, bw='#ffffff')
- self._gnuchess.reskin_from_svg('white_knight', colors, bw='#ffffff')
- self._gnuchess.reskin_from_svg('white_rook', colors, bw='#ffffff')
- self._gnuchess.reskin_from_svg('white_pawn', colors, bw='#ffffff')
+ for piece in PIECES.keys():
+ self._gnuchess.reskin_from_svg('white_%s' % piece, colors,
+ bw='#ffffff')
def do_sugar_skin_cb(self, button=None):
colors = self.colors
@@ -487,51 +394,73 @@ class GNUChessActivity(activity.Activity):
self._white_pieces(colors)
def do_custom_skin_cb(self, button=None):
- for piece in ['white_pawn', 'black_pawn',
- 'white_rook', 'black_rook',
- 'white_knight', 'black_knight',
- 'white_bishop', 'black_bishop',
- 'white_queen', 'black_queen',
- 'white_king', 'black_king']:
- if piece in self.metadata:
- id = self.metadata[piece]
- jobject = datastore.get(id)
- if jobject is not None and jobject.file_path is not None:
- self._do_reskin(piece, jobject.file_path)
-
- def _do_reskin(self, piece, file_path):
+ for piece in PIECES.keys():
+ for color in ['white', 'black']:
+ name = '%s_%s' % (color, piece)
+ if name in self.metadata:
+ id = self.metadata[name]
+ jobject = datastore.get(id)
+ if jobject is not None and jobject.file_path is not None:
+ self._do_reskin(name, jobject.file_path)
+
+ def _do_reskin(self, name, file_path):
''' If we are sharing, only reskin pieces of your color '''
if self._gnuchess.we_are_sharing and self.buddy is not None:
- if 'white' in piece and self.playing_white:
+ if 'white' in name and self.playing_white:
pixbuf = self._gnuchess.reskin_from_file(
- piece, file_path, return_pixbuf=True)
- self.send_piece(piece, pixbuf)
- elif 'black' in piece and not self.playing_white:
+ name, file_path, return_pixbuf=True)
+ self.send_piece(name, pixbuf)
+ elif 'black' in name and not self.playing_white:
pixbuf = self._gnuchess.reskin_from_file(
- piece, file_path, return_pixbuf=True)
- self.send_piece(piece, pixbuf)
+ name, file_path, return_pixbuf=True)
+ self.send_piece(name, pixbuf)
else:
- self._gnuchess.reskin_from_file(piece, file_path)
+ self._gnuchess.reskin_from_file(name, file_path)
return
- def set_timer_cb(self, widget):
+ def _timer_button_cb(self, button):
+ if not self.timer_palette.is_up() and not self.timer_panel_visible:
+ self.timer_palette.popup(
+ immediate=True, state=self.timer_palette.SECONDARY)
+ self.timer_panel_visible = True
+ else:
+ self.timer_palette.popdown(immediate=True)
+ self.timer_panel_visible = False
+
+ def _setup_timer_palette(self):
+ self.timer_values = [None, 30, 180, 600]
+ self.timer_tooltips = ['', _('30 seconds'), _('3 minutes'),
+ _('10 minutes')]
+ self.timer_labels = [_('Disabled'),
+ #TRANS: Lightning chess 30 seconds between moves
+ _('Lightning: %d seconds') % (30),
+ #TRANS: Blitz chess 3 minutes between moves
+ _('Blitz: %d minutes') % (3),
+ #TRANS: Tournament chess 10 minutes between moves
+ _('Tournament: %d minutes') % (10)]
+ self.timer_palette = self.timer_button.get_palette()
+
+ for i, label in enumerate(self.timer_labels):
+ menu_item = MenuItem(icon_name='timer-%d' % (i),
+ text_label=label)
+ menu_item.connect('activate', self._timer_selected_cb, i)
+ self.timer_palette.menu.append(menu_item)
+ menu_item.show()
+
+ def _timer_selected_cb(self, button, index):
game_already_started = 0
if self.time_interval != None:
game_already_started = 1
- if widget.get_active() >= 0:
- timer_type = widget.get_active_text()
- if timer_type == _('Lightning: %d seconds') % (30):
- self.time_interval = 30
- elif timer_type == _('Blitz: %d minutes') % (3):
- self.time_interval = 3 * 60
- elif timer_type == _('Tournament: %d minutes') % (10):
- self.time_interval = 10 * 60
- else:
- self.time_interval = -1
+ self.time_interval = self.timer_values[index]
+ if self.time_interval is None:
+ self.timer_button.set_tooltip(_('Timer off'))
+ else:
+ self.timer_button.set_tooltip(
+ _('Timer') + ' (' + self.timer_tooltips[index] + ')')
if game_already_started:
- self.alert_reset(timer_type)
- if self.time_interval and self.time_interval == -1:
+ self.alert_reset(self.timer_labels[index])
+ if self.time_interval and self.time_interval is not None:
self.stopwatch(self.time_interval, self.alert_time)
else:
GObject.source_remove(self.stopwatch_timer)
@@ -700,7 +629,9 @@ class GNUChessActivity(activity.Activity):
else:
self.metadata['playing_robot'] = 'False'
+ '''
self.metadata['timer_mode'] = self.timer.get_active_text()
+ '''
def read_file(self, file_path):
''' Read project file on relaunch '''
@@ -723,10 +654,11 @@ class GNUChessActivity(activity.Activity):
if self.metadata['playing_robot'] == 'False':
self.playing_robot = False
self.human_button.set_active(True)
+ '''
if 'timer_mode' in self.metadata:
- self.timer.set_active(self.time_list.index(
- self.metadata['timer_mode']))
-
+ self.timer_intervale.set_active(self.timer_list.index(
+ self.metadata['timer_mode']))
+ '''
self._gnuchess.restore_game(self._parse_move_list(self.game_data))
self.do_custom_skin_cb()
@@ -884,6 +816,10 @@ class GNUChessActivity(activity.Activity):
self.human_button.set_active(True)
self.restoring = False
+ self.easy_button.set_sensitive(False)
+ self.hard_button.set_sensitive(False)
+ self.robot_button.set_sensitive(False)
+
def _list_tubes_reply_cb(self, tubes):
''' Reply to a list request. '''
for tube_info in tubes: