PDA

View Full Version : Mouse Selection Area Question


Renato Lima
03-02-2011, 11:10 AM
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.

Renato Lima
03-02-2011, 01:24 PM
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?

Jeff
03-03-2011, 05:34 PM
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:
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)

Renato Lima
03-04-2011, 02:54 AM
Thank you. It did the trick!