File: mouse_drag.html

package info (click to toggle)
python-visual 3.2.9-4.1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,796 kB
  • ctags: 2,664
  • sloc: cpp: 11,958; sh: 8,185; python: 3,709; ansic: 480; makefile: 311
file content (137 lines) | stat: -rw-r--r-- 8,649 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<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">Here is the sequence of mouse events involved in dragging something:</p>
<p class="Normal">1) m1.press is True when you depress the mouse button (it is 
  'left' if left button; nonzero is True in Python).</p>
<p class="Normal"><br>
  2) m1.drag is True when the mouse coordinates change from what they were at 
  the time of m1.press.<br>
  At the time of the drag event, the mouse position is reported to be what it 
  was at the time of the press event, so that the dragging can start at the place 
  where the user first clicked. If the mouse is in motion at the time of the press 
  event, it is quite possible that the next position seen by the computer, at 
  the time of the drag event, could be quite far from the click position. This 
  is why the position of the drag event is reported as though it occurred at the 
  press location.</p>
<p class="Normal"><br>
  3) No events occur while dragging; you continually use scene.mouse.pos to update 
  what you're dragging.</p>
<p class="Normal"><br>
  4) m1.drop is True when you release the mouse button.</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">&nbsp;&nbsp;&nbsp;&nbsp;if scene.mouse.events: </p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m1 = scene.mouse.getevent() 
  # obtain drag or drop event</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if m1.drag 
  and m1.pick == ball: # if clicked on the ball</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drag_pos 
  = m1.pickpos # where on the ball the mouse was</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pick 
  = m1.pick # pick is now True (nonzero)</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scene.cursor.visible 
  = 0 # make cursor invisible </p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif m1.drop: 
  # released the mouse button at end of drag</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pick 
  = None # end dragging (None is False)</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scene.cursor.visible 
  = 1 # cursor visible again</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;if pick: </p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_pos = scene.mouse.project(normal=(0,1,0)) 
  # project onto xz plane</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if new_pos 
  != drag_pos: # if the mouse has moved since last position</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pick.pos 
  += new_pos - drag_pos # offset for where the ball was clicked</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drag_pos 
  = new_pos # update drag position</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 &quot;new&quot; mouse position is in fact different from the previous position 
  before processing the &quot;move&quot;, 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>
<p class="Normal">Only some of the VPython objects can be &quot;picked&quot; by 
  clicking them, including sphere, box, and cylinder. Here is a more general routine 
  which lets you drag either the tail or the tip of an arrow:</p>
<h2 class="program0">scene.range = 10 # fixed size, no autoscaling</h2>
<p class="program"> </p>
<p class="program"><span class="program">pointer = arrow(pos=(0,4,0), axis=(3,2,0), 
  color=color.yellow)</span>)</p>
<p class="program"><span class="program">tolerance = 0.3 # must click within this 
  distance of tail or tip</span></p>
<p class="program"><span class="program">drag = None # have not selected tail 
  or tip of arrow</span></p>
<p class="program">while 1: </p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;if scene.mouse.events: </p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m1 = scene.mouse.getevent() 
  # obtain press or drag or drop event</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if m1.press:</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="program">if 
  mag(pointer.pos-m1.pos) &lt;= tolerance:</span></p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="program">drag 
  = 'tail' # pressed near tail of arrow</span></p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="program">elif 
  mag((pointer.pos+pointer.axis)-m1.pos) &lt;= tolerance:</span></p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="program">drag 
  = 'tip' # pressed near tip of arrow</span></p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="program">drag_pos 
  = m1.pos</span> # save press location</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="program">elif 
  m1.drag and drag: # if drag event and something to drag</span></p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scene.cursor.visible 
  = 0 # make cursor invisible </p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif m1.drop: 
  # released the mouse button at end of drag</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drag 
  = None # end dragging (None is False)</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scene.cursor.visible 
  = 1 # cursor visible again</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;if drag: </p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new_pos = scene.mouse.pos</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if new_pos 
  != drag_pos: # if the mouse has moved since last position</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displace 
  = new_pos - drag_pos # how much the mouse moved</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;drag_pos 
  = new_pos # update drag position</p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if 
  drag == 'tail':<span class="program"></span></p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="program">pointer.pos 
  += displace # displace the tail</span></p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<span class="program"></span></p>
<p class="program">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="program">pointer.axis 
  += displace # displace the tip</span></p>
<p class="program">&nbsp;</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">&nbsp;</h2>
  <div> 
      <h2 class="program0"> </h2>
    </div>
    <h2 class="program0">&nbsp;</h2>
  </div>
</div>
</body>
</html>