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