From bc87c392686f896d2db6bf98bf546403fc75085e Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Fri, 21 Dec 2012 20:02:08 +0000 Subject: Merge commit 'refs/merge-requests/1' of git://git.sugarlabs.org/sugarchess/sugarchess --- diff --git a/GNUChessActivity.py b/GNUChessActivity.py index b0d677f..1cb8f2a 100644 --- a/GNUChessActivity.py +++ b/GNUChessActivity.py @@ -1,5 +1,6 @@ #Copyright (c) 2012 Walter Bender #Copyright (c) 2012 Ignacio Rodriguez +#Copyright (c) 2012 Aneesh Dogra # 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 @@ -26,10 +27,11 @@ from sugar3.graphics.icon import Icon from sugar3.graphics.xocolor import XoColor from toolbar_utils import button_factory, label_factory, separator_factory, \ - radio_factory, entry_factory + radio_factory, entry_factory, combo_factory from utils import json_load, json_dump, get_hardware, \ pixbuf_to_base64, base64_to_pixbuf +import copy import telepathy import dbus from dbus.service import signal @@ -49,7 +51,6 @@ SERVICE = 'org.sugarlabs.GNUChessActivity' IFACE = SERVICE PATH = '/org/augarlabs/GNUChessActivity' - class GNUChessActivity(activity.Activity): ''' Gnuchess interface from Sugar ''' @@ -96,6 +97,8 @@ class GNUChessActivity(activity.Activity): path=activity.get_bundle_path(), colors=self.colors) self._setup_presence_service() + self.stopwatch_running = False + self.time_interval = None if self.game_data is not None: # 'saved_game' in self.metadata: self._restore() @@ -280,6 +283,26 @@ class GNUChessActivity(activity.Activity): self.opponent = label_factory(self.adjust_toolbar, '') + separator_factory(self.adjust_toolbar, False, True) + + self.time_list = [_('Disabled'), + _('Lightning: 30 seconds'), + _('Blitz: 3 minutes'), + _('Tournament: 10 minutes')] + 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.robot_button.set_active(True) button_factory('new-game', @@ -371,7 +394,7 @@ class GNUChessActivity(activity.Activity): self.custom_toolbar, self._reskin_cb, cb_arg='white_king', - tooltip=_('White King')) + tooltip=_('Whitdde King')) button_factory('black-king', self.custom_toolbar, @@ -466,6 +489,30 @@ class GNUChessActivity(activity.Activity): self._gnuchess.reskin_from_file(piece, file_path) return + def set_timer_cb(self, widget): + 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: 30 seconds'): + self.time_interval = 30 + elif timer_type == _('Blitz: 3 minutes'): + self.time_interval = 3 * 60 + elif timer_type == _('Tournament: 10 minutes'): + self.time_interval = 10 * 60 + else: + self.time_interval = -1 + + if game_already_started: + self.alert_reset(timer_type) + if self.time_interval and self.time_interval == -1: + self.stopwatch(self.time_interval, self.alert_time) + else: + GObject.source_remove(self.stopwatch_timer) + else: + self._gnuchess.new_game() + def _reskin_cb(self, button, piece): object_id, file_path = self._choose_skin() if file_path is not None: @@ -628,6 +675,8 @@ 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 ''' fd = open(file_path, 'r') @@ -649,6 +698,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._gnuchess.restore_game(self._parse_move_list(self.game_data)) self.do_custom_skin_cb() @@ -888,6 +942,35 @@ params=%r state=%d' % (id, initiator, type, service, params, state)) self.send_event('N|%s' % (self.nick)) self.send_event('C|%s,%s' % (self.colors[0], self.colors[1])) + def alert_time(self): + def _alert_response_cb(alert, response_id): + self.remove_alert(alert) + + alert = NotifyAlert() + alert.props.title = _('Time Up!') + alert.props.msg = _('Your time is up.') + alert.connect('response', _alert_response_cb) + alert.show() + self.add_alert(alert) + + def alert_reset(self, mode): + def _alert_response_cb(alert, response_id): + self.remove_alert(alert) + + alert = NotifyAlert() + alert.props.title = _('Time Reset') + alert.props.msg = _('The timer mode was reset to %s' % mode) + alert.connect('response', _alert_response_cb) + alert.show() + self.add_alert(alert) + + def stopwatch(self, time, alert_callback): + if self.stopwatch_running: + GObject.source_remove(self.stopwatch_timer) + time = self.time_interval + self.stopwatch_timer = GObject.timeout_add(time * 1000, alert_callback) + self.stopwatch_running = True + def _receive_join(self, payload): _logger.debug('received_join %s' % (payload)) if self.initiating: diff --git a/chess.py b/chess.py index f0f9e00..e8945d1 100644 --- a/chess.py +++ b/chess.py @@ -134,7 +134,6 @@ class Gnuchess(): stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - if my_move == HINT: level = 'hard\nbook on\n' # may as well get a good hint elif self._activity.playing_mode == 'easy': @@ -261,6 +260,11 @@ class Gnuchess(): self._flash_tile([self._xy_to_file_and_rank( self.black[4].get_xy())]) else: + if self._activity.time_interval and \ + (self._activity.time_interval >= 0): + self._activity.stopwatch(self._activity.time_interval, + self._activity.alert_time) + if len(self.move_list) % 2 == 0: self._activity.status.set_label(_("It is White's move.")) else: diff --git a/toolbar_utils.py b/toolbar_utils.py index 72f7100..9eb5762 100644 --- a/toolbar_utils.py +++ b/toolbar_utils.py @@ -18,6 +18,7 @@ from sugar3.graphics.radiotoolbutton import RadioToolButton from sugar3.graphics.toolbutton import ToolButton from sugar3.graphics.combobox import ComboBox from sugar3.graphics.toolcombobox import ToolComboBox +from sugar3.graphics.toggletoolbutton import ToggleToolButton def combo_factory(combo_array, toolbar, callback, cb_arg=None, @@ -120,6 +121,18 @@ def label_factory(toolbar, label_text, width=None): toolitem.show() return label +def toggle_factory(icon_name, callback, toolbar, tooltip=None): + ''' add a toggle button to a toolbar''' + + t_button = ToggleToolButton(icon_name) + t_button.connect('toggled', callback) + if tooltip: + t_button.set_tooltip(tooltip) + if hasattr(toolbar, 'insert'): # the main toolbar + toolbar.insert(t_button, -1) + else: # or a secondary toolbar + toolbar.props.page.insert(t_button, -1) + t_button.show() def separator_factory(toolbar, expand=False, visible=True): ''' add a separator to a toolbar ''' -- cgit v0.9.1