-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
executable file
·361 lines (301 loc) · 12.8 KB
/
MainWindow.xaml.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
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.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
using NeuronDataReaderWraper;
using Newtonsoft.Json;
using WebSocketSharp;
using WebSocketSharp.Server;
namespace demo_cs
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
int _frameCount;
SocketStatusChanged _SocketStatusChanged;
FrameDataReceived _DataReceived;
FrameDataReceived _CalcDataReceived;
BvhDataHeader _bvhHeader;
private float[] _valuesBuffer = new float[354];
CalcDataHeader _calcHeader;
private float[] _valuesBufferCalc = new float[338];
IntPtr sockTCPRef = IntPtr.Zero;
IntPtr sockUDPRef = IntPtr.Zero;
private WebSocketServer wssv;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) //在程序加载期间做一些初始化操作
{
_DataReceived = new FrameDataReceived(bvhDataReceived);
NeuronDataReader.BRRegisterFrameDataCallback(IntPtr.Zero, _DataReceived);
_CalcDataReceived = new FrameDataReceived(calcDataReceived);
NeuronDataReader.BRRegisterCalculationDataCallback(IntPtr.Zero, _CalcDataReceived);
_SocketStatusChanged = new SocketStatusChanged(socketStatusChanged);
NeuronDataReader.BRRegisterSocketStatusCallback(IntPtr.Zero, _SocketStatusChanged);
// 更新接收BVH数据时的BoneID界面UI
{
cbBoneID.Items.Clear();
for (int i = 0; i < 59; i++)
{
string Bone = "Bone";
Bone += i.ToString();
Bone += "\n";
cbBoneID.Items.Add(Bone);
}
cbBoneID.SelectedIndex = 0;
}
// 更新接收Calc数据时的BoneID界面UI
{
cbBoneID2.Items.Clear();
for (int i = 0; i < 21; i++)
{
string Bone = "Bone";
Bone += i.ToString();
Bone += "\n";
cbBoneID2.Items.Add(Bone);
}
cbBoneID2.SelectedIndex = 0;
}
}
private void Window_Closed(object sender, EventArgs e)
{
if (sockTCPRef != IntPtr.Zero)
{
NeuronDataReader.BRCloseSocket(sockTCPRef);
sockTCPRef = IntPtr.Zero;
}
if (sockUDPRef != IntPtr.Zero)
{
NeuronDataReader.BRCloseSocket(sockUDPRef);
sockUDPRef = IntPtr.Zero;
}
}
public class Laputa : WebSocketBehavior
{
protected override void OnMessage(MessageEventArgs e)
{
Console.WriteLine(e.Data);
}
}
private void ButtonConnect_Click(object sender, RoutedEventArgs e)
{
if (NeuronDataReader.BRGetSocketStatus(sockTCPRef) == SocketStatus.CS_Running)
{
NeuronDataReader.BRCloseSocket(sockTCPRef);
sockTCPRef = IntPtr.Zero;
btnConnect.Content = "Connect";
}
else
{
sockTCPRef = NeuronDataReader.BRConnectTo(txtIP.Text, int.Parse(txtPort.Text));
if (sockTCPRef == IntPtr.Zero)
{
string msg = NeuronDataReader.strBRGetLastErrorMessage();
MessageBox.Show(msg, "Connection error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
btnConnect.Content = "Disconnect";
wssv = new WebSocketServer(9000);
wssv.AddWebSocketService<Laputa>("/service");
wssv.Start();
//wssv.Stop();
//serverSocket.Bind(new IPEndPoint(IPAddress.Any, 9000));
//serverSocket.Listen(128);
//serverSocket.BeginAccept(null, 0, OnAccept, null);
_frameCount = 0;
}
}
private void UpdateCalcDataUI(float[] calcData, int boneId)
{
if (boneId > 20 || boneId < 0) return;
tbp_x.Text = calcData[boneId * 16 + 0].ToString();
tbp_y.Text = calcData[boneId * 16 + 1].ToString();
tbp_z.Text = calcData[boneId * 16 + 2].ToString();
tbv_x.Text = calcData[boneId * 16 + 3].ToString();
tbv_y.Text = calcData[boneId * 16 + 4].ToString();
tbv_z.Text = calcData[boneId * 16 + 5].ToString();
tbq_w.Text =calcData[boneId * 16 + 6].ToString();
tbq_x.Text = calcData[boneId * 16 + 7].ToString();
tbq_y.Text = calcData[boneId * 16 + 8].ToString();
tbq_z.Text = calcData[boneId * 16 + 9].ToString();
tba_x.Text = calcData[boneId * 16 + 10].ToString();
tba_y.Text = calcData[boneId * 16 + 11].ToString();
tba_z.Text = calcData[boneId * 16 + 12].ToString();
tbg_x.Text = calcData[boneId * 16 + 13].ToString();
tbg_y.Text = calcData[boneId * 16 + 14].ToString();
tbg_z.Text = calcData[boneId * 16 + 15].ToString();
}
private void UpdateBvhDataUI(float[] bvhData, int boneId, bool withDisp)
{
if (withDisp)
{
tbdisp_x.Text = bvhData[boneId * 6 + 0].ToString();
tbdisp_y.Text = bvhData[boneId * 6 + 1].ToString();
tbdisp_z.Text = bvhData[boneId * 6 + 2].ToString();
tbrt_x.Text = bvhData[boneId * 6 + 3].ToString();
tbrt_y.Text = bvhData[boneId * 6 + 4].ToString();
tbrt_z.Text = bvhData[boneId * 6 + 5].ToString();
}
else
{
if (boneId == 0)
{
tbdisp_x.Text = bvhData[boneId * 6 + 0].ToString();
tbdisp_y.Text = bvhData[boneId * 6 + 1].ToString();
tbdisp_z.Text = bvhData[boneId * 6 + 2].ToString();
tbrt_x.Text = bvhData[boneId * 6 + 3].ToString();
tbrt_y.Text = bvhData[boneId * 6 + 4].ToString();
tbrt_z.Text = bvhData[boneId * 6 + 5].ToString();
}
else
{
tbrt_x.Text = bvhData[3 + boneId * 3 + 0].ToString();
tbrt_y.Text = bvhData[3 + boneId * 3 + 1].ToString();
tbrt_z.Text = bvhData[3 + boneId * 3 + 2].ToString();
tbdisp_x.Text = "0";
tbdisp_y.Text = "0";
tbdisp_z.Text = "0";
}
}
}
private void calcDataReceived(IntPtr customObject, IntPtr sockRef, IntPtr header, IntPtr data)
{
_calcHeader = (CalcDataHeader)Marshal.PtrToStructure(header, typeof(CalcDataHeader));
// Change the buffer length if necessary
if (_calcHeader.DataCount != _valuesBufferCalc.Length)
{
_valuesBufferCalc = new float[_calcHeader.DataCount];
}
Marshal.Copy(data, _valuesBufferCalc, 0, (int)_calcHeader.DataCount);
if (sockRef == this.sockTCPRef)
{
_frameCount++;
this.Dispatcher.BeginInvoke(new Action(delegate()
{
int index = cbBoneID2.SelectedIndex;
UpdateCalcDataUI(_valuesBufferCalc, index);
txtLog.Text = _frameCount.ToString();
}));
}
if (sockRef == this.sockUDPRef)
{
_frameCount++;
this.Dispatcher.BeginInvoke(new Action(delegate()
{
int index = cbBoneID2.SelectedIndex;
UpdateCalcDataUI(_valuesBufferCalc, index);
txtLog.Text = _frameCount.ToString();
}));
}
}
private void bvhDataReceived(IntPtr customObject, IntPtr sockRef, IntPtr header, IntPtr data)
{
_bvhHeader = (BvhDataHeader)Marshal.PtrToStructure(header, typeof(BvhDataHeader));
// Change the buffer length if necessary
if (_bvhHeader.DataCount != _valuesBuffer.Length)
{
_valuesBuffer = new float[_bvhHeader.DataCount];
}
Marshal.Copy(data, _valuesBuffer, 0, (int)_bvhHeader.DataCount);
//_valuesBuffer = (float[])Marshal.PtrToStructure(data, typeof(float[_bvhHeader.DataCount]));
if (sockRef == this.sockTCPRef)
{
_frameCount++;
this.Dispatcher.BeginInvoke(new Action(delegate()
{
int index = cbBoneID.SelectedIndex;
UpdateBvhDataUI(_valuesBuffer, index, _bvhHeader.bWithDisp == 1 ? true : false);
txtLog.Text = _frameCount.ToString();
BroadcastToWebSocket(_bvhHeader.AvatarIndex.ToString(), _valuesBuffer);
}));
}
if (sockRef == this.sockUDPRef)
{
_frameCount++;
this.Dispatcher.BeginInvoke(new Action(delegate()
{
int index = cbBoneID.SelectedIndex;
UpdateBvhDataUI(_valuesBuffer, index, _bvhHeader.bWithDisp == 1 ? true : false);
txtLog.Text = _frameCount.ToString();
}));
}
}
private void BroadcastToWebSocket(string actor, float[] bvhData)
{
string data = String.Join(" ", bvhData);
if (wssv != null && wssv.IsListening)
{
wssv.WebSocketServices.Broadcast("{\""+ actor + "\":\"" + data + "\"}");
}
}
private void socketStatusChanged(IntPtr customObject, IntPtr sockRef, SocketStatus status, [MarshalAs(UnmanagedType.LPStr)]string msg)
{
this.Dispatcher.Invoke(new Action(delegate()
{
txtSockLog.Text = msg;
}));
}
private void btnStartUDPService_Click(object sender, RoutedEventArgs e)
{
int nPort = 0;
try
{
nPort = int.Parse(txtUDPPort.Text);
}
catch (Exception)
{
MessageBox.Show("Wrong port number.", "UDP Service", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
if (btnStartUDPService.Content.ToString() == "Connect")
{
sockUDPRef = NeuronDataReader.BRStartUDPServiceAt(nPort);
if (sockUDPRef!=IntPtr.Zero)
{
btnStartUDPService.Content = "DisConnect";
}
else
{
btnStartUDPService.Content = "Connect";
}
}
else
{
NeuronDataReader.BRCloseSocket(sockUDPRef);
btnStartUDPService.Content = "Connect";
}
}
private void btnExitWindows(object sender, RoutedEventArgs e)
{
//添加状态判断,当TCP服务未断开时直接点击exit,会先关闭TCP连接再退出,避免程序崩溃
if (NeuronDataReader.BRGetSocketStatus(sockTCPRef) == SocketStatus.CS_Running)
{
NeuronDataReader.BRCloseSocket(sockTCPRef);
}
//添加状态判断,当UDP服务未断开时直接点击exit,会先关闭UDP连接再退出,避免程序崩溃
if(sockUDPRef != IntPtr.Zero)
{
NeuronDataReader.BRCloseSocket(sockUDPRef);
}
Environment.Exit(0); //立即
}
}
}