Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cegui0.8_ogre1.9/src/orxonox/chat/ChatInputHandler.cc @ 12208

Last change on this file since 12208 was 11795, checked in by landauf, 7 years ago

merged ogre1.9 (including cegui0.8) into new branch

  • Property svn:eol-style set to native
File size: 12.3 KB
RevLine 
[6776]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Sandro 'smerkli' Merkli
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "ChatInputHandler.h"
[8079]30
31#include <cassert>
[9672]32#include <cstddef>
[8079]33#include <string>
[6776]34
[9675]35#if CEGUI_VERSION >= 0x000800
36#   include <CEGUI/Window.h>
37#   include <CEGUI/WindowManager.h>
38#   include <CEGUI/widgets/Listbox.h>
39#   include <CEGUI/widgets/ListboxItem.h>
40#   include <CEGUI/widgets/ListboxTextItem.h>
41#else
42#   include <CEGUIWindow.h>
43#   include <CEGUIWindowManager.h>
44#   include <elements/CEGUIListbox.h>
45#   include <elements/CEGUIListboxItem.h>
46#   include <elements/CEGUIListboxTextItem.h>
47#endif
48
[10624]49#include "core/singleton/ScopedSingletonIncludes.h"
[8079]50#include "core/CoreIncludes.h"
51#include "core/GUIManager.h"
[10624]52#include "core/command/ConsoleCommandIncludes.h"
[8079]53#include "core/input/InputBuffer.h"
54#include "core/input/InputManager.h"
55#include "core/input/InputState.h"
56
[8829]57#include "chat/ChatManager.h"
[8079]58#include "PlayerManager.h"
59#include "infos/PlayerInfo.h"
60
[7127]61namespace orxonox
[6776]62{
[6777]63  /* singleton */
[10624]64  ManageScopedSingleton( ChatInputHandler, ScopeID::GRAPHICS, false );
[6876]65
66  /* add commands to console */
[7284]67  SetConsoleCommand( "startchat", &ChatInputHandler::activate_static );
68  SetConsoleCommand( "startchat_small", &ChatInputHandler::activate_small_static );
[6777]69
[10624]70  RegisterAbstractClass(ChatInputHandler).inheritsFrom<ChatListener>();
71
[6776]72  /* constructor */
[6788]73  ChatInputHandler::ChatInputHandler()
[6776]74  {
[6777]75    /* register the object  */
76    RegisterObject(ChatInputHandler);
77
[6776]78    /* create necessary objects */
79    this->inpbuf = new InputBuffer();
[6876]80    this->disp_offset = 0;
[11071]81    assert( this->inpbuf != nullptr );
[6776]82
[6870]83    /* generate chatbox ui and chatbox-inputonly ui */
[6846]84    GUIManager::getInstance().loadGUI( "ChatBox" );
[6870]85    GUIManager::getInstance().loadGUI( "ChatBox-inputonly" );
[6846]86
[7043]87    /* setup colors */
88    setupColors();
89
[6788]90    /* configure the input buffer */
[6791]91    configureInputBuffer();
[6788]92
93    this->inputState = InputManager::getInstance().createInputState( "chatinput", false, false, InputStatePriority::Dynamic );
94    this->inputState->setKeyHandler(this->inpbuf);
[6776]95  }
96
[8079]97  ChatInputHandler::~ChatInputHandler()
98  {
99    /* Clean up */
100    InputManager::getInstance().destroyState("chatinput");
101    delete this->inpbuf;
102  }
103
[7043]104  /* configure input buffer, sub for the constructor */
[6776]105  void ChatInputHandler::configureInputBuffer()
106  {
[6910]107    /* INSTALL CALLBACKS */
[6846]108    /* input has changed */
[6788]109    this->inpbuf->registerListener(this, &ChatInputHandler::inputChanged, true);
[6846]110
111    /* add a line */
[6788]112    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\r',   false);
113    this->inpbuf->registerListener(this, &ChatInputHandler::addline,         '\n',   false);
[6776]114
[6846]115    /* backspace */
[6788]116    this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\b',   true);
[6890]117    //this->inpbuf->registerListener(this, &ChatInputHandler::backspace,       '\177', true);
[6776]118
[6846]119    /* exit the chatinputhandler thingy (tbd) */
[6788]120    this->inpbuf->registerListener(this, &ChatInputHandler::exit,            '\033', true); // escape
[6776]121
[6846]122    /* delete character */
[6788]123    this->inpbuf->registerListener(this, &ChatInputHandler::deleteChar,      KeyCode::Delete);
[6776]124
[6846]125    /* cursor movement */
[6788]126    this->inpbuf->registerListener(this, &ChatInputHandler::cursorRight,     KeyCode::Right);
127    this->inpbuf->registerListener(this, &ChatInputHandler::cursorLeft,      KeyCode::Left);
128    this->inpbuf->registerListener(this, &ChatInputHandler::cursorEnd,       KeyCode::End);
129    this->inpbuf->registerListener(this, &ChatInputHandler::cursorHome,      KeyCode::Home);
[6846]130
[6910]131    /* GET WINDOW POINTERS */
[9675]132#if CEGUI_VERSION >= 0x000800
[11795]133    //input = CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->getChild( "ChatBox/input" );
134    //const CEGUI::Window* root = CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->getChild("MenuRootWindow");
135    //std::cout << "Count: " << root->getChildCount() << std::endl;
136    //for (int i = 0; i < root->getChildCount(); ++i) {
137    //  std::cout << "Child: " << root->getChildAtIdx(i)->getName() << std::endl;
138    //}
139    for (auto iterator = CEGUI::WindowManager::getSingleton().getIterator(); !iterator.isAtEnd(); iterator++) {
140        CEGUI::Window* currentWindow = iterator.getCurrentValue();
141        if (currentWindow->getName().compare("ChatBox") == 0) {
142                input = currentWindow->getChild("frame")->getChild("input");
143                CEGUI::Window* history = currentWindow->getChild("frame")->getChild("history");
144                lb_history = dynamic_cast<CEGUI::Listbox*>(history);
145        } else if (currentWindow->getName().compare("ChatBox-inputonly") == 0) {
146                inputonly = currentWindow->getChild("input");
147        }
148    }
149
150    //input = CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->getChild( "ChatBox" )->getChild("frame")->getChild("input");
151    //inputonly = CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->getChild( "orxonox/ChatBox-inputonly/input" );
152    //CEGUI::Window *history = CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->getChild( "orxonox/ChatBox/history" );
[9675]153#else
[6846]154    input = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/input" );
[6870]155    inputonly = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox-inputonly/input" );
[6846]156    CEGUI::Window *history = CEGUI::WindowManager::getSingleton().getWindow( "orxonox/ChatBox/history" );
[6910]157
158    /* cast it to a listbox */
[7127]159    lb_history = dynamic_cast<CEGUI::Listbox*>(history);
[11795]160#endif
[6846]161
162    /* assert wee */
163    assert( lb_history );
[6776]164  }
165
[7043]166  /* setup the colors, sub for the constructor */
167  void ChatInputHandler::setupColors()
168  {
[7049]169    /* auto variables */
[7043]170    float red = 1.0, green = 0.5, blue = 0.5;
171    int i = 0;
[6777]172
[7049]173    // three loops: red tones, blue tones and green tones
[7043]174    // reds
175    for( i = 0; i < NumberOfColors/3; ++i )
[9675]176    { this->text_colors[ i ] = CEGUIColour( red, green, blue );
[7183]177      green += 0.2f, blue += 0.2f;
[7043]178    }
179
180    // greens
181    red = 0.5, green = 1, blue = 0.5;
182    for( ; i < NumberOfColors*2/3; ++i )
[9675]183    { this->text_colors[ i ] = CEGUIColour( red, green, blue );
[7183]184      red += 0.2f, blue += 0.2f;
[7043]185    }
186
[7127]187    // blues
[7043]188    red = 0.5, green = 0.5, blue = 1;
189    for( ; i < NumberOfColors; ++i )
[9675]190    { this->text_colors[ i ] = CEGUIColour( red, green, blue );
[7183]191      red += 0.2f, green += 0.2f;
[7043]192    }
193  }
194
195
[6777]196  /* activate, deactivate */
[6791]197  void ChatInputHandler::activate_static()
[6870]198  { ChatInputHandler::getInstance().activate( true ); }
199
200  void ChatInputHandler::activate_small_static()
201  { ChatInputHandler::getInstance().activate( false ); }
202
203  void ChatInputHandler::activate( bool full )
[6777]204  {
205    /* start listening */
[6788]206    InputManager::getInstance().enterState("chatinput");
[6846]207
208    /* MARK add spawning of chat widget stuff here.*/
[6870]209    if( full )
210      GUIManager::getInstance().showGUI( "ChatBox" );
211    else
212      GUIManager::getInstance().showGUI( "ChatBox-inputonly" );
[6879]213
214    this->fullchat = full;
[6777]215  }
216
[7127]217  void ChatInputHandler::deactivate()
[6777]218  {
219    /* stop listening */
[6788]220    InputManager::getInstance().leaveState("chatinput");
[6846]221
[6876]222    /* un-spawning of chat widget stuff */
[6846]223    GUIManager::getInstance().hideGUI( "ChatBox" );
[6870]224    GUIManager::getInstance().hideGUI( "ChatBox-inputonly" );
[6777]225  }
226
[6885]227
[7043]228  /* subs for incomingChat */
229  void ChatInputHandler::sub_setcolor( CEGUI::ListboxTextItem *tocolor,
230    std::string name )
231  {
[7049]232    /* sanity checks */
[11083]233    if (!tocolor)
234    {
235        orxout(internal_warning) << "Empty ListBoxTextItem given to "
236            "ChatInputhandler::sub_setcolor()." << endl;
237        return;
238    }
[7043]239
240    /* "hash" the name */
241    int hash = 0;
242    for( int i = name.length(); i > 0; --i )
243      hash += name[i-1];
244    hash = hash % this->NumberOfColors;
245
246    /* set the color according to the hash */
[8079]247    tocolor->setTextColours( this->text_colors[ hash ] );
[7043]248  }
249
250  /* handle incoming chat */
[8829]251  void ChatInputHandler::incomingChat(const std::string& message, const std::string& name)
[6876]252  {
[7043]253    /* create item */
[8829]254    CEGUI::ListboxTextItem *toadd = new CEGUI::ListboxTextItem( message );
[7043]255
256    /* setup colors */
[8826]257    if (name != "")
258      sub_setcolor( toadd, name );
[7043]259
260    /* now add */
[6885]261    this->lb_history->addItem( dynamic_cast<CEGUI::ListboxItem*>(toadd) );
[7127]262    this->lb_history->ensureItemIsVisible(
[7043]263      dynamic_cast<CEGUI::ListboxItem*>(toadd) );
[6885]264
[7043]265    /* make sure the history handles it */
[6885]266    this->lb_history->handleUpdatedItemData();
[7127]267  }
[6885]268
269
[7043]270  /* sub for inputchanged */
[7127]271  void ChatInputHandler::sub_adjust_dispoffset( int maxlen,
272    int cursorpos,
[6885]273    int inplen )
274  {
[6876]275    /* already start offsetting 5 characters before end */
276    if( cursorpos+5 > maxlen )
[7127]277    {
[6876]278      /* always stay 5 characters ahead of end, looks better */
279      ((disp_offset = cursorpos-maxlen+5) >= 0) ? 1 : disp_offset = 0;
280
281      /* enforce visibility of cursor */
282      (disp_offset > cursorpos ) ? disp_offset = 0 : 1;
283    }
[7127]284
[6876]285    /* make sure we don't die at substr */
286    if( inplen <= disp_offset ) disp_offset = 0;
287  }
288
[6776]289  /* callbacks for InputBuffer */
290  void ChatInputHandler::inputChanged()
291  {
[6846]292    /* update the cursor and the window */
293    std::string raw = this->inpbuf->get();
[6876]294    int cursorpos = this->inpbuf->getCursorPosition();
[7127]295
[6846]296    /* get string before cursor */
[6876]297    std::string left = raw.substr( 0, cursorpos );
[6837]298
[6846]299    /* see if there's a string after the cursor */
300    std::string right = "";
301    if( raw.length() >= left.length()+1 )
[6876]302      right = raw.substr( cursorpos );
[7127]303
[6846]304    /* set the text */
[6876]305    std::string assembled = "$ " + left + "|" + right;
306
[6879]307    if( this->fullchat )
[7127]308    {
[6879]309      /* adjust curser position - magic number 5 for font width */
[9675]310#if CEGUI_VERSION >= 0x000800
311      sub_adjust_dispoffset( (int)(this->input->getUnclippedInnerRect().get().getWidth()/6), cursorpos, assembled.length() );
312#else
313      sub_adjust_dispoffset( (int)(this->input->getUnclippedInnerRect().getWidth()/6), cursorpos, assembled.length() );
314#endif
[6879]315      this->input->setProperty( "Text", assembled.substr( disp_offset ) );
316    }
317    else
318    {
319      /* adjust curser position - magic number 5 for font width */
[9675]320#if CEGUI_VERSION >= 0x000800
321      sub_adjust_dispoffset( (int)(this->inputonly->getUnclippedInnerRect().get().getWidth()/6), cursorpos, assembled.length() );
322#else
323      sub_adjust_dispoffset( (int)(this->inputonly->getUnclippedInnerRect().getWidth()/6), cursorpos, assembled.length() );
324#endif
[6879]325      this->inputonly->setProperty( "Text", assembled.substr( disp_offset) );
326    }
[6876]327
328    /* reset display offset */
329    disp_offset = 0;
[6776]330  }
331
332  void ChatInputHandler::addline()
333  {
334    /* actually do send what was input */
335    /* a) get the string out of the inputbuffer */
[6788]336    std::string msgtosend = this->inpbuf->get();
[6776]337
[6846]338    if( msgtosend.length() == 0 )
339    { this->deactivate();
340      return;
341    }
342
[6776]343    /* b) clear the input buffer */
[6788]344    if (this->inpbuf->getSize() > 0)
345      this->inpbuf->clear();
[6776]346
347    /* c) send the chat via some call */
[8829]348    ChatManager::chat( msgtosend );
[6776]349
[6885]350    /* d) stop listening to input - only if this is not fullchat */
[6879]351    if( !this->fullchat )
352      this->deactivate();
[6846]353
[6776]354  }
355
356  void ChatInputHandler::backspace()
[6846]357  { this->inpbuf->removeBehindCursor(); }
[6776]358
359  void ChatInputHandler::deleteChar()
[6846]360  { this->inpbuf->removeAtCursor(); }
[6776]361
362  void ChatInputHandler::cursorRight()
[6846]363  { this->inpbuf->increaseCursor(); }
[7127]364
[6776]365  void ChatInputHandler::cursorLeft()
[6846]366  { this->inpbuf->decreaseCursor(); }
[7127]367
[6776]368  void ChatInputHandler::cursorEnd()
[6846]369  { this->inpbuf->setCursorToEnd(); }
[6776]370
371  void ChatInputHandler::cursorHome()
[6846]372  { this->inpbuf->setCursorToBegin(); }
[6776]373
374  void ChatInputHandler::exit()
[6879]375  {
376    /* b) clear the input buffer */
377    if (this->inpbuf->getSize() > 0)
378      this->inpbuf->clear();
[6776]379
[6879]380    /* d) stop listening to input  */
381    this->deactivate();
382  }
383
[6776]384}
Note: See TracBrowser for help on using the repository browser.