source: other-projects/fft-ddr/summer-2014/trunk/FFDDR/Form1.cs@ 29750

Last change on this file since 29750 was 29750, checked in by cct9, 9 years ago

Reworking of pictureBox Paint event so it is using double-buffering correctly without background-erase issue

  • Property svn:executable set to *
File size: 23.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10
11using Microsoft.Kinect;
12using Newtonsoft.Json;
13using NAudio.Wave;
14using MicroLibrary;
15using System.IO;
16using System.Diagnostics;
17
18using System.Drawing.Drawing2D;
19
20namespace FFDDR
21{
22
23
24 public partial class FFDDR : Form
25 {
26 // Wavprocessor variables
27
28 private string[] wavInfo;
29 private long wavFrameIncrement = 0;
30 private WaveFileReader reader;
31 private WaveOut output;
32 private int m = 0;
33 private Graphics graphics;
34 private MicroTimer timer;
35 private int beatBoxChanger = 0;
36 private long timeSinceLastBeat = 0;
37
38
39 // SkeletonTracker variables
40
41 private const float RenderWidth = 640.0f;
42 private const float RenderHeight = 480.0f;
43 private KinectSensor sensor;
44 private Skeleton[] skeletonArray;
45
46 private SkeletonManager skeletonManager;
47
48 private PictureBox[] pboxShowCalibrated;
49
50 private Vector4 centreScreen;
51 private bool twoPlayer = false;
52 private float deadspotSize = 50;
53 private int circleRadius = 400;
54
55 private float footSize = 30, defaultFootSize = 20, enlargedFootSize = 70;
56
57 float defaultScreenHeight;
58 float backgroundScaler = 0.4f;
59
60 int quadrentLeft, quadrentRight;
61
62
63
64 int blueScore = 0, redScore = 0, yellowScore = 0, greenScore = 0;
65
66 private float xCoordLeft, yCoordLeft, xCoordRight, yCoordRight;
67 List<FootStep> footstepList;
68 BeatPicturebox mainScreen;
69 int colourChanger = 0;
70
71 public FFDDR()
72 {
73 InitializeComponent();
74 }
75
76 private void FFDDR_Load(object sender, EventArgs e)
77 {
78 #region Beat detect
79
80 defaultScreenHeight = this.Height;
81 //scoreLabel.Left = (int)(0.7f * (float)this.Width);
82 //scoreLabel.
83 //label1.Font.SizeInPoints = 5;
84
85 // The song to convert + read
86 reader = new WaveFileReader(@"C:\Users\Chris\Downloads\become.wav");
87 output = new WaveOut();
88 output.Init(reader);
89 //output.Play();
90 //graphics = mainScreen.CreateGraphics();
91 mainScreen = new BeatPicturebox();
92 mainScreen.Left = 0;
93 mainScreen.Top = 0;
94 mainScreen.Width = mainScreen1.Width;
95 mainScreen.Height = mainScreen1.Height;
96 this.Controls.Add(mainScreen);
97
98 mainScreen.BringToFront();
99 mainScreen.BackColor = Color.Transparent;//(Color)ColorTranslator.FromHtml("#353535");
100
101
102 this.DoubleBuffered = true;
103
104 // The song to play
105 wavInfo = WavProcessor.ReadWav(@"C:\Users\Chris\Downloads\", "become", 1024, 30);
106
107
108
109 // Play one buffer every (songLength/temp.length(numbuffer)) milliseconds. Both values are contained in the Json string[]
110 WavFrame temp = (WavFrame)JsonConvert.DeserializeObject<WavFrame>(wavInfo[0]);
111
112 timer = new MicroTimer();
113 timer.Interval = (int)((temp.songLength / wavInfo.Length) * 1000);
114
115 // Have the ability to ignore late-firing timer events. Enable if drift issues exist, currently works with.
116 // timer.IgnoreEventIfLateBy = (int)((temp.songLength / wavInfo.Length) )/ 4;
117
118 //timer.Enabled = true;
119 timer.MicroTimerElapsed += new MicroTimer.MicroTimerElapsedEventHandler(timerTick);
120
121 footstepList = new List<FootStep>();
122
123 #endregion
124 #region Initialize and start Kinect
125
126
127
128 foreach (var potentialSensor in KinectSensor.KinectSensors)
129 {
130 if (potentialSensor.Status == KinectStatus.Connected)
131 {
132 this.sensor = potentialSensor;
133 break;
134 }
135 }
136
137
138 if (null != this.sensor)
139 {
140 // Turn on the skeleton stream to receive skeleton frames
141 this.sensor.SkeletonStream.Enable();
142
143 // Add an event handler to be called whenever there is new color frame data
144 this.sensor.SkeletonFrameReady += this.SensorSkeletonFrameReady;
145
146 // Start the sensor!
147 try
148 {
149
150 this.sensor.Start();
151 sensor.ElevationAngle = -5;
152 }
153 catch (IOException)
154 {
155 this.sensor = null;
156 }
157 }
158
159 #endregion
160
161 skeletonManager = new SkeletonManager(6);
162 graphics = mainScreen.CreateGraphics();
163 centreScreen.X = mainScreen.Width / 2;
164 centreScreen.Z = mainScreen.Height / 2;
165
166 mainScreen.Paint += screen1_Paint;
167
168 #region Display pictureboxes showing calibration information
169 pboxShowCalibrated = new PictureBox[6];
170 for (int i = 0; i < pboxShowCalibrated.Length; i++)
171 {
172
173 pboxShowCalibrated[i] = new PictureBox();
174 pboxShowCalibrated[i].Height = 40;
175 pboxShowCalibrated[i].Width = 40;
176 pboxShowCalibrated[i].Left = this.Width - (pboxShowCalibrated[i].Width + 17);
177 pboxShowCalibrated[i].Top = (i * 50);
178 pboxShowCalibrated[i].BackColor = Color.DarkGray;
179 pboxShowCalibrated[i].Visible = true;
180 this.Controls.Add(pboxShowCalibrated[i]);
181 }
182 #endregion
183
184 redLabel.BringToFront();
185 blueLabel.BringToFront();
186 greenLabel.BringToFront();
187 yellowLabel.BringToFront();
188 /*
189 blueLabel = new Label();
190 redLabel = new Label();
191 greenLabel = new Label();
192 yellowLabel = new Label();
193
194 blueLabel.Font = new Font("Microsoft Sans Serif", 20);
195 redLabel.Font = new Font("Microsoft Sans Serif", 20);
196 greenLabel.Font = new Font("Microsoft Sans Serif", 20);
197 yellowLabel.Font = new Font("Microsoft Sans Serif", 20);
198 redLabel.BackColor = Color.Red;
199
200 redLabel.Left = (int)(10);
201 blueLabel.Left = (int)(0.8f * mainScreen.Width);
202 yellowLabel.Left = (int)(0.2f * mainScreen.Width);
203 greenLabel.Left = (int)(0.8f * mainScreen.Width);
204
205 redLabel.Top = (int)(10);
206 blueLabel.Top = (int)(0.1 * mainScreen.Height);
207 yellowLabel.Top = (int)(0.85 * mainScreen.Height);
208 greenLabel.Top = (int)(0.85 * mainScreen.Height);
209
210 redLabel.BringToFront();
211 redLabel.Visible = true;
212 redLabel.Enabled = true;
213 label1.BringToFront();
214 redLabel.Text = "Test";
215
216
217 redLabel.Location = new System.Drawing.Point(10,10 );
218 redLabel.Name = "redLabel";
219 redLabel.Size = new System.Drawing.Size(173, 20);
220
221 // When song finished this is out of bounds. fix this.
222 WavFrame currentFrame = (WavFrame)JsonConvert.DeserializeObject<WavFrame>(wavInfo[wavFrameIncrement]);
223 for (int j = 0; j < currentFrame.bandPowers.Length; j++)
224 {
225 colourChanger++;
226 timeSinceLastBeat++;
227 wavFrameIncrement++;
228 /*
229 if (mainScreen.InvokeRequired)
230 {
231 //mainScreen.Invoke(new MethodInvoker(delegate { mainScreen.Refresh(); }));
232 mainScreen.Invoke(new MethodInvoker(delegate { mainScreen.CreateGraphics().FillRectangle(new SolidBrush(Color.White), 0, 0, mainScreen.Width, mainScreen.Height); }));
233 }
234
235
236 */
237
238 if (colourChanger >= 75)
239 {
240
241 colourChanger = 0;
242 Random random = new Random();
243 if (random.Next(0, 2) == 1)
244 {
245 quadrentRight = random.Next(0, 5);
246 }
247 else
248 {
249 quadrentLeft = random.Next(0, 5);
250 }
251
252 }
253
254 if (footSize > defaultFootSize)
255 {
256 footSize *= 0.92f;
257 if (footSize < defaultFootSize) footSize = defaultFootSize;
258 }
259
260 for (int i = 0; i < footstepList.Count; i++)
261 {
262 footstepList[i].radius *= 1.05f;
263 if (footstepList[i].radius > enlargedFootSize) footstepList.Remove(footstepList[i]);
264 }
265
266
267 if (wavInfo.Length > wavFrameIncrement)
268 {
269 WavFrame currentFrame = (WavFrame)JsonConvert.DeserializeObject<WavFrame>(wavInfo[wavFrameIncrement]);
270
271 /*
272 for (int j = 0; j < currentFrame.bandPowers.Length; j++)
273 {
274 // Calling control change drawing beat visualizer bands) from another thread.
275 if (mainScreen.InvokeRequired)
276 {
277 mainScreen.Invoke(new MethodInvoker(delegate { mainScreen.CreateGraphics().DrawRectangle(new Pen(Color.DarkBlue), j * 15, 0, 10, 6 * currentFrame.bandPowers[j]); }));
278 }
279 }
280 */
281 // If a beat exists on the current frame, display it via BeatBox
282
283 if (currentFrame.beatExists == true)
284 {
285 ChangeBeatBox();
286
287 }
288
289 }
290 else System.Environment.Exit(1);
291 }
292 catch
293 {
294 System.Environment.Exit(1);
295 }
296 }
297
298 private void ChangeBeatBox()
299 {
300
301 // Alternate the box which chanhges to the beat.
302 //graphics = mainScreen.CreateGraphics();
303
304
305
306 if (timeSinceLastBeat >= 20)
307 {
308
309
310 /*
311 beatBoxChanger++;
312 timeSinceLastBeat = 0;
313 PictureBox p;
314 if ((beatBoxChanger %= 2) == 1)
315 {
316 p = beatBox;
317 }
318 else p = beatBox1;
319
320 if (beatBox.BackColor == Color.DarkBlue)
321 {
322 p.BackColor = Color.Turquoise;
323 }
324 else p.BackColor = Color.DarkBlue;
325 */
326
327 //footSize = enlargedFootSize;
328 footstepList.Add(new FootStep((float)defaultFootSize, xCoordLeft, yCoordLeft));
329 footstepList.Add(new FootStep((float)defaultFootSize, xCoordRight, yCoordRight));
330 timeSinceLastBeat = 0;
331 }
332 }
333
334 private void SensorSkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
335 {
336 skeletonArray = new Skeleton[0];
337
338 using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
339 {
340 if (skeletonFrame != null)
341 {
342 skeletonArray = new Skeleton[skeletonFrame.SkeletonArrayLength];
343 skeletonFrame.CopySkeletonDataTo(skeletonArray);
344 skeletonManager.Update(skeletonArray);
345
346 }
347 if (skeletonArray.Length != 0)
348 {
349 //screen.Refresh();
350 //screen2.Refresh();
351
352
353 twoPlayer = false;
354 for (int i = 0; i < skeletonArray.Length; i++)
355 {
356 if (skeletonManager.calibrated[i] == false)
357 {
358 if (skeletonManager.Calibrate(i) == 1)
359 {
360 output.Play();
361 timer.Enabled = true;
362 }
363 }
364
365 if (skeletonManager.calibrated[i] == true)
366 {
367
368 if (skeletonManager.skeletons[i].TrackingState == SkeletonTrackingState.NotTracked)
369 {
370 skeletonManager.calibrated[i] = false;
371 //pboxShowCalibrated[i].BackColor = Color.DarkGray;
372 }
373 else
374 {
375
376 //pboxShowCalibrated[i].BackColor = Color.Cyan;
377 //if (twoPlayer == false) graphics = screen.CreateGraphics();
378 //else graphics = screen2.CreateGraphics();
379 //graphics = mainScreen.CreateGraphics();
380 //mainScreen.g
381 twoPlayer = true;
382
383 int playerQuadrentLeft = skeletonManager.FindQuadrant(skeletonManager.leftFoot[i].Z, skeletonManager.leftFoot[i].X, skeletonManager.centre[i]);
384 int playerQuadrentRight = skeletonManager.FindQuadrant(skeletonManager.rightFoot[i].Z, skeletonManager.rightFoot[i].X, skeletonManager.centre[i]);
385
386
387
388 xCoordLeft = (centreScreen.X - footSize / 2) + (skeletonManager.leftFoot[i].X - skeletonManager.centre[i].X) * (mainScreen.Width / 2) / 1f;
389 yCoordLeft = (centreScreen.Z- footSize / 2) + (skeletonManager.leftFoot[i].Z - skeletonManager.centre[i].Z) * (mainScreen.Height / 2) / 1f;
390 xCoordRight = (centreScreen.X- footSize / 2) + (skeletonManager.rightFoot[i].X - skeletonManager.centre[i].X) * (mainScreen.Width / 2) / 1f;
391 yCoordRight = (centreScreen.Z- footSize / 2) + (skeletonManager.rightFoot[i].Z - skeletonManager.centre[i].Z) * (mainScreen.Height / 2) / 1f;
392
393 mainScreen.Invalidate();
394
395 //drawBackground();
396
397 }
398
399 }
400
401
402
403
404 }
405
406
407
408 }
409
410 }
411
412
413 }
414
415 private void DrawQuadrent(int num, Graphics graphics)
416 {
417
418
419 if (num == 1)
420 {
421 drawSegment(graphics, mainScreen.Width, mainScreen.Height, circleRadius, 180, 90, "#FFFFFF", "#DE00C0");
422 //graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, screen.Width / 2, screen.Height / 2);
423 }
424 else if (num == 2)
425 {
426 drawSegment(graphics, mainScreen.Width, mainScreen.Height, circleRadius, 270, 90, "#FFFFFF", "#00DCE8");
427 //graphics.FillRectangle(new SolidBrush(Color.Blue), screen.Width / 2, 0, screen.Width / 2, screen.Height / 2);
428 }
429 else if (num == 3)
430 {
431 drawSegment(graphics, mainScreen.Width, mainScreen.Height, circleRadius, 0, 90, "#FFFFFF", "#00E800");
432
433 //graphics.FillRectangle(new SolidBrush(Color.Green), screen.Height / 2, screen.Width / 2, screen.Width / 2, screen.Height / 2);
434 }
435 else if (num == 4)
436 {
437 drawSegment(graphics, mainScreen.Width, mainScreen.Height, circleRadius, 90, 90, "#FFFFFF", "#E8E800");
438 //graphics.FillRectangle(new SolidBrush(Color.Yellow), 0, screen.Height / 2, screen.Width / 2, screen.Height / 2);
439 }
440
441 //graphics.FillEllipse(new SolidBrush(Color.Black), screen.Width / 2 - (deadspotSize / 2), screen.Height / 2 - (deadspotSize / 2), deadspotSize, deadspotSize);
442
443
444
445 }
446
447
448 public void screen1_Paint(object sender, PaintEventArgs e)
449 {
450 Graphics graphics = e.Graphics;
451 GraphicsPath path = new GraphicsPath();
452 //test = e.Graphics;
453 path.AddEllipse(0, -((mainScreen.Width - mainScreen.Height) / 2), mainScreen.Width, mainScreen.Width);
454
455 // Create a path gradient brush based on the elliptical path.
456 PathGradientBrush pthGrBrush = new PathGradientBrush(path);
457
458 // Set the color along the entire boundary to
459 Color[] color = { (Color)ColorTranslator.FromHtml("#353535") };
460 pthGrBrush.SurroundColors = color;
461
462 // Set the center color to aqua.
463 pthGrBrush.CenterColor = (Color)ColorTranslator.FromHtml("#E8E8E8"); ;
464
465 // Use the path gradient brush to fill the ellipse.
466 //graphics.FillPath(pthGrBrush, path);
467
468 // Set the focus scales for the path gradient brush.
469 pthGrBrush.FocusScales = new PointF(0.4f, 0.4f);
470
471 // Use the path gradient brush to fill the ellipse again.
472 // Show this filled ellipse to the right of the first filled ellipse.
473 //graphics.TranslateTransform(220.0f, 0.0f);
474 //e.Graphics.FillPath(pthGrBrush, path);
475
476 // Debug.Write("h");
477
478 redLabel.Text = redScore.ToString();
479 blueLabel.Text = blueScore.ToString();
480 yellowLabel.Text = yellowScore.ToString();
481 greenLabel.Text = greenScore.ToString();
482
483 if (quadrentLeft != 1 && quadrentRight != 1) drawSegment(graphics, mainScreen.Width, mainScreen.Height, circleRadius, 180, 90, "#DE00C0", "#780068");
484 if (quadrentLeft != 2 && quadrentRight != 2) drawSegment(graphics, mainScreen.Width, mainScreen.Height, circleRadius, 270, 90, "#000087", "#0000E8");
485 if (quadrentLeft != 3 && quadrentRight != 3) drawSegment(graphics, mainScreen.Width, mainScreen.Height, circleRadius, 0, 90, "#008700", "#00E800");
486 if (quadrentLeft != 4 && quadrentRight != 4) drawSegment(graphics, mainScreen.Width, mainScreen.Height, circleRadius, 90, 90, "#878700", "#E8E800");
487
488
489 DrawQuadrent(quadrentLeft, graphics);
490 DrawQuadrent(quadrentRight, graphics);
491
492 for (int n = 0; n < footstepList.Count; n++)
493 {
494 if (n < footstepList.Count)
495 {
496 graphics.DrawEllipse(new Pen(Color.Black), footstepList[n].xPos - (footstepList[n].radius / 2),
497 footstepList[n].yPos - (footstepList[n].radius / 2), footstepList[n].radius, footstepList[n].radius);
498 }
499 }
500
501 graphics.FillEllipse(new SolidBrush(Color.Black),
502 (int)(xCoordLeft),
503 (int)(yCoordLeft),
504 footSize, footSize);
505
506 graphics.FillEllipse(new SolidBrush(Color.Black),
507 (int)(xCoordRight),
508 (int)(yCoordRight),
509 footSize, footSize);
510
511
512
513 }
514
515 private void drawBackground()
516 {
517 Graphics graphics = mainScreen.CreateGraphics();
518 GraphicsPath path = new GraphicsPath();
519
520 path.AddEllipse(0, -((mainScreen.Width - mainScreen.Height) / 2), mainScreen.Width, mainScreen.Width);
521
522 // Create a path gradient brush based on the elliptical path.
523 PathGradientBrush pthGrBrush = new PathGradientBrush(path);
524
525 // Set the color along the entire boundary to
526 Color[] color = { (Color)ColorTranslator.FromHtml("#353535") };
527 pthGrBrush.SurroundColors = color;
528
529 // Set the center color to aqua.
530 pthGrBrush.CenterColor = (Color)ColorTranslator.FromHtml("#E8E8E8"); ;
531
532 // Use the path gradient brush to fill the ellipse.
533 //graphics.FillPath(pthGrBrush, path);
534
535 // Set the focus scales for the path gradient brush.
536 pthGrBrush.FocusScales = new PointF(backgroundScaler, backgroundScaler);
537
538 // Use the path gradient brush to fill the ellipse again.
539 // Show this filled ellipse to the right of the first filled ellipse.
540 //graphics.TranslateTransform(220.0f, 0.0f);
541 // graphics.FillPath(pthGrBrush, path);
542
543
544
545 }
546
547 private void drawSegment(Graphics graphics, float width, float height, int radius, int startAngle, int finAngle, string innerColour, string outerColour)
548 {
549
550 //graphics.FillPie(new SolidBrush(colour), (width / 2) - (radius / 2), (height / 2) - (radius / 2), radius, radius, startAngle, finAngle);
551
552 // Create a path that consists of a single ellipse.
553 GraphicsPath path = new GraphicsPath();
554 path.AddEllipse((width / 2) - (radius / 2), (height / 2) - (radius / 2), 140, 70);
555 path.AddPie((width / 2) - (radius / 2), (height / 2) - (radius / 2), radius, radius, startAngle, finAngle);
556
557 // Use the path to construct a brush.
558 PathGradientBrush pthGrBrush = new PathGradientBrush(path);
559
560 // Set the center point to a location that is not
561 // the centroid of the path.
562 pthGrBrush.CenterPoint = new PointF((width / 2), (height / 2));
563
564 //orange
565
566 // Set the color at the center of the path to blue.
567
568 pthGrBrush.CenterColor = (Color)ColorTranslator.FromHtml(innerColour);
569
570 // Set the color along the entire boundary
571 // of the path to aqua.
572 Color[] colors = { (Color)ColorTranslator.FromHtml(outerColour) };
573 pthGrBrush.SurroundColors = colors;
574
575 //graphics.FillEllipse(pthGrBrush, 0, 0, 140, 70);
576 graphics.FillPie(pthGrBrush, (width / 2) - (radius / 2), (height / 2) - (radius / 2), radius, radius, startAngle, finAngle);
577
578 }
579 /*
580 private void FFDDR_ResizeEnd(object sender, EventArgs e)
581 {
582 mainScreen.Left = 0;
583 mainScreen.Height = this.Height;
584 mainScreen.Top = 0;
585 mainScreen.Width = this.Width;
586
587 drawBackground();
588 }
589 */
590 private void FFDDR_Resize(object sender, EventArgs e)
591 {
592 mainScreen.Left = 0;
593 mainScreen.Height = this.Height;
594 mainScreen.Top = 0;
595 mainScreen.Width = this.Width;
596
597 drawBackground();
598 }
599 }
600 public class FootStep
601 {
602 public float radius;
603 public float xPos, yPos;
604
605 public FootStep(float Radius, float XPos, float YPos)
606 {
607 radius = Radius;
608 xPos = XPos;
609 yPos = YPos;
610 }
611 }
612}
Note: See TracBrowser for help on using the repository browser.