ConsoleLines.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <console/ConsoleLines.h>
00022 #include <common/DefinesString.h>
00023 #include <string>
00024 
00025 unsigned ConsoleLine::nextLineNumber_ = 0;
00026 
00027 ConsoleLine::ConsoleLine() :
00028         lineType_(eNone), lineNumber_(0)
00029 {
00030 
00031 }
00032 
00033 ConsoleLine::~ConsoleLine()
00034 {
00035 
00036 }
00037 
00038 void ConsoleLine::set(const LangString &line, LineType type)
00039 {
00040         line_ = line;
00041         lineType_ = type;
00042         if (lineType_ == eCommand)
00043         {
00044                 lineNumber_ = ++nextLineNumber_;
00045         }
00046         if (lineType_ == eCommandCont)
00047         {
00048                 lineNumber_ = nextLineNumber_;
00049         }
00050         lineNumberStr_= LANG_STRING(S3D::formatStringBuffer("%4i", lineNumber_));
00051 }
00052 
00053 void ConsoleLine::drawLine(float x, float y, GLFont2d *font)
00054 {
00055         static Vector color(0.9f, 0.9f, 0.9f);
00056         if (lineType_ != eNone)
00057         {
00058                 if (lineType_ == eCommand)
00059                 {
00060                         // We show a line number of those lines with commands
00061                         // on them
00062                         font->draw(color, 12, x, y, 0.0f, lineNumberStr_);
00063                         font->draw(color, 12, x + 50.0f, y, 0.0f, line_);
00064                 }
00065                 else
00066                 {
00067                         font->draw(color, 12, x + 50.0f, y, 0.0f, "...");
00068                         font->draw(color, 12, x + 90.0f, y, 0.0f, line_);
00069                 }
00070         }
00071         else
00072         {
00073                 font->draw(color, 12, x + 50.0f, y, 0.0f, line_);
00074         }
00075 }
00076 
00077 ConsoleLines::ConsoleLines(int maxLines) :
00078         maxLines_(maxLines), currentLine_(0)
00079 {
00080 }
00081 
00082 ConsoleLines::~ConsoleLines()
00083 {
00084 }
00085 
00086 void ConsoleLines::clear()
00087 {
00088         currentLine_ = 0;
00089         while (!lines_.empty())
00090         {
00091                 ConsoleLine *line = lines_.front();
00092                 lines_.pop_front();
00093                 delete line;
00094         }
00095 }
00096 
00097 void ConsoleLines::scroll(int lines)
00098 {
00099         currentLine_ -= lines;
00100         if (currentLine_ < 0) currentLine_ = 0;
00101         else if (currentLine_ >= (int) lines_.size()) 
00102                 currentLine_ = (int) lines_.size();
00103 }
00104 
00105 void ConsoleLines::addLine(const std::string &originalText, bool showPointer)
00106 {
00107         LangString langStringText(LANG_STRING(originalText)), buffer;
00108 
00109         int section = 0;
00110         for (const unsigned int *a=langStringText.c_str(); 
00111                 *a; 
00112                 a++)
00113         {
00114                 if (*a != '\n') buffer.push_back(*a);
00115                 if (buffer.size() > 80 || (buffer.size() > 65 && *a ==' ') || *a=='\n')
00116                 {
00117                         addSmallLine(section++, buffer, showPointer);
00118                         buffer.clear();
00119                 }
00120         }
00121         if (!buffer.empty()) 
00122         {
00123                 addSmallLine(section++, buffer, showPointer);
00124         }
00125 }
00126 
00127 void ConsoleLines::addSmallLine(int sectionNo, const LangString &text, bool showPointer)
00128 {
00129         ConsoleLine::LineType type = ConsoleLine::eNone;
00130         if (showPointer)
00131         {
00132                 if (sectionNo == 0) type = ConsoleLine::eCommand;
00133                 else type = ConsoleLine::eCommandCont;
00134         }
00135 
00136         if (lines_.size() == maxLines_)
00137         {
00138                 // We need to reuse the lines
00139                 // reuse the first line
00140                 ConsoleLine *line = lines_.back();
00141                 lines_.pop_back();
00142                 line->set(text, type);
00143                 lines_.push_front(line);
00144         }
00145         else
00146         {
00147                 // Add a new line
00148                 ConsoleLine *line = new ConsoleLine;
00149                 line->set(text, type);
00150                 lines_.push_front(line);
00151         }
00152 
00153         // Set the lookat and cursor to point at the new line
00154         if (currentLine_ != 0)
00155         {
00156                 currentLine_ ++;
00157                 if (currentLine_ > (int) lines_.size()) 
00158                         currentLine_ = (int) lines_.size();
00159         }
00160 }
00161 
00162 void ConsoleLines::drawLines(GLFont2d *font, float startHeight, float totalHeight, float totalWidth)
00163 {
00164         if (currentLine_ != 0)
00165         {
00166                 // Draw arrows at the bottom of the screen if stuff off bottom
00167                 glColor3f(0.7f, 0.7f, 0.7f);
00168                 glBegin(GL_TRIANGLES);
00169                         for (float a=100.0f; a<totalWidth - 100.0f; a+=totalWidth / 25.0f)
00170                         {
00171                                 glVertex2f(a + 20.0f, startHeight - 4.0f);
00172                                 glVertex2f(a + 24.0f, startHeight - 8.0f);
00173                                 glVertex2f(a + 28.0f, startHeight - 4.0f);
00174                         }
00175                 glEnd();
00176         }
00177 
00178         bool offTop = false;
00179         {
00180                 // Draw all of the text
00181                 // Stops each drawLine() from changing state
00182                 GLState currentState(GLState::TEXTURE_ON | GLState::BLEND_ON);
00183 
00184                 float position = startHeight + 20.0f;
00185                 for (int i=currentLine_; i<(int) lines_.size(); i++)
00186                 {
00187                         ConsoleLine *line = lines_[i];
00188                         line->drawLine(20.0f, position, font);
00189 
00190                         position += 15.0f;
00191                         if (position > totalHeight - 20.0f)
00192                         {
00193                                 offTop = true;
00194                                 break;
00195                         }
00196                 }
00197         }
00198 
00199         if (offTop)
00200         {
00201                 // Draw arrows at top of screen if stuff off top
00202                 glColor3f(0.7f, 0.7f, 0.7f);
00203                 glBegin(GL_TRIANGLES);
00204                         for (float a=100.0f; a<totalWidth - 100.0f; a+=totalWidth / 25.0f)
00205                         {
00206                                 glVertex2f(a + 24.0f, totalHeight - 4.0f);
00207                                 glVertex2f(a + 20.0f, totalHeight - 8.0f);
00208                                 glVertex2f(a + 28.0f, totalHeight - 8.0f);
00209                         }
00210                 glEnd();
00211         }
00212 }

Generated on Mon Feb 16 15:14:37 2009 for Scorched3D by  doxygen 1.5.3