Attachment 'drag-correct.py'

Download

   1 import gtk
   2 
   3 rect_x = 300
   4 rect_y = 220
   5 rect_size = 80
   6 
   7 x_offset_to_rect = 0
   8 y_offset_to_rect = 0
   9 
  10 dragging = False
  11 
  12 draw_area = None
  13 
  14 def constrain_to_viewport ():
  15     global rect_x, rect_y, draw_area
  16 
  17     if rect_x < 0:
  18         rect_x = 0
  19     elif rect_x + rect_size >= draw_area.allocation.width:
  20         rect_x = draw_area.allocation.width - rect_size
  21 
  22     if rect_y < 0:
  23         rect_y = 0
  24     elif rect_y + rect_size >= draw_area.allocation.height:
  25         rect_y = draw_area.allocation.height - rect_size
  26 
  27 def expose_event_cb (widget, event):
  28     cr = widget.window.cairo_create ()
  29     cr.set_source_rgb (1, 1, 1)
  30 
  31     cr.rectangle (0, 0, widget.allocation.width, widget.allocation.height)
  32     cr.fill ()
  33 
  34     cr.set_source_rgb (1, 0, 0)
  35     cr.rectangle (rect_x, rect_y, rect_size, rect_size)
  36     cr.fill ()
  37 
  38     return False
  39 
  40 def button_press_event_cb (widget, event):
  41     global dragging, draw_area, rect_x, rect_y, x_offset_to_rect, y_offset_to_rect
  42 
  43     if (dragging or event.button != 1
  44         or event.x < rect_x or event.x >= rect_x + rect_size
  45         or event.y < rect_y or event.y >= rect_y + rect_size):
  46         return True
  47 
  48     dragging = True
  49     x_offset_to_rect = event.x - rect_x
  50     y_offset_to_rect = event.y - rect_y
  51 
  52     return True
  53 
  54 def button_release_event_cb (widget, event):
  55     global dragging, draw_area, rect_x, rect_y, x_offset_to_rect, y_offset_to_rect
  56 
  57     if not dragging:
  58         return True
  59 
  60     if event.button != 1:
  61         return True
  62 
  63     dragging = False
  64     return True
  65 
  66 def motion_notify_event_cb (widget, event):
  67     global dragging, draw_area, rect_x, rect_y, x_offset_to_rect, y_offset_to_rect
  68 
  69     if not dragging:
  70         return True
  71 
  72     rect_x = event.x - x_offset_to_rect
  73     rect_y = event.y - y_offset_to_rect
  74     constrain_to_viewport ()
  75 
  76     draw_area.queue_draw ()
  77 
  78     return True
  79 
  80 window = gtk.Window (gtk.WINDOW_TOPLEVEL)
  81 
  82 draw_area = gtk.DrawingArea ()
  83 draw_area.set_size_request (640, 480)
  84 draw_area.add_events (gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK)
  85 draw_area.connect ("expose-event", expose_event_cb)
  86 draw_area.connect ("button-press-event", button_press_event_cb)
  87 draw_area.connect ("button-release-event", button_release_event_cb)
  88 draw_area.connect ("motion-notify-event", motion_notify_event_cb)
  89 
  90 window.add (draw_area)
  91 
  92 window.show_all ()
  93 
  94 gtk.main ()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2021-02-25 09:46:48, 2.3 KB) [[attachment:drag-correct.py]]
  • [get | view] (2021-02-25 09:46:48, 2.3 KB) [[attachment:drag-incorrect.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.