diff options
author | Aneesh Dogra <lionaneesh@gmail.com> | 2012-12-23 14:57:34 (GMT) |
---|---|---|
committer | Aneesh Dogra <lionaneesh@gmail.com> | 2012-12-23 14:57:34 (GMT) |
commit | a9d4ebfa1135165c39d0658d5ba087df95271ba7 (patch) | |
tree | f8cc645db1b4533150c8804ddddf982fbd7e9f24 | |
parent | 8e55207462ca6922a1d155595efc8a5937f98682 (diff) |
Add toggle_button to toggle webcam view.
-rw-r--r-- | GNUChessActivity.py | 49 | ||||
-rw-r--r-- | WebcamImages.py | 23 | ||||
-rw-r--r-- | chess.py | 1 |
3 files changed, 49 insertions, 24 deletions
diff --git a/GNUChessActivity.py b/GNUChessActivity.py index e17fb5e..fa52496 100644 --- a/GNUChessActivity.py +++ b/GNUChessActivity.py @@ -25,7 +25,7 @@ from sugar3.graphics.alert import ConfirmationAlert, NotifyAlert 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, combo_factory + radio_factory, entry_factory, combo_factory, toggle_factory from utils import json_load, json_dump, get_hardware, \ pixbuf_to_base64, base64_to_pixbuf @@ -63,6 +63,8 @@ class GNUChessActivity(activity.Activity): except dbus.exceptions.DBusException, e: _logger.error(str(e)) + self.webcam_image = None + self.w_obj = None self.game_data = None self.playing_white = True self.playing_mode = 'easy' @@ -83,27 +85,20 @@ class GNUChessActivity(activity.Activity): self._setup_dispatch_table() self.main_hbox = Gtk.HBox() # contains webcam image viewer and the canvas - self.image_vbox = Gtk.VBox() # Create a canvas - canvas = Gtk.DrawingArea() - canvas.set_size_request(Gdk.Screen.width() - W_I_WIDTH, \ - Gdk.Screen.height()) - self.main_hbox.pack_start(canvas, False, True, 0) + self.chess_canvas = Gtk.DrawingArea() + self.chess_canvas.set_size_request(Gdk.Screen.width(), \ + Gdk.Screen.height()) + self.main_hbox.pack_start(self.chess_canvas, False, True, 0) # make sure that our Webcam images directory is empty - webcam_image = Gtk.Image() - w_obj = WebcamImages(webcam_image, 7) - self.image_vbox.pack_start(webcam_image, False, True, 0) - self.image_vbox.show() - self.main_hbox.pack_end(self.image_vbox, False, True, 0) - self.main_hbox.show_all() self.set_canvas(self.main_hbox) self.main_hbox.show() - canvas.show() + self.chess_canvas.show() self.show_all() self.old_cursor = self.get_window().get_cursor() - self._gnuchess = Gnuchess(canvas, + self._gnuchess = Gnuchess(self.chess_canvas, parent=self, path=activity.get_bundle_path(), colors=self.colors) @@ -314,6 +309,10 @@ class GNUChessActivity(activity.Activity): self.timer.show() self.timer.set_sensitive(True) + toggle_factory('human', self.webcam_toggle, + self.adjust_toolbar, + _('Toggle Webcam')) + self.robot_button.set_active(True) button_factory('new-game', @@ -413,6 +412,28 @@ class GNUChessActivity(activity.Activity): cb_arg='black_king', tooltip=_('Black King')) + def webcam_toggle(self, widget): + if widget.get_active(): + self.webcam_image = Gtk.Image() + self._gnuchess._width = Gdk.Screen.width() - W_I_WIDTH + self.w_obj = WebcamImages(self.webcam_image, 2) + self.image_vbox = Gtk.VBox() + self.chess_canvas.set_size_request(Gdk.Screen.width() - W_I_WIDTH, \ + Gdk.Screen.height()) + self.image_vbox.pack_start(self.webcam_image, False, True, 0) + self.image_vbox.show() + self.main_hbox.pack_end(self.image_vbox, False, True, 0) + self.main_hbox.show_all() + else: + if self.w_obj: + self.w_obj.cleanup() + self.image_vbox.remove(self.webcam_image) + self.webcam_image = None + self.main_hbox.remove(self.image_vbox) + self.chess_canvas.set_size_request(Gdk.Screen.width(), \ + Gdk.Screen.height()) + + def do_default_skin_cb(self, button=None): self._gnuchess.reskin_from_file('black_king', '%s/icons/black-king.svg' % (activity.get_bundle_path())) diff --git a/WebcamImages.py b/WebcamImages.py index a4724f1..3dfb51c 100644 --- a/WebcamImages.py +++ b/WebcamImages.py @@ -31,6 +31,8 @@ class WebcamImages(): fps: No of frames generated per second. """ def __init__(self, image_widget, fps): + self.player = None + self.image_updater_thread = None image_list = os.listdir(WEBCAM_IMAGES_DIR) for image in image_list: os.remove(os.path.join(WEBCAM_IMAGES_DIR, image)) @@ -43,15 +45,15 @@ class WebcamImages(): """ Generates Webcam images, and saves them in WEBCAM_IMAGES_DIR """ - player = gst.parse_launch ("v4l2src ! videorate ! " \ - "video/x-raw-rgb,framerate=%d/1 ! " \ - "pngenc snapshot=false ! " \ - "multifilesink location=%s" % \ - (fps, - os.path.join(WEBCAM_IMAGES_DIR, - '%09d.png'), )) + self.player = gst.parse_launch ("v4l2src ! videorate ! " \ + "video/x-raw-rgb,framerate=%d/1 ! " \ + "pngenc snapshot=false ! " \ + "multifilesink location=%s" % \ + (fps, + os.path.join(WEBCAM_IMAGES_DIR, + '%09d.png'), )) - player.set_state(gst.STATE_PLAYING) + self.player.set_state(gst.STATE_PLAYING) def update_image(self, fps, image_widget): """ @@ -80,4 +82,7 @@ class WebcamImages(): fps, image_widget) def cleanup(self): - GObject.source_remove(self.image_updater_thread) + if self.image_updater_thread: + GObject.source_remove(self.image_updater_thread) + if self.player: + self.player.set_state(gst.STATE_NULL) @@ -84,7 +84,6 @@ class Gnuchess(): self._canvas.connect("button-press-event", self._button_press_cb) self._canvas.connect("button-release-event", self._button_release_cb) self._canvas.connect("motion-notify-event", self._mouse_move_cb) - self._width = Gdk.Screen.width() - W_I_WIDTH self._height = Gdk.Screen.height() self.scale = int((self._height - 55) / 10) |