View Single Post
  #3  
Old 03-03-2011, 05:34 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
For each object in the scene you could get the 2D screen coordinate corresponding to the object's position using the <window>.worldToScreen command. Then you could see if that 2D position falls between the values of the selection box. If it does, that means it was selected.

This world require your selection box includes the objects center to register as a selection:
Code:
import viz
import viztask
viz.go()

viz.mouse(viz.OFF)

objects = []
for x in (-1,0,1):
	for y in (1,2,3):
		object = viz.addChild('white_ball.wrl',pos=[x,y,5],cache=viz.CACHE_COPY)
		objects.append(object)
	
	
def DrawLineTask():
	while True:
		viz.startlayer(viz.QUADS)
		viz.vertexcolor(viz.RED)
		
		yield viztask.waitMouseDown(viz.MOUSEBUTTON_LEFT)
		pos = viz.mouse.getPosition()
		viz.vertex(pos[0],pos[1],0) #0
		viz.vertex(pos[0],pos[1],0) #1
		viz.vertex(pos[0],pos[1],0) #2
		viz.vertex(pos[0],pos[1],0) #3
		line = viz.endlayer(parent=viz.SCREEN)
		line.alpha(0.2)

		VertexLink = vizact.ontimer(.01, copyTransform, viz.Mouse, line.Vertex(0), line.Vertex(1), line.Vertex(2), line.Vertex(3))

		yield viztask.waitMouseUp(viz.MOUSEBUTTON_LEFT)
		pos2 = viz.mouse.getPosition()
		findSelected(pos,pos2)
		VertexLink.remove()
		line.remove()
		
		yield viztask.waitTime(0.5)
		for object in objects:
			object.color(viz.WHITE)
	
viztask.schedule( DrawLineTask() )


def copyTransform(mouse, vertex_a, vertex_b, vertex_c, vertex_d):
	M = mouse.getMatrix()
	A = vertex_a.getMatrix()
	A[12] = M[12]
	A[13] = M[13]
	vertex_c.setMatrix(A)
	A = vertex_a.getMatrix()
	A[12] = M[12]
	vertex_b.setMatrix(A)
	A = vertex_a.getMatrix()
	A[13] = M[13]
	vertex_d.setMatrix(A)


def findSelected(pos,pos2):
	
	for object in objects:
		x,y,d = viz.MainWindow.worldToScreen(object.getPosition())

		if pos[0] < pos2[0]:
			low_x = pos[0]
			high_x = pos2[0]
		else:
			low_x = pos2[0]
			high_x = pos[0]
		if pos[1] < pos2[1]:
			low_y = pos[1]
			high_y = pos2[1]
		else:
			low_y = pos2[1]
			high_y = pos[1]
			
		if x > low_x and x < high_x and y > low_y and y< high_y:
			object.color(viz.RED)
Reply With Quote