Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# 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 "KeosModel.h"
    00026 #include "KeosMatrix4.h"
    00027 #include "KeosRenderSystem.h"
    00028 #include "KeosFontManager.h"
    00029
    00030 namespace Keos {
    00031
    00032         namespace Console {
    00033
    00034                 //=======================================================================
    00035                 // CLookDefault implementation
    00036                 //=======================================================================
    00037
    00038                 //-----------------------------------------------------------------------
    00039                 CLookDefault::CLookDefault(const String& strFont, TVector2F Size, const String& strTexture,
    00040                                                         const CColor& TextColor, const CColor& TextOkColor, const CColor& TextErrorColor) :
    00041                 m_State (STOPPED_INVISIBLE),
    00042                 m_strFont (strFont),
    00043                 m_nSize (Size),
    00044                 m_TextColor (CColor::White),
    00045                 m_TextOkColor (CColor(150, 200, 150)),
    00046                 m_TextErrorColor (CColor(200, 150, 150))
    00047                 {
    00048                         m_nFontHeight = CFontManager::Instance().GetFontHeight(strFont);
    00049
    00050                         // Background creation
    00051                         CModel::TVertex Vertices[] =
    00052                         {
    00053                                 {TVector3F(             0,              0, 0), TVector3F(), Renderer->ConvertColor(CColor(255, 255, 255, 200)), TVector2F(0, 0)},
    00054                                 {TVector3F(Size.x,              0, 0), TVector3F(), Renderer->ConvertColor(CColor(255, 255, 255, 200)), TVector2F(1, 0)},
    00055                                 {TVector3F(             0, Size.y, 0), TVector3F(), Renderer->ConvertColor(CColor(255, 255, 255, 200)), TVector2F(0, 1)},
    00056                                 {TVector3F(Size.x, Size.y, 0), TVector3F(), Renderer->ConvertColor(CColor(255, 255, 255, 200)), TVector2F(1, 1)}
    00057                         };
    00058                         CModel::TIndex Indices[] = {0, 1, 2, 2, 1, 3};
    00059                         m_Background = new CModel(Vertices, 4, Indices, 6, strTexture);
    00060
    00061                         // Initial position: invisible
    00062                         m_Transfo.SetScaling(0, 0, 0);
    00063
    00064                         // Create the first line
    00065                         AddLine("> ", m_TextColor);
    00066
    00067                         // Register special command
    00068                         CConsole::Instance().RegisterCommand("clear", Bind(&std::list<CGraphicString>::clear, m_Lines));
    00069                         char str[128];
    00070                         sprintf(str,"Keos %s", CRoot::Instance().GetVersion().c_str());
    00071                         CConsole::Instance().RegisterCommand("ver", Bind(Print(String(str))));
    00072                 }
    00073
    00074                 //-----------------------------------------------------------------------
    00075                 void CLookDefault::Update()
    00076                 {
    00077                         static uint sTimeAtLastFrame = CRoot::Instance().GetTimer()->GetMilliseconds();
    00078                         uint nCurrentTime = CRoot::Instance().GetTimer()->GetMilliseconds();
    00079                         uint nFrameInterval = nCurrentTime - sTimeAtLastFrame;
    00080                         sTimeAtLastFrame = nCurrentTime;
    00081
    00082                         static float Scale = 0.0f;
    00083
    00084                         if (m_State == SHOWING)
    00085                         {
    00086                                 m_Transfo.SetScaling(1, Scale, 1);
    00087                                 Scale += nFrameInterval / 400.0f;
    00088
    00089                                 if (Scale > 1.0f)
    00090                                 {
    00091                                         m_Transfo.SetScaling(1, 1, 1);
    00092                                         m_State = STOPPED_VISIBLE;
    00093                                         Scale   = 1.0f;
    00094                                 }
    00095                         }
    00096                         else if (m_State == HIDDING)
    00097                         {
    00098                                 m_Transfo.SetScaling(1, Scale, 1);
    00099                                 Scale -= nFrameInterval / 400.0f;
    00100
    00101                                 if (Scale < 0.01f)
    00102                                 {
    00103                                         m_Transfo.SetScaling(0, 0, 0);
    00104                                         m_State = STOPPED_INVISIBLE;
    00105                                         Scale   = 0.0f;
    00106                                 }
    00107                         }
    00108                 }
    00109
    00110                 //-----------------------------------------------------------------------
    00111                 void CLookDefault::Draw() const
    00112                 {
    00113                         if (m_State != STOPPED_INVISIBLE)
    00114                         {
    00115                                 Renderer->PushMatrix(MAT_MODELVIEW);
    00116                                 Renderer->LoadMatrix(MAT_MODELVIEW, m_Transfo);
    00117
    00118                                 Renderer->Enable(RENDER_ZWRITE, false);
    00119                                 Renderer->Enable(RENDER_ALPHABLEND, true);
    00120                                 Renderer->SetupAlphaBlending(BLEND_SRCALPHA, BLEND_INVSRCALPHA);
    00121
    00122                                 m_Background->Render();
    00123
    00124                                 Renderer->Enable(RENDER_ALPHABLEND, false);
    00125
    00126                                 for (std::list<CGraphicString>::const_iterator i = m_Lines.begin(); i != m_Lines.end(); ++i)
    00127                                         i->Draw();
    00128
    00129                                 Renderer->Enable(RENDER_ZWRITE, true);
    00130
    00131                                 Renderer->PopMatrix(MAT_MODELVIEW);
    00132                         }
    00133                 }
    00134
    00135                 //-----------------------------------------------------------------------
    00136                 void CLookDefault::Show(bool bVisible)
    00137                 {
    00138                         m_State = bVisible ? SHOWING : HIDDING;
    00139                 }
    00140
    00141                 //-----------------------------------------------------------------------
    00142                 void CLookDefault::CommandCalled(const String& strResult)
    00143                 {
    00144                         if (!strResult.empty())
    00145                                 AddLine(strResult, m_TextOkColor);
    00146                         AddLine("> ", m_TextColor);
    00147                 }
    00148
    00149                 //-----------------------------------------------------------------------
    00150                 void CLookDefault::TextChanged(const String& strNewText)
    00151                 {
    00152                         m_Lines.front().Text = "> " + strNewText;
    00153                 }
    00154
    00155                 //-----------------------------------------------------------------------
    00156                 void CLookDefault::Error(const String& strMessage)
    00157                 {
    00158                         std::string Line;
    00159                         std::istringstream iss(strMessage);
    00160                         while (std::getline(iss, Line))
    00161                                 AddLine(Line, m_TextErrorColor);
    00162
    00163                         AddLine("> ", m_TextColor);
    00164                 }
    00165
    00166                 //-----------------------------------------------------------------------
    00167                 void CLookDefault::AddLine(const String& strLine, const CColor& Color)
    00168                 {
    00169                         // Delete the last line if necessary
    00170                         if (m_Lines.size() == (int)(((float)m_nSize.y) / ((float)m_nFontHeight)))
    00171                                 m_Lines.pop_back();
    00172
    00173                         // Move the others
    00174                         for (std::list<CGraphicString>::iterator i = m_Lines.begin(); i != m_Lines.end(); ++i)
    00175                         {
    00176                                 i->Position.y -= (int)m_nFontHeight;
    00177                                 i->Color.Set(i->Color.GetRed(), i->Color.GetGreen(), i->Color.GetBlue(), i->Color.GetAlpha() - 20);
    00178                         }
    00179
    00180                         // Create the new line
    00181                         m_Lines.push_front(CGraphicString(TVector2I((int)m_nFontHeight/2, m_nSize.y - (int)((float)m_nFontHeight*1.3)), strLine, Color, m_strFont));
    00182                 }
    00183
    00184         } // namespace Console
    00185
    00186 } // namespace Keos