0031458: Visualization - refine classes across Prs3d and StdPrs packages
[occt.git] / src / AIS / AIS_TextLabel.cxx
CommitLineData
29e2c6d2 1// Created on: 2014-11-10
2// Copyright (c) 2014 OPEN CASCADE SAS
3//
4// This file is part of Open CASCADE Technology software library.
5//
6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
11//
12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
14
15#include <AIS_TextLabel.hxx>
16
3f1eb0ab 17#include <AIS_InteractiveContext.hxx>
18#include <Font_FTFont.hxx>
19#include <Font_FontMgr.hxx>
20#include <Font_Rect.hxx>
29e2c6d2 21#include <Graphic3d_AspectText3d.hxx>
3f1eb0ab 22#include <Graphic3d_RenderingParams.hxx>
29e2c6d2 23
24#include <Prs3d_Text.hxx>
25#include <Prs3d_TextAspect.hxx>
26
27#include <Select3D_SensitivePoint.hxx>
28#include <SelectMgr_Selection.hxx>
29#include <SelectMgr_EntityOwner.hxx>
30
3f1eb0ab 31#include <V3d_Viewer.hxx>
32
92efcf78 33IMPLEMENT_STANDARD_RTTIEXT(AIS_TextLabel,AIS_InteractiveObject)
34
29e2c6d2 35//=======================================================================
36//function : AIS_TextLabel
37//purpose :
38//=======================================================================
39AIS_TextLabel::AIS_TextLabel()
ce01ec26 40: myText ("?"),
3f1eb0ab 41 myHasOrientation3D (Standard_False),
42 myHasFlipping (Standard_False)
29e2c6d2 43{
44 myDrawer->SetTextAspect (new Prs3d_TextAspect());
cc99be36 45 myDrawer->SetDisplayMode (0);
29e2c6d2 46}
47
48//=======================================================================
49//function : SetColor
50//purpose :
51//=======================================================================
52void AIS_TextLabel::SetColor (const Quantity_Color& theColor)
53{
54 hasOwnColor = Standard_True;
f838dac4 55 myDrawer->SetColor (theColor);
29e2c6d2 56 myDrawer->TextAspect()->SetColor (theColor);
226fce20 57 SynchronizeAspects();
29e2c6d2 58}
59
60//=======================================================================
85719f0e 61//function : SetTransparency
29e2c6d2 62//purpose :
63//=======================================================================
85719f0e 64void AIS_TextLabel::SetTransparency (const Standard_Real theValue)
29e2c6d2 65{
85719f0e 66 Quantity_ColorRGBA aTextColor (myDrawer->TextAspect()->Aspect()->Color());
67 aTextColor.SetAlpha (Standard_ShortReal(1.0 - theValue));
68
69 Quantity_ColorRGBA aSubColor (myDrawer->TextAspect()->Aspect()->ColorSubTitle());
70 aSubColor.SetAlpha (aTextColor.Alpha());
71
72 myDrawer->TextAspect()->Aspect()->SetColor (aTextColor);
73 myDrawer->TextAspect()->Aspect()->SetColorSubTitle (aSubColor);
74 myDrawer->SetTransparency (Standard_ShortReal(theValue));
226fce20 75 SynchronizeAspects();
29e2c6d2 76}
77
78//=======================================================================
79//function : SetText
80//purpose :
81//=======================================================================
82void AIS_TextLabel::SetText (const TCollection_ExtendedString& theText)
83{
84 myText = theText;
85}
86
87//=======================================================================
88//function : SetPosition
89//purpose :
90//=======================================================================
91void AIS_TextLabel::SetPosition (const gp_Pnt& thePosition)
92{
ce01ec26 93 myOrientation3D.SetLocation (thePosition);
29e2c6d2 94}
95
96//=======================================================================
97//function : SetHJustification
98//purpose :
99//=======================================================================
100void AIS_TextLabel::SetHJustification (const Graphic3d_HorizontalTextAlignment theHJust)
101{
102 myDrawer->TextAspect()->SetHorizontalJustification (theHJust);
103}
104
105//=======================================================================
106//function : SetVJustification
107//purpose : Setup vertical justification.
108//=======================================================================
109void AIS_TextLabel::SetVJustification (const Graphic3d_VerticalTextAlignment theVJust)
110{
111 myDrawer->TextAspect()->SetVerticalJustification (theVJust);
112}
113
114//=======================================================================
115//function : SetAngle
116//purpose :
117//=======================================================================
118void AIS_TextLabel::SetAngle (const Standard_Real theAngle)
119{
120 myDrawer->TextAspect()->Aspect()->SetTextAngle (theAngle * 180.0 / M_PI);
121}
122
123//=======================================================================
124//function : SetZoom
125//purpose :
126//=======================================================================
127void AIS_TextLabel::SetZoomable (const Standard_Boolean theIsZoomable)
128{
b6472664 129 myDrawer->TextAspect()->Aspect()->SetTextZoomable (theIsZoomable == Standard_True);
29e2c6d2 130}
131
132//=======================================================================
133//function : SetHeight
134//purpose :
135//=======================================================================
136void AIS_TextLabel::SetHeight (const Standard_Real theHeight)
137{
138 myDrawer->TextAspect()->SetHeight (theHeight);
139}
140
141//=======================================================================
142//function : SetAngle
143//purpose :
144//=======================================================================
145void AIS_TextLabel::SetFontAspect (const Font_FontAspect theFontAspect)
146{
147 myDrawer->TextAspect()->Aspect()->SetTextFontAspect (theFontAspect);
148}
149
150//=======================================================================
151//function : SetFont
152//purpose :
153//=======================================================================
154void AIS_TextLabel::SetFont (Standard_CString theFont)
155{
64943a51 156 myDrawer->TextAspect()->SetFont (theFont);
29e2c6d2 157}
158
ce01ec26 159//=======================================================================
160//function : SetOrientation3D
161//purpose :
162//=======================================================================
163void AIS_TextLabel::SetOrientation3D (const gp_Ax2& theOrientation)
164{
165 myHasOrientation3D = Standard_True;
166 myOrientation3D = theOrientation;
167}
168
169//=======================================================================
170//function : UnsetOrientation3D
171//purpose :
172//=======================================================================
173void AIS_TextLabel::UnsetOrientation3D ()
174{
175 myHasOrientation3D = Standard_False;
176}
177
178//=======================================================================
179//function : Position
180//purpose :
181//=======================================================================
182const gp_Pnt& AIS_TextLabel::Position() const
183{
184 return myOrientation3D.Location();
185}
186
64943a51 187//=======================================================================
188//function : FontName
189//purpose :
190//=======================================================================
191const TCollection_AsciiString& AIS_TextLabel::FontName() const
192{
193 return myDrawer->TextAspect()->Aspect()->Font();
194}
195
196//=======================================================================
197//function : FontAspect
198//purpose :
199//=======================================================================
200Font_FontAspect AIS_TextLabel::FontAspect() const
201{
202 return myDrawer->TextAspect()->Aspect()->GetTextFontAspect();
203}
204
ce01ec26 205//=======================================================================
206//function : Orientation3D
207//purpose :
208//=======================================================================
209const gp_Ax2& AIS_TextLabel::Orientation3D() const
210{
211 return myOrientation3D;
212}
213
214//=======================================================================
3f1eb0ab 215//function : HasOrientation3D
ce01ec26 216//purpose :
217//=======================================================================
218Standard_Boolean AIS_TextLabel::HasOrientation3D() const
219{
220 return myHasOrientation3D;
221}
222
3f1eb0ab 223//=======================================================================
224//function : SetFlipping
225//purpose :
226//=======================================================================
227void AIS_TextLabel::SetFlipping (const Standard_Boolean theIsFlipping)
228{
229 myHasFlipping = theIsFlipping;
230}
231
232//=======================================================================
233//function : HasFlipping
234//purpose :
235//=======================================================================
236Standard_Boolean AIS_TextLabel::HasFlipping() const
237{
238 return myHasFlipping;
239}
240
61b0191c 241//=======================================================================
242//function : SetDisplayType
243//purpose :
244//=======================================================================
245void AIS_TextLabel::SetDisplayType (const Aspect_TypeOfDisplayText theDisplayType)
246{
247 myDrawer->TextAspect()->Aspect()->SetDisplayType(theDisplayType);
248}
249
250//=======================================================================
251//function : SetColorSubTitle
252//purpose :
253//=======================================================================
254void AIS_TextLabel::SetColorSubTitle (const Quantity_Color& theColor)
255{
256 myDrawer->TextAspect()->Aspect()->SetColorSubTitle(theColor);
257}
258
29e2c6d2 259//=======================================================================
260//function : Compute
261//purpose :
262//=======================================================================
263void AIS_TextLabel::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
264 const Handle(Prs3d_Presentation)& thePrs,
265 const Standard_Integer theMode)
266{
267 switch (theMode)
268 {
269 case 0:
270 {
271 Handle(Prs3d_TextAspect) anAsp = myDrawer->TextAspect();
3f1eb0ab 272 gp_Pnt aPosition = Position();
ce01ec26 273
274 if (myHasOrientation3D)
275 {
3f1eb0ab 276 Standard_Boolean isInit = Standard_False;
277 if (myHasFlipping)
278 {
279 // Get width and height of text
1bbd7c79 280 Font_FTFontParams aFontParams;
281 aFontParams.PointSize = (unsigned int )anAsp->Height();
282 aFontParams.Resolution = GetContext()->CurrentViewer()->DefaultRenderingParams().Resolution;
283 if (Handle(Font_FTFont) aFont = Font_FTFont::FindAndCreate (anAsp->Aspect()->Font(), anAsp->Aspect()->GetTextFontAspect(), aFontParams))
3f1eb0ab 284 {
285 isInit = Standard_True;
fb0b0531 286 const NCollection_String aText (myText.ToExtString());
1bbd7c79 287 Font_Rect aBndBox = aFont->BoundingBox (aText, anAsp->HorizontalJustification(), anAsp->VerticalJustification());
3f1eb0ab 288 Standard_Real aWidth = Abs (aBndBox.Width());
289 Standard_Real aHeight = Abs (aBndBox.Height());
290 gp_Pnt aCenterOfLabel = aPosition;
291
292 if (anAsp->VerticalJustification() == Graphic3d_VTA_BOTTOM)
293 {
294 aCenterOfLabel.ChangeCoord() += myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
295 }
296 else if (anAsp->VerticalJustification() == Graphic3d_VTA_TOP)
297 {
298 aCenterOfLabel.ChangeCoord() -= myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
299 }
300 if (anAsp->HorizontalJustification() == Graphic3d_HTA_LEFT)
301 {
302 aCenterOfLabel.ChangeCoord() += myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
303 }
304 else if (anAsp->HorizontalJustification() == Graphic3d_HTA_RIGHT)
305 {
306 aCenterOfLabel.ChangeCoord() -= myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
307 }
308
778cd667 309 if (!anAsp->Aspect()->GetTextZoomable()
310 && (TransformPersistence().IsNull()
311 || TransformPersistence()->Mode() == Graphic3d_TMF_ZoomPers))
3f1eb0ab 312 {
313 anAsp->Aspect()->SetTextZoomable (Standard_True);
778cd667 314 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_ZoomPers, aPosition));
3f1eb0ab 315 aPosition = gp::Origin();
316 }
317
318 gp_Ax2 aFlippingAxes (aCenterOfLabel, myOrientation3D.Direction(), myOrientation3D.XDirection());
319 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_True, aFlippingAxes);
320 }
321 }
322
323 gp_Ax2 anOrientation = myOrientation3D;
324 anOrientation.SetLocation (aPosition);
4ad142d9 325 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, myOrientation3D, !myHasFlipping);
3f1eb0ab 326 if (myHasFlipping && isInit)
327 {
328 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_False, gp_Ax2());
329 }
ce01ec26 330 }
331 else
332 {
4ad142d9 333 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, Position());
ce01ec26 334 }
335
29e2c6d2 336 break;
337 }
338 }
339}
340
341//=======================================================================
342//function : ComputeSelection
343//purpose :
344//=======================================================================
345void AIS_TextLabel::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
346 const Standard_Integer theMode)
347{
348 switch (theMode)
349 {
350 case 0:
351 {
352 Handle(SelectMgr_EntityOwner) anEntityOwner = new SelectMgr_EntityOwner (this, 10);
ce01ec26 353 Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint (anEntityOwner, Position());
29e2c6d2 354 theSelection->Add (aSensitivePoint);
355 break;
356 }
357 }
358}