diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-03-13 11:37:47 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-03-13 11:37:47 +0000 |
commit | cd2c4f5861c89f0925a1a706e38535d6c11cc3a0 (patch) | |
tree | 232273a1d853305deea6e3101dd7ceef9fb76930 /src/gui | |
parent | a03175439e5abaae8603a85be0db173daa543f6b (diff) | |
download | mana-client-cd2c4f5861c89f0925a1a706e38535d6c11cc3a0.tar.gz mana-client-cd2c4f5861c89f0925a1a706e38535d6c11cc3a0.tar.bz2 mana-client-cd2c4f5861c89f0925a1a706e38535d6c11cc3a0.tar.xz mana-client-cd2c4f5861c89f0925a1a706e38535d6c11cc3a0.zip |
Converted ChatBox into a proper ChatWindow in response to changes by Chetic.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/chat.cpp | 88 | ||||
-rw-r--r-- | src/gui/chat.h | 27 |
2 files changed, 71 insertions, 44 deletions
diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 82c808be..95f29dbd 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -22,29 +22,39 @@ */ #include "chat.h" +#include "textfield.h" #include "../graphics.h" +#include "../main.h" #include <iostream> -ChatBox::ChatBox(const char *logfile, int item_num) +ChatWindow::ChatWindow(const char *logfile, int item_num): + Window("") { chatlog_file.open(logfile, std::ios::out | std::ios::app); items = 0; items_keep = item_num; - - chatBoxBackground = Image::create(200, 200); - if (chatBoxBackground) { - chatBoxBackground->fillWithColor(255, 255, 255); - chatBoxBackground->setAlpha(0.7f); - } + + setSize(600, 100); + + chatInput = new TextField(); + chatInput->setPosition( + chatInput->getBorderSize(), + 100 - chatInput->getHeight() - chatInput->getBorderSize()); + chatInput->setWidth(600 - 2 * chatInput->getBorderSize()); + chatInput->setEventId("chatinput"); + chatInput->addActionListener(this); + add(chatInput); } -ChatBox::~ChatBox() +ChatWindow::~ChatWindow() { + delete chatInput; + chatlog_file.flush(); chatlog_file.close(); } -void ChatBox::chat_log(std::string line, int own) +void ChatWindow::chat_log(std::string line, int own) { int pos; CHATLOG tmp; @@ -78,7 +88,7 @@ void ChatBox::chat_log(std::string line, int own) // A try to get text sentences no too long... bool finished = false; - int maxLength = 80; + unsigned int maxLength = 80; while (!finished) { @@ -113,44 +123,31 @@ void ChatBox::chat_log(std::string line, int own) } } -void ChatBox::chat_log(CHATSKILL action) { +void ChatWindow::chat_log(CHATSKILL action) +{ chat_log(const_msg(action), BY_SERVER); } -void ChatBox::draw(gcn::Graphics *graphics) +void ChatWindow::draw(gcn::Graphics *graphics) { + // Draw the window border/background and children + Window::draw(graphics); + + // Draw the chat log int x, y; int n = 8; - int texty = getHeight() - 5, i = 0; + int texty = getHeight() - 5 - chatInput->getHeight() - + 2 * chatInput->getBorderSize(); + int i = 0; CHATLOG line; n -= 1; graphics->setColor(gcn::Color(203, 203, 203)); - graphics->drawLine(95, 5, 95, getHeight() - 5); - graphics->drawRectangle(gcn::Rectangle(0, 0, getWidth(), getHeight())); + graphics->drawLine(95, 5, 95, texty); getAbsolutePosition(x, y); - // Potentially recreate background with new size - if (chatBoxBackground) { - if ((chatBoxBackground->getWidth() != getWidth()) || - (chatBoxBackground->getHeight() != getHeight())) - { - delete chatBoxBackground; - chatBoxBackground = Image::create(getWidth(), getHeight()); - if (chatBoxBackground) { - chatBoxBackground->fillWithColor(255, 255, 255); - chatBoxBackground->setAlpha(0.7f); - } - } - } - - // Draw background image - if (chatBoxBackground) { - chatBoxBackground->draw(screen, x, y); - } - for (iter = chatlog.begin(); iter != chatlog.end(); iter++) { line = *iter; texty -= getFont()->getHeight() - 2; @@ -186,7 +183,25 @@ void ChatBox::draw(gcn::Graphics *graphics) } } -char *ChatBox::chat_send(std::string nick, std::string msg) +void ChatWindow::action(const std::string& eventId) +{ + if (eventId == "chatinput") { + std::string message = chatInput->getText(); + + if (message.length() > 0) { + chat_send(char_info[0].name, message.c_str()); + chatInput->setText(""); + } + } +} + +void ChatWindow::requestFocus() +{ + // Give focus to the chat input + chatInput->requestFocus(); +} + +char *ChatWindow::chat_send(std::string nick, std::string msg) { short packid = 0x008c; @@ -221,7 +236,8 @@ char *ChatBox::chat_send(std::string nick, std::string msg) return ""; } -std::string ChatBox::const_msg(CHATSKILL action) { +std::string ChatWindow::const_msg(CHATSKILL action) +{ std::string msg; if (action.success == SKILL_FAILED && action.skill == SKILL_BASIC) { switch (action.bskill) { diff --git a/src/gui/chat.h b/src/gui/chat.h index 39f9ab1c..3102e3bc 100644 --- a/src/gui/chat.h +++ b/src/gui/chat.h @@ -26,11 +26,12 @@ #include <guichan.hpp> #include "../resources/image.h" +#include "../net/network.h" +#include "window.h" #include <SDL.h> #include <list> #include <string> #include <fstream> -#include "../net/network.h" #define BY_GM 0 // those should be self-explanatory =) #define BY_PLAYER 1 @@ -96,17 +97,17 @@ struct CHATSKILL { /** * Simple chatlog object. */ -class ChatBox : public gcn::Widget { +class ChatWindow : public Window, public gcn::ActionListener { public: /** * Constructor. */ - ChatBox(const char *logfile, int item_num); + ChatWindow(const char *logfile, int item_num); /** * Destructor. */ - ~ChatBox(); + ~ChatWindow(); /* * Adds a line of text to our message list. Parameters: @@ -126,6 +127,16 @@ class ChatBox : public gcn::Widget { */ void draw(gcn::Graphics *graphics); + /** + * Performs action. + */ + void action(const std::string &actionId); + + /** + * Request focus. + */ + void requestFocus(); + /* * Determines wether to send a command or an ordinary message, then * contructs packets & sends them @@ -167,11 +178,11 @@ class ChatBox : public gcn::Widget { int items; int items_keep; - /** constructs failed messages for actions */ + /** Constructs failed messages for actions */ std::string const_msg(CHATSKILL); - - // The Alpha-Blended Surface for the background transluency effect. - Image *chatBoxBackground; + + /** Input box for chat messages */ + gcn::TextField *chatInput; }; #endif |