KeosConsole.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 
00022 //==========================================================
00023 // ORIGINAL COPYRIGHT FOLLOWS
00024 //==========================================================
00025 // Yes::Engine - Free C++ 3D engine
00026 //
00027 // Copyright (C) 2004-2005 Laurent Gomila
00028 //
00029 // This program is free software; you can redistribute it and/or
00030 // modify it under the terms of the GNU General Public License
00031 // as published by the Free Software Foundation; either version 2
00032 // of the License, or (at your option) any later version.
00033 //
00034 // This program is distributed in the hope that it will be useful,
00035 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00036 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00037 // GNU General Public License for more details.
00038 //
00039 // You should have received a copy of the GNU General Public License
00040 // along with this program; if not, write to the Free Software
00041 // Foundation, Inc.,
00042 // 59 Temple Place - Suite 330,
00043 // Boston, MA  02111-1307, USA.
00044 //
00045 // E-mail : laurent.gom@gmail.com
00046 //==========================================================
00047 
00048 //==========================================================
00049 // En-têtes
00050 //==========================================================
00051 #include "KeosConsole.h"
00052 #include "KeosException.h"
00053 
00054 namespace Keos
00055 {
00056 
00061   CConsole::CConsole() :
00062       m_Current(""),
00063       m_Look   (NULL),
00064       m_Enabled(false)
00065   {
00066     RegisterCommand("?", Console::Bind(&CConsole::GetCommands, *this));
00067     RegisterCommand("help",   Console::Bind(&CConsole::HelpCommand, *this));
00068   }
00069 
00070 
00077   void CConsole::ChangeLook(Console::ILook* NewLook)
00078   {
00079     Assert(NewLook != NULL);
00080 
00081     m_Look = NewLook;
00082   }
00083 
00084 
00092   void CConsole::RegisterCommand(const std::string& Name, const Console::CFunctor& Function,
00093                                  const std::string& Help)
00094   {
00095     m_Commands[Name] = Function;
00096     m_Helps[Name] = Help;
00097   }
00098 
00099 
00106   void CConsole::SendChar(char Character)
00107   {
00108     // Si la console n'est pas active
00109     // ou qu'il n'y pas de look défini
00110     // on ne traite pas le caractère
00111     if (!m_Enabled || m_Look == NULL)
00112       return;
00113 
00114     // Traitement du caractère
00115     switch (Character)
00116     {
00117         // Saut de ligne : on traite la commande et on efface la ligne
00118       case '\n' :
00119       case '\r' :
00120         if (!m_Current.empty())
00121         {
00122           ProcessCurrent();
00123           m_Current.clear();
00124         }
00125         break;
00126 
00127         // Backspace : on efface le dernier caractère
00128       case '\b' :
00129         if (!m_Current.empty())
00130           m_Current.erase(m_Current.size() - 1);
00131         break;
00132 
00133         // Tout le reste : on ajoute le caractère à la ligne courante
00134       default :
00135         m_Current += Character;
00136         break;
00137     }
00138 
00139     // On notifie au "look" que le texte courant vient de changer
00140     m_Look->TextChanged(m_Current);
00141   }
00142 
00143 
00148   void CConsole::Update()
00149   {
00150     m_Look->Update();
00151   }
00152 
00153 
00158   void CConsole::Draw() const
00159   {
00160     m_Look->Draw();
00161   }
00162 
00163 
00170   void CConsole::Enable(bool Enabled)
00171   {
00172     m_Enabled = Enabled;
00173     if (m_Look != NULL) m_Look->Show(Enabled);
00174   }
00175 
00176 
00183   std::string CConsole::GetCommands() const
00184   {
00185     std::string List;
00186     for (TCommandTable::const_iterator i = m_Commands.begin(); i != m_Commands.end(); ++i)
00187       List += i->first + " ";
00188 
00189     return List;
00190   }
00191 
00192 
00200   String CConsole::HelpCommand(const String& CommandName)
00201   {
00202     THelpTable::iterator It = m_Helps.find(CommandName);
00203 
00204     if (It != m_Helps.end())
00205       return m_Helps[CommandName];
00206     return CommandName + ": Unknown command!";
00207   }
00208 
00209 
00214   void CConsole::ProcessCurrent()
00215   {
00216     // On récupère la commande
00217     std::string Command;
00218     std::istringstream iss(m_Current);
00219     iss >> Command;
00220 
00221     // On recherche la commande dans la table des commandes
00222     TCommandTable::iterator It = m_Commands.find(Command);
00223 
00224     // Si elle s'y trouve on appelle la fonction correspondante, sinon on génère une erreur
00225     if (It != m_Commands.end())
00226     {
00227       // Récupération des paramètres
00228       std::string Params;
00229       std::getline(iss, Params);
00230 
00231       // Appel du foncteur correspondant à la commande -
00232       // s'il y a une erreur on la rattrape et on l'affiche dans la console
00233       try
00234       {
00235         m_Look->CommandCalled(It->second(Params));
00236       }
00237       catch (CBadConversion&)
00238       {
00239         m_Look->Error("Invalid number (or type) of parameters !");
00240       }
00241       catch (std::exception& E)
00242       {
00243         m_Look->Error(E.what());
00244       }
00245     }
00246     else
00247     {
00248       m_Look->Error("Unknown command \"" + Command + "\"");
00249     }
00250   }
00251 
00252 
00253 } // namespace Keos

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