1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="VisualRef.css" CHARSET="ISO-8859-1" TYPE="text/css">
</head>
<body bgcolor="#FFFFFF">
<p class="Normal"><font color="#0000A0">Drag example</font></p>
<p class="Normal">You can program dragging with the mouse simply by continually
reading the current value of <span class="attribute">scene.mouse.pos</span>.
Here is a complete routine for dragging a sphere with the left button down.
Note the statements for making the cursor invisible during the drag.</p>
<h2 class="program0">scene.range = 10 # fixed size, no autoscaling</h2>
<p class="program"> </p>
<p class="program">ball = sphere(pos=(-5,0,0), radius=1., color=color.cyan)</p>
<p class="program">cube = box(pos=(+5,0,0), size=(2,2,2), color=color.red)</p>
<p class="program">pick = None # no object picked out of the scene yet</p>
<p class="program">while 1: </p>
<p class="program"> if scene.mouse.events: </p>
<p class="program"> m1 = scene.mouse.getevent()
# obtain drag or drop event</p>
<p class="program"> if m1.drag
and m1.pick == ball: </p>
<p class="program"> drag_pos
= m1.pickpos</p>
<p class="program"> pick
= m1.pick</p>
<p class="program"> scene.cursor.visible
= 0 # make cursor invisible </p>
<p class="program"> elif m1.drop:
</p>
<p class="program"> pick
= None # end dragging</p>
<p class="program"> scene.cursor.visible
= 1 # cursor visible</p>
<p class="program"> if pick: </p>
<p class="program"> new_pos = scene.mouse.project(normal=(0,0,1))</p>
<p class="program"> if new_pos
!= drag_pos: </p>
<p class="program"> pick.pos
+= new_pos - drag_pos</p>
<p class="program"> drag_pos
= new_pos</p>
<p class="Normal">If you do a lot of processing of each mouse movement, or you
are leaving a trail behind the moving object, you may need to check whether
the "new" mouse position is in fact different from the previous position
before processing the "move", as is done in the example above. For
example, a trail drawn with a curve object that contains a huge number of points
all at the same location may not display properly.</p>
<div>
<div>
<p class="Normal">Choose Back, or <a href="mouse.html">Go to top of mouse
documentation</a></p>
<div>
<h2 class="program0"> </h2>
</div>
<h2 class="program0"> </h2>
<div>
<h2 class="program0"> </h2>
</div>
<h2 class="program0"> </h2>
</div>
</div>
</body>
</html>
|