Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreTextBoxGuiElement.cpp

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002 This source file is a part of OGRE
00003 (Object-oriented Graphics Rendering Engine)
00004 
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This library is free software; you can redistribute it and/or modify it
00011 under the terms of the GNU Lesser General Public License (LGPL) as 
00012 published by the Free Software Foundation; either version 2.1 of the 
00013 License, or (at your option) any later version.
00014 
00015 This library is distributed in the hope that it will be useful, but 
00016 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00017 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00018 License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public License 
00021 along with this library; if not, write to the Free Software Foundation, 
00022 Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA or go to
00023 http://www.gnu.org/copyleft/lesser.txt
00024 -------------------------------------------------------------------------*/
00025 
00026 #include "OgreTextBoxGuiElement.h"
00027 #include "OgreKeyEvent.h"
00028 #include "OgreInput.h"
00029 #include "OgreGuiManager.h"
00030 #include "OgreException.h"
00031 
00032 namespace Ogre {
00033 
00034     //---------------------------------------------------------------------
00035     String TextBoxGuiElement::msTypeName = "TextBox";
00036     TextBoxGuiElement::CmdBackPanel TextBoxGuiElement::msCmdBackPanel;
00037     TextBoxGuiElement::CmdTextArea TextBoxGuiElement::msCmdTextArea;
00038     //---------------------------------------------------------------------
00039     TextBoxGuiElement::TextBoxGuiElement(const String& name)
00040         : PanelGuiElement(name)
00041     {
00042       if (createParamDictionary("TextBoxGuiElement"))
00043         {
00044             addBaseParameters();
00045         }
00046 
00047         setTransparent(true);
00048         mBackPanel = 0;
00049         mTextArea = 0;
00050         mChildrenProcessEvents = false;
00051     }
00052 
00053 
00054     //---------------------------------------------------------------------
00055     TextBoxGuiElement::~TextBoxGuiElement()
00056     {
00057     }
00058     //---------------------------------------------------------------------
00059     const String& TextBoxGuiElement::getTypeName(void) const
00060     {
00061         return msTypeName;
00062     }
00063     //---------------------------------------------------------------------
00064     void TextBoxGuiElement::addBaseParameters(void)
00065     {
00066         PanelGuiElement::addBaseParameters();
00067         ParamDictionary* dict = getParamDictionary();
00068 
00069         dict->addParameter(ParameterDef("back_panel", 
00070            "The template name of the panel to be used behind the text."
00071             , PT_STRING),
00072             &msCmdBackPanel);
00073         dict->addParameter(ParameterDef("text_area", 
00074            "The template name of the textArea to be used for the text."
00075             , PT_STRING),
00076             &msCmdTextArea);
00077 
00078     }
00079     //---------------------------------------------------------------------------------------------
00080     // Char height command object
00081     //
00082     String TextBoxGuiElement::CmdBackPanel::doGet( const void* target ) const
00083     {
00084         return static_cast< const TextBoxGuiElement* >( target )->getBackPanelName();
00085     }
00086     void TextBoxGuiElement::CmdBackPanel::doSet( void* target, const String& val )
00087     {
00088         std::vector<String> vec = val.split("\t\n ", 1);
00089 
00090         if (vec.size() < 2)
00091         {
00092             static_cast<TextBoxGuiElement*>(target)->setBackPanel(val, 5);
00093         }
00094         else
00095         {
00096             static_cast<TextBoxGuiElement*>(target)->setBackPanel(vec[0], StringConverter::parseInt(vec[1]));
00097         }
00098 
00099     }
00100     //---------------------------------------------------------------------------------------------
00101     String TextBoxGuiElement::CmdTextArea::doGet( const void* target ) const
00102     {
00103         return static_cast< const TextBoxGuiElement* >( target )->getTextAreaName();
00104     }
00105 
00106     //-----------------------------------------------------------------------
00107     void TextBoxGuiElement::CmdTextArea::doSet(void* target, const String& val)
00108     {
00109         std::vector<String> vec = val.split("\t\n ", 1);
00110 
00111 
00112         if (vec.size() < 2)
00113         {
00114             static_cast<TextBoxGuiElement*>(target)->setTextArea(val, String(""));
00115         }
00116         else
00117         {
00118             static_cast<TextBoxGuiElement*>(target)->setTextArea(vec[0], vec[1]);
00119         }
00120     }
00121 
00122     //-----------------------------------------------------------------------
00123     String TextBoxGuiElement::getTextAreaName() const
00124     {
00125         return mTextAreaTemplateName + " " + mCaption;
00126     }
00127     //---------------------------------------------------------------------------------------------
00128     String TextBoxGuiElement::getBackPanelName() const
00129     {
00130         return mBackPanelTemplateName;
00131     }
00132     
00133     //-----------------------------------------------------------------------
00134     void TextBoxGuiElement::setTextArea(const String& templateName, const String& name)
00135     {
00136         mTextAreaTemplateName = templateName;
00137         if (mTextArea)
00138         {
00139             removeChild(mTextArea->getName());
00140             GuiManager::getSingleton().destroyGuiElement(mTextArea);
00141             mTextArea = NULL;
00142         }
00143 
00144         mTextArea = static_cast<TextAreaGuiElement*>
00145             (GuiManager::getSingleton().createGuiElementFromTemplate(mTextAreaTemplateName, "", mName + "/textArea"));
00146 
00147         mCaption = name;
00148         setCaptionToTextArea();
00149 
00150         // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned,
00151         // one copy when the children are copied, and another copy when setTextArea is set.
00152         mTextArea->setCloneable(false);
00153         mTextArea->setTop(0);
00154         mTextArea->setLeft(mTextArea->getSpaceWidth()/2);
00155 
00156         if (mBackPanel)
00157         {
00158 //          mBackPanel->setHeight(mTextArea->getCharHeight());
00159             mBackPanel->addChild(mTextArea);
00160         }
00161         else
00162         {
00163             // The textarea was created first, so add it to backpanel when backpanel is created
00164 
00165         }
00166     }
00167     //-----------------------------------------------------------------------
00168     void TextBoxGuiElement::setBackPanel(const String& templateName, int size)
00169     {
00170         mTextAreaSize = size;
00171         mBackPanelTemplateName = templateName;
00172         if (mBackPanel)
00173         {
00174             removeChild(mBackPanel->getName());
00175             GuiManager::getSingleton().destroyGuiElement(mBackPanel);
00176             mBackPanel = NULL;
00177         }
00178 
00179         mBackPanel = static_cast<GuiContainer*>
00180             (GuiManager::getSingleton().createGuiElementFromTemplate(mBackPanelTemplateName, "", mName + "/backPanel"));
00181 
00182         // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned,
00183         // one copy when the children are copied, and another copy when setTextArea is set.
00184         mBackPanel->setCloneable(false);
00185         mBackPanel->setDimensions(getWidth(),getHeight());
00186         mBackPanel->setTop(0);
00187         mBackPanel->setLeft(0);
00188         mBackPanel->setWidth(getWidth());
00189 
00190         addChild(mBackPanel);
00191         if (mTextArea)
00192         {
00193 //          mBackPanel->setHeight(mTextArea->getCharHeight());
00194             mBackPanel->addChild(mTextArea);
00195         }
00196         else
00197         {
00198             // The BackPanel was created first, so add it to backpanel when textarea is created
00199 
00200         }
00201     }
00202 
00203   void TextBoxGuiElement::setCaption(const String& text)
00204   {
00205     mCaption = text;
00206     if (mTextArea != NULL)
00207             mTextArea->setCaption(text);
00208   }
00209 
00210   const String& TextBoxGuiElement::getCaption(void) const
00211   {
00212     if (mTextArea != NULL)
00213       return mTextArea->getCaption();
00214     return mCaption;
00215   }
00216 
00217 
00218     //---------------------------------------------------------------------------------------------
00219     void TextBoxGuiElement::setCaptionToTextArea() 
00220     {
00221         mTextArea->setCaption(mCaption);
00222     }
00223     //---------------------------------------------------------------------------------------------
00224     void TextBoxGuiElement::processEvent(InputEvent* e) 
00225     {
00226         PanelGuiElement::processEvent(e);
00227 
00228         if (mTextArea)
00229         {
00230             if (!e->isConsumed())
00231             {
00232                 switch(e->getID()) 
00233                 {
00234                 case KeyEvent::KE_KEY_PRESSED:
00235                     KeyEvent* ke = static_cast<KeyEvent*> (e);
00236 
00237                     if (ke->getKey() == KC_BACK)
00238                     {
00239                         mCaption = mCaption.substr(0,mCaption.length() -1);
00240                         setCaptionToTextArea();
00241 
00242                     }
00243                     else
00244                     {
00245                         OgreChar newKey = ke->getKeyChar();
00246                         Font* font = static_cast<Font*> (FontManager::getSingleton().getByName(mTextArea->getFontName()));
00247                         if (!font)
00248                             Except( Exception::ERR_ITEM_NOT_FOUND, "Could not find font " + mTextArea->getFontName(),
00249                                 "TextBoxGuiElement::processEvent" );
00250                         
00251                         if (font->getGlyphAspectRatio(newKey)*mTextArea->getCharHeight() + mTextArea->getWidth() < getWidth() - mTextArea->getSpaceWidth())
00252                         {
00253                             mCaption += newKey;
00254                             setCaptionToTextArea();
00255                         }
00256                     }
00257                     break;
00258                 }
00259             }
00260         }
00261     }
00262 
00263 }
00264 

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:30 2004