00001 /****************************************************************************** 00002 * Wormux is a convivial mass murder game. 00003 * Copyright (C) 2001-2004 Lawrence Azzoug. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00018 ****************************************************************************** 00019 * Chat in game session. 00020 * nefertum - Jon de Andres 00021 *****************************************************************************/ 00022 00023 #include "chat.h" 00024 #include "../graphic/text.h" 00025 #include "../game/time.h" 00026 #include <string> 00027 00028 Chat::~Chat(){ 00029 delete chat; 00030 } 00031 00032 Chat::Chat(){ 00033 chat = NULL; 00034 input = NULL; 00035 msg = NULL; 00036 check_input = 0; 00037 } 00038 00039 void Chat::Reset(){ 00040 if(chat == NULL) 00041 chat = new TextList(); 00042 } 00043 00044 void Chat::Show(){ 00045 uint now = Time::GetInstance()->ReadSec(); 00046 00047 if((now - last_time) >= MAXSECONDS){ 00048 chat->DeleteLine(); 00049 last_time = now; 00050 } 00051 00052 chat->Draw(XPOS, YPOS, HEIGHT); 00053 00054 if(check_input) 00055 ShowInput(); 00056 } 00057 00058 void Chat::ShowInput(){ 00059 check_input = 1; 00060 if(input == NULL){ 00061 input = new Text("", c_white); 00062 msg = new Text(SAY, c_red); 00063 } 00064 input->DrawTopLeft(50,500); 00065 msg->DrawTopLeft(25,500); 00066 } 00067 00068 int Chat::CheckInput(){ 00069 return check_input; 00070 } 00071 00072 void Chat::NewMessage(const std::string &msg){ 00073 if(!(chat->Size())){ 00074 uint now = Time::GetInstance()->ReadSec(); 00075 last_time = now; 00076 } 00077 00078 chat->AddText(msg, MAXLINES); 00079 } 00080 00081 00082 void Chat::HandleKey(const SDL_Event *event){ 00083 SDL_KeyboardEvent kbd_event = event->key; 00084 SDL_keysym key = kbd_event.keysym; 00085 std::string txt = input->GetText(); 00086 00087 switch(key.sym){ 00088 00089 case SDLK_RETURN: 00090 check_input = 0; //Hide input widget 00091 if(txt != "" ) 00092 network.SendChatMessage(txt); //Send 'txt' to other players 00093 input->Set(""); 00094 break; 00095 00096 case SDLK_BACKSPACE: 00097 if(kbd_event.state == 1 && txt != "") 00098 txt = txt.substr(0, txt.size()-1); 00099 input->Set(txt); 00100 break; 00101 00102 default: 00103 if(kbd_event.state == 1){ 00104 if(key.unicode < 0x80 && key.unicode > 0) 00105 txt = txt + (char)key.unicode; 00106 input->Set(txt); 00107 } 00108 } 00109 }