GLWTextBox.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <GLEXT/GLState.h>
00022 #include <GLW/GLWFont.h>
00023 #include <GLW/GLWTextBox.h>
00024 #include <GLW/GLWPanel.h>
00025 #include <common/Keyboard.h>
00026 #include <common/DefinesString.h>
00027 
00028 REGISTER_CLASS_SOURCE(GLWTextBox);
00029 
00030 GLWTextBox::GLWTextBox(float x, float y, float w, 
00031         const LangString &startText, unsigned int flags) :
00032         GLWidget(x, y, w, 25.0f), ctime_(0.0f),
00033         flags_(flags),
00034         cursor_(false), maxTextLen_(0), handler_(0),
00035         current_(true), allowUnicode_(true)
00036 {
00037         setText(startText);
00038 }
00039 
00040 GLWTextBox::~GLWTextBox()
00041 {
00042 
00043 }
00044 
00045 void GLWTextBox::simulate(float frameTime)
00046 {
00047         ctime_ += frameTime;
00048         if (ctime_ > 0.5f)
00049         {
00050                 ctime_ = 0.0f;
00051                 cursor_ = !cursor_;
00052         }
00053 }
00054 
00055 void GLWTextBox::draw()
00056 {
00057         GLWidget::draw();
00058         if (current_) glLineWidth(2.0f);
00059         glBegin(GL_LINE_LOOP);
00060                 drawShadedRoundBox(x_, y_, w_, h_, 10.0f, false);
00061         glEnd();
00062         if (current_) glLineWidth(1.0f);
00063 
00064         LangString text = text_;
00065         if (flags_ & eFlagPassword)
00066         {
00067                 text = LangString(text_.size(), '*');
00068         }
00069 
00070         GLWFont::instance()->getGameFont()->drawWidth(
00071                 w_,
00072                 GLWFont::widgetFontColor, 14,
00073                 x_ + 5.0f, y_ + 5.0f, 0.0f, 
00074                 (cursor_&&current_)?text + LANG_STRING("_"):text);
00075 }
00076 
00077 void GLWTextBox::keyDown(char *buffer, unsigned int keyState, 
00078                 KeyboardHistory::HistoryElement *history, int hisCount, 
00079                 bool &skipRest)
00080 {
00081         if (!current_) return;
00082 
00083         // By default swallow all text
00084         skipRest = true;
00085 
00086         for (int i=0; i<hisCount; i++)
00087         {
00088                 unsigned int unicodeKey = history[i].representedUnicode;
00089                 unsigned int dik = history[i].sdlKey;
00090                 if (dik == SDLK_BACKSPACE || dik == SDLK_DELETE)
00091                 {
00092                         if (!text_.empty())
00093                         {
00094                                 text_ = text_.substr(0, text_.length() - 1);
00095                                 if (handler_) handler_->textChanged(id_, text_);
00096                         }
00097                 }
00098                 else if (dik == SDLK_TAB)
00099                 {
00100                         // Find all text boxes in scope
00101                         unsigned int position = 0;
00102                         std::vector<GLWTextBox *> textBoxes;
00103                         std::list<GLWPanel::GLWPanelEntry> &widgets = parent_->getWidgets();
00104                         std::list<GLWPanel::GLWPanelEntry>::iterator itor;
00105                         for (itor = widgets.begin();
00106                                 itor != widgets.end();
00107                                 itor++)
00108                         {
00109                                 GLWPanel::GLWPanelEntry &entry = *itor;
00110                                 if (entry.widget->getMetaClassId() == getMetaClassId())
00111                                 {
00112                                         GLWTextBox *textBox = (GLWTextBox *) entry.widget;
00113                                         if (this == textBox) position = textBoxes.size();
00114                                         textBoxes.push_back(textBox);
00115                                 }
00116                         }
00117 
00118                         // Select the next text box
00119                         position++;
00120                         if (position >= textBoxes.size()) position = 0;
00121                         textBoxes[position]->setCurrent();
00122 
00123                         break;
00124                 }
00125                 else if (unicodeKey >= ' ' && (unicodeKey <= 127 || allowUnicode_))
00126                 {
00127                         if ((maxTextLen_==0) || ((int) text_.size() < maxTextLen_))
00128                         {
00129                                 text_ += unicodeKey;
00130                                 if (handler_) handler_->textChanged(id_, text_);
00131                         }
00132                 }
00133                 else 
00134                 {
00135                         skipRest = false; // Don't swallow return or escape
00136                 }
00137         }
00138 }
00139 
00140 std::string &GLWTextBox::getText() 
00141 {
00142         static std::string result; 
00143         result = LangStringUtil::convertFromLang(text_); 
00144         return result;
00145 }
00146 
00147 void GLWTextBox::mouseDown(int button, float x, float y, bool &skipRest)
00148 {
00149         if (inBox(x, y, x_, y_, w_, h_))
00150         {
00151                 skipRest = true;
00152                 setCurrent();
00153         }
00154 }
00155 
00156 void GLWTextBox::setText(const LangString &text)
00157 { 
00158         text_ = text; 
00159         if (!allowUnicode_)
00160         {
00161                 for (unsigned int *c=(unsigned int *) text.c_str(); *c; c++)
00162                 {
00163                         if (*c > 127) *c = '?';
00164                 }
00165         }
00166         if (handler_) handler_->textChanged(id_, text_);
00167 }
00168 
00169 void GLWTextBox::setParent(GLWPanel *parent)
00170 {
00171         GLWidget::setParent(parent);
00172         setCurrent();
00173 }
00174 
00175 void GLWTextBox::setCurrent()
00176 {
00177         std::list<GLWPanel::GLWPanelEntry> &widgets = parent_->getWidgets();
00178         std::list<GLWPanel::GLWPanelEntry>::iterator itor;
00179         for (itor = widgets.begin();
00180                 itor != widgets.end();
00181                 itor++)
00182         {
00183                 GLWPanel::GLWPanelEntry &entry = *itor;
00184                 if (entry.widget->getMetaClassId() == getMetaClassId())
00185                 {
00186                         GLWTextBox *textBox = (GLWTextBox *) entry.widget;
00187                         textBox->current_ = false;
00188                 }
00189         }
00190         current_ = true;
00191 }

Generated on Mon Feb 16 15:14:46 2009 for Scorched3D by  doxygen 1.5.3