-
Notifications
You must be signed in to change notification settings - Fork 0
/
_3DxPlugin.cs
247 lines (216 loc) · 8.41 KB
/
_3DxPlugin.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lumos.GUI;
using LumosLIB.Kernel.Log;
using org.dmxc.lumos.Kernel.Resource;
using Lumos.GUI.Resource;
using org.dmxc.lumos.Kernel.DeviceProperties;
using Lumos.GUI.Connection;
using Lumos.GUI.Facade.DeviceProperties;
using Lumos.GUI.Input.v2;
using LumosLIB.Kernel.Scene.Fanning;
using org.dmxc.lumos.Kernel.PropertyType;
using LumosLIB.Tools;
using System.Collections.ObjectModel;
using System.Drawing.Imaging;
using System.IO;
using ErrorEventArgs = LumosLIB.Kernel.Plugin.ErrorEventArgs;
using LumosProtobuf.Resource;
namespace Lumos3DconnexionPlugin
{
/// <summary>
/// Plugin Class. Inhertited from GuiPluginBase
/// </summary>
public class _3DxPlugin : Lumos.GUI.Plugin.GuiPluginBase, IResourceProvider
{
/// <summary>
/// Every Plugin needs a unique ID
/// </summary>
const string PLUGIN_ID = "{6C1AC89E-BBED-4D76-9D63-54FEFA74BF15}";
public const string PLUGIN_NAME = "3Dconnexion";
private static readonly ILumosLog log = LumosLogger.getInstance<_3DxPlugin>();
private static readonly LumosResourceMetadata SETTINGS_FILE = new LumosResourceMetadata("_3DxPluginSettings.xml", EResourceContentType.ManagedTree);
private _3DxForm _form;
private int exceptionCounter;
private readonly Dictionary<E3DxAchsis, _3DxInputSource> _inputSources = Enum.GetValues(typeof(E3DxAchsis))
.Cast<E3DxAchsis>().Select(c => new _3DxInputSource(c)).ToDictionary(c => c.Axis);
public _3DxPlugin()
: base(PLUGIN_ID, PLUGIN_NAME + " Plugin")
{
}
/// <summary>
/// Initialize Plugin. This is called once when Plugin is created @ Startup
/// </summary>
protected override void initializePlugin()
{
this._form = new _3DxForm();
this._form.DeviceMotion += _form_DeviceMotion;
this._form.PluginError += _form_PluginError;
this.loadSettings();
ResourceManager.getInstance().registerResourceProvider(this);
}
/// <summary>
/// This is called when Plugin needs to stop (e.g. shutdown of DMXC, or Plugin is disabled by user)
/// </summary>
protected override void shutdownPlugin()
{
this.saveSettings();
if (this._form != null)
{
WindowManager.getInstance().RemoveWindow(this._form);
this._form.PluginEnabled = false;
this._form.Hide();
}
InputManager.getInstance().UnregisterSources(_inputSources.Values);
}
/// <summary>
/// This is called when Plugin is enabled (e.g. startup of DMXC or Plugin enabled by user)
/// </summary>
protected override void startupPlugin()
{
WindowManager.getInstance().AddWindow(this._form);
this._form.PluginEnabled = true;
this.exceptionCounter = 0;
InputManager.getInstance().RegisterSources(_inputSources.Values);
}
/// <summary>
/// Called when a connection to a Kernel has been established
/// </summary>
public override void connectionEstablished()
{
base.connectionEstablished();
}
/// <summary>
/// Called when connection to Kernel is closing
/// </summary>
public override void connectionClosing()
{
base.connectionClosing();
}
private void loadSettings()
{
//Check whether ResourceManager has a setting
if (ResourceManager.getInstance().ExistsResource(EResourceType.Application, SETTINGS_FILE))
{
var r = ResourceManager.getInstance().TryLoadResource(EResourceType.Application, SETTINGS_FILE);
if (r.ManagedData != null)
{
//Loop through all axis and load settings if existing
foreach (E3DxAchsis a in Enum.GetValues(typeof(E3DxAchsis)))
{
if (r.ManagedData.hasValue<int>(a.ToString() + "-Deadzone"))
this._form.SetAxisDeadzone(a, r.ManagedData.getValue<int>(a.ToString() + "-Deadzone"));
else
this._form.SetAxisDeadzone(a, 0);
}
}
}
}
private void saveSettings()
{
//Create new ManagedTreeItem
ManagedTreeItem i = new ManagedTreeItem("3DxPlugin");
//Loop through all axis and save settings
foreach (E3DxAchsis a in Enum.GetValues(typeof(E3DxAchsis)))
{
int dz = this._form.GetAxisDeadzone(a);
i.setValue(a.ToString() + "-Deadzone", dz);
}
LumosResource r = new LumosResource(SETTINGS_FILE.Name, i);
ResourceManager.getInstance().SaveResource(EResourceType.Application, r);
}
protected override void DisposePlugin(bool disposing)
{
base.DisposePlugin(disposing);
if (_form != null)
{
_form.PluginEnabled = false;
_form.Dispose();
_form = null;
}
}
private void _form_PluginError(object sender, ExceptionEventArgs e)
{
exceptionCounter++;
bool? status = null;
if (exceptionCounter >= 5)
status = false;
OnPluginError(new ErrorEventArgs(status, e.ExceptionObject));
}
#region Dispatch 3Dx Value to Lumos
private void _form_DeviceMotion(object sender, _3DxMotionEventArgs e)
{
foreach (var kvp in e.AxisValues)
{
var p = _inputSources.TryGetWithDefault(kvp.Key);
p?.SetValue(kvp.Value);
}
}
public bool existsResource(EResourceDataType type, string name)
{
if (type == EResourceDataType.Symbol)
{
if (name.Equals("3DxIcon") || name.Equals("3DxIcon_16") || name.Equals("3DxIcon_32"))
return true;
}
return false;
}
IReadOnlyList<LumosDataMetadata> IResourceProvider.allResources(EResourceDataType type)
{
if (type == EResourceDataType.Symbol)
{
List<LumosDataMetadata> ret = new List<LumosDataMetadata>()
{
new LumosDataMetadata("3DxIcon"),
new LumosDataMetadata("3DxIcon_16"),
new LumosDataMetadata("3DxIcon_32"),
};
return ret.AsReadOnly();
}
return null;
}
Stream IResourceProvider.loadResource(EResourceDataType type, string name)
{
if (type == EResourceDataType.Symbol)
{
//Properties.Resources.ResourceManager.GetObject("_3DxIcon_32_png")
switch (name)
{
case "3DxIcon":
case "3DxIcon_32":
{
var i = Properties.Resources.ResourceManager.GetObject("_3dxicon_32") as System.Drawing.Image;
if (i != null)
{
var m = new System.IO.MemoryStream();
i.Save(m, System.Drawing.Imaging.ImageFormat.Png);
m.Position = 0;
return m;
}
return null;
}
case "3DxIcon_16":
{
var i = Properties.Resources.ResourceManager.GetObject("_3dxicon_16") as System.Drawing.Image;
if (i != null)
{
var m = new System.IO.MemoryStream();
i.Save(m, System.Drawing.Imaging.ImageFormat.Png);
m.Position = 0;
return m;
}
return null;
}
}
}
return null;
}
#endregion
}
public enum E3DxAchsis
{
TX, TY, TZ, RX, RY, RZ
}
}