KeosConsoleLookDefault.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 #include "KeosConsoleLookDefault.h"
00022 #include "KeosGraphicString.h"
00023 #include "KeosConsoleLook.h"
00024 #include "KeosConsole.h"
00025 #include "KeosMesh.h"
00026 #include "KeosMatrix4.h"
00027 #include "KeosRenderSystem.h"
00028 #include "KeosFontManager.h"
00029 
00030 namespace Keos
00031 {
00032 
00033   namespace Console
00034   {
00035 
00036     //=======================================================================
00037     // CLookDefault implementation
00038     //=======================================================================
00039 
00040     //-----------------------------------------------------------------------
00041     CLookDefault::CLookDefault(const String& strFont, TVector2F Size, const String& strTexture,
00042                                const CColor& TextColor, const CColor& TextOkColor, const CColor& TextErrorColor) :
00043         m_State (STOPPED_INVISIBLE),
00044         m_strFont (strFont),
00045         m_nSize (Size),
00046         m_TextColor (CColor::White),
00047         m_TextOkColor (CColor(150, 200, 150)),
00048         m_TextErrorColor (CColor(200, 150, 150))
00049     {
00050       m_nFontHeight = CFontManager::Instance().GetFontHeight(strFont);
00051 
00052       // Background creation
00053       CMeshGeom::TVertex Vertices[] =
00054         {
00055           {TVector3F(  0,  0, 0), TVector3F(), Renderer->ConvertColor(CColor(255, 255, 255, 200)), TVector2F(0, 0)},
00056           {TVector3F(Size.x,  0, 0), TVector3F(), Renderer->ConvertColor(CColor(255, 255, 255, 200)), TVector2F(1, 0)},
00057           {TVector3F(  0, Size.y, 0), TVector3F(), Renderer->ConvertColor(CColor(255, 255, 255, 200)), TVector2F(0, 1)},
00058           {TVector3F(Size.x, Size.y, 0), TVector3F(), Renderer->ConvertColor(CColor(255, 255, 255, 200)), TVector2F(1, 1)}
00059         };
00060       CMeshGeom::TIndex Indices[] = {0, 1, 2, 2, 1, 3};
00061 
00062       m_Background = new CMesh();
00063       CMeshGeom* pMeshGeom = new CMeshGeom(Vertices, 4, Indices, 6);
00064       m_Background->AddMeshGeom(pMeshGeom);
00065       m_Background->AddMaterial(new CMaterial(strTexture), 0);
00066 
00067       // Initial position: invisible
00068       m_Transfo.SetScaling(0, 0, 0);
00069 
00070       // Create the first line
00071       AddLine("> ", m_TextColor);
00072 
00073       // Register special command
00074       CConsole::Instance().RegisterCommand("clear", Bind(&std::list<CGraphicString>::clear, m_Lines));
00075       char str[128];
00076       sprintf(str, "Keos %s", CRoot::Instance().GetVersion().c_str());
00077       CConsole::Instance().RegisterCommand("ver", Bind(Print(String(str))));
00078     }
00079 
00080     //-----------------------------------------------------------------------
00081     void CLookDefault::Update()
00082     {
00083       static uint sTimeAtLastFrame = CRoot::Instance().GetTimer()->GetMilliseconds();
00084       uint nCurrentTime = CRoot::Instance().GetTimer()->GetMilliseconds();
00085       uint nFrameInterval = nCurrentTime - sTimeAtLastFrame;
00086       sTimeAtLastFrame = nCurrentTime;
00087 
00088       static float Scale = 0.0f;
00089 
00090       if (m_State == SHOWING)
00091       {
00092         m_Transfo.SetScaling(1, Scale, 1);
00093         Scale += nFrameInterval / 400.0f;
00094 
00095         if (Scale > 1.0f)
00096         {
00097           m_Transfo.SetScaling(1, 1, 1);
00098           m_State = STOPPED_VISIBLE;
00099           Scale   = 1.0f;
00100         }
00101       }
00102       else if (m_State == HIDDING)
00103       {
00104         m_Transfo.SetScaling(1, Scale, 1);
00105         Scale -= nFrameInterval / 400.0f;
00106 
00107         if (Scale < 0.01f)
00108         {
00109           m_Transfo.SetScaling(0, 0, 0);
00110           m_State = STOPPED_INVISIBLE;
00111           Scale   = 0.0f;
00112         }
00113       }
00114     }
00115 
00116     //-----------------------------------------------------------------------
00117     void CLookDefault::Draw() const
00118     {
00119       if (m_State != STOPPED_INVISIBLE)
00120       {
00121         Renderer->PushMatrix(MAT_MODELVIEW);
00122         Renderer->LoadMatrix(MAT_MODELVIEW, m_Transfo);
00123 
00124         Renderer->Enable(RENDER_ZWRITE, false);
00125         Renderer->Enable(RENDER_ALPHABLEND, true);
00126         Renderer->SetupAlphaBlending(BLEND_SRCALPHA, BLEND_INVSRCALPHA);
00127 
00128         m_Background->Render();
00129 
00130         Renderer->Enable(RENDER_ALPHABLEND, false);
00131 
00132         for (std::list<CGraphicString>::const_iterator i = m_Lines.begin(); i != m_Lines.end(); ++i)
00133           i->Draw();
00134 
00135         Renderer->Enable(RENDER_ZWRITE, true);
00136 
00137         Renderer->PopMatrix(MAT_MODELVIEW);
00138       }
00139     }
00140 
00141     //-----------------------------------------------------------------------
00142     void CLookDefault::Show(bool bVisible)
00143     {
00144       m_State = bVisible ? SHOWING : HIDDING;
00145     }
00146 
00147     //-----------------------------------------------------------------------
00148     void CLookDefault::CommandCalled(const String& strResult)
00149     {
00150       if (!strResult.empty())
00151         AddLine(strResult, m_TextOkColor);
00152       AddLine("> ", m_TextColor);
00153     }
00154 
00155     //-----------------------------------------------------------------------
00156     void CLookDefault::TextChanged(const String& strNewText)
00157     {
00158       m_Lines.front().Text = "> " + strNewText;
00159     }
00160 
00161     //-----------------------------------------------------------------------
00162     void CLookDefault::Error(const String& strMessage)
00163     {
00164       std::string Line;
00165       std::istringstream iss(strMessage);
00166       while (std::getline(iss, Line))
00167         AddLine(Line, m_TextErrorColor);
00168 
00169       AddLine("> ", m_TextColor);
00170     }
00171 
00172     //-----------------------------------------------------------------------
00173     void CLookDefault::AddLine(const String& strLine, const CColor& Color)
00174     {
00175       // Delete the last line if necessary
00176       if (m_Lines.size() == (int)(((float)m_nSize.y) / ((float)m_nFontHeight)))
00177         m_Lines.pop_back();
00178 
00179       // Move the others
00180       for (std::list<CGraphicString>::iterator i = m_Lines.begin(); i != m_Lines.end(); ++i)
00181       {
00182         i->Position.y -= (int)m_nFontHeight;
00183         i->Color.Set(i->Color.GetRed(), i->Color.GetGreen(), i->Color.GetBlue(), i->Color.GetAlpha() - 20);
00184       }
00185 
00186       // Create the new line
00187       m_Lines.push_front(CGraphicString(TVector2I((int)m_nFontHeight / 2, m_nSize.y - (int)((float)m_nFontHeight*1.3)), strLine, Color, m_strFont));
00188     }
00189 
00190   } // namespace Console
00191 
00192 } // namespace Keos

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1