WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-02-2011, 11:10 AM
Renato Lima Renato Lima is offline
Member
 
Join Date: Sep 2010
Posts: 54
Question Mouse Selection Area Question

Is there any way that I can create a selection box with a mouse click (just like selecting icons on the desktop with the mouse) in vizard? Is there any builtin function for doing this? Ideally, the selection are would return the objects that were selected.

Does anyone have any suggestions for doing this?


Thank you.
Reply With Quote
  #2  
Old 03-02-2011, 01:24 PM
Renato Lima Renato Lima is offline
Member
 
Join Date: Sep 2010
Posts: 54
This is what I managed to do so far:


import viz
import viztask
viz.go()

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)

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)
VertexLink.remove()
line.remove()

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)


How can I make it now do the selection under the square?
Reply With Quote
  #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
  #4  
Old 03-04-2011, 02:54 AM
Renato Lima Renato Lima is offline
Member
 
Join Date: Sep 2010
Posts: 54
Thank you. It did the trick!
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mouse pointer problem Albert Russel Vizard 2 02-21-2011 01:10 AM
Warning mouse() -> Set mouse() of window johannes2 Vizard 1 08-19-2010 02:11 PM
how to remove velocity when mouse is disabled? jvacare1 Vizard 2 02-18-2010 10:25 AM
Navigating an avatar using mouse position(2D) in 3D environment james007 Vizard 1 10-16-2009 11:29 AM
Setting up a 'reaction area' Karthi Vizard 4 01-29-2004 04:39 PM


All times are GMT -7. The time now is 01:34 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC