diff --git a/include/eepp/graphics/console.hpp b/include/eepp/graphics/console.hpp index 076cd3d22..6bc3b254f 100755 --- a/include/eepp/graphics/console.hpp +++ b/include/eepp/graphics/console.hpp @@ -165,7 +165,7 @@ class EE_API Console : protected LogReaderInterface { sCon mCon; Float mCurAlpha; - + TextCache mTextCache; bool mEnabled; bool mVisible; bool mFadeIn; diff --git a/include/eepp/graphics/font.hpp b/include/eepp/graphics/font.hpp index 77160649e..d510ec313 100755 --- a/include/eepp/graphics/font.hpp +++ b/include/eepp/graphics/font.hpp @@ -13,35 +13,6 @@ class EE_API Font { public: virtual ~Font(); - /** Set a text to render - * @param Text The Text - */ - void setText( const String& Text ); - - /** @return The width of the string rendered */ - Float getTextWidth(); - - /** @return Assign a new text and then returns his width */ - Float getTextWidth( const String& Text ); - - /** @return The current text height */ - Float getTextHeight(); - - /** @return The number of lines of the current text */ - virtual int getNumLines(); - - /** @return The Font Color */ - const ColorA& getColor() const; - - /** Set the color of the string rendered */ - void setColor(const ColorA& color); - - /** @return The Shadow Font Color */ - const ColorA& getShadowColor() const; - - /** Set the shadow color of the string rendered */ - void setShadowColor(const ColorA& color); - /** @return The current font size */ Uint32 getFontSize() const; @@ -57,44 +28,6 @@ class EE_API Font { /** @return The font lowest descent (height below base) */ Int32 getFontDescent() const; - /** @return The current text */ - String getText(); - - /** @return The last text rendered or setted lines width */ - const std::vector& getLinesWidth(); - - /** Draw a String on the screen - * @param Text The text to draw - * @param X The start x position - * @param Y The start y position - * @param Flags Set some flags to the rendering ( for text align ) - * @param Scale The string rendered scale - * @param Angle The angle of the string rendered - * @param Effect Set the Blend Mode ( default ALPHA_NORMAL ) - */ - void draw( const String& Text, const Float& X, const Float& Y, const Uint32& Flags = FONT_DRAW_LEFT, const Vector2f& Scale = Vector2f::One, const Float& Angle = 0, const EE_BLEND_MODE& Effect = ALPHA_NORMAL ); - - /** Draw the string seted on the screen - * @param X The start x position - * @param Y The start y position - * @param Flags Set some flags to the rendering ( for text align ) - * @param Scale The string rendered scale - * @param Angle The angle of the string rendered - * @param Effect Set the Blend Mode ( default ALPHA_NORMAL ) - */ - void draw( const Float& X, const Float& Y, const Uint32& Flags = FONT_DRAW_LEFT, const Vector2f& Scale = Vector2f::One, const Float& Angle = 0, const EE_BLEND_MODE& Effect = ALPHA_NORMAL ); - - /** Draw a string on the screen from a cached text - * @param TextCache The cached text - * @param X The start x position - * @param Y The start y position - * @param Flags Set some flags to the rendering ( for text align ) - * @param Scale The string rendered scale - * @param Angle The angle of the string rendered - * @param Effect Set the Blend Mode ( default ALPHA_NORMAL ) - */ - void draw( TextCache& TextCache, const Float& X, const Float& Y, const Uint32& Flags = FONT_DRAW_LEFT, const Vector2f& Scale = Vector2f::One, const Float& Angle = 0, const EE_BLEND_MODE& Effect = ALPHA_NORMAL ); - /** Shrink the String to a max width * @param Str The string to shrink * @param MaxWidth The Max Width posible @@ -126,13 +59,19 @@ class EE_API Font { const Uint32& getId(); /** Finds the closest cursor position to the point position */ - Int32 findClosestCursorPosFromPoint( const String & Text, const Vector2i& pos ); + Int32 findClosestCursorPosFromPoint( const String& Text, const Vector2i& pos ); /** Simulates a selection request and return the initial and end cursor position when the selection worked. Otherwise both parameters will be -1. */ void selectSubStringFromCursor( const String& Text, const Int32& CurPos, Int32& InitCur, Int32& EndCur ); /** @return The cursor position inside the string */ Vector2i getCursorPos( const String& Text, const Uint32& Pos ); + + const eeGlyph& getGlyph( const Uint32& index ); + + const eeTexCoords& getTexCoords( const Uint32& index ); + + Uint32 getGlyphCount() const; protected: Uint32 mType; std::string mFontName; @@ -150,8 +89,6 @@ class EE_API Font { TextCache mTextCache; Font( const Uint32& Type, const std::string& setName ); - - void cacheWidth(); }; }} diff --git a/include/eepp/graphics/textcache.hpp b/include/eepp/graphics/textcache.hpp index 5a3e90538..eb1ce923b 100644 --- a/include/eepp/graphics/textcache.hpp +++ b/include/eepp/graphics/textcache.hpp @@ -107,6 +107,10 @@ class EE_API TextCache { std::vector mRenderCoords; std::vector mColors; + void internalDraw( const Float& X, const Float& Y, const Vector2f& Scale, const Float& Angle, EE_BLEND_MODE Effect ); + + void cacheVerts(const Int32 & X, const Int32 & Y); + void updateCoords(); const bool& cachedCoords() const; diff --git a/include/eepp/ui/uithemeconfig.hpp b/include/eepp/ui/uithemeconfig.hpp index d34069494..12a0064e2 100644 --- a/include/eepp/ui/uithemeconfig.hpp +++ b/include/eepp/ui/uithemeconfig.hpp @@ -81,6 +81,7 @@ class FontStyleConfig { } Graphics::Font * Font; + Uint32 FontCharacterSize; ColorA FontColor; ColorA FontShadowColor; ColorA FontOverColor; diff --git a/src/eepp/graphics/console.cpp b/src/eepp/graphics/console.cpp index bd5e7e8aa..2743bbcf9 100755 --- a/src/eepp/graphics/console.cpp +++ b/src/eepp/graphics/console.cpp @@ -106,6 +106,8 @@ void Console::create( Font* Font, const bool& MakeDefaultCommands, const bool& A mFont = Font; + mTextCache.setFont( mFont ); + mFontSize = (Float)( mFont->getFontSize() * 1.25 ); if ( mFont->getFontHeight() < mFontSize && ( mFont->getFontHeight() != mFont->getFontSize() || mFont->getLineSkip() != (Int32)mFont->getFontHeight() ) ) @@ -155,7 +157,7 @@ void Console::addCommand( const String& Command, ConsoleCallback CB ) { void Console::draw() { if ( mEnabled && NULL != mFont ) { - ColorA OldColor( mFont->getColor() ); + ColorA OldColor( mTextCache.getColor() ); fade(); @@ -188,13 +190,14 @@ void Console::draw() { mCon.ConMin = mEx; mCon.ConMax = (int)mCmdLog.size() - 1; - mFont->setColor( ColorA ( mFontColor.r(), mFontColor.g(), mFontColor.b(), static_cast(mA) ) ); + mTextCache.setColor( ColorA ( mFontColor.r(), mFontColor.g(), mFontColor.b(), static_cast(mA) ) ); for (int i = mCon.ConMax - mCon.ConModif; i >= mCon.ConMin - mCon.ConModif; i-- ) { if ( i < static_cast( mCmdLog.size() ) && i >= 0 ) { CurY = mTempY + mY + mCurHeight - Pos * mFontSize - mFontSize * 2; - mFont->draw( mCmdLog[i], mFontSize, CurY ); + mTextCache.setText( mCmdLog[i] ); + mTextCache.draw( mFontSize, CurY ); Pos++; } @@ -202,29 +205,33 @@ void Console::draw() { CurY = mTempY + mY + mCurHeight - mFontSize - 1; - mFont->setColor( ColorA ( mFontLineColor.r(), mFontLineColor.g(), mFontLineColor.b(), static_cast(mA) ) ); - mFont->setText( "> " + mTBuf->getBuffer() ); - mFont->draw( mFontSize, CurY ); + mTextCache.setColor( ColorA ( mFontLineColor.r(), mFontLineColor.g(), mFontLineColor.b(), static_cast(mA) ) ); + mTextCache.setText( "> " + mTBuf->getBuffer() ); + mTextCache.draw( mFontSize, CurY ); - mFont->setColor( ColorA ( mFontLineColor.r(), mFontLineColor.g(), mFontLineColor.b(), static_cast(mCurAlpha) ) ); + mTextCache.setColor( ColorA ( mFontLineColor.r(), mFontLineColor.g(), mFontLineColor.b(), static_cast(mCurAlpha) ) ); if ( (unsigned int)mTBuf->getCursorPos() == mTBuf->getBuffer().size() ) { - mFont->draw( "_", mFontSize + mFont->getTextWidth() , CurY ); + Uint32 width = mTextCache.getTextWidth(); + mTextCache.setText( "_" ); + mTextCache.draw( mFontSize + width, CurY ); } else { - mFont->setText( "> " + mTBuf->getBuffer().substr( 0, mTBuf->getCursorPos() ) ); - mFont->draw( "_", mFontSize + mFont->getTextWidth() , CurY ); + mTextCache.setText( "> " + mTBuf->getBuffer().substr( 0, mTBuf->getCursorPos() ) ); + Uint32 width = mFontSize + mTextCache.getTextWidth(); + mTextCache.setText( "_" ); + mTextCache.draw( width, CurY ); } - mFont->setColor( OldColor ); + mTextCache.setColor( OldColor ); } } if ( mShowFps && NULL != mFont ) { - ColorA OldColor1( mFont->getColor() ); - mFont->setColor( ColorA () ); - mFont->setText( "FPS: " + String::toStr( mWindow->getFPS() ) ); - mFont->draw( mWindow->getWidth() - mFont->getTextWidth() - 15, 6 ); - mFont->setColor( OldColor1 ); + ColorA OldColor1( mTextCache.getColor() ); + mTextCache.setColor( ColorA () ); + mTextCache.setText( "FPS: " + String::toStr( mWindow->getFPS() ) ); + mTextCache.draw( mWindow->getWidth() - mTextCache.getTextWidth() - 15, 6 ); + mTextCache.setColor( OldColor1 ); } } diff --git a/src/eepp/graphics/font.cpp b/src/eepp/graphics/font.cpp index b0563f868..33e29333f 100644 --- a/src/eepp/graphics/font.cpp +++ b/src/eepp/graphics/font.cpp @@ -1,8 +1,6 @@ #include #include #include -#include -#include #include namespace EE { namespace Graphics { @@ -14,8 +12,7 @@ Font::Font( const Uint32& Type, const std::string& Name ) : mSize(0), mLineSkip(0), mAscent(0), - mDescent(0), - mTextCache( this ) + mDescent(0) { this->setName( Name ); FontManager::instance()->add( this ); @@ -29,39 +26,6 @@ Font::~Font() { } } -void Font::setText( const String& Text ) { - mTextCache.setText( Text ); -} - -const ColorA& Font::getColor() const { - return mTextCache.getColor(); -} - -void Font::setColor(const ColorA& color) { - mTextCache.setColor( color ); -} - -const ColorA& Font::getShadowColor() const { - return mTextCache.getShadowColor(); -} - -void Font::setShadowColor(const ColorA& color) { - mTextCache.setShadowColor( color ); -} - -int Font::getNumLines() { - return mTextCache.getNumLines(); -} - -Float Font::getTextWidth( const String& Text ) { - setText( Text ); - return mTextCache.getTextWidth(); -} - -Float Font::getTextWidth() { - return mTextCache.getTextWidth(); -} - Uint32 Font::getFontSize() const { return mSize; } @@ -82,233 +46,6 @@ Int32 Font::getFontDescent() const { return mDescent; } -String Font::getText() { - return mTextCache.getText(); -} - -Float Font::getTextHeight() { - return (Float)getFontHeight() * (Float)getNumLines(); -} - -const std::vector& Font::getLinesWidth() { - return mTextCache.getLinesWidth(); -} - -void Font::draw( const Float& X, const Float& Y, const Uint32& Flags, const Vector2f& Scale, const Float& Angle, const EE_BLEND_MODE& Effect) { - draw( mTextCache, X, Y, Flags, Scale, Angle, Effect ); -} - -void Font::draw( const String& Text, const Float& X, const Float& Y, const Uint32& Flags, const Vector2f& Scale, const Float& Angle, const EE_BLEND_MODE& Effect ) { - mTextCache.setText( Text ); - mTextCache.setFlags( Flags ); - mTextCache.draw( X, Y, Scale, Angle, Effect ); -} - -void Font::draw( TextCache& TextCache, const Float& X, const Float& Y, const Uint32& Flags, const Vector2f& Scale, const Float& Angle, const EE_BLEND_MODE& Effect ) { - if ( !TextCache.getText().size() ) - return; - - GlobalBatchRenderer::instance()->draw(); - TextureFactory::instance()->bind( mTexId ); - BlendMode::setMode( Effect ); - - if ( Flags & FONT_DRAW_SHADOW ) { - Uint32 f = Flags; - - f &= ~FONT_DRAW_SHADOW; - - ColorA Col = TextCache.getColor(); - - setText( TextCache.getText() ); - - if ( Col.a() != 255 ) { - ColorA ShadowColor = TextCache.getShadowColor(); - - ShadowColor.Alpha = (Uint8)( (Float)ShadowColor.Alpha * ( (Float)Col.a() / (Float)255 ) ); - - setColor( ShadowColor ); - } else { - setColor( TextCache.getShadowColor() ); - } - - Float pd = PixelDensity::getPixelDensity(); - - draw( X + 1 * pd, Y + 1 * pd, f, Scale, Angle, Effect ); - - mTextCache.setFlags( Flags ); - - setColor( Col ); - } - - Float cX = (Float) ( (Int32)X ); - Float cY = (Float) ( (Int32)Y ); - Float nX = 0; - Float nY = 0; - Int16 Char = 0; - unsigned int Line = 0; - unsigned int numvert = 0; - - if ( Angle != 0.0f || Scale != 1.0f ) { - GLi->pushMatrix(); - - Vector2f Center( cX + TextCache.getTextWidth() * 0.5f, cY + TextCache.getTextHeight() * 0.5f ); - GLi->translatef( Center.x , Center.y, 0.f ); - GLi->rotatef( Angle, 0.0f, 0.0f, 1.0f ); - GLi->scalef( Scale.x, Scale.y, 1.0f ); - GLi->translatef( -Center.x + X, -Center.y + Y, 0.f ); - } - - std::vector& RenderCoords = TextCache.getVertextCoords(); - std::vector& Colors = TextCache.getColors(); - - if ( !TextCache.cachedCoords() ) { - if ( !( Flags & FONT_DRAW_VERTICAL ) ) { - switch ( fontHAlignGet( Flags ) ) { - case FONT_DRAW_CENTER: - nX = (Float)( (Int32)( ( TextCache.getTextWidth() - TextCache.getLinesWidth()[ Line ] ) * 0.5f ) ); - Line++; - break; - case FONT_DRAW_RIGHT: - nX = TextCache.getTextWidth() - TextCache.getLinesWidth()[ Line ]; - Line++; - break; - } - } - - Int32 tGlyphSize = (Int32)mGlyphs.size(); - - for ( unsigned int i = 0; i < TextCache.getText().size(); i++ ) { - Char = static_cast( TextCache.getText().at(i) ); - - if ( Char < 0 && Char > -128 ) - Char = 256 + Char; - - if ( Char >= 0 && Char < tGlyphSize ) { - eeTexCoords* C = &mTexCoords[ Char ]; - - switch( Char ) { - case '\v': - { - if ( Flags & FONT_DRAW_VERTICAL ) - nY += getFontHeight(); - else - nX += mGlyphs[ Char ].Advance; - break; - } - case '\t': - { - if ( Flags & FONT_DRAW_VERTICAL ) - nY += getFontHeight() * 4; - else - nX += mGlyphs[ Char ].Advance * 4; - break; - } - case '\n': - { - if ( Flags & FONT_DRAW_VERTICAL ) { - nX += (getFontHeight() * Scale.y); - nY = 0; - } else { - if ( i + 1 < TextCache.getText().size() ) { - switch ( fontHAlignGet( Flags ) ) { - case FONT_DRAW_CENTER: - nX = (Float)( (Int32)( ( TextCache.getTextWidth() - TextCache.getLinesWidth()[ Line ] ) * 0.5f ) ); - break; - case FONT_DRAW_RIGHT: - nX = TextCache.getTextWidth() - TextCache.getLinesWidth()[ Line ]; - break; - default: - nX = 0; - } - } - - nY += (getFontHeight() * Scale.y); - Line++; - } - - break; - } - default: - { - if ( GLi->quadsSupported() ) { - for ( Uint8 z = 0; z < 8; z+=2 ) { - RenderCoords[ numvert ].TexCoords[0] = C->TexCoords[z]; - RenderCoords[ numvert ].TexCoords[1] = C->TexCoords[ z + 1 ]; - RenderCoords[ numvert ].Vertex[0] = cX + C->Vertex[z] + nX; - RenderCoords[ numvert ].Vertex[1] = cY + C->Vertex[ z + 1 ] + nY; - numvert++; - } - } else { - RenderCoords[ numvert ].TexCoords[0] = C->TexCoords[2]; - RenderCoords[ numvert ].TexCoords[1] = C->TexCoords[ 2 + 1 ]; - RenderCoords[ numvert ].Vertex[0] = cX + C->Vertex[2] + nX; - RenderCoords[ numvert ].Vertex[1] = cY + C->Vertex[ 2 + 1 ] + nY; - numvert++; - - RenderCoords[ numvert ].TexCoords[0] = C->TexCoords[0]; - RenderCoords[ numvert ].TexCoords[1] = C->TexCoords[ 0 + 1 ]; - RenderCoords[ numvert ].Vertex[0] = cX + C->Vertex[0] + nX; - RenderCoords[ numvert ].Vertex[1] = cY + C->Vertex[ 0 + 1 ] + nY; - numvert++; - - RenderCoords[ numvert ].TexCoords[0] = C->TexCoords[6]; - RenderCoords[ numvert ].TexCoords[1] = C->TexCoords[ 6 + 1 ]; - RenderCoords[ numvert ].Vertex[0] = cX + C->Vertex[6] + nX; - RenderCoords[ numvert ].Vertex[1] = cY + C->Vertex[ 6 + 1 ] + nY; - numvert++; - - RenderCoords[ numvert ].TexCoords[0] = C->TexCoords[2]; - RenderCoords[ numvert ].TexCoords[1] = C->TexCoords[ 2 + 1 ]; - RenderCoords[ numvert ].Vertex[0] = cX + C->Vertex[2] + nX; - RenderCoords[ numvert ].Vertex[1] = cY + C->Vertex[ 2 + 1 ] + nY; - numvert++; - - RenderCoords[ numvert ].TexCoords[0] = C->TexCoords[4]; - RenderCoords[ numvert ].TexCoords[1] = C->TexCoords[ 4 + 1 ]; - RenderCoords[ numvert ].Vertex[0] = cX + C->Vertex[4] + nX; - RenderCoords[ numvert ].Vertex[1] = cY + C->Vertex[ 4 + 1 ] + nY; - numvert++; - - RenderCoords[ numvert ].TexCoords[0] = C->TexCoords[6]; - RenderCoords[ numvert ].TexCoords[1] = C->TexCoords[ 6 + 1 ]; - RenderCoords[ numvert ].Vertex[0] = cX + C->Vertex[6] + nX; - RenderCoords[ numvert ].Vertex[1] = cY + C->Vertex[ 6 + 1 ] + nY; - numvert++; - } - - if ( Flags & FONT_DRAW_VERTICAL ) - nY += getFontHeight(); - else - nX += mGlyphs[ Char ].Advance; - } - } - } - } - - TextCache.cachedCoords( true ); - TextCache.cachedVerts( numvert ); - } else { - numvert = TextCache.cachedVerts(); - } - - Uint32 alloc = numvert * sizeof(eeVertexCoords); - Uint32 allocC = numvert * GLi->quadVertexs(); - - GLi->colorPointer ( 4, GL_UNSIGNED_BYTE , 0 , reinterpret_cast( &Colors[0] ) , allocC ); - GLi->texCoordPointer( 2, GL_FP , sizeof(eeVertexCoords), reinterpret_cast( &RenderCoords[0] ) , alloc ); - GLi->vertexPointer ( 2, GL_FP , sizeof(eeVertexCoords), reinterpret_cast( &RenderCoords[0] ) + sizeof(Float) * 2 , alloc ); - - if ( GLi->quadsSupported() ) { - GLi->drawArrays( GL_QUADS, 0, numvert ); - } else { - GLi->drawArrays( GL_TRIANGLES, 0, numvert ); - } - - if ( Angle != 0.0f || Scale != 1.0f ) { - GLi->popMatrix(); - } -} - void Font::cacheWidth( const String& Text, std::vector& LinesWidth, Float& CachedWidth, int& NumLines , int& LargestLineCharCount ) { LinesWidth.clear(); @@ -440,6 +177,18 @@ Vector2i Font::getCursorPos( const String& Text, const Uint32& Pos ) { return Vector2i( Width, Height ); } +const eeGlyph& Font::getGlyph(const Uint32 & index) { + return mGlyphs[ index ]; +} + +const eeTexCoords& Font::getTexCoords(const Uint32 & index) { + return mTexCoords[ index ]; +} + +Uint32 Font::getGlyphCount() const { + return mGlyphs.size(); +} + static bool isStopSelChar( Uint32 c ) { return ( !String::isCharacter( c ) && !String::isNumber( c ) ) || ' ' == c || @@ -479,10 +228,6 @@ void Font::selectSubStringFromCursor( const String& Text, const Int32& CurPos, I } } -void Font::cacheWidth() { - mTextCache.cacheWidth(); -} - void Font::shrinkText( std::string& Str, const Uint32& MaxWidth ) { if ( !Str.size() ) return; diff --git a/src/eepp/graphics/textcache.cpp b/src/eepp/graphics/textcache.cpp index b67e46941..858741b17 100644 --- a/src/eepp/graphics/textcache.cpp +++ b/src/eepp/graphics/textcache.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace EE { namespace Graphics { @@ -170,6 +171,220 @@ void TextCache::cacheWidth() { mCachedCoords = false; } +void TextCache::internalDraw( const Float& X, const Float& Y, const Vector2f & Scale, const Float & Angle, EE_BLEND_MODE Effect ) { + GlobalBatchRenderer::instance()->draw(); + TextureFactory::instance()->bind( mFont->getTexId() ); + BlendMode::setMode( Effect ); + + if ( mFlags & FONT_DRAW_SHADOW ) { + Uint32 f = mFlags; + + mFlags &= ~FONT_DRAW_SHADOW; + + ColorA Col = getColor(); + + setText( getText() ); + + if ( Col.a() != 255 ) { + ColorA ShadowColor = getShadowColor(); + + ShadowColor.Alpha = (Uint8)( (Float)ShadowColor.Alpha * ( (Float)Col.a() / (Float)255 ) ); + + setColor( ShadowColor ); + } else { + setColor( getShadowColor() ); + } + + Float pd = PixelDensity::getPixelDensity(); + + internalDraw( X + 1 * pd, Y + 1 * pd, Scale, Angle, Effect ); + + mFlags = f; + + setColor( Col ); + } + + Float cX = (Float) ( (Int32)X ); + Float cY = (Float) ( (Int32)Y ); + unsigned int numvert = 0; + + if ( Angle != 0.0f || Scale != 1.0f ) { + GLi->pushMatrix(); + + Vector2f Center( cX + getTextWidth() * 0.5f, cY + getTextHeight() * 0.5f ); + GLi->translatef( Center.x , Center.y, 0.f ); + GLi->rotatef( Angle, 0.0f, 0.0f, 1.0f ); + GLi->scalef( Scale.x, Scale.y, 1.0f ); + GLi->translatef( -Center.x + X, -Center.y + Y, 0.f ); + } + + std::vector& RenderCoords = getVertextCoords(); + std::vector& Colors = getColors(); + + if ( !cachedCoords() ) { + cacheVerts( X, Y ); + } + + numvert = cachedVerts(); + + Uint32 alloc = numvert * sizeof(eeVertexCoords); + Uint32 allocC = numvert * GLi->quadVertexs(); + + GLi->colorPointer ( 4, GL_UNSIGNED_BYTE , 0 , reinterpret_cast( &Colors[0] ) , allocC ); + GLi->texCoordPointer( 2, GL_FP , sizeof(eeVertexCoords), reinterpret_cast( &RenderCoords[0] ) , alloc ); + GLi->vertexPointer ( 2, GL_FP , sizeof(eeVertexCoords), reinterpret_cast( &RenderCoords[0] ) + sizeof(Float) * 2 , alloc ); + + if ( GLi->quadsSupported() ) { + GLi->drawArrays( GL_QUADS, 0, numvert ); + } else { + GLi->drawArrays( GL_TRIANGLES, 0, numvert ); + } + + if ( Angle != 0.0f || Scale != 1.0f ) { + GLi->popMatrix(); + } +} + +void TextCache::cacheVerts( const Int32& X, const Int32& Y ) { + if ( cachedCoords() ) + return; + + Float nX = 0; + Float nY = 0; + Uint32 Char = 0; + unsigned int Line = 0; + + if ( !( mFlags & FONT_DRAW_VERTICAL ) ) { + switch ( fontHAlignGet( mFlags ) ) { + case FONT_DRAW_CENTER: + nX = (Float)( (Int32)( ( getTextWidth() - getLinesWidth()[ Line ] ) * 0.5f ) ); + Line++; + break; + case FONT_DRAW_RIGHT: + nX = getTextWidth() - getLinesWidth()[ Line ]; + Line++; + break; + } + } + + Uint32 tGlyphSize = mFont->getGlyphCount(); + Float cX = (Float) ( (Int32)X ); + Float cY = (Float) ( (Int32)Y ); + unsigned int numvert = 0; + + for ( unsigned int i = 0; i < getText().size(); i++ ) { + Char = getText().at(i); + + if ( Char < 0 && Char > -128 ) + Char = 256 + Char; + + if ( Char >= 0 && Char < tGlyphSize ) { + eeTexCoords C = mFont->getTexCoords( Char ); + eeGlyph Glyph = mFont->getGlyph( Char ); + + switch( Char ) { + case '\v': + { + if ( mFlags & FONT_DRAW_VERTICAL ) + nY += mFont->getFontHeight(); + else + nX += Glyph.Advance; + break; + } + case '\t': + { + if ( mFlags & FONT_DRAW_VERTICAL ) + nY += mFont->getFontHeight() * 4; + else + nX += Glyph.Advance * 4; + break; + } + case '\n': + { + if ( mFlags & FONT_DRAW_VERTICAL ) { + nX += (mFont->getFontHeight()); + nY = 0; + } else { + if ( i + 1 < getText().size() ) { + switch ( fontHAlignGet( mFlags ) ) { + case FONT_DRAW_CENTER: + nX = (Float)( (Int32)( ( getTextWidth() - getLinesWidth()[ Line ] ) * 0.5f ) ); + break; + case FONT_DRAW_RIGHT: + nX = getTextWidth() - getLinesWidth()[ Line ]; + break; + default: + nX = 0; + } + } + + nY += (mFont->getFontHeight()); + Line++; + } + + break; + } + default: + { + if ( GLi->quadsSupported() ) { + for ( Uint8 z = 0; z < 8; z+=2 ) { + mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[z]; + mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ z + 1 ]; + mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[z] + nX; + mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ z + 1 ] + nY; + numvert++; + } + } else { + mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[2]; + mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 2 + 1 ]; + mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[2] + nX; + mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 2 + 1 ] + nY; + numvert++; + + mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[0]; + mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 0 + 1 ]; + mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[0] + nX; + mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 0 + 1 ] + nY; + numvert++; + + mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[6]; + mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 6 + 1 ]; + mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[6] + nX; + mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 6 + 1 ] + nY; + numvert++; + + mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[2]; + mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 2 + 1 ]; + mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[2] + nX; + mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 2 + 1 ] + nY; + numvert++; + + mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[4]; + mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 4 + 1 ]; + mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[4] + nX; + mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 4 + 1 ] + nY; + numvert++; + + mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[6]; + mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 6 + 1 ]; + mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[6] + nX; + mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 6 + 1 ] + nY; + numvert++; + } + + if ( mFlags & FONT_DRAW_VERTICAL ) + nY += mFont->getFontHeight(); + else + nX += Glyph.Advance; + } + } + } + } + + cachedCoords( true ); + cachedVerts( numvert ); +} + Float TextCache::getTextWidth() { return ( mFlags & FONT_DRAW_VERTICAL ) ? (Float)mFont->getFontHeight() * (Float)mNumLines : mCachedWidth; } @@ -191,11 +406,11 @@ void TextCache::draw( const Float& X, const Float& Y, const Vector2f& Scale, con GlobalBatchRenderer::instance()->draw(); if ( Angle != 0.0f || Scale != 1.0f ) { - mFont->draw( *this, X, Y, mFlags, Scale, Angle, Effect ); + internalDraw( X, Y, Scale, Angle, Effect ); } else { GLi->translatef( X, Y, 0.f ); - mFont->draw( *this, 0, 0, mFlags, Scale, Angle, Effect ); + internalDraw( 0, 0, Scale, Angle, Effect ); GLi->translatef( -X, -Y, 0.f ); } diff --git a/src/eepp/ui/uilistbox.cpp b/src/eepp/ui/uilistbox.cpp index 4edd47250..5f2d9edc7 100644 --- a/src/eepp/ui/uilistbox.cpp +++ b/src/eepp/ui/uilistbox.cpp @@ -143,7 +143,9 @@ Uint32 UIListBox::addListBoxItem( const String& Text ) { mItems.push_back( NULL ); if ( NULL != mFontStyleConfig.Font ) { - Uint32 twidth = mFontStyleConfig.Font->getTextWidth( Text ); + TextCache textCache( mFontStyleConfig.Font ); + textCache.setText( Text ); + Uint32 twidth = textCache.getTextWidth(); if ( twidth > mMaxTextWidth ) { mMaxTextWidth = twidth; @@ -329,14 +331,17 @@ void UIListBox::setHScrollStep() { void UIListBox::findMaxWidth() { Uint32 size = (Uint32)mItems.size(); Int32 width; + TextCache textCache( mFontStyleConfig.Font ); mMaxTextWidth = 0; for ( Uint32 i = 0; i < size; i++ ) { - if ( NULL != mItems[i] ) + if ( NULL != mItems[i] ) { width = (Int32)mItems[i]->getTextWidth(); - else - width = mFontStyleConfig.Font->getTextWidth( mTexts[i] ); + } else { + textCache.setText( mTexts[i] ); + width = textCache.getTextWidth(); + } if ( width > (Int32)mMaxTextWidth ) mMaxTextWidth = width; diff --git a/src/eepp/ui/uitextedit.cpp b/src/eepp/ui/uitextedit.cpp index c69d7d928..1d522f9f5 100644 --- a/src/eepp/ui/uitextedit.cpp +++ b/src/eepp/ui/uitextedit.cpp @@ -345,7 +345,8 @@ void UITextEdit::fixScrollToCursor() { Uint32 NLPos = 0; Uint32 LineNum = mTextInput->getInputTextBuffer()->getCurPosLinePos( NLPos ); - mTextInput->getTextCache()->getFont()->setText( + TextCache textCache( mTextInput->getTextCache()->getFont() ); + textCache.setText( mTextInput->getInputTextBuffer()->getBuffer().substr( NLPos, mTextInput->getInputTextBuffer()->getCursorPos() - NLPos ) @@ -353,8 +354,8 @@ void UITextEdit::fixScrollToCursor() { mSkipValueChange = true; - Float tW = mTextInput->getTextCache()->getFont()->getTextWidth(); - Float tH = (Float)(LineNum + 1) * (Float)mTextInput->getTextCache()->getFont()->getFontHeight(); + Float tW = textCache.getTextWidth(); + Float tH = (Float)(LineNum + 1) * (Float)textCache.getFont()->getFontHeight(); if ( tW > Width ) { mTextInput->setPixelsPosition( mContainerPadding.Left + Width - tW, mTextInput->getRealPosition().y ); diff --git a/src/eepp/ui/uitextinput.cpp b/src/eepp/ui/uitextinput.cpp index 5b3ea8a83..75ffff540 100644 --- a/src/eepp/ui/uitextinput.cpp +++ b/src/eepp/ui/uitextinput.cpp @@ -159,9 +159,11 @@ void UITextInput::alignFix() { Uint32 NLPos = 0; Uint32 LineNum = mTextBuffer.getCurPosLinePos( NLPos ); - mTextCache->getFont()->setText( mTextBuffer.getBuffer().substr( NLPos, mTextBuffer.getCursorPos() - NLPos ) ); + TextCache textCache( mTextCache->getFont() ); - Float tW = mTextCache->getFont()->getTextWidth(); + textCache.setText( mTextBuffer.getBuffer().substr( NLPos, mTextBuffer.getCursorPos() - NLPos ) ); + + Float tW = textCache.getTextWidth(); Float tX = mRealAlignOffset.x + tW; mCurPos.x = tW; diff --git a/src/eepp/ui/uitextinputpassword.cpp b/src/eepp/ui/uitextinputpassword.cpp index 35fa67bce..b039d4f00 100644 --- a/src/eepp/ui/uitextinputpassword.cpp +++ b/src/eepp/ui/uitextinputpassword.cpp @@ -63,9 +63,9 @@ void UITextInputPassword::alignFix() { for ( size_t i = 0; i < curStr.size(); i++ ) pasStr += '*'; - mPassCache->getFont()->setText( pasStr ); + mPassCache->setText( pasStr ); - Float tW = mPassCache->getFont()->getTextWidth(); + Float tW = mPassCache->getTextWidth(); Float tX = mRealAlignOffset.x + tW; mCurPos.x = tW; diff --git a/src/eepp/ui/uithemedefault.cpp b/src/eepp/ui/uithemedefault.cpp index 649b9d861..03a13702b 100644 --- a/src/eepp/ui/uithemedefault.cpp +++ b/src/eepp/ui/uithemedefault.cpp @@ -12,6 +12,7 @@ namespace EE { namespace UI { UIThemeDefault::UIThemeDefault( const std::string& name, const std::string& Abbr, Graphics::Font * defaultFont ) : UITheme( name, Abbr, defaultFont ) { + mFontStyleConfig.FontCharacterSize = 12; mFontStyleConfig.FontColor = ColorA( 230, 230, 230, 255 ); mFontStyleConfig.FontOverColor = mFontStyleConfig.FontSelectedColor = ColorA( 255, 255, 255, 255 ); mFontStyleConfig.FontShadowColor = ColorA( 50, 50, 50, 150 ); diff --git a/src/examples/fonts/fonts.cpp b/src/examples/fonts/fonts.cpp index d10d970b2..32179da5e 100644 --- a/src/examples/fonts/fonts.cpp +++ b/src/examples/fonts/fonts.cpp @@ -6,6 +6,11 @@ TTFFont * TTFO = NULL; TTFFont * TTF2 = NULL; TextureFont * TexF = NULL; TextureFont * TexF2 = NULL; +TextCache TTFCache; +TextCache TTF2Cache; +TextCache TTFOCache; +TextCache TexFCache; +TextCache TexF2Cache; TextCache TxtCache; void mainLoop() @@ -25,21 +30,21 @@ void mainLoop() Float YPos = 32; // Draw the text on screen - TTF->draw( win->getWidth() * 0.5f - TTF->getTextWidth() * 0.5f, YPos ); + TTFCache.draw( win->getWidth() * 0.5f - TTFCache.getTextWidth() * 0.5f, YPos ); - TTFO->draw( win->getWidth() * 0.5f - TTFO->getTextWidth() * 0.5f, ( YPos += TTF->getTextHeight() + 24 ) ); + TTFOCache.draw( ( win->getWidth() - TTFOCache.getTextWidth() ) * 0.5f, ( YPos += TTFCache.getTextHeight() + 24 ) ); - TTF2->draw( win->getWidth() * 0.5f - TTF2->getTextWidth() * 0.5f, ( YPos += TTF->getTextHeight() + 24 ) ); + TTF2Cache.draw( ( win->getWidth() - TTF2Cache.getTextWidth() ) * 0.5f, ( YPos += TTFOCache.getTextHeight() + 24 ) ); - TexF->draw( win->getWidth() * 0.5f - TexF->getTextWidth() * 0.5f, ( YPos += TTF2->getTextHeight() + 24 ) ); + TexFCache.draw( ( win->getWidth() - TexFCache.getTextWidth() ) * 0.5f, ( YPos += TTF2Cache.getTextHeight() + 24 ) ); - TexF2->draw( win->getWidth() * 0.5f - TexF2->getTextWidth() * 0.5f, ( YPos += TexF->getTextHeight() + 24 ) ); + TexF2Cache.draw( ( win->getWidth() - TexF2Cache.getTextWidth() ) * 0.5f, ( YPos += TexFCache.getTextHeight() + 24 ) ); // Draw the cached text - TxtCache.draw( 48, ( YPos += TexF2->getTextHeight() + 24 ) ); + TxtCache.draw( ( win->getWidth() - TxtCache.getTextWidth() ) * 0.5f, ( YPos += TexF2Cache.getTextHeight() + 24 ) ); // Text rotated and scaled - TTF->draw( win->getWidth() * 0.5f - TTF->getTextWidth() * 0.5f, 512, FONT_DRAW_LEFT, Vector2f( 0.75f, 0.75f ), 12.5f ); + TTFCache.draw( ( win->getWidth() - TTFCache.getTextWidth() ) * 0.5f, 512 + 32, Vector2f( 0.75f, 0.75f ), 12.5f ); // Draw frame win->display(); @@ -90,16 +95,26 @@ EE_MAIN_FUNC int main (int argc, char * argv []) TexLoader.load();; TexF2->load( TexLoader.getId(), 32 ); + // Set the font to the text cache + TTFCache.setFont( TTF ); // Set a text to render - TTF->setText( "Lorem ipsum dolor sit amet, consectetur adipisicing elit." ); - TTFO->setText( TTF->getText() ); - TTF2->setText( TTF->getText() ); - TexF->setText( TTF->getText() ); - TexF2->setText( TTF->getText() ); + TTFCache.setText( "Lorem ipsum dolor sit amet, consectetur adipisicing elit." ); + + TTFOCache.setFont( TTFO ); + TTFOCache.setText( TTFCache.getText() ); + + TTF2Cache.setFont( TTF2 ); + TTF2Cache.setText( TTFCache.getText() ); // Set the font color - TTF2->setColor( RGB(0,0,0) ); - TexF->setColor( RGB(0,0,0) ); + TTF2Cache.setColor( RGB(0,0,0) ); + + TexFCache.setFont( TexF ); + TexFCache.setText( TTFCache.getText() ); + TexFCache.setColor( RGB(0,0,0) ); + + TexF2Cache.setFont( TexF2 ); + TexF2Cache.setText( TTFCache.getText() ); // Create a new text string String Txt( "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." ); diff --git a/src/test/eetest.cpp b/src/test/eetest.cpp index 33c319d97..f5e5b77b5 100644 --- a/src/test/eetest.cpp +++ b/src/test/eetest.cpp @@ -84,9 +84,6 @@ void EETest::init() { Scenes[4] = cb::Make0( this, &EETest::screen4 ); Scenes[5] = cb::Make0( this, &EETest::screen5 ); - //InBuf.Start(); - InBuf.isNewLineEnabled( true ); - setRandomSeed( static_cast( Sys::getSystemTime() * 1000 ) ); loadTextures(); @@ -861,7 +858,6 @@ void EETest::onItemClick( const UIEvent * Event ) { setScreen( 5 ); } else if ( "Show Console" == txt ) { Con.toggle(); - InBuf.setActive( !Con.isActive() ); if ( Con.isActive() ) { mWindow->startTextInput(); @@ -1452,23 +1448,6 @@ void EETest::render() { mInfoText.draw( 6.f, 6.f ); - if ( InBuf.isActive() ) { - Uint32 NLPos = 0; - Uint32 LineNum = InBuf.getCurPosLinePos( NLPos ); - if ( InBuf.getCursorPos() == (int)InBuf.getBuffer().size() && !LineNum ) { - FF2->draw( "_", 6.f + FF2->getTextWidth(), 180.f ); - } else { - FF2->setText( InBuf.getBuffer().substr( NLPos, InBuf.getCursorPos() - NLPos ) ); - FF2->draw( "_", 6.f + FF2->getTextWidth(), 180.f + (Float)LineNum * (Float)FF2->getFontHeight() ); - } - - FF2->setText( "FPS: " + String::toStr( mWindow->getFPS() ) ); - FF2->draw( mWindow->getWidth() - FF2->getTextWidth() - 15, 0 ); - - FF2->setText( InBuf.getBuffer() ); - FF2->draw( 6, 180, FONT_DRAW_SHADOW ); - } - UIManager::instance()->draw(); UIManager::instance()->update(); @@ -1550,7 +1529,6 @@ void EETest::input() { if ( KM->isKeyUp( KEY_F3 ) || KM->isKeyUp( KEY_WORLD_26 ) || KM->isKeyUp( KEY_BACKSLASH ) ) { Con.toggle(); - InBuf.setActive( !Con.isActive() ); } if ( KM->isKeyUp(KEY_1) && KM->isControlPressed() ) diff --git a/src/test/eetest.hpp b/src/test/eetest.hpp index a034cac6f..03cbac17d 100644 --- a/src/test/eetest.hpp +++ b/src/test/eetest.hpp @@ -43,7 +43,6 @@ class EETest : private Thread { TextureFactory* TF; System::Log* Log; EE::Window::Input* KM; - InputTextBuffer InBuf; bool side, aside; Float ang, scale, alpha, Ang;