00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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_&¤t_)?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
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
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
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;
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 }