Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / 3d / text_context_user.cpp
blobbdd1e4a51eb7404da1805a1a47d241eee732e3e4
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010-2018 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014-2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
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/>.
20 #include "std3d.h"
22 #include "nel/3d/text_context_user.h"
23 #include "nel/misc/hierarchical_timer.h"
25 #ifdef DEBUG_NEW
26 #define new DEBUG_NEW
27 #endif
29 namespace NL3D
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)
38 H_AUTO2;
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);
66 return hotspotVector;
70 // ***************************************************************************
71 void UTextContext::CStringInfo::convertTo01Size(UDriver *drv)
73 uint32 w,h;
74 drv->getWindowSize(w,h);
75 StringWidth/= w;
76 StringHeight/= h;
77 StringLine/= h;
79 void UTextContext::CStringInfo::convertToPixelSize(UDriver *drv)
81 uint32 w,h;
82 drv->getWindowSize(w,h);
83 StringWidth*= w;
84 StringHeight*= h;
85 StringLine*= h;
90 // ***************************************************************************
91 void CTextContextUser::setColor(NLMISC::CRGBA color)
93 H_AUTO2;
95 _TextContext.setColor(color);
97 void CTextContextUser::setFontSize(uint32 fontSize)
99 H_AUTO2;
101 _TextContext.setFontSize(fontSize);
103 uint32 CTextContextUser::getFontSize() const
105 H_AUTO2;
107 return _TextContext.getFontSize();
109 void CTextContextUser::setEmbolden(bool b)
111 H_AUTO2;
113 _TextContext.setEmbolden(b);
115 bool CTextContextUser::getEmbolden() const
117 H_AUTO2;
119 return _TextContext.getEmbolden();
121 void CTextContextUser::setOblique(bool b)
123 H_AUTO2;
125 _TextContext.setOblique(b);
127 bool CTextContextUser::getOblique() const
129 H_AUTO2;
131 return _TextContext.getOblique();
133 void CTextContextUser::setHotSpot(THotSpot hotSpot)
135 H_AUTO2;
137 _TextContext.setHotSpot((CComputedString::THotSpot)(uint32)hotSpot) ;
139 UTextContext::THotSpot CTextContextUser::getHotSpot() const
141 H_AUTO2;
143 return (THotSpot)(uint32)_TextContext.getHotSpot();
145 void CTextContextUser::setScaleX(float scaleX)
147 H_AUTO2;
149 _TextContext.setScaleX(scaleX);
151 void CTextContextUser::setScaleY(float scaleY)
153 H_AUTO2;
155 _TextContext.setScaleZ(scaleY);
157 float CTextContextUser::getScaleX() const
159 H_AUTO2;
161 return _TextContext.getScaleX();
163 float CTextContextUser::getScaleY() const
165 H_AUTO2;
167 return _TextContext.getScaleZ();
169 void CTextContextUser::setShaded(bool b)
171 H_AUTO2;
173 _TextContext.setShaded(b);
175 bool CTextContextUser::getShaded() const
177 H_AUTO2;
179 return _TextContext.getShaded();
181 void CTextContextUser::setShadeOutline(bool b)
183 H_AUTO2;
185 _TextContext.setShadeOutline(b);
187 bool CTextContextUser::getShadeOutline() const
189 H_AUTO2;
191 return _TextContext.getShadeOutline();
193 void CTextContextUser::setShadeExtent(float x, float y)
195 H_AUTO2;
197 _TextContext.setShadeExtent(x, y);
199 void CTextContextUser::setShadeColor (NLMISC::CRGBA sc)
201 H_AUTO2;
203 _TextContext.setShadeColor (sc);
205 NLMISC::CRGBA CTextContextUser::getShadeColor () const
207 H_AUTO2;
209 return _TextContext.getShadeColor();
211 void CTextContextUser::setKeep800x600Ratio(bool keep)
213 H_AUTO2;
215 _TextContext.setKeep800x600Ratio(keep);
217 bool CTextContextUser::getKeep800x600Ratio() const
219 H_AUTO2;
221 return _TextContext.getKeep800x600Ratio();
224 // ***************************************************************************
225 uint32 CTextContextUser::textPush(const char *format, ...)
227 H_AUTO2;
229 char *str;
230 NLMISC_CONVERT_VARGS (str, format, NLMISC::MaxCStringSize);
232 return _TextContext.textPush(str) ;
234 uint32 CTextContextUser::textPush(NLMISC::CUtfStringView sv)
236 H_AUTO2;
238 return _TextContext.textPush(sv) ;
240 void CTextContextUser::setStringColor(uint32 i, CRGBA newCol)
242 CComputedString *str= _TextContext.getComputedString(i);
243 if(str)
245 str->Color= newCol;
248 void CTextContextUser::setStringSelection(uint32 i, uint32 selectStart, uint32 selectSize)
250 CComputedString *str= _TextContext.getComputedString(i);
251 if(str)
253 str->SelectStart= selectStart;
254 str->SelectSize= selectSize;
257 void CTextContextUser::resetStringSelection(uint32 i)
259 CComputedString *str= _TextContext.getComputedString(i);
260 if(str)
262 str->SelectStart= 0;
263 str->SelectSize= std::numeric_limits<uint32>::max();
266 void CTextContextUser::erase(uint32 i)
268 H_AUTO2;
270 _TextContext.erase(i);
272 UTextContext::CStringInfo CTextContextUser::getStringInfo(uint32 i)
274 H_AUTO2;
276 CComputedString *cstr= _TextContext.getComputedString(i);
277 if(!cstr)
278 return CStringInfo(0, 0, 0, 0);
279 else
280 return CStringInfo(cstr->StringWidth, cstr->StringHeight, cstr->StringLine, cstr->Length);
282 UTextContext::CStringInfo CTextContextUser::getStringInfo(NLMISC::CUtfStringView sv)
284 H_AUTO2;
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)
291 H_AUTO2;
293 _TextContext.computeStringInfo(sv, _CacheString, len);
294 return CStringInfo (_CacheString.StringWidth, _CacheString.StringHeight, _CacheString.StringLine, _CacheString.Length);
296 void CTextContextUser::clear()
298 H_AUTO2;
300 _TextContext.clear();
302 void CTextContextUser::printAt(float x, float y, uint32 i)
304 H_AUTO2;
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)
311 H_AUTO2;
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)
318 H_AUTO2;
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)
331 H_AUTO2;
333 _TextContext.printAt(x, y, sv);
334 _DriverUser->restoreMatrixContext();
336 void CTextContextUser::printfAt(float x, float y, const char * format, ...)
338 H_AUTO2;
340 char *str;
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;
362 char *str;
363 NLMISC_CONVERT_VARGS (str, format, NLMISC::MaxCStringSize);
365 render3D(mat, str);
367 _DriverUser->restoreMatrixContext();
371 float CTextContextUser::getLastXBound() const
373 H_AUTO2;
375 return 0.0f;
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)
394 delete buffer;
397 // ***************************************************************************
398 void CTextContextUser::flushRenderBuffer(URenderStringBuffer *buffer)
400 nlassert(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)
414 nlassert(buffer);
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)
426 H_AUTO2;
428 _TextContext.setLetterColors(static_cast<CLetterColors*>(letterColors), index);
431 // ***************************************************************************
432 ULetterColors * CTextContextUser::createLetterColors()
434 H_AUTO2;
436 ULetterColors * uLetterColors = new CLetterColors();
437 return uLetterColors;
440 bool CTextContextUser::isSameLetterColors(ULetterColors * letterColors, uint index)
442 H_AUTO2;
444 return _TextContext.isSameLetterColors(static_cast<CLetterColors*>(letterColors), index);
447 } // NL3D