40 #ifndef _uitextureatlas_cpp
41 #define _uitextureatlas_cpp
43 #include "UI/textureatlas.h"
45 #include "UI/sprite.h"
46 #include "stringtool.h"
50 #include <OgreTexture.h>
51 #include <OgreMaterial.h>
53 #include <OgreGpuProgram.h>
54 #include <OgreMaterialManager.h>
55 #include <OgreTextureManager.h>
56 #include <OgreRenderSystem.h>
67 Ogre::TexturePtr TATexture;
68 Ogre::MaterialPtr Mat2D;
69 Ogre::MaterialPtr Mat3D;
92 this->TAID->TATexture = Ogre::TextureManager::getSingletonPtr()->createManual(this->AtlasName+
"Texture",GroupName,Ogre::TEX_TYPE_2D,Width,Height,0,Ogre::PF_R8G8B8A8,Ogre::TU_DEFAULT);
102 if( !NameAttrib.
Empty() ) {
103 this->AtlasName = NameAttrib.
AsString();
121 for( FontDataIterator FontIt = this->Fonts.begin() ; FontIt != this->Fonts.end() ; ++FontIt )
123 delete (*FontIt).second;
127 for( SpriteIterator SpriteIt = this->Sprites.begin() ; SpriteIt != this->Sprites.end() ; ++SpriteIt )
129 delete (*SpriteIt).second;
131 this->Sprites.clear();
147 if( !CurrAttrib.
Empty() )
152 CurrAttrib = AtlasTextureNode.
GetAttribute(
"TexFileGroup");
153 if( !CurrAttrib.
Empty() )
157 this->TAID->TATexture = Ogre::TextureManager::getSingletonPtr()->getByName(FileName,GroupName);
158 if(this->TAID->TATexture.isNull())
160 this->TAID->TATexture = Ogre::TextureManager::getSingletonPtr()->load(FileName,GroupName,Ogre::TEX_TYPE_2D,0);
163 this->InverseTextureSize.
X = 1.0 / this->TAID->TATexture->getWidth();
164 this->InverseTextureSize.
Y = 1.0 / this->TAID->TATexture->getHeight();
167 CurrAttrib = AtlasTextureNode.
GetAttribute(
"WhitePixel");
168 if( !CurrAttrib.
Empty() )
172 this->WhitePixel.
X *= this->InverseTextureSize.
X;
173 this->WhitePixel.
Y *= this->InverseTextureSize.
Y;
185 CurrAttrib = (*FontNode).GetAttribute(
"Name");
186 if( !CurrAttrib.
Empty() )
190 FontDataIterator FontIt = Fonts.find(Data->
GetName());
191 if( FontIt == Fonts.end() ) { Fonts[Data->
GetName()] = Data; }
195 CurrAttrib = (*FontNode).GetAttribute(
"OffsetX");
196 if( !CurrAttrib.
Empty() )
197 Offset.
X = CurrAttrib.
AsReal();
200 CurrAttrib = (*FontNode).GetAttribute(
"OffsetY");
201 if( !CurrAttrib.
Empty() )
202 Offset.
Y = CurrAttrib.
AsReal();
205 CurrAttrib = (*FontNode).GetAttribute(
"LineHeight");
206 if( !CurrAttrib.
Empty() )
210 CurrAttrib = (*FontNode).GetAttribute(
"SpaceLength");
211 if( !CurrAttrib.
Empty() )
215 CurrAttrib = (*FontNode).GetAttribute(
"BaseLine");
216 if( !CurrAttrib.
Empty() )
220 CurrAttrib = (*FontNode).GetAttribute(
"LetterSpacing");
221 if( !CurrAttrib.
Empty() )
254 Ogre::RenderSystem* rs = Ogre::Root::getSingletonPtr()->getRenderSystem();
256 Real TexelX = rs->getHorizontalTexelOffset();
257 Real TexelY = rs->getVerticalTexelOffset();
260 Real Left = 0, Top = 0, Right = 0, Bottom = 0;
265 CurrAttrib = (*GlyphIt).GetAttribute(
"ID");
266 if( !CurrAttrib.
Empty() ) ParsedGlyphID = CurrAttrib.
AsUint();
270 CurrAttrib = (*GlyphIt).GetAttribute(
"PositionX");
271 if( !CurrAttrib.
Empty() ) Left = (Offset.
X + CurrAttrib.
AsReal()) - TexelX;
275 CurrAttrib = (*GlyphIt).GetAttribute(
"PositionY");
276 if( !CurrAttrib.
Empty() ) Top = (Offset.
Y + CurrAttrib.
AsReal()) - TexelY;
280 CurrAttrib = (*GlyphIt).GetAttribute(
"SizeX");
281 if( !CurrAttrib.
Empty() ) Right = (Left + CurrAttrib.
AsReal()) - TexelX;
285 CurrAttrib = (*GlyphIt).GetAttribute(
"SizeY");
286 if( !CurrAttrib.
Empty() ) Bottom = (Top + CurrAttrib.
AsReal()) - TexelY;
291 NewGlyph->
GlyphID = ParsedGlyphID;
295 Left *= InverseTextureSize.
X;
296 Top *= InverseTextureSize.
Y;
297 Right *= InverseTextureSize.
X;
298 Bottom *= InverseTextureSize.
Y;
307 CurrAttrib = (*GlyphIt).GetAttribute(
"Advance");
323 CurrAttrib = (*KerningNode).GetAttribute(
"Left");
324 if( !CurrAttrib.
Empty() ) LeftID = CurrAttrib.
AsReal();
328 CurrAttrib = (*KerningNode).GetAttribute(
"Right");
329 if( !CurrAttrib.
Empty() ) RightID = CurrAttrib.
AsReal();
333 CurrAttrib = (*KerningNode).GetAttribute(
"Adjust");
334 if( !CurrAttrib.
Empty() ) Kerning = CurrAttrib.
AsReal();
347 for(
XML::NodeIterator VerticalOffsetNode = VerticalOffsetsNode.
begin() ; VerticalOffsetNode != VerticalOffsetsNode.
end() ; ++VerticalOffsetNode )
350 CurrAttrib = (*VerticalOffsetNode).GetAttribute(
"GlyphID");
351 if( !CurrAttrib.
Empty() ) GlyphID = CurrAttrib.
AsUint();
355 CurrAttrib = (*VerticalOffsetNode).GetAttribute(
"Offset");
356 if( !CurrAttrib.
Empty() ) Offset = CurrAttrib.
AsReal();
366 Ogre::RenderSystem* rs = Ogre::Root::getSingletonPtr()->getRenderSystem();
368 Real TexelX = rs->getHorizontalTexelOffset();
369 Real TexelY = rs->getVerticalTexelOffset();
372 Real Left = 0, Top = 0, Right = 0, Bottom = 0;
377 CurrAttrib = (*SpriteIt).GetAttribute(
"Name");
378 if( !CurrAttrib.
Empty() ) SpriteName = CurrAttrib.
AsString();
382 CurrAttrib = (*SpriteIt).GetAttribute(
"PositionX");
383 if( !CurrAttrib.
Empty() ) Left = CurrAttrib.
AsReal() - TexelX;
387 CurrAttrib = (*SpriteIt).GetAttribute(
"PositionY");
388 if( !CurrAttrib.
Empty() ) Top = CurrAttrib.
AsReal() - TexelY;
392 CurrAttrib = (*SpriteIt).GetAttribute(
"SizeX");
393 if( !CurrAttrib.
Empty() ) Right = (Left + CurrAttrib.
AsReal()) - TexelX;
397 CurrAttrib = (*SpriteIt).GetAttribute(
"SizeY");
398 if( !CurrAttrib.
Empty() ) Bottom = (Top + CurrAttrib.
AsReal()) - TexelY;
402 Left *= InverseTextureSize.
X;
403 Top *= InverseTextureSize.
Y;
404 Right *= InverseTextureSize.
X;
405 Bottom *= InverseTextureSize.
Y;
408 Sprite* NewSprite =
new Sprite(SpriteName,Top,Left,Bottom,Right);
409 SpriteIterator SpIt = this->Sprites.find(SpriteName);
410 if( SpIt == this->Sprites.end() ) {
411 this->Sprites.insert(std::pair<String,Sprite*>(SpriteName,NewSprite));
414 ExceptionStream <<
"Sprite named \"" << SpriteName <<
"\" already exists in Atlas: \"" << this->AtlasName <<
"\".";
422 String MatName =
"Mezz2D." + this->TAID->TATexture->getName();
423 this->TAID->Mat2D = Ogre::MaterialManager::getSingletonPtr()->getByName(MatName);
424 if(!this->TAID->Mat2D.isNull())
430 this->TAID->Pass2D = this->TAID->Mat2D->getTechnique(0)->getPass(0);
431 this->TAID->Pass2D->getTextureUnitState(0)->setTextureName(this->TAID->TATexture->getName());
436 String MatName =
"Mezz3D." + this->TAID->TATexture->getName();
437 this->TAID->Mat3D = Ogre::MaterialManager::getSingletonPtr()->getByName(MatName);
438 if(!this->TAID->Mat3D.isNull())
444 this->TAID->Pass3D = this->TAID->Mat3D->getTechnique(0)->getPass(0);
445 this->TAID->Pass3D->getTextureUnitState(0)->setTextureName(this->TAID->TATexture->getName());
450 Ogre::MaterialPtr Material2D = Ogre::MaterialManager::getSingletonPtr()->getByName(
"Mezz2D");
451 if(Material2D.isNull() ==
false)
453 Ogre::Pass* MatPass = Material2D->getTechnique(0)->getPass(0);
455 if(MatPass->hasVertexProgram())
457 Ogre::GpuProgramPtr gpuPtr = MatPass->getVertexProgram();
461 if(MatPass->hasFragmentProgram())
463 Ogre::GpuProgramPtr gpuPtr = MatPass->getFragmentProgram();
470 Material2D = Ogre::MaterialManager::getSingletonPtr()->create(
"Mezz2D",
"UI");
471 Ogre::Pass* MatPass = Material2D->getTechnique(0)->getPass(0);
472 MatPass->setCullingMode(Ogre::CULL_NONE);
473 MatPass->setDepthCheckEnabled(
false);
474 MatPass->setDepthWriteEnabled(
false);
475 MatPass->setLightingEnabled(
false);
476 MatPass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
478 Ogre::TextureUnitState* TexUnit = MatPass->createTextureUnitState();
479 TexUnit->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
480 TexUnit->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);
486 Ogre::MaterialPtr Material3D = Ogre::MaterialManager::getSingletonPtr()->getByName(
"Mezz3D");
487 if(Material3D.isNull() ==
false)
489 Ogre::Pass* MatPass = Material3D->getTechnique(0)->getPass(0);
491 if(MatPass->hasVertexProgram())
493 Ogre::GpuProgramPtr gpuPtr = MatPass->getVertexProgram();
497 if(MatPass->hasFragmentProgram())
499 Ogre::GpuProgramPtr gpuPtr = MatPass->getFragmentProgram();
506 Material3D = Ogre::MaterialManager::getSingletonPtr()->create(
"Mezz3D",
"UI");
507 Ogre::Pass* MatPass = Material3D->getTechnique(0)->getPass(0);
508 MatPass->setCullingMode(Ogre::CULL_NONE);
509 MatPass->setDepthCheckEnabled(
false);
510 MatPass->setDepthWriteEnabled(
false);
511 MatPass->setLightingEnabled(
false);
512 MatPass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
514 Ogre::TextureUnitState* TexUnit = MatPass->createTextureUnitState();
515 TexUnit->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
516 TexUnit->setTextureFiltering(Ogre::FO_ANISOTROPIC, Ogre::FO_ANISOTROPIC, Ogre::FO_ANISOTROPIC);
533 ConstFontDataIterator it = Fonts.find(FontName);
534 if (it == Fonts.end())
546 ConstSpriteIterator it = Sprites.find(Name);
547 if (it == Sprites.end())
556 {
return WhitePixel; }
559 {
return WhitePixel.
X; }
562 {
return WhitePixel.
Y; }
565 {
return Vector2(
Real( this->TAID->TATexture->getWidth() ),
Real( this->TAID->TATexture->getHeight() ) ); }
568 {
return 1.0 /
Real(this->TAID->TATexture->getWidth()); }
571 {
return 1.0 /
Real(this->TAID->TATexture->getHeight()); }
578 if(this->TAID->Mat2D.isNull()) {
581 return this->TAID->Mat2D;
586 if(this->TAID->Mat3D.isNull()) {
589 return this->TAID->Mat3D;
593 {
return this->TAID->TATexture; }
596 {
return this->TAID->Pass2D; }
599 {
return this->TAID->Pass3D; }