Mana
Loading...
Searching...
No Matches
chattab.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2008-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * 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, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "gui/widgets/chattab.h"
23
24#include "actorspritemanager.h"
25#include "chatlogger.h"
26#include "client.h"
27#include "commandhandler.h"
28#include "configuration.h"
29#include "localplayer.h"
30#include "sound.h"
31
32#include "gui/recorder.h"
33
37
38#include "net/chathandler.h"
39#include "net/net.h"
40
41#include "resources/iteminfo.h"
42#include "resources/itemdb.h"
43
44#include "utils/gettext.h"
45#include "utils/stringutils.h"
46
47#include <guichan/widgets/tabbedarea.hpp>
48
49ChatTab::ChatTab(const std::string &name)
50{
51 setCaption(name);
52
57
59 mScrollArea->setScrollPolicy(gcn::ScrollArea::SHOW_NEVER,
60 gcn::ScrollArea::SHOW_ALWAYS);
61 mScrollArea->setScrollAmount(0, 1);
62 mScrollArea->setOpaque(false);
63
64 chatWindow->addTab(this);
65
67
68 // Initiate the text format
70}
71
73{
74 if (chatWindow)
75 chatWindow->removeTab(this);
76
77 delete mTextOutput;
78 delete mScrollArea;
79}
80
82{
83 if (alpha > 200)
84 {
87 }
88 else if (alpha <= 200 && alpha > 100)
89 {
92 }
93 else
94 {
97 }
98}
99
100void ChatTab::event(Event::Channel channel, const Event &event)
101{
102 // Update the text outline and shadow according to the gui opacity.
103 if (channel == Event::ConfigChannel &&
104 event.getType() == Event::ConfigOptionChanged &&
105 event.hasValue(&Config::guiAlpha))
106 {
108 }
109}
110
111void ChatTab::chatLog(std::string line, Own own, bool ignoreRecord)
112{
113 // Trim whitespace
114 trim(line);
115
116 if (line.empty())
117 return;
118
119 CHATLOG tmp;
120 tmp.own = own;
121 tmp.text = line;
122
123 std::string::size_type pos = line.find(" : ");
124 if (pos != std::string::npos)
125 {
126 tmp.nick = line.substr(0, pos);
127 tmp.text = line.substr(pos + 3);
128 }
129 else
130 {
131 // Fix the owner of welcome message.
132 if (line.substr(0, 7) == "Welcome")
133 {
134 own = BY_SERVER;
135 }
136 }
137
138 // *implements actions in a backwards compatible way*
139 if ((own == BY_PLAYER || own == BY_OTHER) &&
140 tmp.text.at(0) == '*' &&
141 tmp.text.at(tmp.text.length()-1) == '*')
142 {
143 tmp.text[0] = ' ';
144 tmp.text.erase(tmp.text.length() - 1);
145 own = ACT_IS;
146 }
147
148 std::string lineColor = "##C";
149 switch (own)
150 {
151 case BY_GM:
152 if (tmp.nick.empty())
153 {
154 tmp.nick = std::string(_("Global announcement:"));
155 tmp.nick += " ";
156 lineColor = "##G";
157 }
158 else
159 {
160 tmp.nick = strprintf(_("Global announcement from %s:"),
161 tmp.nick.c_str());
162 tmp.nick += " ";
163 lineColor = "##g";
164 }
165 break;
166 case BY_PLAYER:
167 tmp.nick += ": ";
168 lineColor = "##Y";
169 break;
170 case BY_OTHER:
171 tmp.nick += ": ";
172 lineColor = "##C";
173 break;
174 case BY_SERVER:
175 tmp.nick = _("Server:");
176 tmp.nick += " ";
177 tmp.text = line;
178 lineColor = "##S";
179 break;
180 case BY_CHANNEL:
181 tmp.nick.clear();
182 // TODO: Use a predefined color
183 lineColor = "##2"; // Equiv. to BrowserBox::GREEN
184 break;
185 case ACT_WHISPER:
186 tmp.nick = strprintf(_("%s whispers: %s"), tmp.nick.c_str(), "");
187 lineColor = "##W";
188 break;
189 case ACT_IS:
190 lineColor = "##I";
191 break;
192 case BY_LOGGER:
193 tmp.nick.clear();
194 tmp.text = line;
195 lineColor = "##L";
196 break;
197 }
198
199 if (tmp.nick == ": ")
200 {
201 tmp.nick.clear();
202 lineColor = "##S";
203 }
204
205 // Get the current system time
206 time_t t;
207 time(&t);
208
209 // Format the time string properly
210 std::stringstream timeStr;
211 timeStr << "[" << ((((t / 60) / 60) % 24 < 10) ? "0" : "")
212 << (int) (((t / 60) / 60) % 24)
213 << ":" << (((t / 60) % 60 < 10) ? "0" : "")
214 << (int) ((t / 60) % 60)
215 << "] ";
216
217 line = lineColor + timeStr.str() + tmp.nick + tmp.text;
218
219 if (config.enableChatLog && !ignoreRecord)
220 saveToLogFile(line);
221
222 // We look if the Vertical Scroll Bar is set at the max before
223 // adding a row, otherwise the max will always be a row higher
224 // at comparison.
225 if (mScrollArea->getVerticalScrollAmount() >= mScrollArea->getVerticalMaxScroll())
226 {
227 mTextOutput->addRow(line);
228 mScrollArea->setVerticalScrollAmount(mScrollArea->getVerticalMaxScroll());
229 }
230 else
231 {
232 mTextOutput->addRow(line);
233 }
234
236
237 chatWindow->mRecorder->record(line.substr(3));
238
239 if (own != BY_PLAYER)
240 {
241 bool currentTab = getTabbedArea()->getSelectedTab() == this;
242
243 if (!currentTab)
244 setFlash(true);
245
246 if (!(currentTab && Client::hasInputFocus()) && own != BY_SERVER)
247 if (checkNotify(own))
248 sound.playNotification("system/newmessage.ogg");
249 }
250}
251
252void ChatTab::chatLog(const std::string &nick, const std::string &msg)
253{
254 chatLog(nick + " : " + msg,
255 nick == local_player->getName() ? BY_PLAYER : BY_OTHER,
256 false);
257}
258
259void ChatTab::chatInput(const std::string &message)
260{
261 std::string msg = message;
262 trim(msg);
263
264 if (msg.empty())
265 return;
266
267 // Check for item link
268 std::string::size_type start = msg.find('[');
269 while (start != std::string::npos && msg[start+1] != '@')
270 {
271 std::string::size_type end = msg.find(']', start);
272 if (start + 1 != end && end != std::string::npos)
273 {
274 // Catch multiple embeds and ignore them
275 // so it doesn't crash the client.
276 while ((msg.find('[', start + 1) != std::string::npos) &&
277 (msg.find('[', start + 1) < end))
278 {
279 start = msg.find('[', start + 1);
280 }
281
282 std::string temp = msg.substr(start + 1, end - start - 1);
283
284 const ItemInfo &itemInfo = itemDb->get(temp);
285 if (itemInfo.id != 0)
286 {
287 msg.insert(end, "@@");
288 msg.insert(start + 1, "|");
289 msg.insert(start + 1, toString(itemInfo.id));
290 msg.insert(start + 1, "@@");
291 }
292 }
293 start = msg.find('[', start + 1);
294 }
295
296 // Prepare ordinary message
297 if (msg[0] != '/')
298 handleInput(msg);
299 else
300 handleCommand(std::string(msg, 1));
301}
302
303void ChatTab::scroll(int amount)
304{
305 int range = mScrollArea->getHeight() / 8 * amount;
306 gcn::Rectangle scr;
307 scr.y = mScrollArea->getVerticalScrollAmount() + range;
308 scr.height = abs(range);
309 mTextOutput->showPart(scr);
310}
311
313{
315}
316
317void ChatTab::handleInput(const std::string &msg)
318{
320}
321
322void ChatTab::handleCommand(const std::string &msg)
323{
324 commandHandler->handleCommand(msg, this);
325}
326
328{
329 return own == ACT_WHISPER;
330}
331
332void ChatTab::getAutoCompleteList(std::vector<std::string> &names) const
333{
335}
336
337void ChatTab::saveToLogFile(std::string &msg)
338{
339 if (chatLogger)
340 chatLogger->log(msg);
341}
ActorSpriteManager * actorSpriteManager
Definition game.cpp:110
ChatLogger * chatLogger
Chat log object.
Definition client.cpp:100
Own
Definition chatwindow.h:46
@ BY_GM
Definition chatwindow.h:47
@ BY_CHANNEL
Definition chatwindow.h:51
@ ACT_WHISPER
Definition chatwindow.h:52
@ ACT_IS
Definition chatwindow.h:53
@ BY_LOGGER
Definition chatwindow.h:54
@ BY_OTHER
Definition chatwindow.h:49
@ BY_PLAYER
Definition chatwindow.h:48
@ BY_SERVER
Definition chatwindow.h:50
AutoCompleteLister * getPlayerNPCNameLister() const
virtual void getAutoCompleteList(std::vector< std::string > &) const
Definition textfield.h:63
const std::string & getName() const
Returns the name of the being.
Definition being.h:190
A simple browser box able to handle links and forward events to the parent conteiner.
Definition browserbox.h:74
void setOutlinedText(bool outline)
Sets whether the font will use a shadow for text.
Definition browserbox.h:110
void setShadowedText(bool shadows)
Sets whether the font will use a shadow for text.
Definition browserbox.h:105
void setMaxRows(unsigned maxRows)
Sets the maximum numbers of rows in the browser box.
Definition browserbox.h:115
void addRow(std::string_view row)
Adds a text row to the browser.
void setLinkHandler(LinkHandler *handler)
Sets the handler for links.
Definition browserbox.h:88
void clearRows()
Remove all rows.
@ AUTO_WRAP
Maybe it needs a fix or to be redone.
Definition browserbox.h:79
void setWrapIndent(int indent)
Sets the wrap indent for the browser box.
Definition browserbox.h:100
void log(std::string str)
Enters a message in the log.
void chatLog(std::string line, Own own=BY_SERVER, bool ignoreRecord=false)
Adds a line of text to our message list.
Definition chattab.cpp:111
ChatTab(const std::string &name)
Definition chattab.cpp:49
virtual void handleInput(const std::string &msg)
Definition chattab.cpp:317
virtual bool handleCommand(const std::string &type, const std::string &args)
Handle special commands.
Definition chattab.h:94
~ChatTab() override
Definition chattab.cpp:72
ScrollArea * mScrollArea
Definition chattab.h:130
void updateTextFormat(int alpha)
Adapts the text format to the current gui opacity, for better readability.
Definition chattab.cpp:81
virtual void saveToLogFile(std::string &msg)
Definition chattab.cpp:337
void scroll(int amount)
Scrolls the chat window.
Definition chattab.cpp:303
void getAutoCompleteList(std::vector< std::string > &names) const override
Definition chattab.cpp:332
void event(Event::Channel channel, const Event &event) override
Definition chattab.cpp:100
void chatInput(const std::string &msg)
Determines whether the message is a command or message, then sends the given message to the game serv...
Definition chattab.cpp:259
void clearText()
Clears the text from the tab.
Definition chattab.cpp:312
BrowserBox * mTextOutput
Definition chattab.h:131
virtual bool checkNotify(Own own) const
Returns whether a notify sound may be played for the given type of message.
Definition chattab.cpp:327
ItemLinkHandler * mItemLinkHandler
Used for showing item popup on clicking links.
Definition chatwindow.h:186
void removeTab(ChatTab *tab)
Remove the given tab from the window.
Recorder * mRecorder
Definition chatwindow.h:187
void addTab(ChatTab *tab)
Add the tab to the window.
static bool hasInputFocus()
Definition client.cpp:1276
void handleCommand(const std::string &command, ChatTab *tab=localChatTab)
Parse and handle the given command.
void listen(Event::Channel channel)
Definition event.h:42
@ ConfigOptionChanged
Definition event.h:68
Channel
Definition event.h:45
@ ConfigChannel
Definition event.h:51
const ItemInfo & get(int id) const
Definition itemdb.cpp:145
Defines a class for storing generic item infos.
Definition iteminfo.h:100
int id
Item ID.
Definition iteminfo.h:112
virtual void talk(const std::string &text)=0
void record(const std::string &msg)
Outputs the message to the recorder file.
Definition recorder.cpp:62
A scroll area.
Definition scrollarea.h:38
void logic() override
Logic function optionally adapts width or height of contents.
void setOpaque(bool opaque)
Sets whether the widget should draw its background or not.
void playNotification(const std::string &path)
Plays a sound on the notification channel.
Definition sound.cpp:283
void setCaption(const std::string &caption)
Sets the caption of the tab.
Definition tab.cpp:49
void setFlash(bool flash)
Set tab flashing state.
Definition tab.cpp:122
static int getGuiAlpha()
Gets the alpha value used by the window, in a Guichan usable format.
Definition window.cpp:707
Config config
Global settings (config.xml)
Definition client.cpp:97
Sound sound
Definition client.cpp:109
ItemDB * itemDb
Items info database.
Definition client.cpp:106
CommandHandler * commandHandler
Definition game.cpp:112
ChatWindow * chatWindow
Definition game.cpp:93
#define _(s)
Definition gettext.h:38
LocalPlayer * local_player
ChatHandler * getChatHandler()
Definition net.cpp:70
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.
std::string & trim(std::string &str)
Trims spaces off the end and the beginning of the given string.
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68
One item in the chat log.
Definition chatwindow.h:59
Own own
Definition chatwindow.h:62
std::string text
Definition chatwindow.h:61
std::string nick
Definition chatwindow.h:60
float guiAlpha
bool enableChatLog
int chatLogLength