-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
222 lines (202 loc) · 8.42 KB
/
Form1.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
using System;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using Valve.VR;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
namespace AndroidNotificationVRPusher {
public partial class Form1 : Form {
int m_nNotifyTime;
HmdMatrix34_t m_PosMatrix;
SolidBrush m_TextBrush;
SolidBrush m_BackgroundBrush;
int m_nFontSize;
CVRSystem m_HMD;
ulong m_ulOverlayHandle;
bool m_bRunning;
TcpClient server;
StreamReader serverStream;
public Form1() {
InitializeComponent();
LoadConfig();
}
private void Form1_Shown(object sender, EventArgs e) {
if( !Init() ) {
btnStartVR.Enabled = true;
}
if (ConfigurationManager.AppSettings["AutoConnect"] == "1") {
SetTimeout(() => {
ConnectServer();
}, 500);
}
}
public void LoadConfig() {
m_nNotifyTime = Convert.ToInt32(ConfigurationManager.AppSettings["NotifyTime"]);
string[] matrix = ConfigurationManager.AppSettings["NotificationPositionMatrix"].Split(',');
m_PosMatrix.m0 = (float)Convert.ToDouble(matrix[0]);
m_PosMatrix.m1 = (float)Convert.ToDouble(matrix[1]);
m_PosMatrix.m2 = (float)Convert.ToDouble(matrix[2]);
m_PosMatrix.m3 = (float)Convert.ToDouble(matrix[3]);
m_PosMatrix.m4 = (float)Convert.ToDouble(matrix[4]);
m_PosMatrix.m5 = (float)Convert.ToDouble(matrix[5]);
m_PosMatrix.m6 = (float)Convert.ToDouble(matrix[6]);
m_PosMatrix.m7 = (float)Convert.ToDouble(matrix[7]);
m_PosMatrix.m8 = (float)Convert.ToDouble(matrix[8]);
m_PosMatrix.m9 = (float)Convert.ToDouble(matrix[9]);
m_PosMatrix.m10 = (float)Convert.ToDouble(matrix[10]);
m_PosMatrix.m11 = (float)Convert.ToDouble(matrix[11]);
txtServerIP.Text = ConfigurationManager.AppSettings["DefaultIP"];
m_TextBrush = new SolidBrush(Color.FromArgb(Convert.ToInt32(ConfigurationManager.AppSettings["TextColor"].ToString(), 16)));
m_BackgroundBrush = new SolidBrush(Color.FromArgb(Convert.ToInt32(ConfigurationManager.AppSettings["BackgroundColor"].ToString(), 16)));
m_nFontSize = Convert.ToInt32(ConfigurationManager.AppSettings["FontSize"]);
}
public bool Init() {
EVRInitError peError = EVRInitError.None;
m_HMD = OpenVR.Init(ref peError, EVRApplicationType.VRApplication_Overlay);
if(peError != EVRInitError.None) {
txtLog.AppendText("OpenVR啟動失敗: " + peError + '\n');
return false;
}
if (OpenVR.Overlay != null) {
EVROverlayError overlayError = OpenVR.Overlay.CreateOverlay("AndroidNotificationVRPusher", "AndroidNotifier", ref m_ulOverlayHandle);
if (overlayError == EVROverlayError.None) {
OpenVR.Overlay.SetOverlayWidthInMeters(m_ulOverlayHandle, 1f);
OpenVR.Overlay.SetOverlayTransformTrackedDeviceRelative(m_ulOverlayHandle, OpenVR.k_unTrackedDeviceIndex_Hmd, ref m_PosMatrix);
OpenVR.Overlay.SetOverlayAlpha(m_ulOverlayHandle, 0.7f);
txtLog.AppendText("OpenVR啟動成功\n");
return true;
}
}
return false;
}
public void ShutDown() {
m_bRunning = false;
if (server != null && server.Connected) {
serverStream.Close();
server.Close();
}
OpenVR.Shutdown();
}
private void btnStartVR_Click(object sender, EventArgs e) {
btnStartVR.Enabled = false;
if( !Init()) {
btnStartVR.Enabled = true;
}
}
int count = 0;
private void button1_Click(object sender, EventArgs e) {
ShowNotification("Test Msg "+count++);
}
void ShowNotification(string msg) {
string strImgPath = Directory.GetCurrentDirectory() + "\\Msg.png";
Bitmap bitmap = new Bitmap(1024, 512);
Graphics g = Graphics.FromImage(bitmap);
Font textFont = new Font("Arial", m_nFontSize);
SizeF textSize = g.MeasureString(msg, textFont);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.FillRectangle(m_BackgroundBrush, new Rectangle(0, 0, (int)textSize.Width+20, (int)textSize.Height+20));
RectangleF rectf = new RectangleF(10, 10, textSize.Width, textSize.Height);
g.DrawString(msg, textFont, m_TextBrush, rectf);
g.Flush();
bitmap.Save(strImgPath);
OpenVR.Overlay.SetOverlayFromFile(m_ulOverlayHandle, strImgPath);
OpenVR.Overlay.ShowOverlay(m_ulOverlayHandle);
ClearAllTimeout();
SetTimeout(() => {
OpenVR.Overlay.HideOverlay(m_ulOverlayHandle);
}, m_nNotifyTime);
}
private void btnConnect_Click(object sender, EventArgs e) {
ConnectServer();
}
void ConnectServer() {
try {
server = new TcpClient(txtServerIP.Text, 17749);
serverStream = new StreamReader(server.GetStream(), Encoding.UTF8);
Thread thread = new Thread(ReceiveTask);
thread.Start();
m_bRunning = true;
this.Invoke((MethodInvoker)delegate {
txtStatus.Text = "已連線";
});
}
catch (SocketException ex) {
this.Invoke((MethodInvoker)delegate {
txtStatus.Text = "連線失敗";
});
}
}
void ReceiveTask() {
while (m_bRunning) {
if (server.Connected) {
try {
string s = serverStream.ReadLine();
if(s.Length > 2) {
s = s.Substring(2);
Console.WriteLine(s);
this.Invoke((MethodInvoker)delegate {
txtLog.AppendText("收到通知: " + s + '\n');
});
ShowNotification(s.Replace(';','\n'));
}
}
catch(Exception ex) {
this.Invoke((MethodInvoker)delegate {
txtStatus.Text = "未連線";
});
ConnectServer();
}
}
else {
this.Invoke((MethodInvoker)delegate {
txtStatus.Text = "未連線";
});
ConnectServer();
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
ShutDown();
}
#region SetTimeout/ClearTimeout Simulation
static Dictionary<Guid, Thread> _setTimeoutHandles =
new Dictionary<Guid, Thread>();
static Guid SetTimeout(Action cb, int delay) {
return SetTimeout(cb, delay, null);
}
static Guid SetTimeout(Action cb, int delay, Form uiForm) {
Guid g = Guid.NewGuid();
Thread t = new Thread(() => {
Thread.Sleep(delay);
_setTimeoutHandles.Remove(g);
if (uiForm != null)
uiForm.Invoke(cb);
else
cb();
});
_setTimeoutHandles.Add(g, t);
t.Start();
return g;
}
static void ClearTimeout(Guid g) {
if (!_setTimeoutHandles.ContainsKey(g))
return;
_setTimeoutHandles[g].Abort();
_setTimeoutHandles.Remove(g);
}
static void ClearAllTimeout() {
foreach(var g in _setTimeoutHandles) {
g.Value.Abort();
}
_setTimeoutHandles.Clear();
}
#endregion
}
}