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 "OgreActionEvent.h" 00027 #include "OgreTextBoxGuiElement.h" 00028 #include "OgreKeyEvent.h" 00029 #include "OgreInput.h" 00030 #include "OgreGuiManager.h" 00031 #include "OgreException.h" 00032 00033 namespace Ogre { 00034 00035 //--------------------------------------------------------------------- 00036 String TextBoxGuiElement::msTypeName = "TextBox"; 00037 TextBoxGuiElement::CmdBackPanel TextBoxGuiElement::msCmdBackPanel; 00038 TextBoxGuiElement::CmdTextArea TextBoxGuiElement::msCmdTextArea; 00039 //--------------------------------------------------------------------- 00040 TextBoxGuiElement::TextBoxGuiElement(const String& name) 00041 : PanelGuiElement(name) 00042 { 00043 if (createParamDictionary("TextBoxGuiElement")) 00044 { 00045 addBaseParameters(); 00046 } 00047 00048 mActionOnReturn = false; 00049 setTransparent(true); 00050 mBackPanel = 0; 00051 mTextArea = 0; 00052 mChildrenProcessEvents = false; 00053 } 00054 00055 00056 //--------------------------------------------------------------------- 00057 TextBoxGuiElement::~TextBoxGuiElement() 00058 { 00059 } 00060 //--------------------------------------------------------------------- 00061 const String& TextBoxGuiElement::getTypeName(void) const 00062 { 00063 return msTypeName; 00064 } 00065 //--------------------------------------------------------------------- 00066 void TextBoxGuiElement::addBaseParameters(void) 00067 { 00068 PanelGuiElement::addBaseParameters(); 00069 ParamDictionary* dict = getParamDictionary(); 00070 00071 dict->addParameter(ParameterDef("back_panel", 00072 "The template name of the panel to be used behind the text." 00073 , PT_STRING), 00074 &msCmdBackPanel); 00075 dict->addParameter(ParameterDef("text_area", 00076 "The template name of the textArea to be used for the text." 00077 , PT_STRING), 00078 &msCmdTextArea); 00079 00080 } 00081 //--------------------------------------------------------------------------------------------- 00082 // Char height command object 00083 // 00084 String TextBoxGuiElement::CmdBackPanel::doGet( const void* target ) const 00085 { 00086 return static_cast< const TextBoxGuiElement* >( target )->getBackPanelName(); 00087 } 00088 void TextBoxGuiElement::CmdBackPanel::doSet( void* target, const String& val ) 00089 { 00090 std::vector<String> vec = StringUtil::split(val, "\t\n ", 1); 00091 00092 if (vec.size() < 2) 00093 { 00094 static_cast<TextBoxGuiElement*>(target)->setBackPanel(val, 5); 00095 } 00096 else 00097 { 00098 static_cast<TextBoxGuiElement*>(target)->setBackPanel(vec[0], StringConverter::parseInt(vec[1])); 00099 } 00100 00101 } 00102 //--------------------------------------------------------------------------------------------- 00103 String TextBoxGuiElement::CmdTextArea::doGet( const void* target ) const 00104 { 00105 return static_cast< const TextBoxGuiElement* >( target )->getTextAreaName(); 00106 } 00107 00108 //----------------------------------------------------------------------- 00109 void TextBoxGuiElement::CmdTextArea::doSet(void* target, const String& val) 00110 { 00111 std::vector<String> vec = StringUtil::split(val, "\t\n ", 1); 00112 00113 00114 if (vec.size() < 2) 00115 { 00116 static_cast<TextBoxGuiElement*>(target)->setTextArea(val, String("")); 00117 } 00118 else 00119 { 00120 static_cast<TextBoxGuiElement*>(target)->setTextArea(vec[0], vec[1]); 00121 } 00122 } 00123 00124 //----------------------------------------------------------------------- 00125 String TextBoxGuiElement::getTextAreaName() const 00126 { 00127 return mTextAreaTemplateName + " " + mCaption; 00128 } 00129 //--------------------------------------------------------------------------------------------- 00130 String TextBoxGuiElement::getBackPanelName() const 00131 { 00132 return mBackPanelTemplateName; 00133 } 00134 00135 //----------------------------------------------------------------------- 00136 void TextBoxGuiElement::setTextArea(const String& templateName, const String& name) 00137 { 00138 mTextAreaTemplateName = templateName; 00139 if (mTextArea) 00140 { 00141 removeChild(mTextArea->getName()); 00142 GuiManager::getSingleton().destroyGuiElement(mTextArea); 00143 mTextArea = NULL; 00144 } 00145 00146 mTextArea = static_cast<TextAreaGuiElement*> 00147 (GuiManager::getSingleton().createGuiElementFromTemplate(mTextAreaTemplateName, "", mName + "/textArea")); 00148 00149 mCaption = name; 00150 setCaptionToTextArea(); 00151 00152 // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned, 00153 // one copy when the children are copied, and another copy when setTextArea is set. 00154 mTextArea->setCloneable(false); 00155 mTextArea->setTop(0); 00156 mTextArea->setLeft(mTextArea->getSpaceWidth()/2); 00157 00158 if (mBackPanel) 00159 { 00160 // mBackPanel->setHeight(mTextArea->getCharHeight()); 00161 mBackPanel->addChild(mTextArea); 00162 } 00163 else 00164 { 00165 // The textarea was created first, so add it to backpanel when backpanel is created 00166 00167 } 00168 } 00169 //----------------------------------------------------------------------- 00170 void TextBoxGuiElement::setBackPanel(const String& templateName, int size) 00171 { 00172 mTextAreaSize = size; 00173 mBackPanelTemplateName = templateName; 00174 if (mBackPanel) 00175 { 00176 removeChild(mBackPanel->getName()); 00177 GuiManager::getSingleton().destroyGuiElement(mBackPanel); 00178 mBackPanel = NULL; 00179 } 00180 00181 mBackPanel = static_cast<GuiContainer*> 00182 (GuiManager::getSingleton().createGuiElementFromTemplate(mBackPanelTemplateName, "", mName + "/backPanel")); 00183 00184 // do not make this cloneable, otherwise there will be 2 copies of it when it is cloned, 00185 // one copy when the children are copied, and another copy when setTextArea is set. 00186 mBackPanel->setCloneable(false); 00187 mBackPanel->setDimensions(getWidth(),getHeight()); 00188 mBackPanel->setTop(0); 00189 mBackPanel->setLeft(0); 00190 mBackPanel->setWidth(getWidth()); 00191 00192 addChild(mBackPanel); 00193 if (mTextArea) 00194 { 00195 // mBackPanel->setHeight(mTextArea->getCharHeight()); 00196 mBackPanel->addChild(mTextArea); 00197 } 00198 else 00199 { 00200 // The BackPanel was created first, so add it to backpanel when textarea is created 00201 00202 } 00203 } 00204 00205 void TextBoxGuiElement::setCaption(const String& text) 00206 { 00207 mCaption = text; 00208 if (mTextArea != NULL) 00209 mTextArea->setCaption(text); 00210 } 00211 00212 const String& TextBoxGuiElement::getCaption(void) const 00213 { 00214 if (mTextArea != NULL) 00215 return mTextArea->getCaption(); 00216 return mCaption; 00217 } 00218 00219 00220 //--------------------------------------------------------------------------------------------- 00221 void TextBoxGuiElement::setCaptionToTextArea() 00222 { 00223 mTextArea->setCaption(mCaption); 00224 } 00225 //--------------------------------------------------------------------------------------------- 00226 void TextBoxGuiElement::processEvent(InputEvent* e) 00227 { 00228 PanelGuiElement::processEvent(e); 00229 00230 if (mTextArea) 00231 { 00232 if (!e->isConsumed()) 00233 { 00234 switch(e->getID()) 00235 { 00236 case KeyEvent::KE_KEY_PRESSED: 00237 KeyEvent* ke = static_cast<KeyEvent*> (e); 00238 00239 switch (ke->getKey()) 00240 { 00241 case KC_BACK : 00242 mCaption = mCaption.substr(0,mCaption.length() -1); 00243 setCaptionToTextArea(); 00244 break; 00245 00246 case KC_RETURN : 00247 if (mActionOnReturn) 00248 { 00249 ActionEvent* ae = new ActionEvent(this, ActionEvent::AE_ACTION_PERFORMED, 0, 0, mName); 00250 processActionEvent(ae); 00251 delete ae; 00252 } 00253 // fall through 00254 00255 default : { 00256 OgreChar newKey = ke->getKeyChar(); 00257 if (newKey != '\0') 00258 { 00259 Font* font = static_cast<Font*> (FontManager::getSingleton().getByName(mTextArea->getFontName())); 00260 if (!font) 00261 Except( Exception::ERR_ITEM_NOT_FOUND, "Could not find font " + mTextArea->getFontName(), 00262 "TextBoxGuiElement::processEvent" ); 00263 00264 if (font->getGlyphAspectRatio(newKey)*mTextArea->getCharHeight() + mTextArea->getWidth() < getWidth() - mTextArea->getSpaceWidth()) 00265 { 00266 mCaption += newKey; 00267 setCaptionToTextArea(); 00268 } 00269 } 00270 } 00271 break; 00272 } 00273 break; 00274 } 00275 } 00276 } 00277 } 00278 00279 } 00280
Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:49 2004