# MovingBallWithMouseGTK.py Copyright (c) 2006 Kari Laitinen # http://www.naturalprogramming.com # 2006-09-25 File created. # 2009-02-11 Last modification. # This program draws three Ball objects onto the screen. # It is possible to move these balls by dragging them with # the mouse. The Ball class provides a method with which it is # possible to find out whether a clicked point is inside the # ball. In addition the Ball class provides methods for moving # the ball. # During compilation the file BallGTK.py must be in the same # folder (directory) with this file. # The development of this program has been sponsored by Nokia Multimedia. import gtk from BallGTK import Ball class MovingBallWithMouseApplication : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "DRAG A BALL WITH THE MOUSE" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 600, 500 ) self.drawing_area = gtk.DrawingArea() self.drawing_area.set_size_request( 600, 450 ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) self.drawing_area.connect( "button_press_event", self.mouse_button_pressed ) self.drawing_area.connect( "motion_notify_event", self.mouse_motion_notified ) self.drawing_area.connect( "button_release_event", self.mouse_button_released ) self.drawing_area.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.POINTER_MOTION_HINT_MASK | gtk.gdk.BUTTON_RELEASE_MASK ) self.application_window.add( self.drawing_area ) window_width, window_height = self.application_window.get_size() self.first_ball = Ball( window_width / 2 - 160, window_height / 2 - 40, "red" ) self.second_ball = Ball( window_width / 2 - 40, window_height / 2 - 40, "green" ) self.third_ball = Ball( window_width / 2 + 80, window_height / 2 - 40, "blue" ) self.ball_being_moved = None self.previous_mouse_position_x = 0 self.previous_mouse_position_y = 0 self.drawing_area.show() self.application_window.show() def mouse_button_pressed( self, widget, event ) : mouse_position_x = int( event.x ) mouse_position_y = int( event.y ) if self.first_ball.contains_point( mouse_position_x, mouse_position_y ) : self.ball_being_moved = self.first_ball self.ball_being_moved.activate_ball() elif self.second_ball.contains_point( mouse_position_x, mouse_position_y ) : self.ball_being_moved = self.second_ball self.ball_being_moved.activate_ball() elif self.third_ball.contains_point( mouse_position_x, mouse_position_y ) : self.ball_being_moved = self.third_ball self.ball_being_moved.activate_ball() self.previous_mouse_position_x = mouse_position_x self.previous_mouse_position_y = mouse_position_y self.drawing_area.queue_draw() def mouse_motion_notified( self, widget, event ) : if self.ball_being_moved != None : if event.is_hint : new_mouse_position_x, new_mouse_position_y, state = \ event.window.get_pointer() else : # It seems that this else block is never entered. print "A non-hint motion event" new_mouse_position_x = event.x new_mouse_position_y = event.y state = event.state mouse_movement_x = int( new_mouse_position_x ) \ - self.previous_mouse_position_x mouse_movement_y = int( new_mouse_position_y ) \ - self.previous_mouse_position_y self.previous_mouse_position_x = int( new_mouse_position_x ) self.previous_mouse_position_y = int( new_mouse_position_y ) self.ball_being_moved.move_this_ball( mouse_movement_x, mouse_movement_y ) self.drawing_area.queue_draw() return True def mouse_button_released( self, widget, event ) : if self.ball_being_moved != None : self.ball_being_moved.deactivate_ball() self.ball_being_moved = None self.drawing_area.queue_draw() return True def drawing_area_exposed( self, area, event ) : self.first_ball.draw( self.drawing_area ) self.second_ball.draw( self.drawing_area ) self.third_ball.draw( self.drawing_area ) return True def run( self ) : gtk.main() if __name__ == "__main__": this_application = MovingBallWithMouseApplication() this_application.run()