summaryrefslogblamecommitdiff
path: root/src/gui/chat.cpp
blob: 731cc0d0f0da0678673036453e77eafbf16dae93 (plain) (tree)
1
  

















                                                                             

        
   
 
                 
 

                  
                                   
                          
 
                       
                      

                       
                          
                   
 
                        

                          
                              
                            
 
                                                   
              
 
                          
                                                                      
              
                    
 
                       

                                                               
 
                                

                                       
 

                                                       
                                            




                                                                       
                                 
 
                    
                   


                                                                      
                            

 
                         
 

                         

 
                        
 













                                                                         
 

                                                    
                                               
                                              


                           










                                        
                  
                                       
                               
     




























                                                                 














                                                        
 











                                                                                      

 
                                        
 
                                        

 

                                                   

                               


                                                   
                                                                        
                                                                   

                                           

                                     
                                    

                                             
                                                                  

                                                 

                                   
 
                                      
                                   


     
                                   

                                   
                                


                              




                                 

                                                        
 
                                       

                      
                                  
                              
                                                                
                                             
                            
           
                                   
                                                                         

          


                      
                            

     
                             
                      

                                        
                                          
 
 
                                                
 
                    

                                                                  



















                                              
                             


































                                                                              
                           












                                                    
 
 
                                              
 






                                                                       
         

                               

         






                                                                      
     
 





                                                    
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  The Mana World is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id$
 */

#include "chat.h"

#include <sstream>

#include <guichan/focushandler.hpp>
#include <guichan/key.hpp>

#include "browserbox.h"
#include "chatinput.h"
#include "scrollarea.h"

#include "../playerinfo.h"
#include "../log.h"

#include "../graphics.h"
extern Graphics *graphics;

#include "../net/messageout.h"
#include "../net/protocol.h"

ChatWindow::ChatWindow(const std::string &logfile):
    Window("")
{
    setWindowName("Chat");
    chatlog_file.open(logfile.c_str(), std::ios::out | std::ios::app);
    items = 0;
    items_keep = 20;

    setResizable(true);
    setDefaultSize(0, (graphics->getHeight() - 123), 600, 100);
    loadWindowState();

    chatInput = new ChatInput();
    chatInput->setEventId("chatinput");
    chatInput->addActionListener(this);

    textOutput = new BrowserBox(BrowserBox::AUTO_WRAP);
    textOutput->setOpaque(false);
    textOutput->disableLinksAndUserColors();
    scrollArea = new ScrollArea(textOutput);
    scrollArea->setPosition(
            scrollArea->getBorderSize(), scrollArea->getBorderSize());
    scrollArea->setScrollPolicy(
            gcn::ScrollArea::SHOW_NEVER, gcn::ScrollArea::SHOW_ALWAYS);
    scrollArea->setOpaque(false);

    add(scrollArea);
    add(chatInput);

    // Add key listener to chat input to be able to respond to up/down
    chatInput->addKeyListener(this);
    curHist = history.end();
}

ChatWindow::~ChatWindow()
{
    chatlog_file.flush();
    chatlog_file.close();
}

void ChatWindow::logic()
{
    chatInput->setPosition(
            chatInput->getBorderSize(),
            getContent()->getHeight() - chatInput->getHeight() -
                chatInput->getBorderSize());
    chatInput->setWidth(
            getContent()->getWidth() - 2 * chatInput->getBorderSize());

    scrollArea->setWidth(
            getContent()->getWidth() - 2 * scrollArea->getBorderSize());
    scrollArea->setHeight(
            getContent()->getHeight() - 2 * scrollArea->getBorderSize() -
                chatInput->getHeight() - 5);
    scrollArea->logic();
}

void ChatWindow::chat_log(std::string line, int own)
{
    // Delete overhead from the end of the list
    while ((int)chatlog.size() > items_keep) {
        chatlog.pop_back();
    }

    CHATLOG tmp;
    tmp.own  = own;
    tmp.nick = "";

    // Fix the owner of welcome message.
    if (line.substr(0, 7) == "Welcome")
    {
        own = BY_SERVER;
    }
    
    int pos = line.find(" : ");
    if (pos > 0) {
        tmp.nick = line.substr(0, pos);
        line.erase(0, pos + 3);
    }
        
    std::string lineColor = "##0"; // Equiv. to BrowserBox::BLACK
    switch (own) {
        case BY_GM:
            tmp.nick += std::string("Global announcement: ");
            lineColor = "##1"; // Equiv. to BrowserBox::RED
            break;
        case BY_PLAYER:
            tmp.nick += CAT_NORMAL;
            lineColor = "##2"; // Equiv. to BrowserBox::GREEN
            break;
        case BY_OTHER:
            tmp.nick += CAT_NORMAL;
            lineColor = "##4"; // Equiv. to BrowserBox::ORANGE
            break;
        case BY_SERVER:
            tmp.nick += std::string("Server: ");
            lineColor = "##7"; // Equiv. to BrowserBox::PINK
            break;
        case ACT_WHISPER:
            tmp.nick += CAT_WHISPER;
            lineColor = "##3"; // Equiv. to BrowserBox::BLUE
            break;
        case ACT_IS:
            tmp.nick += CAT_IS;
            lineColor = "##5"; // Equiv. to BrowserBox::YELLOW
            break;
    }
    
    // Get the current system time
    time_t t;
    time(&t);

    // Format the time string properly
    std::stringstream timeStr;
    timeStr << "[";
    timeStr << ((((t / 60) / 60) % 24 < 10) ? "0" : "");
    timeStr << (int)(((t / 60) / 60) % 24);
    timeStr << ":";
    timeStr << (((t / 60) % 60 < 10) ? "0" : "");
    timeStr << (int)((t / 60) % 60);
    timeStr << "] ";
    
    line = lineColor + timeStr.str() + tmp.nick + line;

    // We look if the Vertical Scroll Bar is set at the max before
    // adding a row, otherwise the max will always be a row higher
    // at comparison.
    if ( scrollArea->getVerticalScrollAmount() == scrollArea->getVerticalMaxScroll() )
    {
        textOutput->addRow(line);
        scrollArea->setVerticalScrollAmount(scrollArea->getVerticalMaxScroll());
    }
    else
    {
        textOutput->addRow(line);
    }
}

void ChatWindow::chat_log(CHATSKILL act)
{
    chat_log(const_msg(act), BY_SERVER);
}

void ChatWindow::action(const std::string& eventId)
{
    if (eventId == "chatinput")
    {
        std::string message = chatInput->getText();

        if (message.length() > 0) {
            // If message different from previous, put it in the history
            if (history.size() == 0 || message != history.back()) {
                history.push_back(message);
            }

            // Reset history iterator
            curHist = history.end();

            // Send the message to the server
            chat_send(player_info->name.c_str(), message.c_str());

            // Clear the text from the chat input
            chatInput->setText("");
        }

        // Remove focus and hide input
        mFocusHandler->focusNone();
    }
}

void ChatWindow::requestChatFocus()
{
    // Give focus to the chat input
    chatInput->setVisible(true);
    chatInput->requestFocus();
}

bool ChatWindow::isFocused()
{
    return chatInput->hasFocus();
}

void
ChatWindow::chat_send(std::string nick, std::string msg)
{
    short packetId = CMSG_CHAT_MESSAGE;

    // prepare command
    if (msg.substr(0, 1) == "/") {
        // global announcement
        /*if(msg.substr(0, IS_ANNOUNCE_LENGTH) == IS_ANNOUNCE) {
            msg.erase(0, IS_ANNOUNCE_LENGTH);
            packid = 0x0099;
        }*/
        // prepare ordinary message
        chat_log("Sorry but /commands are not available yet", BY_PLAYER);
    }
    else {
        nick += " : ";
        nick += msg;
        msg = nick;
        //packetId = 0x008c;
    }

    // send processed message
    MessageOut outMsg;
    outMsg.writeInt16(packetId);
    outMsg.writeInt16(msg.length() + 4);
    outMsg.writeString(msg, msg.length());
}

std::string ChatWindow::const_msg(CHATSKILL act)
{
    std::string msg;
    if (act.success == SKILL_FAILED && act.skill == SKILL_BASIC) {
        switch (act.bskill) {
            case BSKILL_TRADE :
                msg = "Trade failed!";
                break;
            case BSKILL_EMOTE :
                msg = "Emote failed!";
                break;
            case BSKILL_SIT :
                msg = "Sit failed!";
                break;
            case BSKILL_CREATECHAT :
                msg = "Chat creating failed!";
                break;
            case BSKILL_JOINPARTY :
                msg = "Could not join party!";
                break;
            case BSKILL_SHOUT :
                msg = "Cannot shout!";
                break;
        }

        switch (act.reason) {
            case RFAIL_SKILLDEP :
                msg += " You have not yet reached a high enough lvl!";
                break;
            case RFAIL_INSUFHP :
                msg += " Insufficient HP!";
                break;
            case RFAIL_INSUFSP :
                msg += " Insufficient SP!";
                break;
            case RFAIL_NOMEMO :
                msg += " You have no memos!";
                break;
            case RFAIL_SKILLDELAY :
                msg += " You cannot do that right now!";
                break;
            case RFAIL_ZENY :
                msg += " Seems you need more Zeny... ;-)";
                break;
            case RFAIL_WEAPON :
                msg += " You cannot use this skill with that kind of weapon!";
                break;
            case RFAIL_REDGEM :
                msg += " You need another red gem!";
                break;
            case RFAIL_BLUEGEM :
                msg += " You need another blue gem!";
                break;
            case RFAIL_OVERWEIGHT :
                msg += " You're carrying to much to do this!";
                break;
            default :
                msg += " Huh? What's that?";
                break;
        }
    } else {
        switch(act.skill) {
            case SKILL_WARP :
                msg = "Warp failed...";
                break;
            case SKILL_STEAL :
                msg = "Could not steal anything...";
                break;
            case SKILL_ENVENOM :
                msg = "Poison had no effect...";
                break;
        }
    }

    return msg;
}

void ChatWindow::keyPress(const gcn::Key &key)
{
    if (key.getValue() == key.DOWN && curHist != history.end())
    {
        // Move forward through the history
        std::list<std::string>::iterator prevHist = curHist++;
        if (curHist != history.end()) {
            chatInput->setText(*curHist);
            chatInput->setCaretPosition(chatInput->getText().length());
        }
        else {
            curHist = prevHist;
        }
    }
    else if (key.getValue() == key.UP && curHist != history.begin() &&
            history.size() > 0)
    {
        // Move backward through the history
        curHist--;
        chatInput->setText(*curHist);
        chatInput->setCaretPosition(chatInput->getText().length());
    }
}

void ChatWindow::setInputText(std::string input_str)
{
     chatInput->setText(input_str + " ");
     requestChatFocus();
}