1 /////////////////////////////////////////
2 //
3 //             OpenLieroX
4 //
5 // code under LGPL, based on JasonBs work,
6 // enhanced by Dark Charlie and Albert Zeyer
7 //
8 //
9 /////////////////////////////////////////
10 
11 
12 // Chat Box class
13 // Created 26/8/03
14 // Jason Boettcher
15 
16 // TODO: why is everything here so complicated? make it simpler! use one single list and not 3!
17 
18 
19 #include "LieroX.h"
20 
21 #include "StringUtils.h"
22 #include "CChatBox.h"
23 
24 #define MAX_LINES 200
25 
26 ///////////////////
27 // Clear the chatbox
Clear()28 void CChatBox::Clear()
29 {
30 	Lines.clear();
31 	NewLines.clear();
32 }
33 
34 
35 ///////////////////
36 // Add a line of text to the chat box
AddText(const std::string & txt,Color colour,TXT_TYPE TextType,const AbsTime & time)37 void CChatBox::AddText(const std::string& txt, Color colour, TXT_TYPE TextType, const AbsTime& time)
38 {
39 	if (txt.empty())
40 		return;
41 
42 	// Create a new line and copy the info
43 	line_t newline;
44 
45 	newline.fTime = time;
46 	newline.iColour = colour;
47 	newline.iTextType = TextType;
48 	newline.strLine = txt;
49 	newline.iID = Lines.size();
50 
51 	// Add to lines
52 	Lines.push_back(newline);
53 
54 	while(Lines.size() > MAX_LINES)
55 		Lines.pop_front();
56 
57 	// Add to new lines
58 	NewLines.push_back(newline);
59 }
60 
61 /////////////////////
62 // Get a new line from the chatbox
63 // WARNING: not threadsafe
GetNewLine(line_t & res)64 bool CChatBox::GetNewLine(line_t& res)
65 {
66 	if (NewLines.empty())
67 		return false;
68 
69 	res = *NewLines.begin();
70 	NewLines.erase(NewLines.begin());
71 
72 	return true;
73 }
74 
75 ////////////////////
76 // Convert the index to iterator
At(int i)77 lines_iterator CChatBox::At(int i)  {
78 	// Checks
79 	if (i <= 0)
80 		return Lines.begin();
81 
82 	if (i >= (int)Lines.size())
83 		return Lines.end();
84 
85 	// Go to the right iterator
86 	lines_iterator it = Lines.begin();
87 	while (i)  {
88 		it++;
89 		i--;
90 	}
91 
92 	return it;
93 }
94