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 |
33 | IMPLEMENT_STANDARD_RTTIEXT(AIS_TextLabel,AIS_InteractiveObject) |
34 | |
29e2c6d2 |
35 | //======================================================================= |
36 | //function : AIS_TextLabel |
37 | //purpose : |
38 | //======================================================================= |
39 | AIS_TextLabel::AIS_TextLabel() |
ce01ec26 |
40 | : myText ("?"), |
41 | myFont ("Courier"), |
42 | myFontAspect (Font_FA_Regular), |
3f1eb0ab |
43 | myHasOrientation3D (Standard_False), |
44 | myHasFlipping (Standard_False) |
29e2c6d2 |
45 | { |
46 | myDrawer->SetTextAspect (new Prs3d_TextAspect()); |
47 | |
48 | SetDisplayMode (0); |
49 | } |
50 | |
51 | //======================================================================= |
52 | //function : SetColor |
53 | //purpose : |
54 | //======================================================================= |
55 | void AIS_TextLabel::SetColor (const Quantity_Color& theColor) |
56 | { |
57 | hasOwnColor = Standard_True; |
f838dac4 |
58 | myDrawer->SetColor (theColor); |
29e2c6d2 |
59 | myDrawer->TextAspect()->SetColor (theColor); |
60 | } |
61 | |
62 | //======================================================================= |
85719f0e |
63 | //function : SetTransparency |
29e2c6d2 |
64 | //purpose : |
65 | //======================================================================= |
85719f0e |
66 | void AIS_TextLabel::SetTransparency (const Standard_Real theValue) |
29e2c6d2 |
67 | { |
85719f0e |
68 | Quantity_ColorRGBA aTextColor (myDrawer->TextAspect()->Aspect()->Color()); |
69 | aTextColor.SetAlpha (Standard_ShortReal(1.0 - theValue)); |
70 | |
71 | Quantity_ColorRGBA aSubColor (myDrawer->TextAspect()->Aspect()->ColorSubTitle()); |
72 | aSubColor.SetAlpha (aTextColor.Alpha()); |
73 | |
74 | myDrawer->TextAspect()->Aspect()->SetColor (aTextColor); |
75 | myDrawer->TextAspect()->Aspect()->SetColorSubTitle (aSubColor); |
76 | myDrawer->SetTransparency (Standard_ShortReal(theValue)); |
29e2c6d2 |
77 | } |
78 | |
79 | //======================================================================= |
80 | //function : SetText |
81 | //purpose : |
82 | //======================================================================= |
83 | void AIS_TextLabel::SetText (const TCollection_ExtendedString& theText) |
84 | { |
85 | myText = theText; |
86 | } |
87 | |
88 | //======================================================================= |
89 | //function : SetPosition |
90 | //purpose : |
91 | //======================================================================= |
92 | void AIS_TextLabel::SetPosition (const gp_Pnt& thePosition) |
93 | { |
ce01ec26 |
94 | myOrientation3D.SetLocation (thePosition); |
29e2c6d2 |
95 | } |
96 | |
97 | //======================================================================= |
98 | //function : SetHJustification |
99 | //purpose : |
100 | //======================================================================= |
101 | void AIS_TextLabel::SetHJustification (const Graphic3d_HorizontalTextAlignment theHJust) |
102 | { |
103 | myDrawer->TextAspect()->SetHorizontalJustification (theHJust); |
104 | } |
105 | |
106 | //======================================================================= |
107 | //function : SetVJustification |
108 | //purpose : Setup vertical justification. |
109 | //======================================================================= |
110 | void AIS_TextLabel::SetVJustification (const Graphic3d_VerticalTextAlignment theVJust) |
111 | { |
112 | myDrawer->TextAspect()->SetVerticalJustification (theVJust); |
113 | } |
114 | |
115 | //======================================================================= |
116 | //function : SetAngle |
117 | //purpose : |
118 | //======================================================================= |
119 | void AIS_TextLabel::SetAngle (const Standard_Real theAngle) |
120 | { |
121 | myDrawer->TextAspect()->Aspect()->SetTextAngle (theAngle * 180.0 / M_PI); |
122 | } |
123 | |
124 | //======================================================================= |
125 | //function : SetZoom |
126 | //purpose : |
127 | //======================================================================= |
128 | void AIS_TextLabel::SetZoomable (const Standard_Boolean theIsZoomable) |
129 | { |
b6472664 |
130 | myDrawer->TextAspect()->Aspect()->SetTextZoomable (theIsZoomable == Standard_True); |
29e2c6d2 |
131 | } |
132 | |
133 | //======================================================================= |
134 | //function : SetHeight |
135 | //purpose : |
136 | //======================================================================= |
137 | void AIS_TextLabel::SetHeight (const Standard_Real theHeight) |
138 | { |
139 | myDrawer->TextAspect()->SetHeight (theHeight); |
140 | } |
141 | |
142 | //======================================================================= |
143 | //function : SetAngle |
144 | //purpose : |
145 | //======================================================================= |
146 | void AIS_TextLabel::SetFontAspect (const Font_FontAspect theFontAspect) |
147 | { |
148 | myDrawer->TextAspect()->Aspect()->SetTextFontAspect (theFontAspect); |
149 | } |
150 | |
151 | //======================================================================= |
152 | //function : SetFont |
153 | //purpose : |
154 | //======================================================================= |
155 | void AIS_TextLabel::SetFont (Standard_CString theFont) |
156 | { |
157 | myFont = theFont; |
158 | myDrawer->TextAspect()->SetFont (myFont.ToCString()); |
159 | } |
160 | |
ce01ec26 |
161 | //======================================================================= |
162 | //function : SetOrientation3D |
163 | //purpose : |
164 | //======================================================================= |
165 | void AIS_TextLabel::SetOrientation3D (const gp_Ax2& theOrientation) |
166 | { |
167 | myHasOrientation3D = Standard_True; |
168 | myOrientation3D = theOrientation; |
169 | } |
170 | |
171 | //======================================================================= |
172 | //function : UnsetOrientation3D |
173 | //purpose : |
174 | //======================================================================= |
175 | void AIS_TextLabel::UnsetOrientation3D () |
176 | { |
177 | myHasOrientation3D = Standard_False; |
178 | } |
179 | |
180 | //======================================================================= |
181 | //function : Position |
182 | //purpose : |
183 | //======================================================================= |
184 | const gp_Pnt& AIS_TextLabel::Position() const |
185 | { |
186 | return myOrientation3D.Location(); |
187 | } |
188 | |
189 | //======================================================================= |
190 | //function : Orientation3D |
191 | //purpose : |
192 | //======================================================================= |
193 | const gp_Ax2& AIS_TextLabel::Orientation3D() const |
194 | { |
195 | return myOrientation3D; |
196 | } |
197 | |
198 | //======================================================================= |
3f1eb0ab |
199 | //function : HasOrientation3D |
ce01ec26 |
200 | //purpose : |
201 | //======================================================================= |
202 | Standard_Boolean AIS_TextLabel::HasOrientation3D() const |
203 | { |
204 | return myHasOrientation3D; |
205 | } |
206 | |
3f1eb0ab |
207 | //======================================================================= |
208 | //function : SetFlipping |
209 | //purpose : |
210 | //======================================================================= |
211 | void AIS_TextLabel::SetFlipping (const Standard_Boolean theIsFlipping) |
212 | { |
213 | myHasFlipping = theIsFlipping; |
214 | } |
215 | |
216 | //======================================================================= |
217 | //function : HasFlipping |
218 | //purpose : |
219 | //======================================================================= |
220 | Standard_Boolean AIS_TextLabel::HasFlipping() const |
221 | { |
222 | return myHasFlipping; |
223 | } |
224 | |
61b0191c |
225 | //======================================================================= |
226 | //function : SetDisplayType |
227 | //purpose : |
228 | //======================================================================= |
229 | void AIS_TextLabel::SetDisplayType (const Aspect_TypeOfDisplayText theDisplayType) |
230 | { |
231 | myDrawer->TextAspect()->Aspect()->SetDisplayType(theDisplayType); |
232 | } |
233 | |
234 | //======================================================================= |
235 | //function : SetColorSubTitle |
236 | //purpose : |
237 | //======================================================================= |
238 | void AIS_TextLabel::SetColorSubTitle (const Quantity_Color& theColor) |
239 | { |
240 | myDrawer->TextAspect()->Aspect()->SetColorSubTitle(theColor); |
241 | } |
242 | |
29e2c6d2 |
243 | //======================================================================= |
244 | //function : Compute |
245 | //purpose : |
246 | //======================================================================= |
247 | void AIS_TextLabel::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/, |
248 | const Handle(Prs3d_Presentation)& thePrs, |
249 | const Standard_Integer theMode) |
250 | { |
251 | switch (theMode) |
252 | { |
253 | case 0: |
254 | { |
255 | Handle(Prs3d_TextAspect) anAsp = myDrawer->TextAspect(); |
3f1eb0ab |
256 | gp_Pnt aPosition = Position(); |
ce01ec26 |
257 | |
258 | if (myHasOrientation3D) |
259 | { |
3f1eb0ab |
260 | Standard_Boolean isInit = Standard_False; |
261 | if (myHasFlipping) |
262 | { |
263 | // Get width and height of text |
264 | Font_FTFont aFont; |
3f1eb0ab |
265 | unsigned int aResolution = GetContext()->CurrentViewer()->DefaultRenderingParams().Resolution; |
b6472664 |
266 | if (aFont.Init (anAsp->Aspect()->Font().ToCString(), |
267 | anAsp->Aspect()->GetTextFontAspect(), (unsigned int)anAsp->Height(), aResolution)) |
3f1eb0ab |
268 | { |
269 | isInit = Standard_True; |
fb0b0531 |
270 | const NCollection_String aText (myText.ToExtString()); |
3f1eb0ab |
271 | Font_Rect aBndBox = aFont.BoundingBox (aText, anAsp->HorizontalJustification(), anAsp->VerticalJustification()); |
272 | Standard_Real aWidth = Abs (aBndBox.Width()); |
273 | Standard_Real aHeight = Abs (aBndBox.Height()); |
274 | gp_Pnt aCenterOfLabel = aPosition; |
275 | |
276 | if (anAsp->VerticalJustification() == Graphic3d_VTA_BOTTOM) |
277 | { |
278 | aCenterOfLabel.ChangeCoord() += myOrientation3D.YDirection().XYZ() * aHeight * 0.5; |
279 | } |
280 | else if (anAsp->VerticalJustification() == Graphic3d_VTA_TOP) |
281 | { |
282 | aCenterOfLabel.ChangeCoord() -= myOrientation3D.YDirection().XYZ() * aHeight * 0.5; |
283 | } |
284 | if (anAsp->HorizontalJustification() == Graphic3d_HTA_LEFT) |
285 | { |
286 | aCenterOfLabel.ChangeCoord() += myOrientation3D.XDirection().XYZ() * aWidth * 0.5; |
287 | } |
288 | else if (anAsp->HorizontalJustification() == Graphic3d_HTA_RIGHT) |
289 | { |
290 | aCenterOfLabel.ChangeCoord() -= myOrientation3D.XDirection().XYZ() * aWidth * 0.5; |
291 | } |
292 | |
778cd667 |
293 | if (!anAsp->Aspect()->GetTextZoomable() |
294 | && (TransformPersistence().IsNull() |
295 | || TransformPersistence()->Mode() == Graphic3d_TMF_ZoomPers)) |
3f1eb0ab |
296 | { |
297 | anAsp->Aspect()->SetTextZoomable (Standard_True); |
778cd667 |
298 | SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_ZoomPers, aPosition)); |
3f1eb0ab |
299 | aPosition = gp::Origin(); |
300 | } |
301 | |
302 | gp_Ax2 aFlippingAxes (aCenterOfLabel, myOrientation3D.Direction(), myOrientation3D.XDirection()); |
303 | Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_True, aFlippingAxes); |
304 | } |
305 | } |
306 | |
307 | gp_Ax2 anOrientation = myOrientation3D; |
308 | anOrientation.SetLocation (aPosition); |
4ad142d9 |
309 | Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, myOrientation3D, !myHasFlipping); |
3f1eb0ab |
310 | if (myHasFlipping && isInit) |
311 | { |
312 | Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_False, gp_Ax2()); |
313 | } |
ce01ec26 |
314 | } |
315 | else |
316 | { |
4ad142d9 |
317 | Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, Position()); |
ce01ec26 |
318 | } |
319 | |
29e2c6d2 |
320 | break; |
321 | } |
322 | } |
323 | } |
324 | |
325 | //======================================================================= |
326 | //function : ComputeSelection |
327 | //purpose : |
328 | //======================================================================= |
329 | void AIS_TextLabel::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection, |
330 | const Standard_Integer theMode) |
331 | { |
332 | switch (theMode) |
333 | { |
334 | case 0: |
335 | { |
336 | Handle(SelectMgr_EntityOwner) anEntityOwner = new SelectMgr_EntityOwner (this, 10); |
ce01ec26 |
337 | Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint (anEntityOwner, Position()); |
29e2c6d2 |
338 | theSelection->Add (aSensitivePoint); |
339 | break; |
340 | } |
341 | } |
342 | } |