pitivi r1176 - in branches/SOC_2008_BLEWIS: . pitivi pitivi/ui



Author: blewis
Date: Thu Jul 17 00:42:33 2008
New Revision: 1176
URL: http://svn.gnome.org/viewvc/pitivi?rev=1176&view=rev

Log:
* pitivi/ui/testHList.py:
* pitivi/ui/testcomplex.py:
* pitivi/ui/util.py:
added a function called "magnetize" which implements "edge snapping"
* pitivi/utils.py:
added a function, closest_item() which returns the element closest to
item in an array


Removed:
   branches/SOC_2008_BLEWIS/pitivi/ui/testHList.py
   branches/SOC_2008_BLEWIS/pitivi/ui/testcomplex.py
Modified:
   branches/SOC_2008_BLEWIS/ChangeLog
   branches/SOC_2008_BLEWIS/pitivi/ui/util.py
   branches/SOC_2008_BLEWIS/pitivi/utils.py

Modified: branches/SOC_2008_BLEWIS/pitivi/ui/util.py
==============================================================================
--- branches/SOC_2008_BLEWIS/pitivi/ui/util.py	(original)
+++ branches/SOC_2008_BLEWIS/pitivi/ui/util.py	Thu Jul 17 00:42:33 2008
@@ -22,6 +22,7 @@
 import gobject
 import goocanvas
 import gtk
+from pitivi.utils import closest_item
 
 ## GooCanvas Convenience Functions
 
@@ -126,6 +127,22 @@
 def center(item):
     return point_sum(pos(item), point_mul(0.5, size(item)))
 
+def magnetize(obj, coord, magnets, deadband):
+    # remember that objects have two ends
+    left_res, left_diff = closest_magnet(coord, magnets)
+    right_res, right_diff = closest_magnet(coord + width(obj), magnets)
+
+    if left_diff <= right_diff:
+        res = left_res
+        diff = left_diff
+    else:
+        res = right_res - width(obj)
+        diff = right_diff
+    if diff <= deadband:
+        return res
+    # otherwise, return x
+    return coord
+
 def make_item(factory):
     """Create a new goocanvas item given factory, a tuple of 
     * <class> - the class to create

Modified: branches/SOC_2008_BLEWIS/pitivi/utils.py
==============================================================================
--- branches/SOC_2008_BLEWIS/pitivi/utils.py	(original)
+++ branches/SOC_2008_BLEWIS/pitivi/utils.py	Thu Jul 17 00:42:33 2008
@@ -37,15 +37,16 @@
             return True
     return False
 
-# Python re-implementation of binary search found here:
+# Python re-implementation of binary search algorithm found here:
 # http://en.wikipedia.org/wiki/Binary_search
 #
 # This is the iterative version without the early termination branch, which
-# also tells us the element of A that are nearest to Value, if the element we want
-# is not found. This is useful for implementing edge snaping in the UI, where
-# we repeatedly search through a list of control points for the one closes to
-# the cursor. Because we don't care whether the cursor position matches the
-# list, this function returns the element closest to value in the array.
+# also tells us the element of A that are nearest to Value, if the element we
+# want is not found. This is useful for implementing edge snaping in the UI,
+# where we repeatedly search through a list of control points for the one
+# closes to the cursor. Because we don't care whether the cursor position
+# matches the list, this function returns the index of the lement closest to
+# value in the array.
 
 def binary_search(A, value):
     low = 0
@@ -59,3 +60,25 @@
             #so high can't be < mid if A[mid] == value
             high = mid; 
     return low
+
+# Returns the element of seq nearest to item, and the difference between them
+
+def closest_item(seq, item):
+    index = binary_search(seq, item)
+    if index >= len(seq):
+        index = len(seq) - 1
+    res = seq[index]
+    diff = abs(res - item)
+
+    # binary_search returns largest element closest to item.
+    # if there is a smaller element...
+    if index - 1 >= 0:
+        res_a = seq[index - 1]
+        # ...and it is closer to the pointer...
+        diff_a = abs(res_a - item)
+        if diff_a < diff:
+            # ...use it instead.
+            res = res_a
+            diff = diff_a
+    return res, diff
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]