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>
52 #include <OgreTechnique.h>
54 #include <OgreGpuProgram.h>
55 #include <OgreMaterialManager.h>
56 #include <OgreTextureManager.h>
57 #include <OgreRenderSystem.h>
74 struct MEZZ_LIB TextureAtlasInternalData
88 TextureAtlasInternalData()
106 this->
TAID =
new TextureAtlasInternalData();
109 this->
TAID->TATexture = Ogre::TextureManager::getSingletonPtr()->createManual(this->
AtlasName+
"Texture",GroupName,Ogre::TEX_TYPE_2D,Width,Height,0,Ogre::PF_R8G8B8A8,Ogre::TU_DEFAULT);
116 this->
TAID =
new TextureAtlasInternalData();
119 if( !NameAttrib.
Empty() ) {
140 delete (*FontIt).second;
146 delete (*SpriteIt).second;
164 if( !CurrAttrib.
Empty() )
169 CurrAttrib = AtlasTextureNode.
GetAttribute(
"TexFileGroup");
170 if( !CurrAttrib.
Empty() )
174 this->
TAID->TATexture = Ogre::TextureManager::getSingletonPtr()->getByName(FileName,GroupName);
175 if(this->
TAID->TATexture.isNull())
177 this->
TAID->TATexture = Ogre::TextureManager::getSingletonPtr()->load(FileName,GroupName,Ogre::TEX_TYPE_2D,0);
184 CurrAttrib = AtlasTextureNode.
GetAttribute(
"WhitePixel");
185 if( !CurrAttrib.
Empty() )
202 CurrAttrib = (*FontNode).GetAttribute(
"Name");
203 if( !CurrAttrib.
Empty() )
212 CurrAttrib = (*FontNode).GetAttribute(
"OffsetX");
213 if( !CurrAttrib.
Empty() )
214 Offset.
X = CurrAttrib.
AsReal();
217 CurrAttrib = (*FontNode).GetAttribute(
"OffsetY");
218 if( !CurrAttrib.
Empty() )
219 Offset.
Y = CurrAttrib.
AsReal();
222 CurrAttrib = (*FontNode).GetAttribute(
"LineHeight");
223 if( !CurrAttrib.
Empty() )
227 CurrAttrib = (*FontNode).GetAttribute(
"SpaceLength");
228 if( !CurrAttrib.
Empty() )
232 CurrAttrib = (*FontNode).GetAttribute(
"BaseLine");
233 if( !CurrAttrib.
Empty() )
237 CurrAttrib = (*FontNode).GetAttribute(
"LetterSpacing");
238 if( !CurrAttrib.
Empty() )
273 Ogre::RenderSystem* rs = Ogre::Root::getSingletonPtr()->getRenderSystem();
275 Real TexelX = rs->getHorizontalTexelOffset();
276 Real TexelY = rs->getVerticalTexelOffset();
279 Real Left = 0, Top = 0, Right = 0, Bottom = 0;
284 CurrAttrib = (*GlyphIt).GetAttribute(
"ID");
285 if( !CurrAttrib.
Empty() ) ParsedGlyphID = CurrAttrib.
AsUint();
289 CurrAttrib = (*GlyphIt).GetAttribute(
"PositionX");
290 if( !CurrAttrib.
Empty() ) Left = (Offset.
X + CurrAttrib.
AsReal()) - TexelX;
294 CurrAttrib = (*GlyphIt).GetAttribute(
"PositionY");
295 if( !CurrAttrib.
Empty() ) Top = (Offset.
Y + CurrAttrib.
AsReal()) - TexelY;
299 CurrAttrib = (*GlyphIt).GetAttribute(
"SizeX");
300 if( !CurrAttrib.
Empty() ) Right = (Left + CurrAttrib.
AsReal()) - TexelX;
304 CurrAttrib = (*GlyphIt).GetAttribute(
"SizeY");
305 if( !CurrAttrib.
Empty() ) Bottom = (Top + CurrAttrib.
AsReal()) - TexelY;
310 NewGlyph->
GlyphID = ParsedGlyphID;
326 CurrAttrib = (*GlyphIt).GetAttribute(
"Advance");
342 CurrAttrib = (*KerningNode).GetAttribute(
"Left");
343 if( !CurrAttrib.
Empty() ) LeftID = CurrAttrib.
AsReal();
347 CurrAttrib = (*KerningNode).GetAttribute(
"Right");
348 if( !CurrAttrib.
Empty() ) RightID = CurrAttrib.
AsReal();
352 CurrAttrib = (*KerningNode).GetAttribute(
"Adjust");
353 if( !CurrAttrib.
Empty() ) Kerning = CurrAttrib.
AsReal();
366 for(
XML::NodeIterator VerticalOffsetNode = VerticalOffsetsNode.
begin() ; VerticalOffsetNode != VerticalOffsetsNode.
end() ; ++VerticalOffsetNode )
369 CurrAttrib = (*VerticalOffsetNode).GetAttribute(
"GlyphID");
370 if( !CurrAttrib.
Empty() ) GlyphID = CurrAttrib.
AsUint();
374 CurrAttrib = (*VerticalOffsetNode).GetAttribute(
"Offset");
375 if( !CurrAttrib.
Empty() ) Offset = CurrAttrib.
AsReal();
385 Ogre::RenderSystem* rs = Ogre::Root::getSingletonPtr()->getRenderSystem();
387 Real TexelX = rs->getHorizontalTexelOffset();
388 Real TexelY = rs->getVerticalTexelOffset();
391 Real Left = 0, Top = 0, Right = 0, Bottom = 0;
396 CurrAttrib = (*SpriteIt).GetAttribute(
"Name");
397 if( !CurrAttrib.
Empty() ) SpriteName = CurrAttrib.
AsString();
401 CurrAttrib = (*SpriteIt).GetAttribute(
"PositionX");
402 if( !CurrAttrib.
Empty() ) Left = CurrAttrib.
AsReal() - TexelX;
406 CurrAttrib = (*SpriteIt).GetAttribute(
"PositionY");
407 if( !CurrAttrib.
Empty() ) Top = CurrAttrib.
AsReal() - TexelY;
411 CurrAttrib = (*SpriteIt).GetAttribute(
"SizeX");
412 if( !CurrAttrib.
Empty() ) Right = (Left + CurrAttrib.
AsReal()) - TexelX;
416 CurrAttrib = (*SpriteIt).GetAttribute(
"SizeY");
417 if( !CurrAttrib.
Empty() ) Bottom = (Top + CurrAttrib.
AsReal()) - TexelY;
427 Sprite* NewSprite =
new Sprite(SpriteName,Top,Left,Bottom,Right,
this);
429 if( SpIt == this->
Sprites.end() ) {
430 this->
Sprites.insert(std::pair<String,Sprite*>(SpriteName,NewSprite));
433 ExceptionStream <<
"Sprite named \"" << SpriteName <<
"\" already exists in Atlas: \"" << this->
AtlasName <<
"\".";
441 String MatName =
"Mezz2D." + this->
TAID->TATexture->getName();
442 this->
TAID->Mat2D = Ogre::MaterialManager::getSingletonPtr()->getByName(MatName);
443 if(!this->
TAID->Mat2D.isNull())
449 this->
TAID->Pass2D = this->
TAID->Mat2D->getTechnique(0)->getPass(0);
450 this->
TAID->Pass2D->getTextureUnitState(0)->setTextureName(this->
TAID->TATexture->getName());
455 String MatName =
"Mezz3D." + this->
TAID->TATexture->getName();
456 this->
TAID->Mat3D = Ogre::MaterialManager::getSingletonPtr()->getByName(MatName);
457 if(!this->
TAID->Mat3D.isNull())
463 this->
TAID->Pass3D = this->
TAID->Mat3D->getTechnique(0)->getPass(0);
464 this->
TAID->Pass3D->getTextureUnitState(0)->setTextureName(this->
TAID->TATexture->getName());
469 Ogre::MaterialPtr Material2D = Ogre::MaterialManager::getSingletonPtr()->getByName(
"Mezz2D");
470 if(Material2D.isNull() ==
false)
472 Ogre::Pass* MatPass = Material2D->getTechnique(0)->getPass(0);
474 if(MatPass->hasVertexProgram())
476 Ogre::GpuProgramPtr gpuPtr = MatPass->getVertexProgram();
480 if(MatPass->hasFragmentProgram())
482 Ogre::GpuProgramPtr gpuPtr = MatPass->getFragmentProgram();
489 Material2D = Ogre::MaterialManager::getSingletonPtr()->create(
"Mezz2D",
"UI");
490 Ogre::Pass* MatPass = Material2D->getTechnique(0)->getPass(0);
491 MatPass->setCullingMode(Ogre::CULL_NONE);
492 MatPass->setDepthCheckEnabled(
false);
493 MatPass->setDepthWriteEnabled(
false);
494 MatPass->setLightingEnabled(
false);
495 MatPass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
497 Ogre::TextureUnitState* TexUnit = MatPass->createTextureUnitState();
498 TexUnit->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
499 TexUnit->setTextureFiltering(Ogre::FO_NONE, Ogre::FO_NONE, Ogre::FO_NONE);
505 Ogre::MaterialPtr Material3D = Ogre::MaterialManager::getSingletonPtr()->getByName(
"Mezz3D");
506 if(Material3D.isNull() ==
false)
508 Ogre::Pass* MatPass = Material3D->getTechnique(0)->getPass(0);
510 if(MatPass->hasVertexProgram())
512 Ogre::GpuProgramPtr gpuPtr = MatPass->getVertexProgram();
516 if(MatPass->hasFragmentProgram())
518 Ogre::GpuProgramPtr gpuPtr = MatPass->getFragmentProgram();
525 Material3D = Ogre::MaterialManager::getSingletonPtr()->create(
"Mezz3D",
"UI");
526 Ogre::Pass* MatPass = Material3D->getTechnique(0)->getPass(0);
527 MatPass->setCullingMode(Ogre::CULL_NONE);
528 MatPass->setDepthCheckEnabled(
false);
529 MatPass->setDepthWriteEnabled(
false);
530 MatPass->setLightingEnabled(
false);
531 MatPass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
533 Ogre::TextureUnitState* TexUnit = MatPass->createTextureUnitState();
534 TexUnit->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
535 TexUnit->setTextureFiltering(Ogre::FO_ANISOTROPIC, Ogre::FO_ANISOTROPIC, Ogre::FO_ANISOTROPIC);
553 if( it == this->
Fonts.end() )
566 if( it == this->
Sprites.end() )
587 {
return 1.0 /
Real(this->
TAID->TATexture->getWidth()); }
590 {
return 1.0 /
Real(this->
TAID->TATexture->getHeight()); }
597 if(this->
TAID->Mat2D.isNull()) {
600 return this->
TAID->Mat2D;
605 if(this->
TAID->Mat3D.isNull()) {
608 return this->
TAID->Mat3D;
612 {
return this->
TAID->TATexture; }
615 {
return this->
TAID->Pass2D; }
618 {
return this->
TAID->Pass3D; }
void _SetSpaceLength(const Real SL)
Sets the length of a space for this font.
Thrown when duplicates of teh same identity string exist.
This class represents a collection of Glyphs in a common visual style.
Vector2 GetTextureSize() const
Gets the size of the TextureAtlas.
A light-weight handle for manipulating attributes in DOM tree.
const String & GetName() const
Gets the name of this Texture Atlas.
Class used to describe a single glyph or character available for text operations. ...
void _SetLetterSpacing(const Real LS)
Sets the LetterSpacing of this font.
void _GenerateWhitespaceGlyphs()
Generates Whitespace Glyphs from this font's data for this font.
Real GetInvTextureCoordsX() const
Gets the inverse size of the TextureAtlas on the X size.
Ogre::TexturePtr _GetTexture()
Gets the texture being used by this Atlas.
void ParseFonts(XML::Node &AtlasFontsNode)
Parses the data for a font.
KerningContainer Kernings
List of all the Kernings that apply to this glyph.
Real GetWidth() const
Gets the glyphs width on the Texture.
void _SetName(const String &Name)
Sets the name of this font.
TextureAtlas(const String &Name, const UInt32 &Width, const UInt32 &Height)
Proceadural constructor.
Ogre::MaterialPtr GetOrCreate2DMasterMaterial()
Gets or creates the 2D "template" or "master" material that is to be used to copy from for all Atlase...
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Real GetWhitePixelY() const
Gets the Y coordinate for the WhitePixel.
SpriteContainer::const_iterator ConstSpriteIterator
Const Iterator type for images stored in this class.
void ParseKernings(XML::Node &KerningsNode, FontData *FontD)
Parses the kerning section of the Texture Atlas.
SpriteContainer & GetSprites()
Gets the full listing of all the Sprite's in this TextureAtlas.
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
Ogre::MaterialPtr GetOrCreate3DMasterMaterial()
Gets or creates the 3D "template" or "master" material that is to be used to copy from for all Atlase...
~TextureAtlas()
Class destructor.
SpriteContainer::iterator SpriteIterator
Iterator type for images stored in this class.
std::stringstream StringStream
A Datatype used for streaming operations with strings.
Vector2 WhitePixel
A known position on the atlas where the pixel is flat white. Surrounding pixels should also be white...
Real VerticalOffset
The amount of pixels the glyph is to be adjusted on the Y axis.
Real GlyphAdvance
The number of pixels to advance the cursor for the next glyph.
float Real
A Datatype used to represent a real floating point number.
Real GetInvTextureCoordsY() const
Gets the inverse size of the TextureAtlas on the Y size.
FontDataContainer Fonts
A container of the fonts packed into this atlas.
SpriteContainer Sprites
A container of the images packed into this atlas.
FontDataContainer::const_iterator ConstFontDataIterator
Const Iterator type for fonts stored in this class.
Vector2 InverseTextureSize
This is (Size / 1) on the X and Y. Useful for doing math.
Real Y
Coordinate on the Y vector.
Child node iterator (a bidirectional iterator over a collection of Node)
A light-weight handle for manipulating nodes in DOM tree.
iterator begin() const
Get a Child node iterator that references the first child Node.
Real X
Coordinate on the X vector.
unsigned int AsUint(unsigned int def=0) const
Attempts to convert the value of the attribute to an unsigned int and returns the results...
uint32_t UInt32
An 32-bit unsigned integer.
This is used to represent a point on a 2 dimentional area, such as a screen.
const String & GetName() const
Gets the name of this font.
iterator end() const
Get a Child node iterator that references one past the last child Node.
TextureAtlasInternalData * TAID
A pointer to internal rendering data that cannot be exposed here.
bool Empty() const
Is this storing anything at all?
Basic class used to describe Kerning for a given Glyph.
std::map< String, FontData * > FontDataContainer
Container type for storing fonts packed into this atlas.
void ParseTexture(XML::Node &AtlasTextureNode)
Parses the texture section of the Texture Atlas.
Real AsReal(Real def=0) const
Attempts to convert the value of the attribute to a Real and returns the results. ...
void _SetBaseLine(const Real BL)
Sets the height of the largest glyph in this font.
Vector2 AtlasCoords[4]
The 4 corner coordinates on the Texture.
void SetValues(const Real &x, const Real &y)
Sets the X and Y values of this vector2.
void Create3DMaterial()
Creates the material to be used by 3D interfaces with this Atlas.
String AtlasName
The name of this atlas.
std::map< String, Sprite * > SpriteContainer
Container type for storing images packed into this atlas.
Ogre::MaterialPtr _GetOrCreate3DMaterial()
Gets the 3D Material for this TextureAtlas, creating it also if neccessary.
Thrown when the identity string wasn't valid at all.
Real GetWhitePixelX() const
Gets the X coordinate for the WhitePixel.
void _SetLineHeight(const Real LH)
Sets the height of a single line of text in this font.
FontDataContainer::iterator FontDataIterator
Iterator type for fonts stored in this class.
Sprite * GetSprite(const String &SpriteName) const
Gets a Sprite by name.
The bulk of the engine components go in this namspace.
void ParseSprites(XML::Node &AtlasSpritesNode)
Parses the sprites section of the Texture Atlas.
void ParseGlyphs(XML::Node &GlyphsNode, const Vector2 &Offset, FontData *GlyphD)
Parses the glyphs section of the Texture Atlas.
Ogre::Pass * _Get3DPass() const
Gets the 3D Material Pass for this TextureAtlas.
Vector2 GetWhitePixel() const
Gets the location of the WhitePixel on the TextureAtlas.
FontData * GetFont(const String &FontName) const
Gets the set of Glyphs of the specified name.
FontDataContainer & GetFonts()
Gets the full listing of all the FontData instances in this TextureAtlas.
void Create2DMaterial()
Creates the material to be used by 2D interfaces with this Atlas.
Ogre::Pass * _Get2DPass() const
Gets the 2D Material Pass for this TextureAtlas.
void ParseVerticalOffsets(XML::Node &VerticalOffsetsNode, FontData *FontD)
Parses the vertical offsets section of the Texture Atlas.
Basic class used to describe a portion of a texture to be applied to a Quad.
std::string String
A datatype used to a series of characters.
Ogre::MaterialPtr _GetOrCreate2DMaterial()
Gets the 2D Material for this TextureAtlas, creating it also if neccessary.
void _AddGlyph(Glyph *NewGlyph)
Adds a new glyph to this Font. exception will be thrown if a glyph with the same ID is already taken...
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
UInt32 GlyphID
The Character this glyph information represents.
Glyph * GetGlyph(const UInt32 &GlyphID) const
Gets the glyph corresponding to the provided characters UTF-8 code.
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.