-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathChatControl.xaml.cs
More file actions
203 lines (184 loc) · 6.96 KB
/
ChatControl.xaml.cs
File metadata and controls
203 lines (184 loc) · 6.96 KB
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
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using NLog;
using Torch;
using Sandbox;
using Sandbox.Engine.Multiplayer;
using Sandbox.Game.Gui;
using Sandbox.Game.Multiplayer;
using Sandbox.Game.World;
using Torch.API;
using Torch.API.Managers;
using Torch.API.Session;
using Torch.Managers;
using Torch.Server.Managers;
using VRage.Game;
namespace Torch.Server
{
/// <summary>
/// Interaction logic for ChatControl.xaml
/// </summary>
public partial class ChatControl : UserControl
{
private static Logger _log = LogManager.GetCurrentClassLogger();
private ITorchServer _server;
public ChatControl()
{
InitializeComponent();
this.IsVisibleChanged += OnIsVisibleChanged;
}
private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (IsVisible)
{
//I hate this and I hate myself. You should hate me too
Task.Run(() =>
{
Thread.Sleep(100);
Dispatcher.Invoke(() =>
{
Message.Focus();
Keyboard.Focus(Message);
});
});
}
}
public void BindServer(ITorchServer server)
{
_server = server;
server.Initialized += Server_Initialized ;
}
private void Server_Initialized(ITorchServer obj)
{
Dispatcher.InvokeAsync(() =>
{
ChatItems.Document.Blocks.Clear();
});
var sessionManager = _server.Managers.GetManager<ITorchSessionManager>();
if (sessionManager != null)
sessionManager.SessionStateChanged += SessionStateChanged;
}
private void SessionStateChanged(ITorchSession session, TorchSessionState state)
{
switch (state)
{
case TorchSessionState.Loading:
Dispatcher.InvokeAsync(() => ChatItems.Document.Blocks.Clear());
break;
case TorchSessionState.Loaded:
{
var chatMgr = session.Managers.GetManager<IChatManagerClient>();
if (chatMgr != null)
chatMgr.MessageRecieved += OnMessageRecieved;
}
break;
case TorchSessionState.Unloading:
{
var chatMgr = session.Managers.GetManager<IChatManagerClient>();
if (chatMgr != null)
chatMgr.MessageRecieved -= OnMessageRecieved;
}
break;
case TorchSessionState.Unloaded:
break;
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
private void OnMessageRecieved(TorchChatMessage msg, ref bool consumed)
{
InsertMessage(msg);
}
private static readonly Dictionary<string, Brush> _brushes = new Dictionary<string, Brush>();
private static Brush LookupBrush(string font)
{
if (_brushes.TryGetValue(font, out Brush result))
return result;
Brush brush = typeof(Brushes).GetField(font, BindingFlags.Static)?.GetValue(null) as Brush ?? Brushes.Blue;
_brushes.Add(font, brush);
return brush;
}
private void InsertMessage(TorchChatMessage msg)
{
if (Dispatcher.CheckAccess())
{
bool atBottom = ChatScroller.VerticalOffset + 8 > ChatScroller.ScrollableHeight;
// We'll maintain a single paragraph for all messages:
if (!(ChatItems.Document.Blocks.FirstBlock is Paragraph firstBlock))
{
firstBlock = new Paragraph();
ChatItems.Document.Blocks.Add(firstBlock);
}
var span = new Span();
span.Inlines.Add($"{msg.Timestamp} ");
switch (msg.Channel)
{
case ChatChannel.Faction:
span.Inlines.Add(new Run($"[{MySession.Static.Factions.TryGetFactionById(msg.Target)?.Tag ?? "???"}] ") { Foreground = Brushes.Green });
break;
case ChatChannel.Private:
span.Inlines.Add(new Run($"[to {MySession.Static.Players.TryGetIdentity(msg.Target)?.DisplayName ?? "???"}] ") { Foreground = Brushes.DeepPink });
break;
}
span.Inlines.Add(new Run(msg.Author) { Foreground = LookupBrush(msg.Font) });
span.Inlines.Add($": {msg.Message}");
span.Inlines.Add(new LineBreak());
firstBlock.Inlines.Add(span);
if (atBottom)
ChatScroller.ScrollToBottom();
}
else
Dispatcher.InvokeAsync(() => InsertMessage(msg));
}
private void SendButton_Click(object sender, RoutedEventArgs e)
{
OnMessageEntered();
}
private void Message_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
OnMessageEntered();
}
private void OnMessageEntered()
{
//Can't use Message.Text directly because of object ownership in WPF.
var text = Message.Text;
if (string.IsNullOrEmpty(text))
return;
var commands = _server.CurrentSession?.Managers.GetManager<Torch.Commands.CommandManager>();
if (commands != null && commands.IsCommand(text))
{
InsertMessage(new TorchChatMessage(TorchBase.Instance.Config.ChatName, text, TorchBase.Instance.Config.ChatColor));
_server.Invoke(() =>
{
if (!commands.HandleCommandFromServer(text, InsertMessage))
{
InsertMessage(new TorchChatMessage(TorchBase.Instance.Config.ChatName, "Invalid command.", TorchBase.Instance.Config.ChatColor));
return;
}
});
}
else
{
_server.CurrentSession?.Managers.GetManager<IChatManagerClient>().SendMessageAsSelf(text);
}
Message.Text = "";
}
}
}