-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatControl.cs
190 lines (156 loc) · 5.42 KB
/
ChatControl.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using MetroFramework.Controls;
using MowChat.Data;
namespace MowChat
{
public sealed partial class ChatControl : MetroUserControl
{
private ChatChannel Channel { get; set; }
private int UnreadCount { get; set; }
public MetroTabPage TabPage { private get; set; }
private MetroTabControl _tabControl;
public MetroTabControl TabControl
{
private get { return _tabControl; }
set
{
_tabControl = value;
// Reset unread counter when opening the tab
value.SelectedIndexChanged += delegate
{
if (TabControl.SelectedTab == TabPage)
ResetUnreadCounter();
};
}
}
private static readonly Color[] FactionColors =
{
Color.FromArgb(0x999bc3), // 1 = Alliance
Color.FromArgb(0x8ba477), // 2 = Junta
Color.FromArgb(0xa5a4a4), // 3 = Empire
Color.FromArgb(0x60bbce), // 4 = Republic
Color.FromArgb(0xe06666), // 5 = Union
Color.FromArgb(0xca9e37) // 6 = Warlords
};
public ChatControl(ChatChannel channel)
{
InitializeComponent();
Channel = channel;
// Anchor (grow) in all directions
Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
// Get some chat history
if (channel.Messages == null || channel.Messages.Count == 0)
API.Instance.Get<ChatMessageList>(OnHistoryReceived, string.Format("chat/messages/{0}", Channel.Id));
else
OnHistoryReceived(channel.Messages);
// Receive messages from WebSync
Websync.Instance.Subscribe(channel.WebsyncChannel, OnMessageReceived);
// Events for sending chat message
sendButton.Click += (sender, args) => SendMessage();
chatText.KeyPress += (sender, args) =>
{
if (args.KeyChar != (char) Keys.Enter) return;
// Send the message
SendMessage();
// Prevent default handling of input
args.Handled = true;
};
}
private void SendMessage()
{
var text = chatText.Text;
chatText.Text = "";
// No callback needed since we'll receive the message via WebSync.
API.Instance.Post<ChatMessage>(null, string.Format("chat/channels/{0}", Channel.Id), new Dictionary<string, string>
{
{ "message", text }
});
}
private void OnMessageReceived(WebsyncMessage obj)
{
if (obj == null || obj.Type != "message" || obj.ChatMessages == null)
return;
Invoke((MethodInvoker) delegate
{
// Add messages to text box
foreach (var message in obj.ChatMessages)
AddMessage(message);
// If tab is not active, show unread indicator
if (TabControl.SelectedTab != TabPage)
IncreaseUnreadCounter();
});
}
private void IncreaseUnreadCounter()
{
TabPage.Text = string.Format("{0} ({1})", Channel.Name, ++UnreadCount);
}
private void ResetUnreadCounter()
{
UnreadCount = 0;
TabPage.Text = Channel.Name;
}
private void OnHistoryReceived(ChatMessageList list)
{
Invoke((MethodInvoker) (() => OnHistoryReceived(list.Records)));
}
private void OnHistoryReceived(List<ChatMessage> messages)
{
// Oldest is at the top oops
messages.Reverse();
foreach (var message in messages)
AddMessage(message);
}
private static Color GetFactionColor(IHasCharacterData message)
{
if (message.FactionId >= 1 && message.FactionId <= 6)
return FactionColors[message.FactionId - 1];
return Color.Black;
}
private void AddMessage(ChatMessage message)
{
if (messagesContainer.Text.Length > 0)
messagesContainer.AppendText(Environment.NewLine);
// Add message's sender to player store
PlayerStore.Instance.StorePlayer(message);
// If the chat message mentions the selected character, highlight the line
var highlighted = false;
var oldBackColor = messagesContainer.SelectionBackColor;
if (PlayerStore.TextContainsMe(message.Message))
{
highlighted = true;
messagesContainer.SelectionBackColor = Color.LightGray;
}
// Add the timestamp and sender.
var oldColor = messagesContainer.SelectionColor;
messagesContainer.AppendText(message.Date + " ");
messagesContainer.SelectionColor = GetFactionColor(message);
messagesContainer.SelectionFont = new Font(messagesContainer.Font, FontStyle.Bold);
messagesContainer.AppendText("[" + message.UserCharacterName + "]");
messagesContainer.SelectionFont = messagesContainer.Font;
messagesContainer.SelectionColor = oldColor;
messagesContainer.AppendText(" ");
// Loop over the sections of the text.
var sections = PlayerStore.Instance.FindPlayerReferences(message.Message);
foreach (var section in sections)
{
if (section.CharacterMention != null)
{
messagesContainer.SelectionColor = GetFactionColor(section.CharacterMention);
messagesContainer.SelectionFont = new Font(messagesContainer.Font, FontStyle.Bold);
}
messagesContainer.AppendText(section.Text);
messagesContainer.SelectionColor = oldColor;
messagesContainer.SelectionFont = messagesContainer.Font;
}
// Reset back color
if (highlighted)
messagesContainer.SelectionBackColor = oldBackColor;
// Scroll to end
messagesContainer.Select(messagesContainer.Text.Length, 0);
messagesContainer.ScrollToCaret();
}
}
}