1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import Numeric
24 from copy import deepcopy
25 from opengltk.OpenGL import GL
26 from DejaVu.Common2d3dObject import Common2d3dObject
27
28
30 """Base class for 2d inserts that can be displayed in a camera
31 """
32 keywords = Common2d3dObject.keywords + [
33 'anchor',
34 'position',
35 'size',
36 ]
37
38 - def __init__(self, name='insert2d', check=1, **kw):
39
40
41 self.anchor = [1, 0]
42 self.position = [1, 0]
43
44 self.size = [40, 30]
45
46 self.polygonContour = None
47
48
49 self.lastPickEventTime = 0
50 self.isMoving = None
51
52
53 apply( Common2d3dObject.__init__, (self, name, check), kw)
54
55 self.initialAnchor = deepcopy(self.anchor)
56 self.initialPosition = deepcopy(self.position)
57 self.initialSize = deepcopy(self.size)
58
59
60 - def Set(self, check=1, redo=1, updateOwnGui=True, **kw):
61 """set data for this object
62 check=1 : verify that all the keywords present can be handle by this func
63 redo=1 : append self to viewer.objectsNeedingRedo
64 updateOwnGui=True : allow to update owngui at the end this func
65 """
66
67 redrawFlag, \
68 updateOwnGuiFlag, \
69 redoViewerDisplayListFlag, \
70 redoDisplayListFlag, \
71 redoTemplateFlag, \
72 redoDisplayChildrenListFlag = apply( Common2d3dObject.Set, (self, check, 0), kw)
73
74 anchor = kw.get( 'anchor')
75 if anchor \
76 and (anchor[0] >= 0.) and (anchor[0] <= 1.) \
77 and (anchor[1] >= 0.) and (anchor[1] <= 1.):
78 self.anchor = anchor
79 redoDisplayListFlag = True
80
81 position = kw.get( 'position')
82 if position \
83 and (position[0] >= 0.) and (position[0] <= 1.) \
84 and (position[1] >= 0.) and (position[1] <= 1.):
85 self.position = position
86 redoDisplayListFlag = True
87
88 size = kw.get( 'size')
89 if size \
90 and (size[0] >= 0.) \
91 and (size[1] >= 0.):
92 self.size = size
93 redoDisplayListFlag = True
94
95 if redo and self.viewer:
96 if redoTemplateFlag is True:
97 self.redoTemplate()
98 redrawFlag = True
99 if redoDisplayListFlag is True:
100 if self not in self.viewer.objectsNeedingRedo.keys():
101 self.viewer.objectsNeedingRedo[self] = None
102 redrawFlag = True
103 if redoDisplayChildrenListFlag is True:
104 lObjectsNeedingRedo = self.viewer.objectsNeedingRedo.keys()
105 for child in self.AllObjects():
106 if child not in lObjectsNeedingRedo:
107 self.viewer.objectsNeedingRedo[child] = None
108 redrawFlag = True
109 if redoViewerDisplayListFlag is True:
110 self.viewer.deleteOpenglList()
111 redrawFlag = True
112 if updateOwnGui is True and updateOwnGuiFlag is True and self.ownGui is not None:
113 self.updateOwnGui()
114 if redrawFlag is True:
115 self.viewer.Redraw()
116 return redrawFlag, updateOwnGuiFlag, redoViewerDisplayListFlag, redoDisplayListFlag, redoTemplateFlag, redoDisplayChildrenListFlag
117
118
119
121 """return a dictionary describing this object's state
122 This dictionary can be passed to the Set method to restore the object's state
123 """
124 state = Common2d3dObject.getState(self).copy()
125 state.update( {
126 'anchor': self.anchor,
127 'position': self.position,
128 'size': self.size,
129 } )
130 return state
131
132
133 - def setViewer(self, viewer, buildDisplayList):
138
139
141 """set the projection matrices, the subclass must call this in their Draw func,
142 otherwise tile rendering and stereo won't work
143 """
144
145
146 GL.glMatrixMode(GL.GL_PROJECTION)
147 if self.viewer.tileRender:
148 tile = self.getTile()
149 GL.glOrtho(float(tile[0]), float(tile[1]), float(tile[2]), float(tile[3]), -1, 1)
150 else:
151 GL.glOrtho(0,
152 float(self.viewer.currentCamera.width),
153 0,
154 float(self.viewer.currentCamera.height),
155 -1, 1)
156 GL.glMatrixMode(GL.GL_MODELVIEW)
157
158
160 """called by the picking process to operate the selection
161 """
162
163 if self.polygonContour is not None:
164
165 GL.glMatrixMode(GL.GL_PROJECTION)
166 GL.glPushMatrix()
167
168 GL.glLoadMatrixf(self.viewer.currentCamera.pickMatrix)
169 GL.glOrtho(0, float(self.viewer.currentCamera.width),
170 0, float(self.viewer.currentCamera.height), -1, 1)
171 GL.glMatrixMode(GL.GL_MODELVIEW)
172 GL.glPushMatrix()
173 GL.glLoadIdentity()
174 GL.glPolygonMode(GL.GL_FRONT, GL.GL_FILL)
175 GL.glPushName(0)
176 GL.glBegin(GL.GL_QUADS)
177 GL.glVertex2fv(self.polygonContour[0])
178 GL.glVertex2fv(self.polygonContour[1])
179 GL.glVertex2fv(self.polygonContour[2])
180 GL.glVertex2fv(self.polygonContour[3])
181 GL.glEnd()
182 GL.glPopName()
183
184 GL.glMatrixMode(GL.GL_PROJECTION)
185 GL.glPopMatrix()
186 GL.glMatrixMode(GL.GL_MODELVIEW)
187 GL.glPopMatrix()
188
189
191 """the trackball transmit the translation info
192 """
193 self.size[0] = event.x - self.coord2d[0]
194 self.size[1] = self.coord2d[1] - event.y
195
196 if self.size[0] < 1:
197 self.size[0] = 1
198 if self.size[1] < 1:
199 self.size[1] = 1
200
201 if self.size[0] > self.viewer.currentCamera.width:
202 self.size[0] = self.viewer.currentCamera.width
203 if self.size[1] > self.viewer.currentCamera.height:
204 self.size[1] = self.viewer.currentCamera.height
205
206 if self.viewer:
207 self.viewer.objectsNeedingRedo[self] = None
208
209
211 """the trackball transmit the translation info
212 """
213
214 self.position = [ event.x / float(self.viewer.currentCamera.width),
215 event.y / float(self.viewer.currentCamera.height)
216 ]
217
218 if self.position[0] < 0:
219 self.position[0] = 0
220 elif self.position[0] > 1:
221 self.position[0] = 1
222
223 if self.position[1] < 0:
224 self.position[1] = 0
225 elif self.position[1] > 1:
226 self.position[1] = 1
227
228 self.viewer.objectsNeedingRedo[self] = None
229
230
232 insert2dTopLeftFromScreenTopleft0 = self.position[0] * self.viewer.currentCamera.width \
233 - self.anchor[0] * self.size[0]
234 insert2dTopLeftFromScreenTopleft1 = self.position[1] * self.viewer.currentCamera.height \
235 - self.anchor[1] * self.size[1]
236
237 self.anchor = [ (event.x - insert2dTopLeftFromScreenTopleft0) / float(self.size[0]),
238 (event.y - insert2dTopLeftFromScreenTopleft1) / float(self.size[1])
239 ]
240
241 self.position = [ event.x / float(self.viewer.currentCamera.width),
242 event.y / float(self.viewer.currentCamera.height)
243 ]
244
245
247 self.anchor = deepcopy(self.initialAnchor)
248 self.position = deepcopy(self.initialPosition)
249 if self.viewer:
250 self.viewer.objectsNeedingRedo[self] = None
251
252
254
255 self.size = deepcopy(self.initialSize)
256 if self.viewer:
257 self.viewer.objectsNeedingRedo[self] = None
258
259
265
266
268 """the trackball transmit the event info
269 """
270 if self.isMoving is not None:
271 if self.isMoving is True:
272 self.setPosition(event)
273 else:
274 self.setSize(event)
275 self.viewer.Redraw()
276
277
279 """to be overidden
280 """
281 pass
282
283
285
286
287
288
289
290
291
292
293
294
295
296 if ( len(pick.hits) == 1) and pick.hits.has_key(self):
297 if self.viewer.currentObject != self:
298
299
300 self.viewer.SetCurrentObject(self)
301 self.isMoving = True
302 self.calculateAnchorAndPosition(pick.event)
303 elif pick.event.time - self.lastPickEventTime < 200:
304 self.viewer.SetCurrentObject(self.viewer.rootObject)
305 self.respondToDoubleClick(pick.event)
306 elif pick.hits[self][0][0] == 1:
307
308
309 self.isMoving = False
310 elif pick.hits[self][0][0] == 0:
311
312
313 self.isMoving = True
314
315 self.calculateAnchorAndPosition(pick.event)
316
317 if self.viewer:
318 self.viewer.objectsNeedingRedo[self] = None
319
320 elif self.viewer.currentObject == self:
321
322 self.isMoving = None
323 self.viewer.SetCurrentObject(self.viewer.rootObject)
324
325 self.lastPickEventTime = pick.event.time
326