summaryrefslogtreecommitdiffstats
path: root/src/CompositeChat.h
blob: a0264d8646e24a1406a404bcbb2ff37c82a02f1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

// CompositeChat.h

// Declares the cCompositeChat class used to wrap a chat message with multiple parts (text, url, cmd)

#include "Defines.h"





// tolua_begin
/** Container for a single chat message composed of multiple functional parts.
Each part corresponds roughly to the behavior supported by the client messaging:
	- plain text, optionaly colorized / styled
	- clickable URLs
	- clickable commands (run)
	- clickable commands (suggest)
Each part has a text assigned to it that can be styled. The style is specified using a string,
each character / character combination in the string specifies the style to use:
	- b = bold
	- i = italic
	- u = underlined
	- s = strikethrough
	- o = obfuscated
	- @X = color X (X is 0 - 9 or a - f, same as dye meta
If the protocol version doesn't support all the features, it degrades gracefully.
*/
class cCompositeChat
{
public:
	// tolua_end
	
	enum ePartType
	{
		ptText,
		ptClientTranslated,
		ptUrl,
		ptRunCommand,
		ptSuggestCommand,
		ptShowAchievement,
	} ;
	
	class cBasePart
	{
	public:
		ePartType m_PartType;
		AString m_Text;
		AString m_Style;
		
		cBasePart(ePartType a_PartType, const AString & a_Text, const AString & a_Style = "");
		
		// Force a virtual destructor in descendants
		virtual ~cBasePart() {}
	} ;
	
	class cTextPart :
		public cBasePart
	{
		typedef cBasePart super;
	public:
		cTextPart(const AString & a_Text, const AString & a_Style = "");
	} ;
	
	class cClientTranslatedPart :
		public cBasePart
	{
		typedef cBasePart super;
	public:
		AStringVector m_Parameters;
		
		cClientTranslatedPart(const AString & a_TranslationID, const AStringVector & a_Parameters, const AString & a_Style = "");
	} ;
	
	class cUrlPart :
		public cBasePart
	{
		typedef cBasePart super;
	public:
		AString m_Url;
		
		cUrlPart(const AString & a_Text, const AString & a_Url, const AString & a_Style = "");
	} ;
	
	class cCommandPart :
		public cBasePart
	{
		typedef cBasePart super;
	public:
		AString m_Command;
		
		cCommandPart(ePartType a_PartType, const AString & a_Text, const AString & a_Command, const AString & a_Style = "");
	} ;
	
	class cRunCommandPart :
		public cCommandPart
	{
		typedef cCommandPart super;
	public:
		cRunCommandPart(const AString & a_Text, const AString & a_Command, const AString & a_Style = "");
	} ;
	
	class cSuggestCommandPart :
		public cCommandPart
	{
		typedef cCommandPart super;
	public:
		cSuggestCommandPart(const AString & a_Text, const AString & a_Command, const AString & a_Style = "");
	} ;

	class cShowAchievementPart :
		public cBasePart
	{
		typedef cBasePart super;
	public:
		AString m_PlayerName;
		cShowAchievementPart(const AString & a_PlayerName, const AString & a_Achievement, const AString & a_Style = "");
	} ;
	
	typedef std::vector<cBasePart *> cParts;
	
	// tolua_begin
	
	/** Creates a new empty chat message */
	cCompositeChat(void);
	
	/** Creates a new chat message and parses the text into parts.
	Recognizes "http:" and "https:" links and @color-codes.
	Uses ParseText() for the actual parsing. */
	cCompositeChat(const AString & a_ParseText, eMessageType a_MessageType = mtCustom);
	
	~cCompositeChat();
	
	/** Removes all parts from the object. */
	void Clear(void);
	
	// tolua_end

	// The following are exported in ManualBindings in order to support chaining - they return *this in Lua (#755)
	
	/** Adds a plain text part, with optional style.
	The default style is plain white text. */
	void AddTextPart(const AString & a_Message, const AString & a_Style = "");
	
	/** Adds a part that is translated client-side, with the formatting parameters and optional style. */
	void AddClientTranslatedPart(const AString & a_TranslationID, const AStringVector & a_Parameters, const AString & a_Style = "");
	
	// tolua_begin
	
	/** Adds a part that opens an URL when clicked.
	The default style is underlined light blue text. */
	void AddUrlPart(const AString & a_Text, const AString & a_Url, const AString & a_Style = "u@c");
	
	/** Adds a part that runs a command when clicked.
	The default style is underlined light green text. */
	void AddRunCommandPart(const AString & a_Text, const AString & a_Command, const AString & a_Style = "u@a");
	
	/** Adds a part that suggests a command (enters it into the chat message area, but doesn't send) when clicked.
	The default style is underlined yellow text. */
	void AddSuggestCommandPart(const AString & a_Text, const AString & a_SuggestedCommand, const AString & a_Style = "u@b");

	/** Adds a part that fully formats a specified achievement using client translatable strings
	Takes achievement name and player awarded to. Displays as {player} has earned the achievement {achievement_name}.
	*/
	void AddShowAchievementPart(const AString & a_PlayerName, const AString & a_Achievement, const AString & a_Style = "");
	
	/** Parses text into various parts, adds those.
	Recognizes "http:" and "https:" URLs and @color-codes. */
	void ParseText(const AString & a_ParseText);
	
	/** Sets the message type, which is indicated by prefixes added to the message when serializing. */
	void SetMessageType(eMessageType a_MessageType);
	
	/** Adds the "underline" style to each part that is an URL. */
	void UnderlineUrls(void);
	
	// tolua_begin

	/** Returns the message type set previously by SetMessageType(). */
	eMessageType GetMessageType(void) const { return m_MessageType; }
	
	/** Returns the text from the parts that comprises the human-readable data.
	Used for older protocols that don't support composite chat
	and for console-logging. */
	AString ExtractText(void) const;
	
	// tolua_end
	
	const cParts & GetParts(void) const { return m_Parts; }
	
	/** Converts the MessageType to a LogLevel value.
	Used by the logging bindings when logging a cCompositeChat object. */
	static cMCLogger::eLogLevel MessageTypeToLogLevel(eMessageType a_MessageType);
	
protected:
	/** All the parts that */
	cParts m_Parts;
	
	/** The message type, as indicated by prefixes. */
	eMessageType m_MessageType;
	
	
	/** Adds a_AddStyle to a_Style; overwrites the existing style if appropriate.
	If the style already contains something that a_AddStyle overrides, it is erased first. */
	void AddStyle(AString & a_Style, const AString & a_AddStyle);
} ;  // tolua_export