GLWTalkBox.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 <GLW/GLWTalkBox.h>
00022 #include <GLW/GLWFont.h>
00023 #include <GLW/GLWPanel.h>
00024 #include <GLW/GLWWindowManager.h>
00025 #include <GLEXT/GLState.h>
00026 #include <XML/XMLParser.h>
00027 #include <console/Console.h>
00028 #include <common/Defines.h>
00029 #include <client/ClientChannelManager.h>
00030 
00031 REGISTER_CLASS_SOURCE(GLWTalkBox);
00032 
00033 GLWTalkBox::GLWTalkBox() :
00034         ctime_(0.0f), cursor_(false), maxTextLen_(0),
00035         parentSized_(false), mode_(eSay), defaultMode_(eSay)
00036 {
00037 }
00038 
00039 GLWTalkBox::~GLWTalkBox()
00040 {
00041 }
00042 
00043 void GLWTalkBox::simulate(float frameTime)
00044 {
00045         ctime_ += frameTime;
00046         if (ctime_ > 0.5f)
00047         {
00048                 ctime_ = 0.0f;
00049                 cursor_ = !cursor_;
00050         }
00051 }
00052 
00053 void GLWTalkBox::draw()
00054 {
00055         if (parent_ && parentSized_)
00056         {
00057                 w_ = parent_->getW();
00058                 h_ = parent_->getH();
00059         }
00060 
00061         GLWidget::draw();
00062         glColor4f(0.4f, 0.6f, 0.8f, 0.6f);
00063 
00064         {
00065                 GLState currentStateBlend(GLState::BLEND_ON);
00066                 glBegin(GL_TRIANGLE_FAN);
00067                         glVertex2f(x_ + 20.0f, y_ + 5.0f);
00068                         glVertex2f(x_ + 20.0f, y_);
00069                         drawRoundBox(x_, y_, w_, h_, 10.0f);
00070                         glVertex2f(x_ + 20.0f, y_);
00071                 glEnd();
00072         }
00073 
00074         glColor3f(0.0f, 0.0f, 0.0f);
00075         glBegin(GL_LINE_LOOP);
00076                 drawRoundBox(x_, y_, w_, h_, 10.0f);
00077         glEnd();
00078 
00079         Vector white(0.9f, 0.9f, 1.0f);
00080         GLWFont::instance()->getGameFont()->drawWidthRhs(
00081                 w_ - 10.0f,
00082                 white, 12,
00083                 x_ + 5.0f, y_ + 5.0f, 0.0f, 
00084                 S3D::formatStringBuffer("%s: %s%s", 
00085                 ((mode_ == eSay)?"Say":"Team Say"), text_.c_str(), cursor_?" ":"_"));
00086 }
00087 
00088 void GLWTalkBox::keyDown(char *buffer, unsigned int keyState, 
00089                 KeyboardHistory::HistoryElement *history, int hisCount, 
00090                 bool &skipRest)
00091 {
00092         for (int i=0; i<hisCount; i++)
00093         {
00094                 unsigned int dik = history[i].sdlKey;
00095                 unsigned int unicodeChar = history[i].representedUnicode;
00096                 if (dik == SDLK_BACKSPACE || dik == SDLK_DELETE)
00097                 {
00098                         if (!text_.empty())
00099                         {
00100                                 text_ = text_.substr(0, text_.length() - 1);
00101                         }
00102                 }
00103                 else if (dik == SDLK_ESCAPE)
00104                 {
00105                         GLWWindowManager::instance()->hideWindow(parent_->getId());
00106                 }
00107                 else if (dik == SDLK_RETURN)
00108                 {
00109                         if (!text_.empty())
00110                         {
00111                                 for (unsigned int *text = (unsigned int *) text_.c_str();
00112                                         *text;
00113                                         text++)
00114                                 {
00115                                         if (*text == '#') *text = '\'';
00116                                 }
00117 
00118                                 ChannelText message("general", text_);
00119                                 ClientChannelManager::instance()->sendText(message);
00120 
00121                                 text_.clear();
00122                         }
00123                         GLWWindowManager::instance()->hideWindow(parent_->getId());
00124                 }
00125                 else if (unicodeChar >= ' ')
00126                 {
00127                         if ((maxTextLen_==0) || ((int) text_.size() < maxTextLen_))
00128                         {
00129                                 text_ += unicodeChar;
00130 
00131                                 if (text_[0] == '\\' || text_[0] == '/')
00132                                 {
00133                                         LangString part(&text_[1]);
00134 
00135                                         if (part == LANG_STRING("say ") ||
00136                                                 part == LANG_STRING("s "))
00137                                         {
00138                                                 text_.clear();
00139                                                 mode_ = eSay;
00140                                         }
00141                                         else if (part == LANG_STRING("team ") ||
00142                                                 part == LANG_STRING("t "))
00143                                         {
00144                                                 text_.clear();
00145                                                 mode_ = eTeamSay;
00146                                         }
00147                                 }
00148                         }
00149                 }
00150         }
00151         skipRest = true;
00152 }
00153 
00154 void GLWTalkBox::display()
00155 {
00156         mode_ = defaultMode_;
00157         text_.clear();
00158 }
00159 
00160 bool GLWTalkBox::initFromXML(XMLNode *node)
00161 {
00162         if (!GLWidget::initFromXML(node)) return false;
00163 
00164         if (!node->getNamedChild("parentsized", parentSized_)) return false;
00165 
00166         std::string mode;
00167         if (!node->getNamedChild("mode", mode)) return false;
00168         defaultMode_ = ((0 == strcmp(mode.c_str(), "say"))?eSay:eTeamSay);
00169 
00170         return true;
00171 }

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