7e56e0e5e698f2c5b4c0a8e8fc38eabd64f43e10
[occt.git] / src / Visual3d / Visual3d_Layer.cxx
1 /***********************************************************************
2
3 FONCTION :
4 --------
5 Classe Visual3d_Layer :
6
7 HISTORIQUE DES MODIFICATIONS :
8 ----------------------------
9 Novembre 1998 : CAL : Creation.
10 ??-??-?? : ??? ; 
11
12 ************************************************************************/
13
14 // for the class
15 #include <Visual3d_Layer.ixx>
16
17 #include <Graphic3d_GraphicDevice.hxx>
18 #include <Visual3d_ViewManager.hxx>
19
20 //-Aliases
21 #define MyViewManager ((Visual3d_ViewManager *) MyPtrViewManager)
22
23 //-Global data definitions
24 static Standard_Boolean theLayerState = Standard_False;
25 static Graphic3d_TypeOfPrimitive theTypeOfPrimitive = Graphic3d_TOP_UNDEFINED;
26
27 #define NO_TRACE_CALL
28 #define NO_DEBUG
29
30 //
31 //-Constructors
32 //
33
34 Visual3d_Layer::Visual3d_Layer (const Handle(Visual3d_ViewManager)& AManager, const Aspect_TypeOfLayer ALayerType, const Standard_Boolean AFlag) {
35 #ifdef TRACE_CALL
36   cout << "Visual3d_Layer::Visual3d_Layer" << endl;
37 #endif
38   MyPtrViewManager  = (void *) AManager.operator->();
39
40   Handle(Aspect_GraphicDriver) agd =
41     (MyViewManager->GraphicDevice ())->GraphicDriver ();
42
43   MyGraphicDriver = *(Handle(Graphic3d_GraphicDriver) *) &agd;
44
45   MyCLayer.layerType  = int (ALayerType);
46   MyCLayer.ptrLayer = NULL;  
47   MyCLayer.ortho[0] = -1.0;
48   MyCLayer.ortho[1] =  1.0;
49   MyCLayer.ortho[2] = -1.0;
50   MyCLayer.ortho[3] =  1.0;
51   MyCLayer.attach   = 0; // Aspect_TOC_BOTTOM_LEFT
52   MyCLayer.sizeDependent  = (AFlag ? 1 : 0);
53
54   MyViewManager->SetLayer (this);
55   MyGraphicDriver->Layer (MyCLayer);
56   MyCLayer.ptrLayer->layerData = this;
57
58 #ifdef DEBUG
59   cout << "Visual3d_Layer::Visual3d_Layer" << endl;
60   call_def_ptrLayer ptrLayer;
61   ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
62   if (ptrLayer == NULL)
63     cout << "\tptrLayer == NULL" << endl;
64   else
65     cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
66 #endif
67 }
68
69 //
70 //-Destructors
71 //
72
73 void Visual3d_Layer::Destroy () {
74 #ifdef TRACE_CALL
75   cout << "Visual3d_Layer::Destroy" << endl;
76 #endif
77   MyGraphicDriver->RemoveLayer (MyCLayer);
78 }
79
80 //-Methods, in order
81
82 void Visual3d_Layer::Begin () {
83 #ifdef TRACE_CALL
84   cout << "Visual3d_Layer::Begin" << endl;
85 #endif
86   if (theLayerState)
87     Visual3d_LayerDefinitionError::Raise
88     ("Layer is already open !");
89
90   MyGraphicDriver->BeginLayer (MyCLayer);
91   theLayerState = Standard_True;
92 }
93
94 void Visual3d_Layer::End () {
95 #ifdef TRACE_CALL
96   cout << "Visual3d_Layer::End" << endl;
97 #endif
98 #ifdef DEBUG
99   cout << "Visual3d_Layer::End" << endl;
100   call_def_ptrLayer ptrLayer;
101   ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
102   if (ptrLayer == NULL)
103     cout << "\tptrLayer == NULL" << endl;
104   else
105     cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
106 #endif
107   if (! theLayerState)
108     Visual3d_LayerDefinitionError::Raise
109     ("Layer is not open !");
110
111   // Flush all graphics
112   MyGraphicDriver->EndLayer ();
113   theLayerState = Standard_False;
114 #ifdef DEBUG
115   cout << "Visual3d_Layer::End" << endl;
116   ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
117   if (ptrLayer == NULL)
118     cout << "\tptrLayer == NULL" << endl;
119   else
120     cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
121 #endif
122 }
123
124 void Visual3d_Layer::Clear () {
125 #ifdef TRACE_CALL
126   cout << "Visual3d_Layer::Clear" << endl;
127 #endif
128   if (theLayerState)
129     Visual3d_LayerDefinitionError::Raise
130     ("Layer is open !");
131
132   MyGraphicDriver->ClearLayer (MyCLayer);
133 }
134
135 //
136 //-Graphic definition methods
137 //
138
139 void Visual3d_Layer::BeginPolyline () {
140 #ifdef TRACE_CALL
141   cout << "Visual3d_Layer::BeginPolyline" << endl;
142 #endif
143   if (! theLayerState)
144     Visual3d_LayerDefinitionError::Raise
145     ("Layer is not open !");
146   if( theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
147     Visual3d_LayerDefinitionError::Raise
148     ("One primitive is already open !");
149
150   theTypeOfPrimitive = Graphic3d_TOP_POLYLINE;
151
152   MyGraphicDriver->BeginPolyline2d ();
153 }
154
155 void Visual3d_Layer::BeginPolygon () {
156 #ifdef TRACE_CALL
157   cout << "Visual3d_Layer::BeginPolygon" << endl;
158 #endif
159   if (! theLayerState)
160     Visual3d_LayerDefinitionError::Raise
161     ("Layer is not open !");
162   if( theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
163     Visual3d_LayerDefinitionError::Raise
164     ("One primitive is already open !");
165
166   theTypeOfPrimitive = Graphic3d_TOP_POLYGON;
167
168   MyGraphicDriver->BeginPolygon2d ();
169 }
170
171 void Visual3d_Layer::AddVertex (const Standard_Real X, const Standard_Real Y, const Standard_Boolean AFlag) {
172 #ifdef TRACE_CALL
173   cout << "Visual3d_Layer::AddVertex" << endl;
174 #endif
175   Standard_ShortReal x = Standard_ShortReal(X);
176   Standard_ShortReal y = Standard_ShortReal(Y);
177
178   switch (theTypeOfPrimitive) {
179     case Graphic3d_TOP_POLYLINE :
180       if( AFlag ) MyGraphicDriver->Draw (x, y);
181       else        MyGraphicDriver->Move (x, y);
182       break;
183     case Graphic3d_TOP_POLYGON :
184       MyGraphicDriver->Edge (x, y);
185       break;
186     default:
187       Visual3d_LayerDefinitionError::Raise
188         ("Bad Primitive type!");
189   }
190 }
191
192 void Visual3d_Layer::ClosePrimitive () {
193 #ifdef TRACE_CALL
194   cout << "Visual3d_Layer::ClosePrimitive" << endl;
195 #endif
196   switch (theTypeOfPrimitive) {
197     case Graphic3d_TOP_POLYLINE :
198       MyGraphicDriver->EndPolyline2d ();
199       break;
200     case Graphic3d_TOP_POLYGON :
201       MyGraphicDriver->EndPolygon2d ();
202       break;
203     default:
204       Visual3d_LayerDefinitionError::Raise
205         ("Bad Primitive type!");
206   }
207
208   theTypeOfPrimitive = Graphic3d_TOP_UNDEFINED;
209 }
210
211 void Visual3d_Layer::DrawRectangle (const Standard_Real X, const Standard_Real Y, const Standard_Real Width, const Standard_Real Height) {
212 #ifdef TRACE_CALL
213   cout << "Visual3d_Layer::DrawRectangle" << endl;
214 #endif
215   if (! theLayerState)
216     Visual3d_LayerDefinitionError::Raise
217     ("Layer is not open !");
218   if (theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
219     Visual3d_LayerDefinitionError::Raise
220     ("One primitive is already open !");
221
222   Standard_ShortReal x = Standard_ShortReal (X);
223   Standard_ShortReal y = Standard_ShortReal (Y);
224   Standard_ShortReal width = Standard_ShortReal (Width);
225   Standard_ShortReal height = Standard_ShortReal (Height);
226   MyGraphicDriver->Rectangle (x, y, width, height);
227 }
228
229 #ifdef WNT
230 #undef DrawText
231 #endif
232 void Visual3d_Layer::DrawText (const Standard_CString AText, const Standard_Real X, const Standard_Real Y, const Standard_Real AHeight) {
233 #ifdef TRACE_CALL
234   cout << "Visual3d_Layer::DrawText" << endl;
235 #endif
236   if (! theLayerState)
237     Visual3d_LayerDefinitionError::Raise
238     ("Layer is not open !");
239   if (theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
240     Visual3d_LayerDefinitionError::Raise
241     ("One primitive is already open !");
242
243   Standard_ShortReal x = Standard_ShortReal (X);
244   Standard_ShortReal y = Standard_ShortReal (Y);
245   Standard_ShortReal height = Standard_ShortReal (AHeight);
246   MyGraphicDriver->Text (AText, x, y, height);
247 }
248
249 void Visual3d_Layer::SetColor (const Quantity_Color& AColor) {
250 #ifdef TRACE_CALL
251   cout << "Visual3d_Layer::SetColor" << endl;
252 #endif
253   if (! theLayerState)
254     Visual3d_LayerDefinitionError::Raise
255     ("Layer is not open !");
256
257   Standard_ShortReal r = Standard_ShortReal (AColor.Red());
258   Standard_ShortReal g = Standard_ShortReal (AColor.Green());
259   Standard_ShortReal b = Standard_ShortReal (AColor.Blue());
260 #ifdef TRACE_CALL
261   cout << "\tSetColor : " << r << ", " << g << ", " << b << endl;
262 #endif
263   MyGraphicDriver->SetColor (r, g, b);
264 }
265
266 void Visual3d_Layer::SetTransparency (const Standard_ShortReal ATransparency) {
267 #ifdef TRACE_CALL
268   cout << "Visual3d_Layer::SetTransparency" << endl;
269 #endif
270   if (! theLayerState)
271     Visual3d_LayerDefinitionError::Raise
272     ("Layer is not open !");
273
274 #ifdef TRACE_CALL
275   cout << "\tSetTransparency : " << ATransparency << endl;
276 #endif
277   MyGraphicDriver->SetTransparency (ATransparency);
278 }
279
280 void Visual3d_Layer::UnsetTransparency () {
281 #ifdef TRACE_CALL
282   cout << "Visual3d_Layer::UnsetTransparency" << endl;
283 #endif
284   if (! theLayerState)
285     Visual3d_LayerDefinitionError::Raise
286     ("Layer is not open !");
287
288 #ifdef TRACE_CALL
289   cout << "\tUnsetTransparency" << endl;
290 #endif
291   MyGraphicDriver->UnsetTransparency ();
292 }
293
294 #ifdef WNT
295 #undef DrawText
296 #endif
297 void Visual3d_Layer::TextSize (const Standard_CString AText,  const Standard_Real AHeight, Standard_Real& AWidth, Standard_Real& AnAscent, Standard_Real& ADescent) const {
298   Standard_ShortReal aWidth, anAscent, aDescent;
299
300 #ifdef TRACE_CALL
301   cout << "Visual3d_Layer::TextSize" << endl;
302 #endif
303   if (! theLayerState)
304     Visual3d_LayerDefinitionError::Raise
305     ("Layer is not open !");
306   if (theTypeOfPrimitive != Graphic3d_TOP_UNDEFINED )
307     Visual3d_LayerDefinitionError::Raise
308     ("One primitive is already open !");
309
310   MyGraphicDriver->TextSize (AText, AHeight, aWidth, anAscent, aDescent);
311   AWidth = aWidth;
312   AnAscent = anAscent;
313   ADescent = aDescent;
314 }
315
316 void Visual3d_Layer::SetLineAttributes (const Aspect_TypeOfLine AType, const Standard_Real AWidth) {
317 #ifdef TRACE_CALL
318   cout << "Visual3d_Layer::SetLineAttributes" << endl;
319 #endif
320   if (! theLayerState)
321     Visual3d_LayerDefinitionError::Raise
322     ("Layer is not open !");
323
324   Standard_Integer type = AType;
325   Standard_ShortReal width = Standard_ShortReal (AWidth);
326 #ifdef TRACE_CALL
327   cout << "\tSetLineAttributes : " << type << ", " << width << endl;
328 #endif
329   MyGraphicDriver->SetLineAttributes (type, width);
330 }
331
332 void Visual3d_Layer::SetTextAttributes (const Standard_CString AFont, const Aspect_TypeOfDisplayText AType, const Quantity_Color& AColor) {
333 #ifdef TRACE_CALL
334   cout << "Visual3d_Layer::SetTextAttributes" << endl;
335 #endif
336   if (! theLayerState)
337     Visual3d_LayerDefinitionError::Raise
338     ("Layer is not open !");
339
340   Standard_CString font = (char*)AFont;
341   Standard_Integer type = AType;
342   Standard_ShortReal r = Standard_ShortReal (AColor.Red());
343   Standard_ShortReal g = Standard_ShortReal (AColor.Green());
344   Standard_ShortReal b = Standard_ShortReal (AColor.Blue());
345 #ifdef TRACE_CALL
346   cout << "\tSetTextAttributes : " << font << ", " << type << endl;
347   cout << "\t                    " << r << ", " << g << ", " << b << endl;
348 #endif
349   MyGraphicDriver->SetTextAttributes (font, type, r, g, b);
350 }
351
352 void Visual3d_Layer::SetOrtho (const Standard_Real Left, const Standard_Real Right, const Standard_Real Bottom, const Standard_Real Top, const Aspect_TypeOfConstraint Attach) {
353 #ifdef TRACE_CALL
354   cout << "Visual3d_Layer::SetOrtho" << endl;
355 #endif
356
357   MyCLayer.ortho[0] = Standard_ShortReal (Left);
358   MyCLayer.ortho[1] = Standard_ShortReal (Right);
359   MyCLayer.ortho[2] = Standard_ShortReal (Bottom);
360   MyCLayer.ortho[3] = Standard_ShortReal (Top);
361   MyCLayer.attach   = Standard_Integer (Attach);
362 }
363
364 void Visual3d_Layer::SetViewport (const Standard_Integer Width, const Standard_Integer Height) {
365 #ifdef TRACE_CALL
366   cout << "Visual3d_Layer::SetViewport" << endl;
367 #endif
368
369   MyCLayer.viewport[0]  = float (Width);
370   MyCLayer.viewport[1]  = float (Height);
371 }
372
373 Aspect_CLayer2d Visual3d_Layer::CLayer () const {
374 #ifdef TRACE_CALL
375   cout << "Visual3d_Layer::CLayer" << endl;
376 #endif
377 #ifdef DEBUG
378   cout << "Visual3d_Layer::CLayer" << endl;
379   call_def_ptrLayer ptrLayer;
380   ptrLayer = (call_def_ptrLayer) MyCLayer.ptrLayer;
381   if (ptrLayer == NULL)
382     cout << "\tptrLayer == NULL" << endl;
383   else
384     cout << "\tptrLayer->listIndex = " << ptrLayer->listIndex << endl;
385 #endif
386   return MyCLayer;
387 }
388
389 Aspect_TypeOfLayer Visual3d_Layer::Type () const {
390 #ifdef TRACE_CALL
391   cout << "Visual3d_Layer::Type" << endl;
392 #endif
393   return Aspect_TypeOfLayer (MyCLayer.layerType);
394 }
395
396
397 void Visual3d_Layer::AddLayerItem( const Handle(Visual3d_LayerItem&) Item )
398 {
399   MyListOfLayerItems.Append( Item );
400 }
401
402 void Visual3d_Layer::RemoveLayerItem( const Handle(Visual3d_LayerItem&) Item )
403 {
404   if ( MyListOfLayerItems.IsEmpty() )
405     return;
406
407   Visual3d_NListOfLayerItem::Iterator it( MyListOfLayerItems );
408   for( ; it.More(); it.Next() ) {
409     if ( it.Value() == Item ) {
410       MyListOfLayerItems.Remove( it );
411       break;
412     }
413   }
414 }
415
416 void Visual3d_Layer::RemoveAllLayerItems()
417 {
418   MyListOfLayerItems.Clear();
419 }
420
421 const Visual3d_NListOfLayerItem& Visual3d_Layer::GetLayerItemList() const
422 {
423   return MyListOfLayerItems;
424 }
425
426 void Visual3d_Layer::RenderLayerItems() const
427 {
428   theLayerState = Standard_True;
429
430   const Visual3d_NListOfLayerItem& items = GetLayerItemList();
431   Visual3d_NListOfLayerItem::Iterator it(items);
432   for(; it.More(); it.Next() ) {
433     it.Value()->RedrawLayerPrs();
434   }
435
436   theLayerState = Standard_False;
437 }