lugre_ColourClipTextOverlay.cpp

Go to the documentation of this file.
00001 #include "lugre_prefix.h"
00002 #include "lugre_ColourClipTextOverlay.h"
00003 #include <OgreOverlayElementFactory.h>
00004 #include <OgreOverlayManager.h>
00005 #include <OgreMaterialManager.h>
00006 #include <OgreFont.h>
00007 #include <OgreFontManager.h>
00008 #include "lugre_ogrewrapper.h"
00009 #include "lugre_ogrefonthelper.h"
00010 
00011 
00012 using namespace Ogre;
00013 
00014 namespace Lugre {
00015 
00016 String cColourClipTextOverlay::msTypeName = "ColourClipText";
00017 
00018 // ***** ***** ***** ***** ***** Factory
00019 
00021 class ColourClipTextOverlayElementFactory: public OverlayElementFactory { public:
00023     OverlayElement* createOverlayElement(const String& instanceName) {
00024         return new cColourClipTextOverlay(instanceName);
00025     }
00027     const String& getTypeName(void) const {
00028         return cColourClipTextOverlay::msTypeName;
00029     }
00030 };
00031 
00032 void    cColourClipTextOverlay::RegisterFactory () {
00033     OverlayManager::getSingleton().addOverlayElementFactory(new ColourClipTextOverlayElementFactory());
00034 }
00035 
00036 
00037 
00038 
00039 // ***** ***** ***** ***** ***** cColourClipTextOverlay
00040 
00041 
00042 cColourClipTextOverlay::cColourClipTextOverlay(const Ogre::String& name) 
00043     : cColourClipPaneOverlay(name) {
00044     mTransparent = false;
00045     mColourTop = ColourValue::White;
00046     mColourBottom = ColourValue::White;
00047     
00048     mCharHeight = 0.02;
00049     mPixelCharHeight = 12;
00050     mSpaceWidth = 0;
00051     mPixelSpaceWidth = 0;
00052     mViewportAspectCoef = 1;
00053     mPixelWrapMaxW = 0;
00054     mWrapMaxW = 0;
00055     mAlignment = Ogre::GHA_LEFT;
00056 }
00057     
00058 cColourClipTextOverlay::~cColourClipTextOverlay() { 
00059 }
00060 
00061 void    cColourClipTextOverlay::setColour           (const Ogre::ColourValue& col) { mColourBottom = mColourTop = col;  mGeomPositionsOutOfDate = true; }
00062 void    cColourClipTextOverlay::setColourBottom     (const Ogre::ColourValue& col) { mColourBottom = col;               mGeomPositionsOutOfDate = true; }
00063 void    cColourClipTextOverlay::setColourTop        (const Ogre::ColourValue& col) { mColourTop = col;                  mGeomPositionsOutOfDate = true; }
00064 
00065 const ColourValue&  cColourClipTextOverlay::getColour       () const { return mColourBottom; }
00066 const ColourValue&  cColourClipTextOverlay::getColourBottom () const { return mColourBottom; }
00067 const ColourValue&  cColourClipTextOverlay::getColourTop    () const { return mColourTop; }
00068 
00069 void                cColourClipTextOverlay::setCaption( const Ogre::UTFString& caption )    { mCaption = caption; mGeomPositionsOutOfDate = true; }
00070 const Ogre::UTFString&  cColourClipTextOverlay::getCaption() const                  { return mCaption; }
00071 
00072 void cColourClipTextOverlay::setFontName( const String& font ) {
00073     mpFont = FontManager::getSingleton().getByName( font );
00074     if (mpFont.isNull()) OGRE_EXCEPT( Exception::ERR_ITEM_NOT_FOUND, "Could not find font " + font, "cColourClipTextOverlay::setFontName" );
00075     mpFont->load();
00076     mpMaterial = mpFont->getMaterial();
00077     mpMaterial->setDepthCheckEnabled(false);
00078     mpMaterial->setLightingEnabled(false);
00079     
00080     mGeomPositionsOutOfDate = true;
00081 }
00082 const String& cColourClipTextOverlay::getFontName() const {
00083     return mpFont->getName();
00084 }
00085 
00086 
00087 void cColourClipTextOverlay::setCharHeight( Real height ) {
00088     if (mMetricsMode != GMM_RELATIVE)
00089             mPixelCharHeight = (Ogre::ushort)height;
00090     else    mCharHeight = height;
00091     mGeomPositionsOutOfDate = true;
00092 }
00093 Real cColourClipTextOverlay::getCharHeight() const {
00094     if (mMetricsMode == GMM_PIXELS)
00095             return mPixelCharHeight;
00096     else    return mCharHeight;
00097 }
00098 
00099 void cColourClipTextOverlay::setSpaceWidth( Real width ) {
00100     if (mMetricsMode != GMM_RELATIVE)
00101             mPixelSpaceWidth = (Ogre::ushort)width;
00102     else    mSpaceWidth = width;
00103     mGeomPositionsOutOfDate = true;
00104 }
00105 Real cColourClipTextOverlay::getSpaceWidth() const {
00106     if (mMetricsMode == GMM_PIXELS)
00107             return mPixelSpaceWidth;
00108     else    return mSpaceWidth;
00109 }
00110 
00112 const String& cColourClipTextOverlay::getTypeName(void) const {
00113     return msTypeName;
00114 }
00115 
00117 void    cColourClipTextOverlay::SetAutoWrap     (Ogre::Real fMaxW) {
00118     if (mMetricsMode != GMM_RELATIVE)
00119             mPixelWrapMaxW = (Ogre::ushort)fMaxW;
00120     else    mWrapMaxW = fMaxW;
00121     mGeomPositionsOutOfDate = true;
00122 }
00123 
00124 
00125 void    cColourClipTextOverlay::UpdateVars      () {
00126     Real vpWidth = (Real) (cOgreWrapper::GetSingleton().GetViewportWidth());
00127     Real vpHeight = (Real) (cOgreWrapper::GetSingleton().GetViewportHeight());
00128     mViewportAspectCoef = vpHeight/vpWidth;
00129     if (mMetricsMode != GMM_RELATIVE) {
00130         mCharHeight = (Real) mPixelCharHeight / vpHeight;
00131         mSpaceWidth = (Real) mPixelSpaceWidth / vpWidth;
00132         mWrapMaxW = (Real) mPixelWrapMaxW / vpWidth;
00133     }
00134     
00135     // Derive space width
00136     if (mSpaceWidth == 0)
00137         mSpaceWidth = mpFont->getGlyphAspectRatio(cOgreFontHelper::UNICODE_ZERO) * mCharHeight * mViewportAspectCoef;
00138 }
00139 
00141 void cColourClipTextOverlay::updatePositionGeometry(void) {
00142     if (mpFont.isNull()) return;
00143     UpdateVars();
00144     
00145     // big vars static for efficiency
00146     static VertexRect clipped;
00147     static Ogre::Rectangle clippingRegion;
00148     
00149     // calc clipping
00150     if (mbClipInitialized) {
00151         clippingRegion = mClip;
00152         // calc clip region in screen-relative coords
00153         if (mMetricsMode != GMM_RELATIVE) {
00154             clippingRegion.left     *= mPixelScaleX;
00155             clippingRegion.right    *= mPixelScaleX;
00156             clippingRegion.top      *= mPixelScaleY;
00157             clippingRegion.bottom   *= mPixelScaleY;
00158         }
00159     }
00160     
00161     // set up variables used in loop
00162     clipped.lt.col = clipped.rt.col = mColourTop;
00163     clipped.lb.col = clipped.rb.col = mColourBottom;
00164     float left = _getDerivedLeft();
00165     float top = _getDerivedTop();
00166     cOgreFontHelper myFontHelper(mpFont,mCharHeight * mViewportAspectCoef,mCharHeight,mSpaceWidth,mWrapMaxW,cOgreFontHelper::Alignment(mAlignment));
00167     cOgreFontHelper::cTextIterator itor(myFontHelper,mCaption);
00168     Real z = -1.0;
00169     
00170     // iterate over all chars in caption
00171     Begin(mCaption.size() * 6,0,false,false,Ogre::RenderOperation::OT_TRIANGLE_LIST);
00172     while (itor.HasNext()) {
00173         cOgreFontHelper::unicode_char c = itor.Next();
00174         if (cOgreFontHelper::IsWhiteSpace(c)) {
00175             // whitespace character, skip triangles
00176             cRobRenderOp::SkipVertices(6);
00177         } else {
00178             // draw character
00179             clipped.SetLTWH(left+itor.x,top+itor.y,myFontHelper.GetCharWidth(c),mCharHeight);
00180             // TODO : if (mbClipInitialized && invis by clip) { cRobRenderOp::SkipVertices(6); continue; }
00181 
00182             Ogre::Font::UVRect uvRect = mpFont->getGlyphTexCoords( c );
00183             clipped.SetUV(uvRect.left,uvRect.top,uvRect.right,uvRect.bottom);
00184             if (mbClipInitialized) clipped = clipped.Intersect(clippingRegion);
00185             clipped.DrawList(this,z);
00186         }
00187     }
00188     End();
00189 }
00190 
00192 void    cColourClipTextOverlay::GetTextBounds   (Ogre::Real& w,Ogre::Real& h) {
00193     _update(); // get the variables right
00194     UpdateVars();
00195     // set up variables used in loop
00196     cOgreFontHelper myFontHelper(mpFont,mCharHeight * mViewportAspectCoef,mCharHeight,mSpaceWidth,mWrapMaxW,cOgreFontHelper::Alignment(mAlignment));
00197     myFontHelper.GetTextBounds(mCaption,w,h);
00198     w *= (Real) (cOgreWrapper::GetSingleton().GetViewportWidth());
00199     h *= (Real) (cOgreWrapper::GetSingleton().GetViewportHeight());
00200 }
00201 
00204 void    cColourClipTextOverlay::GetGlyphBounds  (const size_t iIndex,Ogre::Real& l,Ogre::Real& t,Ogre::Real& r,Ogre::Real& b) {
00205     _update(); // get the variables right
00206     UpdateVars();
00207     l=t=r=b=0;
00208     if (mpFont.isNull()) return;
00209     
00210     // set up variables used in loop
00211     cOgreFontHelper myFontHelper(mpFont,mCharHeight * mViewportAspectCoef,mCharHeight,mSpaceWidth,mWrapMaxW,cOgreFontHelper::Alignment(mAlignment));
00212     myFontHelper.GetGlyphBounds(mCaption,iIndex,l,t,r,b);
00213     
00214     float left = _getDerivedLeft();
00215     float top = _getDerivedTop();
00216     float vw = float(cOgreWrapper::GetSingleton().GetViewportWidth());
00217     float vh = float(cOgreWrapper::GetSingleton().GetViewportHeight());
00218     l = (l + left)*vw; r = (r + left)*vw;
00219     t = (t + top)*vh; b = (b + top)*vh;
00220 }
00221 
00227 int     cColourClipTextOverlay::GetGlyphAtPos   (const size_t x,const size_t y) {
00228         _update(); // get the variables right
00229     UpdateVars();
00230     if (mpFont.isNull()) return -1;
00231     
00232     // set up variables used in loop
00233     float left = _getDerivedLeft();
00234     float top = _getDerivedTop();
00235     float vw = float(cOgreWrapper::GetSingleton().GetViewportWidth());
00236     float vh = float(cOgreWrapper::GetSingleton().GetViewportHeight());
00237     if (vw == 0) vw = 1; // avoid division by zero
00238     if (vh == 0) vh = 1; // avoid division by zero
00239     cOgreFontHelper myFontHelper(mpFont,mCharHeight * mViewportAspectCoef,mCharHeight,mSpaceWidth,mWrapMaxW,cOgreFontHelper::Alignment(mAlignment));
00240     return myFontHelper.GetGlyphAtPos(mCaption,x/vw-left,y/vh-top);
00241 }
00242 
00243 };

Generated on Fri Sep 10 06:00:08 2010 for cpp by  doxygen 1.5.6