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