Quad Pang
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TextDisplay.cpp
Go to the documentation of this file.
00001 
00002 #include "TextDisplay.h"
00003 #include "Stroke.h"
00004 #include <ctype.h>
00005 
00006 static const double STROKEBG_TRANSP = 0.45;
00007 static const double STROKEBG_FOCUS = 0.9;
00008 
00009 static const int COL = 0;
00010 static const int ROW = 1;
00011 
00012 typedef char* PCHAR;
00013 
00014 StrokeDisplay::StrokeDisplay(
00015     int cols, int rows, int fontSize,
00016      bool bg, bool centered)
00017     : mCols(cols), mRows(rows)
00018     , mFontSize(fontSize),  mpStroke(0) 
00019     , mbBackground(bg), mbCentered(centered)
00020 {
00021     memset(mBgColor, 0, sizeof(mBgColor) );
00022     mBgColor[3] = STROKEBG_TRANSP;
00023     memset(mFontColor, 0, sizeof(mFontColor) );
00024     mpText = new PCHAR[rows];
00025     mLineWidth = new int[rows];
00026     mLinePadding = new double[rows];
00027 
00028     for (int i=0; i<rows; i++)
00029     {
00030         mpText[i] = new char[cols+1];
00031     }
00032     Clear();
00033 }
00034 
00035 
00036 void StrokeDisplay::Clear()
00037 {
00038     for (int i=0; i <mRows; i++)
00039     {
00040         *mpText[i] = '\0';
00041     }
00042     mColsPos = mRowsPos = 0;
00043     memset(mLinePadding, 0, sizeof(double) * mRows);
00044     memset(mLineWidth, 0, sizeof(int) * mRows);
00045 }
00046 
00047 StrokeDisplay::~StrokeDisplay()
00048 {
00049     delete mpText;
00050     delete mLineWidth;
00051     delete mLinePadding;
00052 }
00053    
00054 bool StrokeDisplay::Print(const char* msg)
00055 {
00056     char* pom = (char*)msg;
00057 
00058     while (*pom)
00059     {
00060         if (mColsPos == mCols || *pom == '\n')
00061         {
00062             mpText[mRowsPos][mColsPos] = '\0';
00063             if (*pom == '\n')
00064             {
00065                 pom++;
00066             }
00067             mColsPos = 0;         
00068             // Calcualte padding for mRowsPos Row
00069       
00070             if (mpStroke)
00071             {
00072                 if (mLineWidth[mRowsPos] > 0 && mLineWidth[mRowsPos] < mCols)
00073                     mLinePadding[mRowsPos] = ( (mCols - mLineWidth[mRowsPos]) - 0.3) * mpStroke->FontWidth() / 2;
00074                 else
00075                     mLinePadding[mRowsPos] = 0.0;
00076             }
00077 
00078             mRowsPos = (mRowsPos + 1) % mRows;
00079             mLineWidth[mRowsPos] = 0;
00080         }    
00081         else
00082         {
00083             mpText[mRowsPos][mColsPos] = toupper(*pom);
00084             mColsPos++;
00085             pom++;
00086             mLineWidth[mRowsPos]++;
00087         }
00088     }
00089 
00090     mpText[mRowsPos][mColsPos] = '\0';
00091     // Calcualte padding for mRowsPos Row
00092     if (mpStroke)
00093     {
00094         if (mLineWidth[mRowsPos] > 0 && mLineWidth[mRowsPos] < mCols)
00095             mLinePadding[mRowsPos] = ( (mCols - mLineWidth[mRowsPos]) - 0.3) * mpStroke->FontWidth() / 2;
00096         else
00097             mLinePadding[mRowsPos] = 0.0;
00098     }
00099     
00100     return true;
00101 }
00102 
00105 void StrokeDisplay::SetFocus(bool focus)
00106 {
00107     if (focus)
00108         mBgColor[3] = STROKEBG_FOCUS;
00109     else
00110         mBgColor[3] = STROKEBG_TRANSP;
00111 }
00112 
00113 bool StrokeDisplay::Display()
00114 {
00115     // If there is no text, don't display anything
00116     if (mColsPos == 0 && mRowsPos == 0)
00117     {
00118         return true;
00119     }
00120 
00121     // create stroke on first draw
00122     if (!mpStroke)
00123     {
00124         mpStroke = new Stroke(mFontSize);
00125    
00126         if (mLineWidth[mRowsPos] > 0 && mLineWidth[mRowsPos] < mCols)
00127                 mLinePadding[mRowsPos] = ( (mCols - mLineWidth[mRowsPos]) - 0.3) * mpStroke->FontWidth() / 2;
00128             else
00129                 mLinePadding[mRowsPos] = 0.0;
00130 
00131         if (!mpStroke->Init() )
00132         {
00133             delete mpStroke;
00134             mpStroke = 0;
00135             return false;
00136         }
00137 
00138         for (int i=0; i<mRows; i++)
00139         {
00140             if (mLineWidth[i] > 0 && mLineWidth[i] < mCols)
00141                 mLinePadding[i] = ( (mCols - mLineWidth[i]) - 0.3) * mpStroke->FontWidth() / 2;
00142             else
00143                 mLinePadding[i] = 0.0;
00144         }
00145     }
00146 
00147     // draw background
00148     if (mbBackground)
00149     {
00150         double offset = 0.4;
00151   
00152         glEnable (GL_BLEND);
00153         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00154         glColor4f(0.2, 0.5, 0.65, mBgColor[3]);
00155         glBegin(GL_POLYGON);
00156         glVertex3f(0.0-2*offset, 0.0-2*offset, 0.0);
00157         glVertex3f(mCols * mpStroke->FontWidth() + 5 * offset, 0.0 - 2*offset, 0.0);
00158         glVertex3f( mCols * mpStroke->FontWidth() + 5 * offset, mRows * mFontSize * 1.5 - offset, 0.0);
00159         glVertex3f(0.0 - 2*offset, mRows * mFontSize * 1.5 - offset, 0.0);
00160         glEnd();
00161     }
00162     int rows = mRows;
00163     int curr_row = (mRowsPos + 1) % mRows;
00164     glColor3f(mFontColor[0], mFontColor[1], mFontColor[2]);
00165    
00166     glDisable (GL_BLEND);
00167   
00168     // draw stroke text 
00169     glTranslatef(0.0, (mRows - 1) * 1.5 * mFontSize, 0.0);
00170 
00171     while (rows)
00172     {
00173         if (mpText[curr_row] != '\0')
00174         {
00175             glPushMatrix();
00176             if (mbCentered)
00177             {
00178                 glTranslatef(mLinePadding[curr_row], 0.0, 0.0);
00179             }
00180 
00181             mpStroke->DrawString(mpText[curr_row]);
00182             glPopMatrix();
00183             glTranslatef(0.0, -1.5 * mFontSize, 0.0);
00184         }    
00185         curr_row = (curr_row + 1) % mRows;
00186         rows--;
00187     }
00188     return true;
00189 }