-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboblightGUI.cs
More file actions
549 lines (489 loc) · 22.8 KB
/
boblightGUI.cs
File metadata and controls
549 lines (489 loc) · 22.8 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Xml;
using System.IO;
namespace boblight_net
{
public partial class boblightGUI : Form
{
private boblightClient client;
private boblightClient sparkleClient;
public boblightGUI()
{
InitializeComponent();
//If the configuration file doesn't exist, start it.
if (!File.Exists("bobClient.xml"))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create("bobClient.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("Last used boblightConfiguration");
writer.WriteStartElement("boblightClient");
writer.WriteStartElement("LastServer");
writer.WriteAttributeString("IP", "127.0.0.1");
writer.WriteAttributeString("Port", "19333");
writer.WriteAttributeString("Priority", "128");
writer.WriteEndElement();
writer.WriteStartElement("Servers");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
XmlReader reader = XmlReader.Create("bobClient.xml");
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "LastServer")
{
textIP.Text = reader.GetAttribute("IP");
textPort.Text = reader.GetAttribute("Port");
textPriority.Text = reader.GetAttribute("Priority");
}
}
reader.Close();
}
private void buttonConnect_Click(object sender, EventArgs e)
{
if (client == null || client.isConnected() == false)
{
client = new boblightClient(textIP.Text, Int32.Parse(textPort.Text), Int32.Parse(textPriority.Text));
if (client.isConnected())
{
panelProperties.Enabled = true;
panelIterate.Enabled = true;
tabsControl.Enabled = true;
buttonMisc.Enabled = true;
textIP.Enabled = false;
textPort.Enabled = false;
textPriority.Enabled = false;
lblStatus.Text = "Connected";
buttonConnect.Text = "Disconnect";
//Process the configuration file
XmlDocument doc = new XmlDocument();
doc.Load("bobClient.xml");
//Get to the beginning of things
XmlNode root = doc.DocumentElement;
//Update last server info
XmlNode lastServ = root.SelectSingleNode("LastServer");
lastServ.Attributes["IP"].Value = textIP.Text;
lastServ.Attributes["Port"].Value = textPort.Text;
lastServ.Attributes["Priority"].Value = textPriority.Text;
//Look for a server node that matches our IP and Port (assuming the client isn't really going to be ported)
XmlNode myServ = root.SelectSingleNode("Servers/Server[@IP='" + textIP.Text + "' and @Port='" + textPort.Text + "']");
if (myServ != null)
{
//Config exists, check it
XmlNode myLights = myServ.SelectSingleNode("Lights");
bool flagMatch = true;
foreach (XmlNode xLight in myLights.ChildNodes)
{
bool flagFound = false;
foreach (light light in client.getLights())
{
if (xLight.Attributes["Name"].Value == light.getName())
flagFound = true;
}
if (!flagFound)
{
flagMatch = false;
break;
}
}
//Config didn't match, start from scratch
if (!flagMatch)
{
myLights.RemoveAll();
foreach (light light in client.getLights())
{
XmlElement newLight = doc.CreateElement("Light");
newLight.SetAttribute("Name", light.getName());
newLight.SetAttribute("Color", "000000");
newLight.SetAttribute("Speed", "100.0");
newLight.SetAttribute("Use", "true");
newLight.SetAttribute("Interpolate", "false");
myLights.AppendChild(newLight);
}
}
}
else
{
//Config doesnt exist, create it
root = doc.DocumentElement.SelectSingleNode("Servers");
XmlElement newServer = doc.CreateElement("Server");
newServer.SetAttribute("IP", textIP.Text);
newServer.SetAttribute("Port", textPort.Text);
XmlElement newLights = doc.CreateElement("Lights");
foreach (light light in client.getLights())
{
XmlElement newLight = doc.CreateElement("Light");
newLight.SetAttribute("Name", light.getName());
newLight.SetAttribute("Color", "000000");
newLight.SetAttribute("Speed", "100.0");
newLight.SetAttribute("Use", "true");
newLight.SetAttribute("Interpolate", "false");
newLights.AppendChild(newLight);
}
newServer.AppendChild(newLights);
XmlElement newGroups = doc.CreateElement("LightGroups");
newServer.AppendChild(newGroups);
root.AppendChild(newServer);
}
foreach (light light in client.getLights())
{
listLights.Items.Add(light.getName());
XmlNode lightNode = root.SelectSingleNode("Servers/Server[@IP='" + textIP.Text + "' and @Port='" + textPort.Text + "']/Lights/Light[@Name='" + light.getName() + "']");
//client.setInterpolation(light, bool.Parse(lightNode.Attributes["Interpolate"].Value));
//client.setSpeed(light, float.Parse(lightNode.Attributes["Speed"].Value));
//client.setColor(light, lightNode.Attributes["Color"].Value);
//client.setUse(light, bool.Parse(lightNode.Attributes["Use"].Value));
}
client.syncLights();
foreach(XmlNode groupNode in doc.DocumentElement.SelectNodes("Servers/Server[@IP='" + textIP.Text + "' and @Port='" + textPort.Text + "']/LightGroups/Group"))
{
ICollection<light> lightsCollection = new List<light>();
foreach (XmlNode lightNode in groupNode.ChildNodes)
{
foreach (light light in client.getLights())
{
if (light.getName() == lightNode.Attributes["Name"].Value)
lightsCollection.Add(light);
}
}
listGroups.Items.Add(new lightGroup(lightsCollection.ToArray(), groupNode.Attributes["Name"].Value));
}
//Save configuration file
doc.Save("bobClient.xml");
}
}
else
{
client.disconnect();
listLights.Items.Clear();
listGroups.Items.Clear();
panelProperties.Enabled = false;
panelIterate.Enabled = false;
tabsControl.Enabled = false;
buttonMisc.Enabled = false;
textIP.Enabled = true;
textPort.Enabled = true;
textPriority.Enabled = true;
lblStatus.Text = "Disconnected";
buttonConnect.Text = "Connect";
}
}
private light[] getSelectedLights()
{
if (tabsControl.SelectedIndex == tabLights.TabIndex)
{
light[] lights = new light[listLights.SelectedIndices.Count];
int i = 0;
foreach (light light in client.getLights())
{
if (listLights.SelectedItems.Contains(light.getName()))
{
lights[i] = light;
i++;
}
}
return lights;
}
else
{
ICollection<light> lightsCollection = new List<light>();
foreach (lightGroup lGroup in listGroups.SelectedItems)
{
foreach (light light in lGroup.lights)
{
lightsCollection.Add(light);
}
}
return lightsCollection.ToArray();
}
}
private void buttonColor_Click(object sender, EventArgs e)
{
client.setColor(getSelectedLights(), textHex.Text);
client.syncLights();
//Update configuration
XmlDocument doc = new XmlDocument();
doc.Load("bobClient.xml");
//Get to the beginning of things
XmlNode root = doc.DocumentElement;
foreach (light light in getSelectedLights())
{
XmlNode lightNode = root.SelectSingleNode("Servers/Server[@IP='" + textIP.Text + "' and @Port='" + textPort.Text + "']/Lights/Light[@Name='" + light.getName() + "']");
lightNode.Attributes["Color"].Value = textHex.Text;
}
doc.Save("bobClient.xml");
}
private void buttonSpeed_Click(object sender, EventArgs e)
{
client.setSpeed(getSelectedLights(), float.Parse(textSpeed.Text));
client.syncLights();
//Update configuration
XmlDocument doc = new XmlDocument();
doc.Load("bobClient.xml");
//Get to the beginning of things
XmlNode root = doc.DocumentElement;
foreach (light light in getSelectedLights())
{
XmlNode lightNode = root.SelectSingleNode("Servers/Server[@IP='" + textIP.Text + "' and @Port='" + textPort.Text + "']/Lights/Light[@Name='" + light.getName() + "']");
lightNode.Attributes["Speed"].Value = textSpeed.Text;
}
doc.Save("bobClient.xml");
}
private void buttonUse_Click(object sender, EventArgs e)
{
client.setUse(getSelectedLights(), checkUse.Checked);
client.syncLights();
//Update configuration
XmlDocument doc = new XmlDocument();
doc.Load("bobClient.xml");
//Get to the beginning of things
XmlNode root = doc.DocumentElement;
foreach (light light in getSelectedLights())
{
XmlNode lightNode = root.SelectSingleNode("Servers/Server[@IP='" + textIP.Text + "' and @Port='" + textPort.Text + "']/Lights/Light[@Name='" + light.getName() + "']");
lightNode.Attributes["Use"].Value = checkUse.Checked.ToString();
}
doc.Save("bobClient.xml");
}
private void tabsControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabsControl.SelectedIndex == tabLights.TabIndex)
{
buttonMisc.Text = "Group Lights";
}
else if (tabsControl.SelectedIndex == tabGroups.TabIndex)
{
buttonMisc.Text = "Delete Group";
}
}
private void buttonMisc_Click(object sender, EventArgs e)
{
if (tabsControl.SelectedIndex == tabLights.TabIndex)
{
string groupName = Interaction.InputBox("Please enter a name for this light group:", "New Light Group");
if (!String.IsNullOrWhiteSpace(groupName) && listLights.SelectedItems.Count != 0)
{
listGroups.Items.Add(new lightGroup(getSelectedLights(), groupName));
//Add group to config file
//Update configuration
XmlDocument doc = new XmlDocument();
doc.Load("bobClient.xml");
//Get to the beginning of things
XmlNode root = doc.DocumentElement;
root = root.SelectSingleNode("Servers/Server[@IP='" + textIP.Text + "' and @Port='" + textPort.Text + "']/LightGroups");
XmlElement newGroup = doc.CreateElement("Group");
newGroup.SetAttribute("Name", groupName);
foreach (light light in getSelectedLights())
{
XmlElement newGroupLight = doc.CreateElement("Light");
newGroupLight.SetAttribute("Name", light.getName());
newGroup.AppendChild(newGroupLight);
}
root.AppendChild(newGroup);
doc.Save("bobClient.xml");
tabsControl.SelectedIndex = tabGroups.TabIndex;
}
}
else if(tabsControl.SelectedIndex == tabGroups.TabIndex)
{
//Add group to config file
//Update configuration
XmlDocument doc = new XmlDocument();
doc.Load("bobClient.xml");
foreach(lightGroup lGroup in listGroups.SelectedItems)
{
XmlNode root = doc.DocumentElement;
root = root.SelectSingleNode("Servers/Server[@IP='" + textIP.Text + "' and @Port='" + textPort.Text + "']/LightGroups/Group[@Name='" + lGroup.name + "']");
root.ParentNode.RemoveChild(root);
}
while (listGroups.SelectedItems.Count != 0)
{
listGroups.Items.Remove(listGroups.SelectedItems[0]);
}
doc.Save("bobClient.xml");
}
}
private void btnIterate_Click(object sender, EventArgs e)
{
iterateLightString();
}
private void iterateLightString(bool reverse = false)
{
light[] allLights = getSelectedLights();
int lightCount = allLights.Length;
if(reverse)
{
Stack<light> lightStack = new Stack<light>();
foreach(light light in allLights)
{
lightStack.Push(light);
}
allLights = lightStack.Cast<light>().ToArray();
}
//In order to avoid double setting the first color, we separately set the first light selected
client.setColor(allLights[0], listIterate.Items[0].ToString(), 1.0F);
int lastLight = 0;
//Loop through all the iterative colors within the user's list
for (int i = 1; i < listIterate.Items.Count; i++)
{
int numLights = (int)Math.Ceiling((float)(((lightCount - 1) / (listIterate.Items.Count - 1)) * i) - (lastLight));
light[] lightString = new light[numLights];
int tempIndex = 0;
for (int j = lastLight + 1; j < numLights + lastLight + 1; j++)
{
lightString[tempIndex] = allLights[j];
tempIndex++;
}
iterateLightColors(lightString, listIterate.Items[i - 1].ToString(), listIterate.Items[i].ToString());
lastLight = lastLight + numLights;
}
//Make sure that the boblightd properly syncs all the updated we just sent it.
client.syncLights();
}
private void iterateLightColors(light[] lights, string fromColor, string toColor)
{
//Initialize working colors
int fromColorRed = Int32.Parse(fromColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
int fromColorGreen = Int32.Parse(fromColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
int fromColorBlue = Int32.Parse(fromColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
int toColorRed = Int32.Parse(toColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
int toColorGreen = Int32.Parse(toColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
int toColorBlue = Int32.Parse(toColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
float diffRed = toColorRed - fromColorRed;
float diffGreen = toColorGreen - fromColorGreen;
float diffBlue = toColorBlue - fromColorBlue;
float tempRed, tempGreen, tempBlue;
//Initialize looping variables
int curIndex = 1;
int totalSize = lights.Length;
foreach (light curLight in lights)
{
tempRed = fromColorRed + ((diffRed / totalSize) * curIndex);
tempGreen = fromColorGreen + ((diffGreen / totalSize) * curIndex);
tempBlue = fromColorBlue + ((diffBlue / totalSize) * curIndex);
curIndex++;
client.setColor(curLight, (int)Math.Round(tempRed), (int)Math.Round(tempGreen), (int)Math.Round(tempBlue));
}
}
private void btnAddItColor_Click(object sender, EventArgs e)
{
listIterate.Items.Add(txtIterateFrom.Text);
}
private void btnRemoveItColor_Click(object sender, EventArgs e)
{
while (listIterate.SelectedItems.Count != 0)
{
listIterate.Items.Remove(listIterate.SelectedItems[0]);
}
}
private void button1_Click(object sender, EventArgs e)
{
iterateLightString(true);
}
private void txtIterateFrom_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
listIterate.Items.Add(txtIterateFrom.Text);
txtIterateFrom.SelectAll();
}
}
private void button1_Click_1(object sender, EventArgs e)
{
if (sparkleTimer.Enabled == false)
{
sparkleClient = new boblightClient(textIP.Text, Int32.Parse(textPort.Text), Int32.Parse(textPriority.Text) - 1);
if (sparkleClient.isConnected())
{
foreach (light light in sparkleClient.getLights())
{
sparkleClient.setSpeed(light, float.Parse(txtSparkleSpeed.Text));
sparkleClient.setColor(light, txtSparkleColor.Text);
sparkleClient.setUse(light, false);
}
sparkleTimer.Interval = Int32.Parse(txtSparkleCycle.Text);
sparkleTimer.Start();
}
}
else
{
sparkleTimer.Enabled = false;
sparkleClient.disconnect();
}
}
private void sparkleTimer_Tick(object sender, EventArgs e)
{
//Is this still even worth doing?
if (sparkleClient.isConnected())
{
light[] sparkleLights = sparkleClient.getLights();
//Unsparkle any previously sparkled lights
foreach (light light in sparkleLights)
{
if (light.use())
sparkleClient.setUse(light, false);
}
if(Int32.Parse(txtSparkleCount.Text) > 0)
{
for(int sparkleNum = 1; sparkleNum <= Int32.Parse(txtSparkleCount.Text); sparkleNum++)
{
Random randomSparkle = new Random();
int sparkleIndex = randomSparkle.Next(0, sparkleLights.Length - 1);
for(int radius = 0; radius <= Int32.Parse(txtSparkleRadius.Text); radius++)
{
if(sparkleIndex + radius < sparkleLights.Length)
sparkleClient.setUse(sparkleLights[sparkleIndex + radius], true);
if(sparkleIndex - radius >= 0)
sparkleClient.setUse(sparkleLights[sparkleIndex - radius], true);
}
}
}
sparkleClient.syncLights();
}
else
{
sparkleTimer.Enabled = false;
}
}
private void barLumosity_ValueChanged(object sender, EventArgs e)
{
client.luminosityModifier = barLumosity.Value / 10D;
lblLuminosityValue.Text = client.luminosityModifier.ToString();
foreach (light light in client.getLights())
{
client.setLight(light, light.red, light.green, light.blue);
}
}
private void barSaturation_ValueChanged(object sender, EventArgs e)
{
client.saturationModifier = barSaturation.Value / 10D;
lblSaturationValue.Text = client.saturationModifier.ToString();
foreach (light light in client.getLights())
{
client.setLight(light, light.red, light.green, light.blue);
}
}
private void barHue_ValueChanged(object sender, EventArgs e)
{
client.hueModifier = barHue.Value;
lblHueValue.Text = client.hueModifier.ToString();
foreach (light light in client.getLights())
{
client.setLight(light, light.red, light.green, light.blue);
}
}
}
}