#p.vpy more comments and edits 26nov15 #install slorot and graffiti #This still uses "polling" see http://vpython.org/contents/docs/mouse_drag.html #callbacks in v6 allows callbacks. Finally. #planar.py is from Examples, w/o author information #additional comments, gf 1nov15 from visual import * from random import uniform, randint scene.forward = (-0.25,-0.25,-1) nboxes = 8 nlinks = 16 maxgrid = 10 # Create some random cylinders: nodes = [] for t in arange(-pi,pi,2*pi/nboxes): b = cylinder( pos=(10*sin(t),0,10*cos(t)) ) b.color = b.icolor = (0.5,0.5,1) height = uniform(0.5,4) b.axis = (0,height,0) nodes.append( b ) # Create some random links: links = [] for l in range(nlinks): while True: i = randint(0,nboxes-1) j = randint(0,nboxes-1) if i != j: break c = curve( ends=[nodes[i].pos,nodes[j].pos], radius=0.2 ) c.red = 0 c.green = uniform(0.3,1) c.blue = 0 c.pos = c.ends links.append(c) # Draw a grid: for i in range(maxgrid+1): curve(pos=[(2*i-maxgrid,0,-maxgrid),(2*i-maxgrid,0,maxgrid)], color=color.cyan) curve(pos=[(-maxgrid,0,2*i-maxgrid),(maxgrid,0,2*i-maxgrid)], color=color.cyan) box(pos=(0,-0.6,0),width=2*maxgrid,length=2*maxgrid,height=1,color=(0,0,0.1)) # Drag and drop loop mydrag = None #originally just drag. Pick's up scene.mouse.getevent().pick anything in queue while True: rate(100) if scene.mouse.events: # the events queue is a list of events, skip if empty c = scene.mouse.getevent() # what the next event in the queue is if mydrag and (c.drop or c.click): # drop the selected object newpos = c.project(normal=scene.up, d=yoffset)+offset if abs(newpos.x)<=maxgrid and abs(newpos.z)<=maxgrid: mydrag.pos = newpos mydrag.color = mydrag.icolor mydrag = None elif c.pick and hasattr(c.pick,"icolor"): # pick up the object mydrag = c.pick #first thing done when entering while True loop mydrag.color = color.white yoffset = c.pickpos.y offset = mydrag.pos-c.project(normal=scene.up, d=yoffset) if mydrag: newpos = scene.mouse.project(normal=scene.up, d=yoffset)+offset if abs(newpos.x)<=maxgrid and abs(newpos.z)<=maxgrid: mydrag.pos = newpos #GF not the clearest way of writing a contingency branched mouse input for lnk in links: lnk.pos = lnk.ends