1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010-2018 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "nel/3d/text_context_user.h"
23 #include "nel/misc/hierarchical_timer.h"
32 H_AUTO_DECL( NL3D_Render_TextContext_3D
)
33 #define NL3D_HAUTO_RENDER_3D_TEXTCONTEXT H_AUTO_USE( NL3D_Render_TextContext_3D )
35 // ***************************************************************************
36 NLMISC::CVector
UTextContext::CStringInfo::getHotSpotVector(UTextContext::THotSpot hotspot
)
40 NLMISC::CVector
hotspotVector(0,0,0);
42 if(hotspot
==UTextContext::MiddleLeft
)
43 hotspotVector
= CVector(0,0,-StringHeight
/2);
45 if(hotspot
==UTextContext::TopLeft
)
46 hotspotVector
= CVector(0,0,StringHeight
);
48 if(hotspot
==UTextContext::MiddleBottom
)
49 hotspotVector
= CVector(-StringWidth
/2,0,0);
51 if(hotspot
==UTextContext::MiddleMiddle
)
52 hotspotVector
= CVector(-StringWidth
/2,0,-StringHeight
/2);
54 if(hotspot
==UTextContext::MiddleTop
)
55 hotspotVector
= CVector(-StringWidth
/2,0,-StringHeight
);
57 if(hotspot
==UTextContext::BottomRight
)
58 hotspotVector
= CVector(-StringWidth
,0,0);
60 if(hotspot
==UTextContext::MiddleRight
)
61 hotspotVector
= CVector(-StringWidth
,0,-StringHeight
/2);
63 if(hotspot
==UTextContext::TopRight
)
64 hotspotVector
= CVector(-StringWidth
,0,-StringHeight
);
70 // ***************************************************************************
71 void UTextContext::CStringInfo::convertTo01Size(UDriver
*drv
)
74 drv
->getWindowSize(w
,h
);
79 void UTextContext::CStringInfo::convertToPixelSize(UDriver
*drv
)
82 drv
->getWindowSize(w
,h
);
90 // ***************************************************************************
91 void CTextContextUser::setColor(NLMISC::CRGBA color
)
95 _TextContext
.setColor(color
);
97 void CTextContextUser::setFontSize(uint32 fontSize
)
101 _TextContext
.setFontSize(fontSize
);
103 uint32
CTextContextUser::getFontSize() const
107 return _TextContext
.getFontSize();
109 void CTextContextUser::setEmbolden(bool b
)
113 _TextContext
.setEmbolden(b
);
115 bool CTextContextUser::getEmbolden() const
119 return _TextContext
.getEmbolden();
121 void CTextContextUser::setOblique(bool b
)
125 _TextContext
.setOblique(b
);
127 bool CTextContextUser::getOblique() const
131 return _TextContext
.getOblique();
133 void CTextContextUser::setHotSpot(THotSpot hotSpot
)
137 _TextContext
.setHotSpot((CComputedString::THotSpot
)(uint32
)hotSpot
) ;
139 UTextContext::THotSpot
CTextContextUser::getHotSpot() const
143 return (THotSpot
)(uint32
)_TextContext
.getHotSpot();
145 void CTextContextUser::setScaleX(float scaleX
)
149 _TextContext
.setScaleX(scaleX
);
151 void CTextContextUser::setScaleY(float scaleY
)
155 _TextContext
.setScaleZ(scaleY
);
157 float CTextContextUser::getScaleX() const
161 return _TextContext
.getScaleX();
163 float CTextContextUser::getScaleY() const
167 return _TextContext
.getScaleZ();
169 void CTextContextUser::setShaded(bool b
)
173 _TextContext
.setShaded(b
);
175 bool CTextContextUser::getShaded() const
179 return _TextContext
.getShaded();
181 void CTextContextUser::setShadeOutline(bool b
)
185 _TextContext
.setShadeOutline(b
);
187 bool CTextContextUser::getShadeOutline() const
191 return _TextContext
.getShadeOutline();
193 void CTextContextUser::setShadeExtent(float x
, float y
)
197 _TextContext
.setShadeExtent(x
, y
);
199 void CTextContextUser::setShadeColor (NLMISC::CRGBA sc
)
203 _TextContext
.setShadeColor (sc
);
205 NLMISC::CRGBA
CTextContextUser::getShadeColor () const
209 return _TextContext
.getShadeColor();
211 void CTextContextUser::setKeep800x600Ratio(bool keep
)
215 _TextContext
.setKeep800x600Ratio(keep
);
217 bool CTextContextUser::getKeep800x600Ratio() const
221 return _TextContext
.getKeep800x600Ratio();
224 // ***************************************************************************
225 uint32
CTextContextUser::textPush(const char *format
, ...)
230 NLMISC_CONVERT_VARGS (str
, format
, NLMISC::MaxCStringSize
);
232 return _TextContext
.textPush(str
) ;
234 uint32
CTextContextUser::textPush(NLMISC::CUtfStringView sv
)
238 return _TextContext
.textPush(sv
) ;
240 void CTextContextUser::setStringColor(uint32 i
, CRGBA newCol
)
242 CComputedString
*str
= _TextContext
.getComputedString(i
);
248 void CTextContextUser::setStringSelection(uint32 i
, uint32 selectStart
, uint32 selectSize
)
250 CComputedString
*str
= _TextContext
.getComputedString(i
);
253 str
->SelectStart
= selectStart
;
254 str
->SelectSize
= selectSize
;
257 void CTextContextUser::resetStringSelection(uint32 i
)
259 CComputedString
*str
= _TextContext
.getComputedString(i
);
263 str
->SelectSize
= std::numeric_limits
<uint32
>::max();
266 void CTextContextUser::erase(uint32 i
)
270 _TextContext
.erase(i
);
272 UTextContext::CStringInfo
CTextContextUser::getStringInfo(uint32 i
)
276 CComputedString
*cstr
= _TextContext
.getComputedString(i
);
278 return CStringInfo(0, 0, 0, 0);
280 return CStringInfo(cstr
->StringWidth
, cstr
->StringHeight
, cstr
->StringLine
, cstr
->Length
);
282 UTextContext::CStringInfo
CTextContextUser::getStringInfo(NLMISC::CUtfStringView sv
)
286 _TextContext
.computeStringInfo(sv
, _CacheString
);
287 return CStringInfo (_CacheString
.StringWidth
, _CacheString
.StringHeight
, _CacheString
.StringLine
, _CacheString
.Length
);
289 UTextContext::CStringInfo
CTextContextUser::getStringInfo(NLMISC::CUtfStringView sv
, size_t len
)
293 _TextContext
.computeStringInfo(sv
, _CacheString
, len
);
294 return CStringInfo (_CacheString
.StringWidth
, _CacheString
.StringHeight
, _CacheString
.StringLine
, _CacheString
.Length
);
296 void CTextContextUser::clear()
300 _TextContext
.clear();
302 void CTextContextUser::printAt(float x
, float y
, uint32 i
)
306 _TextContext
.printAt(x
, y
, i
);
307 _DriverUser
->restoreMatrixContext();
309 void CTextContextUser::printClipAt(URenderStringBuffer
&renderBuffer
, float x
, float y
, uint32 i
, float xmin
, float ymin
, float xmax
, float ymax
)
313 _TextContext
.printClipAt(static_cast<CRenderStringBuffer
&>(renderBuffer
), x
, y
, i
, xmin
, ymin
, xmax
, ymax
);
314 // Don't need to restore Matrix context here since no driver change
316 void CTextContextUser::printClipAtUnProjected(URenderStringBuffer
&renderBuffer
, class NL3D::CFrustum
&frustum
, const NLMISC::CMatrix
&scaleMatrix
, float x
, float y
, float depth
, uint32 i
, float xmin
, float ymin
, float xmax
, float ymax
)
320 _TextContext
.printClipAtUnProjected(static_cast<CRenderStringBuffer
&>(renderBuffer
), frustum
, scaleMatrix
, x
, y
, depth
, i
, xmin
, ymin
, xmax
, ymax
);
321 // Don't need to restore Matrix context here since no driver change
323 void CTextContextUser::printClipAtOld (float x
, float y
, uint32 i
, float xmin
, float ymin
, float xmax
, float ymax
)
325 static CRenderStringBuffer rdrBuffer
;
326 printClipAt(rdrBuffer
, x
, y
,i
, xmin
, ymin
, xmax
, ymax
);
327 flushRenderBuffer(&rdrBuffer
);
329 void CTextContextUser::printAt(float x
, float y
, NLMISC::CUtfStringView sv
)
333 _TextContext
.printAt(x
, y
, sv
);
334 _DriverUser
->restoreMatrixContext();
336 void CTextContextUser::printfAt(float x
, float y
, const char * format
, ...)
341 NLMISC_CONVERT_VARGS (str
, format
, NLMISC::MaxCStringSize
);
343 _TextContext
.printAt(x
, y
, str
) ;
344 _DriverUser
->restoreMatrixContext();
347 void CTextContextUser::render3D(const CMatrix
&mat
, NLMISC::CUtfStringView sv
)
349 NL3D_HAUTO_RENDER_3D_TEXTCONTEXT
;
351 CComputedString computedStr
;
352 _TextContext
.computeString(sv
,computedStr
);
354 computedStr
.render3D(*_Driver
,mat
);
356 _DriverUser
->restoreMatrixContext();
358 void CTextContextUser::render3D(const CMatrix
&mat
, const char *format
, ...)
360 NL3D_HAUTO_RENDER_3D_TEXTCONTEXT
;
363 NLMISC_CONVERT_VARGS (str
, format
, NLMISC::MaxCStringSize
);
367 _DriverUser
->restoreMatrixContext();
371 float CTextContextUser::getLastXBound() const
378 // ***************************************************************************
379 void CTextContextUser::dumpCacheTexture (const char *filename
)
381 _TextContext
.dumpCache (filename
);
385 // ***************************************************************************
386 URenderStringBuffer
*CTextContextUser::createRenderBuffer()
388 return new CRenderStringBuffer
;
391 // ***************************************************************************
392 void CTextContextUser::deleteRenderBuffer(URenderStringBuffer
*buffer
)
397 // ***************************************************************************
398 void CTextContextUser::flushRenderBuffer(URenderStringBuffer
*buffer
)
401 CRenderStringBuffer
*rdrBuffer
= static_cast<CRenderStringBuffer
*>(buffer
);
402 if(rdrBuffer
->NumQuads
)
404 rdrBuffer
->flush(*_Driver
, _TextContext
.getFontManager()->getFontMaterial());
406 // must restore the Matrix context if some display done. Need just for Frustum/Matrixes
407 _DriverUser
->restoreMatrixContextMatrixOnly();
411 // ***************************************************************************
412 void CTextContextUser::flushRenderBufferUnProjected(URenderStringBuffer
*buffer
, bool zwrite
)
415 CRenderStringBuffer
*rdrBuffer
= static_cast<CRenderStringBuffer
*>(buffer
);
416 if(rdrBuffer
->NumQuads
)
418 rdrBuffer
->flushUnProjected(*_Driver
, _TextContext
.getFontManager()->getFontMaterial(), zwrite
);
423 // ***************************************************************************
424 void CTextContextUser::setLetterColors(ULetterColors
* letterColors
, uint index
)
428 _TextContext
.setLetterColors(static_cast<CLetterColors
*>(letterColors
), index
);
431 // ***************************************************************************
432 ULetterColors
* CTextContextUser::createLetterColors()
436 ULetterColors
* uLetterColors
= new CLetterColors();
437 return uLetterColors
;
440 bool CTextContextUser::isSameLetterColors(ULetterColors
* letterColors
, uint index
)
444 return _TextContext
.isSameLetterColors(static_cast<CLetterColors
*>(letterColors
), index
);