[1] | 1 | /*------------------------------------------------------------------------- |
---|
| 2 | This source file is a part of OGRE |
---|
| 3 | (Object-oriented Graphics Rendering Engine) |
---|
| 4 | |
---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
---|
| 6 | |
---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
| 8 | Also see acknowledgements in Readme.html |
---|
| 9 | |
---|
| 10 | This library is free software; you can redistribute it and/or modify it |
---|
| 11 | under the terms of the GNU Lesser General Public License (LGPL) as |
---|
| 12 | published by the Free Software Foundation; either version 2.1 of the |
---|
| 13 | License, or (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This library is distributed in the hope that it will be useful, but |
---|
| 16 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
| 17 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
| 18 | License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU Lesser General Public License |
---|
| 21 | along with this library; if not, write to the Free Software Foundation, |
---|
| 22 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or go to |
---|
| 23 | http://www.gnu.org/copyleft/lesser.txt |
---|
| 24 | -------------------------------------------------------------------------*/ |
---|
| 25 | #include "OgreStableHeaders.h" |
---|
| 26 | |
---|
| 27 | #include "OgreTextAreaOverlayElement.h" |
---|
| 28 | #include "OgreRoot.h" |
---|
| 29 | #include "OgreLogManager.h" |
---|
| 30 | #include "OgreOverlayManager.h" |
---|
| 31 | #include "OgreHardwareBufferManager.h" |
---|
| 32 | #include "OgreHardwareVertexBuffer.h" |
---|
| 33 | #include "OgreException.h" |
---|
| 34 | #include "OgreStringConverter.h" |
---|
| 35 | #include "OgreFont.h" |
---|
| 36 | #include "OgreFontManager.h" |
---|
| 37 | |
---|
| 38 | namespace Ogre { |
---|
| 39 | |
---|
| 40 | #define DEFAULT_INITIAL_CHARS 12 |
---|
| 41 | //--------------------------------------------------------------------- |
---|
| 42 | String TextAreaOverlayElement::msTypeName = "TextArea"; |
---|
| 43 | TextAreaOverlayElement::CmdCharHeight TextAreaOverlayElement::msCmdCharHeight; |
---|
| 44 | TextAreaOverlayElement::CmdSpaceWidth TextAreaOverlayElement::msCmdSpaceWidth; |
---|
| 45 | TextAreaOverlayElement::CmdFontName TextAreaOverlayElement::msCmdFontName; |
---|
| 46 | TextAreaOverlayElement::CmdColour TextAreaOverlayElement::msCmdColour; |
---|
| 47 | TextAreaOverlayElement::CmdColourBottom TextAreaOverlayElement::msCmdColourBottom; |
---|
| 48 | TextAreaOverlayElement::CmdColourTop TextAreaOverlayElement::msCmdColourTop; |
---|
| 49 | TextAreaOverlayElement::CmdAlignment TextAreaOverlayElement::msCmdAlignment; |
---|
| 50 | //--------------------------------------------------------------------- |
---|
| 51 | #define POS_TEX_BINDING 0 |
---|
| 52 | #define COLOUR_BINDING 1 |
---|
| 53 | #define UNICODE_NEL 0x0085 |
---|
| 54 | #define UNICODE_CR 0x000D |
---|
| 55 | #define UNICODE_LF 0x000A |
---|
| 56 | #define UNICODE_SPACE 0x0020 |
---|
| 57 | #define UNICODE_ZERO 0x0030 |
---|
| 58 | //--------------------------------------------------------------------- |
---|
| 59 | TextAreaOverlayElement::TextAreaOverlayElement(const String& name) |
---|
| 60 | : OverlayElement(name) |
---|
| 61 | { |
---|
| 62 | mTransparent = false; |
---|
| 63 | mAlignment = Left; |
---|
| 64 | |
---|
| 65 | mColourTop = ColourValue::White; |
---|
| 66 | mColourBottom = ColourValue::White; |
---|
| 67 | mColoursChanged = true; |
---|
| 68 | |
---|
| 69 | mAllocSize = 0; |
---|
| 70 | |
---|
| 71 | mCharHeight = 0.02; |
---|
| 72 | mPixelCharHeight = 12; |
---|
| 73 | mSpaceWidth = 0; |
---|
| 74 | mPixelSpaceWidth = 0; |
---|
| 75 | mViewportAspectCoef = 1; |
---|
| 76 | |
---|
| 77 | if (createParamDictionary("TextAreaOverlayElement")) |
---|
| 78 | { |
---|
| 79 | addBaseParameters(); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void TextAreaOverlayElement::initialise(void) |
---|
| 84 | { |
---|
| 85 | if (!mInitialised) |
---|
| 86 | { |
---|
| 87 | // Set up the render op |
---|
| 88 | // Combine positions and texture coords since they tend to change together |
---|
| 89 | // since character sizes are different |
---|
| 90 | mRenderOp.vertexData = new VertexData(); |
---|
| 91 | VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration; |
---|
| 92 | size_t offset = 0; |
---|
| 93 | // Positions |
---|
| 94 | decl->addElement(POS_TEX_BINDING, offset, VET_FLOAT3, VES_POSITION); |
---|
| 95 | offset += VertexElement::getTypeSize(VET_FLOAT3); |
---|
| 96 | // Texcoords |
---|
| 97 | decl->addElement(POS_TEX_BINDING, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0); |
---|
| 98 | offset += VertexElement::getTypeSize(VET_FLOAT2); |
---|
| 99 | // Colours - store these in a separate buffer because they change less often |
---|
| 100 | decl->addElement(COLOUR_BINDING, 0, VET_COLOUR, VES_DIFFUSE); |
---|
| 101 | |
---|
| 102 | mRenderOp.operationType = RenderOperation::OT_TRIANGLE_LIST; |
---|
| 103 | mRenderOp.useIndexes = false; |
---|
| 104 | mRenderOp.vertexData->vertexStart = 0; |
---|
| 105 | // Vertex buffer will be created in checkMemoryAllocation |
---|
| 106 | |
---|
| 107 | checkMemoryAllocation( DEFAULT_INITIAL_CHARS ); |
---|
| 108 | |
---|
| 109 | mInitialised = true; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | void TextAreaOverlayElement::checkMemoryAllocation( size_t numChars ) |
---|
| 115 | { |
---|
| 116 | if( mAllocSize < numChars) |
---|
| 117 | { |
---|
| 118 | // Create and bind new buffers |
---|
| 119 | // Note that old buffers will be deleted automatically through reference counting |
---|
| 120 | |
---|
| 121 | // 6 verts per char since we're doing tri lists without indexes |
---|
| 122 | // Allocate space for positions & texture coords |
---|
| 123 | VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration; |
---|
| 124 | VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding; |
---|
| 125 | |
---|
| 126 | mRenderOp.vertexData->vertexCount = numChars * 6; |
---|
| 127 | |
---|
| 128 | // Create dynamic since text tends to change alot |
---|
| 129 | // positions & texcoords |
---|
| 130 | HardwareVertexBufferSharedPtr vbuf = |
---|
| 131 | HardwareBufferManager::getSingleton(). |
---|
| 132 | createVertexBuffer( |
---|
| 133 | decl->getVertexSize(POS_TEX_BINDING), |
---|
| 134 | mRenderOp.vertexData->vertexCount, |
---|
| 135 | HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); |
---|
| 136 | bind->setBinding(POS_TEX_BINDING, vbuf); |
---|
| 137 | |
---|
| 138 | // colours |
---|
| 139 | vbuf = HardwareBufferManager::getSingleton(). |
---|
| 140 | createVertexBuffer( |
---|
| 141 | decl->getVertexSize(COLOUR_BINDING), |
---|
| 142 | mRenderOp.vertexData->vertexCount, |
---|
| 143 | HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY); |
---|
| 144 | bind->setBinding(COLOUR_BINDING, vbuf); |
---|
| 145 | |
---|
| 146 | mAllocSize = numChars; |
---|
| 147 | mColoursChanged = true; // force colour buffer regeneration |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | void TextAreaOverlayElement::updatePositionGeometry() |
---|
| 153 | { |
---|
| 154 | float *pVert; |
---|
| 155 | |
---|
| 156 | if (mpFont.isNull()) |
---|
| 157 | { |
---|
| 158 | // not initialised yet, probably due to the order of creation in a template |
---|
| 159 | return; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | size_t charlen = mCaption.size(); |
---|
| 163 | checkMemoryAllocation( charlen ); |
---|
| 164 | |
---|
| 165 | mRenderOp.vertexData->vertexCount = charlen * 6; |
---|
| 166 | // Get position / texcoord buffer |
---|
| 167 | HardwareVertexBufferSharedPtr vbuf = |
---|
| 168 | mRenderOp.vertexData->vertexBufferBinding->getBuffer(POS_TEX_BINDING); |
---|
| 169 | pVert = static_cast<float*>( |
---|
| 170 | vbuf->lock(HardwareBuffer::HBL_DISCARD) ); |
---|
| 171 | |
---|
| 172 | float largestWidth = 0; |
---|
| 173 | float left = _getDerivedLeft() * 2.0 - 1.0; |
---|
| 174 | float top = -( (_getDerivedTop() * 2.0 ) - 1.0 ); |
---|
| 175 | |
---|
| 176 | // Derive space with from a number 0 |
---|
| 177 | if (mSpaceWidth == 0) |
---|
| 178 | { |
---|
| 179 | mSpaceWidth = mpFont->getGlyphAspectRatio(UNICODE_ZERO) * mCharHeight * 2.0 * mViewportAspectCoef; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | // Use iterator |
---|
| 183 | DisplayString::iterator i, iend; |
---|
| 184 | iend = mCaption.end(); |
---|
| 185 | bool newLine = true; |
---|
| 186 | for( i = mCaption.begin(); i != iend; ++i ) |
---|
| 187 | { |
---|
| 188 | if( newLine ) |
---|
| 189 | { |
---|
| 190 | Real len = 0.0f; |
---|
| 191 | for( DisplayString::iterator j = i; j != iend; j++ ) |
---|
| 192 | { |
---|
| 193 | Font::CodePoint character = OGRE_DEREF_DISPLAYSTRING_ITERATOR(j); |
---|
| 194 | if (character == UNICODE_CR |
---|
| 195 | || character == UNICODE_NEL |
---|
| 196 | || character == UNICODE_LF) |
---|
| 197 | { |
---|
| 198 | break; |
---|
| 199 | } |
---|
| 200 | else if (character == UNICODE_SPACE) // space |
---|
| 201 | { |
---|
| 202 | len += mSpaceWidth; |
---|
| 203 | } |
---|
| 204 | else |
---|
| 205 | { |
---|
| 206 | len += mpFont->getGlyphAspectRatio(character) * mCharHeight * 2.0 * mViewportAspectCoef; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | if( mAlignment == Right ) |
---|
| 211 | left -= len; |
---|
| 212 | else if( mAlignment == Center ) |
---|
| 213 | left -= len * 0.5; |
---|
| 214 | |
---|
| 215 | newLine = false; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | Font::CodePoint character = OGRE_DEREF_DISPLAYSTRING_ITERATOR(i); |
---|
| 219 | if (character == UNICODE_CR |
---|
| 220 | || character == UNICODE_NEL |
---|
| 221 | || character == UNICODE_LF) |
---|
| 222 | { |
---|
| 223 | left = _getDerivedLeft() * 2.0 - 1.0; |
---|
| 224 | top -= mCharHeight * 2.0; |
---|
| 225 | newLine = true; |
---|
| 226 | // Also reduce tri count |
---|
| 227 | mRenderOp.vertexData->vertexCount -= 6; |
---|
| 228 | |
---|
| 229 | // consume CR/LF in one |
---|
| 230 | if (character == UNICODE_CR) |
---|
| 231 | { |
---|
| 232 | DisplayString::iterator peeki = i; |
---|
| 233 | peeki++; |
---|
| 234 | if (peeki != iend && OGRE_DEREF_DISPLAYSTRING_ITERATOR(peeki) == UNICODE_LF) |
---|
| 235 | { |
---|
| 236 | i = peeki; // skip both as one newline |
---|
| 237 | // Also reduce tri count |
---|
| 238 | mRenderOp.vertexData->vertexCount -= 6; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | } |
---|
| 242 | continue; |
---|
| 243 | } |
---|
| 244 | else if (character == UNICODE_SPACE) // space |
---|
| 245 | { |
---|
| 246 | // Just leave a gap, no tris |
---|
| 247 | left += mSpaceWidth; |
---|
| 248 | // Also reduce tri count |
---|
| 249 | mRenderOp.vertexData->vertexCount -= 6; |
---|
| 250 | continue; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | Real horiz_height = mpFont->getGlyphAspectRatio(character) * mViewportAspectCoef ; |
---|
| 254 | const Font::UVRect& uvRect = mpFont->getGlyphTexCoords(character); |
---|
| 255 | |
---|
| 256 | // each vert is (x, y, z, u, v) |
---|
| 257 | //------------------------------------------------------------------------------------- |
---|
| 258 | // First tri |
---|
| 259 | // |
---|
| 260 | // Upper left |
---|
| 261 | *pVert++ = left; |
---|
| 262 | *pVert++ = top; |
---|
| 263 | *pVert++ = -1.0; |
---|
| 264 | *pVert++ = uvRect.left; |
---|
| 265 | *pVert++ = uvRect.top; |
---|
| 266 | |
---|
| 267 | top -= mCharHeight * 2.0; |
---|
| 268 | |
---|
| 269 | // Bottom left |
---|
| 270 | *pVert++ = left; |
---|
| 271 | *pVert++ = top; |
---|
| 272 | *pVert++ = -1.0; |
---|
| 273 | *pVert++ = uvRect.left; |
---|
| 274 | *pVert++ = uvRect.bottom; |
---|
| 275 | |
---|
| 276 | top += mCharHeight * 2.0; |
---|
| 277 | left += horiz_height * mCharHeight * 2.0; |
---|
| 278 | |
---|
| 279 | // Top right |
---|
| 280 | *pVert++ = left; |
---|
| 281 | *pVert++ = top; |
---|
| 282 | *pVert++ = -1.0; |
---|
| 283 | *pVert++ = uvRect.right; |
---|
| 284 | *pVert++ = uvRect.top; |
---|
| 285 | //------------------------------------------------------------------------------------- |
---|
| 286 | |
---|
| 287 | //------------------------------------------------------------------------------------- |
---|
| 288 | // Second tri |
---|
| 289 | // |
---|
| 290 | // Top right (again) |
---|
| 291 | *pVert++ = left; |
---|
| 292 | *pVert++ = top; |
---|
| 293 | *pVert++ = -1.0; |
---|
| 294 | *pVert++ = uvRect.right; |
---|
| 295 | *pVert++ = uvRect.top; |
---|
| 296 | |
---|
| 297 | top -= mCharHeight * 2.0; |
---|
| 298 | left -= horiz_height * mCharHeight * 2.0; |
---|
| 299 | |
---|
| 300 | // Bottom left (again) |
---|
| 301 | *pVert++ = left; |
---|
| 302 | *pVert++ = top; |
---|
| 303 | *pVert++ = -1.0; |
---|
| 304 | *pVert++ = uvRect.left; |
---|
| 305 | *pVert++ = uvRect.bottom; |
---|
| 306 | |
---|
| 307 | left += horiz_height * mCharHeight * 2.0; |
---|
| 308 | |
---|
| 309 | // Bottom right |
---|
| 310 | *pVert++ = left; |
---|
| 311 | *pVert++ = top; |
---|
| 312 | *pVert++ = -1.0; |
---|
| 313 | *pVert++ = uvRect.right; |
---|
| 314 | *pVert++ = uvRect.bottom; |
---|
| 315 | //------------------------------------------------------------------------------------- |
---|
| 316 | |
---|
| 317 | // Go back up with top |
---|
| 318 | top += mCharHeight * 2.0; |
---|
| 319 | |
---|
| 320 | float currentWidth = (left + 1)/2 - _getDerivedLeft(); |
---|
| 321 | if (currentWidth > largestWidth) |
---|
| 322 | { |
---|
| 323 | largestWidth = currentWidth; |
---|
| 324 | |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | // Unlock vertex buffer |
---|
| 328 | vbuf->unlock(); |
---|
| 329 | |
---|
| 330 | if (mMetricsMode == GMM_PIXELS) |
---|
| 331 | { |
---|
| 332 | // Derive parametric version of dimensions |
---|
| 333 | Real vpWidth; |
---|
| 334 | vpWidth = (Real) (OverlayManager::getSingleton().getViewportWidth()); |
---|
| 335 | |
---|
| 336 | largestWidth *= vpWidth; |
---|
| 337 | }; |
---|
| 338 | |
---|
| 339 | if (getWidth() < largestWidth) |
---|
| 340 | setWidth(largestWidth); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | void TextAreaOverlayElement::updateTextureGeometry() |
---|
| 344 | { |
---|
| 345 | // Nothing to do, we combine positions and textures |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | void TextAreaOverlayElement::setCaption( const DisplayString& caption ) |
---|
| 349 | { |
---|
| 350 | mCaption = caption; |
---|
| 351 | mGeomPositionsOutOfDate = true; |
---|
| 352 | mGeomUVsOutOfDate = true; |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | void TextAreaOverlayElement::setFontName( const String& font ) |
---|
| 356 | { |
---|
| 357 | mpFont = FontManager::getSingleton().getByName( font ); |
---|
| 358 | if (mpFont.isNull()) |
---|
| 359 | OGRE_EXCEPT( Exception::ERR_ITEM_NOT_FOUND, "Could not find font " + font, |
---|
| 360 | "TextAreaOverlayElement::setFontName" ); |
---|
| 361 | mpFont->load(); |
---|
| 362 | mpMaterial = mpFont->getMaterial(); |
---|
| 363 | mpMaterial->setDepthCheckEnabled(false); |
---|
| 364 | mpMaterial->setLightingEnabled(false); |
---|
| 365 | |
---|
| 366 | mGeomPositionsOutOfDate = true; |
---|
| 367 | mGeomUVsOutOfDate = true; |
---|
| 368 | } |
---|
| 369 | const String& TextAreaOverlayElement::getFontName() const |
---|
| 370 | { |
---|
| 371 | return mpFont->getName(); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | void TextAreaOverlayElement::setCharHeight( Real height ) |
---|
| 375 | { |
---|
| 376 | if (mMetricsMode != GMM_RELATIVE) |
---|
| 377 | { |
---|
| 378 | mPixelCharHeight = static_cast<unsigned>(height); |
---|
| 379 | } |
---|
| 380 | else |
---|
| 381 | { |
---|
| 382 | mCharHeight = height; |
---|
| 383 | } |
---|
| 384 | mGeomPositionsOutOfDate = true; |
---|
| 385 | } |
---|
| 386 | Real TextAreaOverlayElement::getCharHeight() const |
---|
| 387 | { |
---|
| 388 | if (mMetricsMode == GMM_PIXELS) |
---|
| 389 | { |
---|
| 390 | return mPixelCharHeight; |
---|
| 391 | } |
---|
| 392 | else |
---|
| 393 | { |
---|
| 394 | return mCharHeight; |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | void TextAreaOverlayElement::setSpaceWidth( Real width ) |
---|
| 399 | { |
---|
| 400 | if (mMetricsMode != GMM_RELATIVE) |
---|
| 401 | { |
---|
| 402 | mPixelSpaceWidth = static_cast<unsigned>(width); |
---|
| 403 | } |
---|
| 404 | else |
---|
| 405 | { |
---|
| 406 | mSpaceWidth = width; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | mGeomPositionsOutOfDate = true; |
---|
| 410 | } |
---|
| 411 | Real TextAreaOverlayElement::getSpaceWidth() const |
---|
| 412 | { |
---|
| 413 | if (mMetricsMode == GMM_PIXELS) |
---|
| 414 | { |
---|
| 415 | return mPixelSpaceWidth; |
---|
| 416 | } |
---|
| 417 | else |
---|
| 418 | { |
---|
| 419 | return mSpaceWidth; |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | //--------------------------------------------------------------------- |
---|
| 424 | TextAreaOverlayElement::~TextAreaOverlayElement() |
---|
| 425 | { |
---|
| 426 | delete mRenderOp.vertexData; |
---|
| 427 | } |
---|
| 428 | //--------------------------------------------------------------------- |
---|
| 429 | const String& TextAreaOverlayElement::getTypeName(void) const |
---|
| 430 | { |
---|
| 431 | return msTypeName; |
---|
| 432 | } |
---|
| 433 | //--------------------------------------------------------------------- |
---|
| 434 | void TextAreaOverlayElement::getRenderOperation(RenderOperation& op) |
---|
| 435 | { |
---|
| 436 | op = mRenderOp; |
---|
| 437 | } |
---|
| 438 | //--------------------------------------------------------------------- |
---|
| 439 | void TextAreaOverlayElement::setMaterialName(const String& matName) |
---|
| 440 | { |
---|
| 441 | OverlayElement::setMaterialName(matName); |
---|
| 442 | } |
---|
| 443 | //--------------------------------------------------------------------- |
---|
| 444 | void TextAreaOverlayElement::addBaseParameters(void) |
---|
| 445 | { |
---|
| 446 | OverlayElement::addBaseParameters(); |
---|
| 447 | ParamDictionary* dict = getParamDictionary(); |
---|
| 448 | |
---|
| 449 | dict->addParameter(ParameterDef("char_height", |
---|
| 450 | "Sets the height of the characters in relation to the screen." |
---|
| 451 | , PT_REAL), |
---|
| 452 | &msCmdCharHeight); |
---|
| 453 | |
---|
| 454 | dict->addParameter(ParameterDef("space_width", |
---|
| 455 | "Sets the width of a space in relation to the screen." |
---|
| 456 | , PT_REAL), |
---|
| 457 | &msCmdSpaceWidth); |
---|
| 458 | |
---|
| 459 | dict->addParameter(ParameterDef("font_name", |
---|
| 460 | "Sets the name of the font to use." |
---|
| 461 | , PT_STRING), |
---|
| 462 | &msCmdFontName); |
---|
| 463 | |
---|
| 464 | dict->addParameter(ParameterDef("colour", |
---|
| 465 | "Sets the colour of the font (a solid colour)." |
---|
| 466 | , PT_STRING), |
---|
| 467 | &msCmdColour); |
---|
| 468 | |
---|
| 469 | dict->addParameter(ParameterDef("colour_bottom", |
---|
| 470 | "Sets the colour of the font at the bottom (a gradient colour)." |
---|
| 471 | , PT_STRING), |
---|
| 472 | &msCmdColourBottom); |
---|
| 473 | |
---|
| 474 | dict->addParameter(ParameterDef("colour_top", |
---|
| 475 | "Sets the colour of the font at the top (a gradient colour)." |
---|
| 476 | , PT_STRING), |
---|
| 477 | &msCmdColourTop); |
---|
| 478 | |
---|
| 479 | dict->addParameter(ParameterDef("alignment", |
---|
| 480 | "Sets the alignment of the text: 'left', 'center' or 'right'." |
---|
| 481 | , PT_STRING), |
---|
| 482 | &msCmdAlignment); |
---|
| 483 | } |
---|
| 484 | //--------------------------------------------------------------------- |
---|
| 485 | void TextAreaOverlayElement::setColour(const ColourValue& col) |
---|
| 486 | { |
---|
| 487 | mColourBottom = mColourTop = col; |
---|
| 488 | mColoursChanged = true; |
---|
| 489 | } |
---|
| 490 | //--------------------------------------------------------------------- |
---|
| 491 | const ColourValue& TextAreaOverlayElement::getColour(void) const |
---|
| 492 | { |
---|
| 493 | // Either one |
---|
| 494 | return mColourTop; |
---|
| 495 | } |
---|
| 496 | //--------------------------------------------------------------------- |
---|
| 497 | void TextAreaOverlayElement::setColourBottom(const ColourValue& col) |
---|
| 498 | { |
---|
| 499 | mColourBottom = col; |
---|
| 500 | mColoursChanged = true; |
---|
| 501 | } |
---|
| 502 | //--------------------------------------------------------------------- |
---|
| 503 | const ColourValue& TextAreaOverlayElement::getColourBottom(void) const |
---|
| 504 | { |
---|
| 505 | return mColourBottom; |
---|
| 506 | } |
---|
| 507 | //--------------------------------------------------------------------- |
---|
| 508 | void TextAreaOverlayElement::setColourTop(const ColourValue& col) |
---|
| 509 | { |
---|
| 510 | mColourTop = col; |
---|
| 511 | mColoursChanged = true; |
---|
| 512 | } |
---|
| 513 | //--------------------------------------------------------------------- |
---|
| 514 | const ColourValue& TextAreaOverlayElement::getColourTop(void) const |
---|
| 515 | { |
---|
| 516 | return mColourTop; |
---|
| 517 | } |
---|
| 518 | //--------------------------------------------------------------------- |
---|
| 519 | void TextAreaOverlayElement::updateColours(void) |
---|
| 520 | { |
---|
| 521 | // Convert to system-specific |
---|
| 522 | RGBA topColour, bottomColour; |
---|
| 523 | Root::getSingleton().convertColourValue(mColourTop, &topColour); |
---|
| 524 | Root::getSingleton().convertColourValue(mColourBottom, &bottomColour); |
---|
| 525 | |
---|
| 526 | HardwareVertexBufferSharedPtr vbuf = |
---|
| 527 | mRenderOp.vertexData->vertexBufferBinding->getBuffer(COLOUR_BINDING); |
---|
| 528 | |
---|
| 529 | RGBA* pDest = static_cast<RGBA*>( |
---|
| 530 | vbuf->lock(HardwareBuffer::HBL_DISCARD) ); |
---|
| 531 | |
---|
| 532 | for (size_t i = 0; i < mAllocSize; ++i) |
---|
| 533 | { |
---|
| 534 | // First tri (top, bottom, top) |
---|
| 535 | *pDest++ = topColour; |
---|
| 536 | *pDest++ = bottomColour; |
---|
| 537 | *pDest++ = topColour; |
---|
| 538 | // Second tri (top, bottom, bottom) |
---|
| 539 | *pDest++ = topColour; |
---|
| 540 | *pDest++ = bottomColour; |
---|
| 541 | *pDest++ = bottomColour; |
---|
| 542 | } |
---|
| 543 | vbuf->unlock(); |
---|
| 544 | |
---|
| 545 | } |
---|
| 546 | //----------------------------------------------------------------------- |
---|
| 547 | void TextAreaOverlayElement::setMetricsMode(GuiMetricsMode gmm) |
---|
| 548 | { |
---|
| 549 | Real vpWidth, vpHeight; |
---|
| 550 | |
---|
| 551 | vpWidth = (Real) (OverlayManager::getSingleton().getViewportWidth()); |
---|
| 552 | vpHeight = (Real) (OverlayManager::getSingleton().getViewportHeight()); |
---|
| 553 | mViewportAspectCoef = vpHeight/vpWidth; |
---|
| 554 | |
---|
| 555 | OverlayElement::setMetricsMode(gmm); |
---|
| 556 | if (gmm != GMM_RELATIVE) |
---|
| 557 | { |
---|
| 558 | // Set pixel variables based on viewport multipliers |
---|
| 559 | mPixelCharHeight = static_cast<unsigned>(mCharHeight * vpHeight); |
---|
| 560 | mPixelSpaceWidth = static_cast<unsigned>(mSpaceWidth * vpHeight); |
---|
| 561 | } |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | //----------------------------------------------------------------------- |
---|
| 565 | void TextAreaOverlayElement::_update(void) |
---|
| 566 | { |
---|
| 567 | Real vpWidth, vpHeight; |
---|
| 568 | |
---|
| 569 | vpWidth = (Real) (OverlayManager::getSingleton().getViewportWidth()); |
---|
| 570 | vpHeight = (Real) (OverlayManager::getSingleton().getViewportHeight()); |
---|
| 571 | mViewportAspectCoef = vpHeight/vpWidth; |
---|
| 572 | |
---|
| 573 | if (mMetricsMode != GMM_RELATIVE && |
---|
| 574 | (OverlayManager::getSingleton().hasViewportChanged() || mGeomPositionsOutOfDate)) |
---|
| 575 | { |
---|
| 576 | // Recalc character size |
---|
| 577 | |
---|
| 578 | mCharHeight = (Real) mPixelCharHeight / vpHeight; |
---|
| 579 | mSpaceWidth = (Real) mPixelSpaceWidth / vpHeight; |
---|
| 580 | mGeomPositionsOutOfDate = true; |
---|
| 581 | } |
---|
| 582 | OverlayElement::_update(); |
---|
| 583 | |
---|
| 584 | if (mColoursChanged && mInitialised) |
---|
| 585 | { |
---|
| 586 | updateColours(); |
---|
| 587 | mColoursChanged = false; |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | //--------------------------------------------------------------------------------------------- |
---|
| 591 | // Char height command object |
---|
| 592 | // |
---|
| 593 | String TextAreaOverlayElement::CmdCharHeight::doGet( const void* target ) const |
---|
| 594 | { |
---|
| 595 | return StringConverter::toString( |
---|
| 596 | static_cast< const TextAreaOverlayElement* >( target )->getCharHeight() ); |
---|
| 597 | } |
---|
| 598 | void TextAreaOverlayElement::CmdCharHeight::doSet( void* target, const String& val ) |
---|
| 599 | { |
---|
| 600 | static_cast< TextAreaOverlayElement* >( target )->setCharHeight( |
---|
| 601 | StringConverter::parseReal( val ) ); |
---|
| 602 | } |
---|
| 603 | //--------------------------------------------------------------------------------------------- |
---|
| 604 | // Space width command object |
---|
| 605 | // |
---|
| 606 | String TextAreaOverlayElement::CmdSpaceWidth::doGet( const void* target ) const |
---|
| 607 | { |
---|
| 608 | return StringConverter::toString( |
---|
| 609 | static_cast< const TextAreaOverlayElement* >( target )->getSpaceWidth() ); |
---|
| 610 | } |
---|
| 611 | void TextAreaOverlayElement::CmdSpaceWidth::doSet( void* target, const String& val ) |
---|
| 612 | { |
---|
| 613 | static_cast< TextAreaOverlayElement* >( target )->setSpaceWidth( |
---|
| 614 | StringConverter::parseReal( val ) ); |
---|
| 615 | } |
---|
| 616 | //--------------------------------------------------------------------------------------------- |
---|
| 617 | |
---|
| 618 | //--------------------------------------------------------------------------------------------- |
---|
| 619 | // Font name command object |
---|
| 620 | // |
---|
| 621 | String TextAreaOverlayElement::CmdFontName::doGet( const void* target ) const |
---|
| 622 | { |
---|
| 623 | return static_cast< const TextAreaOverlayElement* >( target )->getFontName(); |
---|
| 624 | } |
---|
| 625 | void TextAreaOverlayElement::CmdFontName::doSet( void* target, const String& val ) |
---|
| 626 | { |
---|
| 627 | static_cast< TextAreaOverlayElement* >( target )->setFontName( val ); |
---|
| 628 | } |
---|
| 629 | //--------------------------------------------------------------------------------------------- |
---|
| 630 | //--------------------------------------------------------------------------------------------- |
---|
| 631 | // Colour command object |
---|
| 632 | // |
---|
| 633 | String TextAreaOverlayElement::CmdColour::doGet( const void* target ) const |
---|
| 634 | { |
---|
| 635 | return StringConverter::toString ( |
---|
| 636 | static_cast< const TextAreaOverlayElement* >( target )->getColour()); |
---|
| 637 | } |
---|
| 638 | void TextAreaOverlayElement::CmdColour::doSet( void* target, const String& val ) |
---|
| 639 | { |
---|
| 640 | static_cast< TextAreaOverlayElement* >( target )->setColour( |
---|
| 641 | StringConverter::parseColourValue(val) ); |
---|
| 642 | } |
---|
| 643 | //--------------------------------------------------------------------------------------------- |
---|
| 644 | //--------------------------------------------------------------------------------------------- |
---|
| 645 | //--------------------------------------------------------------------------------------------- |
---|
| 646 | // Top colour command object |
---|
| 647 | // |
---|
| 648 | String TextAreaOverlayElement::CmdColourTop::doGet( const void* target ) const |
---|
| 649 | { |
---|
| 650 | return StringConverter::toString ( |
---|
| 651 | static_cast< const TextAreaOverlayElement* >( target )->getColourTop()); |
---|
| 652 | } |
---|
| 653 | void TextAreaOverlayElement::CmdColourTop::doSet( void* target, const String& val ) |
---|
| 654 | { |
---|
| 655 | static_cast< TextAreaOverlayElement* >( target )->setColourTop( |
---|
| 656 | StringConverter::parseColourValue(val) ); |
---|
| 657 | } |
---|
| 658 | //--------------------------------------------------------------------------------------------- |
---|
| 659 | //--------------------------------------------------------------------------------------------- |
---|
| 660 | //--------------------------------------------------------------------------------------------- |
---|
| 661 | // Bottom colour command object |
---|
| 662 | // |
---|
| 663 | String TextAreaOverlayElement::CmdColourBottom::doGet( const void* target ) const |
---|
| 664 | { |
---|
| 665 | return StringConverter::toString ( |
---|
| 666 | static_cast< const TextAreaOverlayElement* >( target )->getColourBottom()); |
---|
| 667 | } |
---|
| 668 | void TextAreaOverlayElement::CmdColourBottom::doSet( void* target, const String& val ) |
---|
| 669 | { |
---|
| 670 | static_cast< TextAreaOverlayElement* >( target )->setColourBottom( |
---|
| 671 | StringConverter::parseColourValue(val) ); |
---|
| 672 | } |
---|
| 673 | //--------------------------------------------------------------------------------------------- |
---|
| 674 | //--------------------------------------------------------------------------------------------- |
---|
| 675 | //--------------------------------------------------------------------------------------------- |
---|
| 676 | // Alignment command object |
---|
| 677 | // |
---|
| 678 | String TextAreaOverlayElement::CmdAlignment::doGet( const void* target ) const |
---|
| 679 | { |
---|
| 680 | Alignment align = static_cast< const TextAreaOverlayElement* >( target )->getAlignment(); |
---|
| 681 | switch (align) |
---|
| 682 | { |
---|
| 683 | case Left: |
---|
| 684 | return "left"; |
---|
| 685 | case Center: |
---|
| 686 | return "center"; |
---|
| 687 | case Right: |
---|
| 688 | return "right"; |
---|
| 689 | |
---|
| 690 | } |
---|
| 691 | // To keep compiler happy |
---|
| 692 | return "left"; |
---|
| 693 | } |
---|
| 694 | void TextAreaOverlayElement::CmdAlignment::doSet( void* target, const String& val ) |
---|
| 695 | { |
---|
| 696 | if (val == "center") |
---|
| 697 | { |
---|
| 698 | static_cast< TextAreaOverlayElement* >( target )->setAlignment(Center); |
---|
| 699 | } |
---|
| 700 | else if (val == "right") |
---|
| 701 | { |
---|
| 702 | static_cast< TextAreaOverlayElement* >( target )->setAlignment(Right); |
---|
| 703 | } |
---|
| 704 | else |
---|
| 705 | { |
---|
| 706 | static_cast< TextAreaOverlayElement* >( target )->setAlignment(Left); |
---|
| 707 | } |
---|
| 708 | } |
---|
| 709 | //--------------------------------------------------------------------------------------------- |
---|
| 710 | } |
---|