← Back to team overview

openshot.code team mailing list archive

[Branch ~openshot.code/openshot/main] Rev 696: Improved transitions to snap only to the left edges of clips, and resize themselves correctly bet...

 

------------------------------------------------------------
revno: 696
fixes bug: https://launchpad.net/bugs/529944
committer: Jonathan Thomas <Jonathan.Oomph@xxxxxxxxx>
branch nick: openshot
timestamp: Sun 2012-09-09 14:58:31 -0500
message:
  Improved transitions to snap only to the left edges of clips, and resize themselves correctly between 2 clips.  This works when dropping a new transition onto the timeline, or simply moving one near a clip (with Snapping enabled)
modified:
  openshot/classes/track.py
  openshot/classes/transition.py
  openshot/windows/MainGTK.py


--
lp:openshot
https://code.launchpad.net/~openshot.code/openshot/main

Your team OpenShot Code is subscribed to branch lp:openshot.
To unsubscribe from this branch go to https://code.launchpad.net/~openshot.code/openshot/main/+edit-subscription
=== modified file 'openshot/classes/track.py'
--- openshot/classes/track.py	2012-05-23 19:41:04 +0000
+++ openshot/classes/track.py	2012-09-09 19:58:31 +0000
@@ -73,6 +73,7 @@
 
 		# create a new transition object
 		new_transition = transition.transition(transition_name, position_on_track, length, resource, self)
+		new_transition.stored_x = 0.0
 
 		# insert new transition
 		self.transitions.append(new_transition)	

=== modified file 'openshot/classes/transition.py'
--- openshot/classes/transition.py	2012-09-08 19:50:06 +0000
+++ openshot/classes/transition.py	2012-09-09 19:58:31 +0000
@@ -18,6 +18,7 @@
 
 import os, uuid, locale
 import gtk, goocanvas
+import time
 
 ########################################################################
 class transition:
@@ -407,9 +408,8 @@
 		return True
 
 
-
-	def on_button_release_x (self, item, target, event):
-		""" This method drops a clip, and snaps the clip to the nearest valid track """
+	def drop_canvas_item(self, item):
+		""" Drop the canvas item, and snap and resize it if 'snapping' enabled. """
 
 		# Get theme settings
 		theme_settings = self.parent.parent.project.theme_settings.settings
@@ -418,10 +418,6 @@
 		self.parent.parent.play_head.raise_(None)
 		self.parent.parent.play_head_line.raise_(None)
 
-		# get reference to the canvas, and stop dragging the item
-		canvas = item.get_canvas()
-		canvas.pointer_ungrab (item, event.time)
-
 		# determine what cursor mode is enable (arrow, razor, snap, etc...)
 		(isArrow, isRazor, isSnap, isResize) = self.parent.parent.project.form.get_toolbar_options()
 
@@ -432,6 +428,7 @@
 			# get new parent track
 			drop_track = self.get_valid_drop(item.get_bounds().x1, item.get_bounds().y1)
 			bottom_y = 0
+			new_length = 0
 
 			if drop_track == None:
 				# keep old parent, if no track found
@@ -443,14 +440,14 @@
 			# get the pixels per second from the parent sequence
 			pixels_per_second = self.parent.parent.get_pixels_per_second()
 
-			# deterime the direction of the drag
+			# determine the direction of the drag
 			if isSnap:
 				distance_from_clip = self.get_snap_difference(self, item)
 			else:
 				distance_from_clip = 0.0
 
 			# update the position 
-			self.position_on_track = float(item.get_bounds().x1 + distance_from_clip) / pixels_per_second
+			new_position_on_track = float(item.get_bounds().x1 + distance_from_clip) / pixels_per_second
 
 			# update the parent (the primary track)
 			self.update_parent(drop_track)
@@ -467,11 +464,42 @@
 				# refresh image
 				self.change_direction_image(item)
 
+				# Change length of transition (if snap is enabled)
+				track_index = self.parent.parent.tracks.index(self.parent)
+				bottom_track = None
+				if (track_index + 1) <= (len(self.parent.parent.tracks) - 1):
+					bottom_track = self.parent.parent.tracks[track_index + 1]
+				
+				# get distance from nearby clips (to the Right)
+				edges = [self.get_edge_of_clip(new_position_on_track, "right", self.parent, 10.0) * pixels_per_second]
+				if bottom_track:
+					edges.append(self.get_edge_of_clip(new_position_on_track, "right", bottom_track, 10.0) * pixels_per_second)
+				
+				# Change length
+				if sorted(edges)[0] > new_position_on_track + self.length:
+					new_length = (sorted(edges)[0] - (new_position_on_track * pixels_per_second)) / pixels_per_second
+					length_diff = new_length - self.length
+					
+					if distance_from_clip:
+						# re-render transition (at correct width)
+						self.length = new_length
+
+			# Set the new position
+			self.position_on_track = new_position_on_track
+
 			# Animate the transition to it's new position
-			item.animate(distance_from_clip, bottom_y - item.get_bounds().y1, 1.0, 0.0, False, 200, 4, goocanvas.ANIMATE_FREEZE)
+			#item.animate(distance_from_clip, bottom_y - item.get_bounds().y1, 1.0, 0.0, False, 200, 4, goocanvas.ANIMATE_FREEZE)
 
 			# move the clip object on the timeline to correct position (snapping to the y and x of the track)		
-			item.translate (distance_from_clip, bottom_y - item.get_bounds().y1)
+			#item.translate (distance_from_clip, bottom_y - item.get_bounds().y1)
+
+			# remove old canvas item
+			parent = item.get_parent()
+			child_num = parent.find_child (item)
+			parent.remove_child (child_num)
+
+			# create new transition canvas item (possibly a bigger size)
+			item = self.Render(None, 0)
 
 		# Hide the length label (so it doesn't clutter the timeline)
 		text1 = self.get_canvas_child(item, "tran_text")
@@ -492,6 +520,15 @@
 		self.parent.parent.project.set_project_modified(is_modified=True, refresh_xml=True, type = type_of_event )
 
 
+	def on_button_release_x (self, item, target, event):
+		""" This method drops a clip, and snaps the clip to the nearest valid track """
+
+		# get reference to the canvas, and stop dragging the item
+		canvas = item.get_canvas()
+		canvas.pointer_ungrab (item, event.time)
+
+		return self.drop_canvas_item(item)
+
 	def get_valid_drop(self, x1, y1):
 		""" A clip must be dropped on a track.  This method returns the track 
 		object that is under the clip's current position """
@@ -546,80 +583,47 @@
 		closest_clip = None
 		distance_from_clip = 0.0
 		distance_from_left_clip = 0.0
-		distance_from_right_clip = 0.0
 		distance_from_playhead = 0.0
 
 		# determine the direction of the drag
-		direction = ""
-		if canvas_item.get_bounds().x1 < old_x:
-			direction = "left"
-		else:
-			direction = "right"
+		direction = "left"
 
 		# Look on TOP TRACK
 		# get distance from nearby clips (to the LEFT)
 		nearby_clip_left = self.get_edge_of_clip(canvas_item.get_bounds().x1 / pixels_per_second, "left", self.parent) * pixels_per_second
 		distance_from_left_clip = nearby_clip_left - canvas_item.get_bounds().x1
 
-		# get distance from nearby clips (to the RIGHT)
-		nearby_clip_right = self.get_edge_of_clip(canvas_item.get_bounds().x2 / pixels_per_second, "right", self.parent) * pixels_per_second
-		distance_from_right_clip = (nearby_clip_right + 1) - canvas_item.get_bounds().x2
-
 		# check bottom track (if any)
 		BOTTOM_distance_from_left_clip = 0.0
-		BOTTOM_distance_from_right_clip = 0.0
 		if bottom_track:
 			# BOTTOM TRACK
 			# get distance from nearby clips (to the LEFT)
 			BOTTOM_nearby_clip_left = self.get_edge_of_clip(canvas_item.get_bounds().x1 / pixels_per_second, "left", bottom_track) * pixels_per_second
 			BOTTOM_distance_from_left_clip = BOTTOM_nearby_clip_left - canvas_item.get_bounds().x1
 
-			# get distance from nearby clips (to the RIGHT)
-			BOTTOM_nearby_clip_right = self.get_edge_of_clip(canvas_item.get_bounds().x2 / pixels_per_second, "right", bottom_track) * pixels_per_second
-			BOTTOM_distance_from_right_clip = (BOTTOM_nearby_clip_right + 1) - canvas_item.get_bounds().x2
-
-
 		# distance from the play-head
 		playhead_time = clip_object.parent.parent.play_head_position
 		playhead_pixels = playhead_time * pixels_per_second
 		distance_from_playhead = playhead_pixels - canvas_item.get_bounds().x1
 
-#		print "----------------"
-#		print "distance_from_left_clip: %s" % distance_from_left_clip
-#		print "distance_from_right_clip: %s" % distance_from_right_clip
-#		print "BOTTOM_distance_from_left_clip: %s" % BOTTOM_distance_from_left_clip
-#		print "BOTTOM_distance_from_right_clip: %s" % BOTTOM_distance_from_right_clip
-#		print "distance_from_playhead: %s" % distance_from_playhead
-
 		# limit the left /right snapping to 10 pixels
 		if distance_from_left_clip > 10 or distance_from_left_clip < -10:
 			distance_from_left_clip = 0.0
-		if distance_from_right_clip > 10 or distance_from_right_clip < -10:
-			distance_from_right_clip = 0.0
 		if BOTTOM_distance_from_left_clip > 10 or BOTTOM_distance_from_left_clip < -10:
 			BOTTOM_distance_from_left_clip = 0.0
-		if BOTTOM_distance_from_right_clip > 10 or BOTTOM_distance_from_right_clip < -10:
-			BOTTOM_distance_from_right_clip = 0.0
 		if distance_from_playhead > 10 or distance_from_playhead < -10:
 			distance_from_playhead = 0.0
 
 		# determine which direction and what clip to snap to (based on the direction the clip is moving)
 		if direction == "left" and distance_from_left_clip != 0:
 			distance_from_clip = distance_from_left_clip
+			
 		if direction == "left" and BOTTOM_distance_from_left_clip != 0:
 			distance_from_clip = BOTTOM_distance_from_left_clip
-		elif direction == "right" and distance_from_right_clip != 0:
-			distance_from_clip = distance_from_right_clip
-		elif direction == "right" and BOTTOM_distance_from_right_clip != 0:
-			distance_from_clip = BOTTOM_distance_from_right_clip
 		elif distance_from_left_clip != 0:
 			distance_from_clip = distance_from_left_clip
-		elif distance_from_right_clip != 0:
-			distance_from_clip = distance_from_right_clip
 		elif BOTTOM_distance_from_left_clip != 0:
 			distance_from_clip = BOTTOM_distance_from_left_clip
-		elif distance_from_right_clip != 0:
-			distance_from_clip = distance_from_right_clip
 		elif distance_from_playhead != 0:
 			distance_from_clip = distance_from_playhead
 
@@ -627,7 +631,7 @@
 		return distance_from_clip
 
 
-	def get_edge_of_clip(self, current_position, direction, track): 
+	def get_edge_of_clip(self, current_position, direction, track, threashold=1.0): 
 		""" Get the position of the closest edge of a track """
 
 		if direction == "left":
@@ -637,8 +641,8 @@
 				# get right edge of clip
 				next_edge = float(clip.position_on_track)
 
-				# if clip within 1/2 second distance
-				if abs(current_position - next_edge) <= 1.0:
+				# if clip within 1 second distance
+				if abs(current_position - next_edge) <= threashold:
 					edge = next_edge
 					break
 
@@ -652,8 +656,8 @@
 				# get right edge of clip
 				next_edge = float(clip.position_on_track) + float(clip.length())
 
-				# if clip within 1/2 second distance 
-				if abs(current_position - next_edge) <= 1.0:
+				# if clip within 1 second distance 
+				if abs(current_position - next_edge) <= threashold:
 					edge = next_edge
 					break
 
@@ -697,16 +701,17 @@
 		# loop through clips on the BOTTOM track
 		BOTTOM_left_position = 0.0
 		BOTTOM_found = False
-		for clip in bottom_track.clips:
-			# get right edge of clip
-			clip_start = float(clip.position_on_track)
-			clip_end = float(clip.position_on_track + clip.length())
-
-			# does clip overlap transition
-			if (clip_end >= transition_start and clip_end <= transition_end) or (clip_start >= transition_start and clip_start <= transition_end) or (clip_start >= transition_start and clip_end <= transition_end) or (clip_start <= transition_start and clip_end >= transition_end):
-				BOTTOM_found = True
-				BOTTOM_left_position = clip_start
-				break
+		if bottom_track:
+			for clip in bottom_track.clips:
+				# get right edge of clip
+				clip_start = float(clip.position_on_track)
+				clip_end = float(clip.position_on_track + clip.length())
+	
+				# does clip overlap transition
+				if (clip_end >= transition_start and clip_end <= transition_end) or (clip_start >= transition_start and clip_start <= transition_end) or (clip_start >= transition_start and clip_end <= transition_end) or (clip_start <= transition_start and clip_end >= transition_end):
+					BOTTOM_found = True
+					BOTTOM_left_position = clip_start
+					break
 
 		if TOP_found == True and BOTTOM_found == True:
 			if TOP_left_position <= BOTTOM_left_position:

=== modified file 'openshot/windows/MainGTK.py'
--- openshot/windows/MainGTK.py	2012-09-07 06:17:26 +0000
+++ openshot/windows/MainGTK.py	2012-09-09 19:58:31 +0000
@@ -1379,36 +1379,10 @@
 			
 			# update the track_A (the primary track)
 			if drop_track:
-				self.new_trans_object.update_parent(drop_track)
-				drop_track.reorder_transitions()
 				
-				distance_from_clip = self.new_trans_object.get_snap_difference(self.new_trans_object, self.new_transition)
-
-				# determine the direction of the drag
-				if isSnap:
-					# get pixel settings
-					pixels_per_second = self.project.sequences[0].get_pixels_per_second()
-					distance_from_clip = self.new_trans_object.get_snap_difference(self.new_trans_object, self.new_transition)
-					direction = self.new_trans_object.get_direction(self.new_trans_object, self.new_transition)
-					if direction == "down":
-						self.new_trans_object.reverse = True
-					self.new_trans_object.position_on_track = float(self.new_transition.get_bounds().x1 + distance_from_clip) / pixels_per_second
-					self.new_trans_object.change_direction_image(self.new_transition)
-
-				# re-render the transition
-				parent = self.new_transition.get_parent()
-				if parent:
-
-					# Animate the transition to it's new position
-					bottom_y = drop_track.y_bottom - 20
-					self.new_transition.animate(distance_from_clip, bottom_y - self.new_transition.get_bounds().y1, 1.0, 0.0, False, 200, 4, goocanvas.ANIMATE_FREEZE)
-					
-					# move the clip object on the timeline to correct position (snapping to the y and x of the track)		
-					self.new_transition.translate (distance_from_clip, bottom_y - self.new_transition.get_bounds().y1)
-
-					# mark project as modified
-					self.project.set_project_modified(is_modified=True, refresh_xml=True, type="Added transition")
-					
+				# Drop transition
+				self.new_trans_object.drop_canvas_item (self.new_transition)
+
 			else:
 				# remove this transition (from the project)
 				self.new_trans_object.parent.transitions.remove(self.new_trans_object)