forked from isaacliao/planktonpopulations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlanktonPopulations.cs
1936 lines (1721 loc) · 94.4 KB
/
PlanktonPopulations.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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using C3.XNA; // https://bitbucket.org/jcpmcdonald/2d-xna-primitives/wiki/Home
using TUIO;
namespace PlanktonPopulations
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class PlanktonPopulations : Microsoft.Xna.Framework.Game, TuioListener
{
// Declare global constants
long MOUSECLICK_ID = 0;
public static int TEMPORAL_RESOLUTION = 72 * 15;
// Declare some class and instance variables (pretty much the same thing since this is a singleton)
public static SpriteBatch spriteBatch;
public static GameTime gameTime;
public static Texture2D[][] planktonImages = new Texture2D[4][];
public static Texture2D[] guideImagesLeft = new Texture2D[6];
public static Texture2D[] guideImagesRight = new Texture2D[6];
public static Texture2D CloseTabLeftImage, CloseTabRightImage, OpenTabLeftImage, OpenTabRightImage;
public static Texture2D currentFrame;
public static Dictionary<string, Texture2D[]> readoutImages;
public static Random rand = new Random();
bool zoomsOpen = false; // Remembers whether there are currently magnified views open. Important for figuring out when state transitions happen.
bool mouseWasPressed = false; // Helps determine whether the mouse is held continuously or if there is a new click
double lastMousepress; // Remembers the time of the last mousepress in milliseconds
double lastObjectTime; // Remembers the time the last object was seen in milliseconds
public static Rectangle movieDestination;
ConcurrentDictionary<long, ZoomCircle> zoomedCircles = new ConcurrentDictionary<long, ZoomCircle>();
ConcurrentDictionary<long, ZoomCircle> queuedCircles = new ConcurrentDictionary<long, ZoomCircle>();
ConcurrentDictionary<long, TuioObject> tuioObjects = new ConcurrentDictionary<long, TuioObject>();
ConcurrentDictionary<long, TuioCursor> tuioCursors = new ConcurrentDictionary<long, TuioCursor>();
ConcurrentQueue<TuioCursor> tuioCursorAddQueue = new ConcurrentQueue<TuioCursor>();
ConcurrentQueue<TuioCursor> tuioCursorUpdateQueue = new ConcurrentQueue<TuioCursor>();
ConcurrentQueue<TuioCursor> tuioCursorRemoveQueue = new ConcurrentQueue<TuioCursor>();
ConcurrentDictionary<long, TuioHand> tuioHands = new ConcurrentDictionary<long, TuioHand>();
ConcurrentQueue<TuioHand> tuioHandAddOrUpdateQueue = new ConcurrentQueue<TuioHand>();
ConcurrentQueue<TuioHand> tuioHandRemoveQueue = new ConcurrentQueue<TuioHand>();
ConcurrentQueue<TempTool> tempTools = new ConcurrentQueue<TempTool>();
ConcurrentQueue<NutrientTool> nutrientTools = new ConcurrentQueue<NutrientTool>();
public static Texture2D maskTexture;
public static Texture2D tempIcon, hourglassIcon, questionButton;
public static SpriteFont smallFont, mediumFont, largeFont, extraLargeFont;
public static GraphicsDeviceManager graphicsDeviceManager;
public static Color[] maskTextureArray;
public static Dictionary<int, byte[][]> phygrpData = new Dictionary<int, byte[][]>();
public static Dictionary<string, Dictionary<int, byte[]>> theData = new Dictionary<string, Dictionary<int, byte[]>>(); // A data structure to hold all plankton population and environmental data
public static byte[] landmaskArray = new byte[583200]; // A byte array to hold landmask data
public static List<string> dataNames;
public static RenderTarget2D maskTarget;
public static RenderTarget2D fullScreenTarget;
public static BlendState subtractAlpha;
public static bool showZoomedCircleContents = true;
public static float movieVerticalOffset;
public static float movieScale;
public static Vector2 ArrowsOffset = new Vector2();
public static Effect LensEffect;
private VideoPlayer videoPlayer;
private Video video;
private Timeline timeline;
private TuioClient tuioClient;
private Texture2D continentsImage;
private System.Windows.Forms.Form myForm;
private bool readyForInput = false;
public PlanktonPopulations()
{
Settings.LoadSettings("Settings.txt");
// Possible extra debug settings here
#if DEBUG
#endif
graphicsDeviceManager = new GraphicsDeviceManager(this);
graphicsDeviceManager.PreferredBackBufferWidth = Settings.RESOLUTION_X;
graphicsDeviceManager.PreferredBackBufferHeight = Settings.RESOLUTION_Y;
graphicsDeviceManager.IsFullScreen = Settings.FULLSCREEN;
graphicsDeviceManager.PreferMultiSampling = Settings.ANTIALIASING;
graphicsDeviceManager.ApplyChanges();
this.IsMouseVisible = Settings.SHOW_MOUSE;
Content.RootDirectory = "Content";
PlanktonPopulations.gameTime = new GameTime();
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// Moves starting window location
myForm = (System.Windows.Forms.Form)System.Windows.Forms.Control.FromHandle(this.Window.Handle);
myForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
myForm.Location = new System.Drawing.Point(0, 0);
// Explicitly set number of samples for antialiasing; may not be necessary
//GraphicsDevice.PresentationParameters.MultiSampleCount = 16;
//Debug.WriteLine(graphicsDevice.PresentationParameters.MultiSampleCount);
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//planktonTarget = new RenderTarget2D(GraphicsDevice, (int)(CIRCLE_RADIUS + CIRCLE_RADIUS_OVERSCAN) * 2, (int)(Game1.CIRCLE_RADIUS + Game1.CIRCLE_RADIUS_OVERSCAN) * 2);
// Create a new renderTarget to prerender before drawing on screen.
PresentationParameters pp = GraphicsDevice.PresentationParameters;
fullScreenTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, true, GraphicsDevice.DisplayMode.Format, DepthFormat.Depth24);
maskTextureArray = new Color[(int)((Settings.CIRCLE_RADIUS + Settings.CIRCLE_RADIUS_OVERSCAN) * 2 * (Settings.CIRCLE_RADIUS + Settings.CIRCLE_RADIUS_OVERSCAN) * 2)];
subtractAlpha = new BlendState();
subtractAlpha.AlphaBlendFunction = BlendFunction.ReverseSubtract;
subtractAlpha.AlphaSourceBlend = Blend.One;
subtractAlpha.AlphaDestinationBlend = Blend.One;
subtractAlpha.ColorBlendFunction = BlendFunction.ReverseSubtract;
subtractAlpha.ColorSourceBlend = Blend.One;
subtractAlpha.ColorDestinationBlend = Blend.One;
// Initialize tools
for (int i = 0; i < Settings.NUM_NUTRIENTTOOLS; i++)
{
nutrientTools.Enqueue(new NutrientTool(new Vector2(50, 350 + 100 * i)));
}
for (int i = 0; i < Settings.NUM_TEMPTOOLS; i++)
{
tempTools.Enqueue(new TempTool(new Vector2(50, 150 + i * 100)));
}
// Add datanames to the list
//"T", "SiO2", "POSi"
dataNames = new List<string>() { "PhyGrp1", "PhyGrp3", "PhyGrp4", "PhyGrp5" }; // PhyGrp data needs to be the first four
if (Settings.SHOW_LIGHT)
dataNames.Add("PAR");
if (Settings.SHOW_NITRATE)
dataNames.Add("NO3");
if (Settings.SHOW_TEMP || Settings.NUM_TEMPTOOLS > 0)
dataNames.Add("T");
if (Settings.SHOW_SILICA || Settings.NUM_NUTRIENTTOOLS > 0)
dataNames.Add("SiO2");
// Set up data structure for all the data
for (int i = 0; i < dataNames.Count; i++)
theData.Add(dataNames[i], new Dictionary<int, byte[]>());
// Draw a loading rectangle
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.DrawRectangle(new Rectangle(0, 0, 100, 100), Color.White);
spriteBatch.End();
base.Draw(new GameTime());
// Load data from files
FileStream[] fileStreams = new FileStream[dataNames.Count];
for (int timestamp = 52704; timestamp <= 210384; timestamp += 1080)
{
Debug.WriteLine(string.Format("Reading data, {0}% complete...", ((float)timestamp - 52704.0) * 100.0 / (210384.0 - 52704.0)));
for (int i = 0; i < dataNames.Count; i++)
{
//// Read data from file into a float array
//float[] floats = new float[583200/4];
//byte[] fourBytes = new byte[4];
//fileStreams[i] = new FileStream(string.Format("Data\\{0}_{1}.data", dataNames[i], timestamp), FileMode.Open);
//Debug.WriteLine(string.Format("Reading {0}_{1}.data...", dataNames[i], timestamp));
//for (int j = 0; j < 583200/4; j++)
//{
// // Read four bytes from the file
// for (int k = 0; k < 4; k++)
// fourBytes[k] = (byte)fileStreams[i].ReadByte();
// // Convert four bytes to float (big-endian, so reverse it)
// floats[j] = BitConverter.ToSingle(fourBytes.Reverse().ToArray(), 0);
// // Check for NaN's
// if (float.IsNaN(floats[j]))
// floats[j] = 0;
//}
//// Add float data to the data structure
//theData[dataNames[i]].Add(timestamp, floats);
//// Done with this file
//fileStreams[i].Close();
// Read data from file
byte[] bytes = new byte[583200];
float[] floats = new float[583200 / 4];
byte[] fourBytes = new byte[4];
fileStreams[i] = new FileStream(string.Format("Data\\{0}_{1}.data", dataNames[i], timestamp), FileMode.Open);
// Read data from file into a byte array
fileStreams[i].Read(bytes, 0, 583200);
// Done with this file
fileStreams[i].Close();
//// Convert byte array to float array
//for (int j = 0; j < 583200 / 4; j++)
//{
// // Read four bytes from the file
// for (int k = 0; k < 4; k++)
// fourBytes[k] = bytes[j*4+k];
// // Convert four bytes to float (big-endian, so reverse it)
// floats[j] = BitConverter.ToSingle(fourBytes.Reverse().ToArray(), 0);
// // Check for NaN's
// if (float.IsNaN(floats[j]))
// floats[j] = 0;
//}
// Add byte array to the data structure
theData[dataNames[i]].Add(timestamp, bytes);
}
//Debug.WriteLine(timestamp);
//// Save phytoplankton group distributions in a dictionary of timestamps
//byte[][] values = new byte[4][];
//for (int i = 0; i < 4; i++)
//{
// values[i] = new byte[583200];
// fileStreams[i].Read(values[i], 0, 583200);
// fileStreams[i].Close();
//}
//phygrpData.Add(timestamp, values);
// Load landmask data from file
FileStream landmaskFS = new FileStream("Data\\landmask-2-540x270.data", FileMode.Open);
landmaskFS.Read(landmaskArray, 0, 583200);
landmaskFS.Close();
}
base.Initialize();
// Initialize a TUIO client and set this LivingLiquid instance as a listener
tuioClient = new TuioClient(3333);
//Debug.WriteLine("Removing all TUIO listeners...");
//tuioClient.removeAllTuioListeners();
//Debug.WriteLine("Disconnecting TUIO client...is connected: " + tuioClient.isConnected());
//tuioClient.disconnect();
Debug.WriteLine("Adding TUIO listener......is connected: " + tuioClient.isConnected());
tuioClient.addTuioListener(this);
Debug.WriteLine("Connecting TUIO client...is connected: " + tuioClient.isConnected());
tuioClient.connect();
Debug.WriteLine("TUIO client connected: " + tuioClient.isConnected());
// Initialize Tuio Time
TuioTime.initSession();
// Flag ready for input
this.readyForInput = true;
// Because the movie has greater than a 2:1 aspect ratio, need to vertically center on screen
// This section locates a rectangle on screen where the movie should be displayed
// (unless we're using a really weird narrow screen, in which case, redo this section)
movieScale = (float)GraphicsDevice.Viewport.Width / (float)(video.Width);
float movieScaledHeight = (float)(video.Height * movieScale);
movieVerticalOffset = (GraphicsDevice.Viewport.Height - movieScaledHeight) / 2;
movieDestination = new Rectangle(GraphicsDevice.Viewport.X,
(int)(GraphicsDevice.Viewport.Y + movieVerticalOffset),
GraphicsDevice.Viewport.Width,
(int)movieScaledHeight);
InitializeCircles();
}
private void InitializeCircles()
{
freeObjects = new ConcurrentDictionary<long, TuioObject>();
draggedObjects = new ConcurrentDictionary<long, TuioObject>();
zoomedCircles = new ConcurrentDictionary<long, ZoomCircle>();
tuioObjects.Clear();
// If touch_only, add 3 tuio objects that will stay forever
if (Settings.TOUCHONLY)
{
for (int i = 0; i < 3; i++)
{
TuioObject tobj = new TuioObject(new TuioTime(), rand.Next(), i, (float)rand.Next(movieDestination.Left, movieDestination.Right) / Settings.RESOLUTION_X, (float)rand.Next(movieDestination.Top, movieDestination.Bottom) / Settings.RESOLUTION_Y, 0f);
this.addTuioObject(tobj);
if (!freeObjects.TryAdd(tobj.getSymbolID(), tobj))
{
Debug.WriteLine("Failed to add to freeObjects in InitializeCircles()");
};
}
}
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Load lens shader
LensEffect = Content.Load<Effect>("Lens");
LensEffect.CurrentTechnique = LensEffect.Techniques["Technique1"];
// Load zoom circle image for touch-only interaction
ZoomCircle.ZoomCircleImage = Content.Load<Texture2D>("ZoomCircle");
// Load question button
questionButton = Content.Load<Texture2D>("Question_button");
// Load hourglass icon
hourglassIcon = Content.Load<Texture2D>("hourglass");
// Load thermometer icon
tempIcon = Content.Load<Texture2D>("thermometer");
// Load callouts
for (int i = 0; i < 5; i++)
{
guideImagesLeft[i] = Content.Load<Texture2D>("Guide" + i + "L");
}
for (int i = 0; i < 5; i++)
{
guideImagesRight[i] = Content.Load<Texture2D>("Guide" + i + "R");
}
// Load callout tabs
CloseTabLeftImage = Content.Load<Texture2D>("GuideCloseTabLeft");
CloseTabRightImage = Content.Load<Texture2D>("GuideCloseTabRight");
OpenTabLeftImage = Content.Load<Texture2D>("GuideOpenTabLeft");
OpenTabRightImage = Content.Load<Texture2D>("GuideOpenTabRight");
// Load readout icons
readoutImages = new Dictionary<string, Texture2D[]>();
readoutImages["T"] = new Texture2D[2];
readoutImages["T"][0] = Content.Load<Texture2D>("Temperature_empty");
readoutImages["T"][1] = Content.Load<Texture2D>("Temperature_fill");
readoutImages["SiO2"] = new Texture2D[2];
readoutImages["SiO2"][0] = Content.Load<Texture2D>("Silica_empty");
readoutImages["SiO2"][1] = Content.Load<Texture2D>("Silica_fill");
readoutImages["NO3"] = new Texture2D[2];
readoutImages["NO3"][0] = Content.Load<Texture2D>("Nitrogen_empty");
readoutImages["NO3"][1] = Content.Load<Texture2D>("Nitrogen_fill");
readoutImages["PAR"] = new Texture2D[2];
readoutImages["PAR"][0] = Content.Load<Texture2D>("Sunlight_empty");
readoutImages["PAR"][1] = Content.Load<Texture2D>("Sunlight_fill");
// Load plankton textures
planktonImages[0] = new Texture2D[1];
planktonImages[0][0] = Content.Load<Texture2D>("prochlorococcus"); // PhyGrp1
planktonImages[1] = new Texture2D[1];
planktonImages[1][0] = Content.Load<Texture2D>("synechococcus"); // PhyGrp3
planktonImages[2] = new Texture2D[2];
planktonImages[2][0] = Content.Load<Texture2D>("Dinoflagellate1"); // PhyGrp4
planktonImages[2][1] = Content.Load<Texture2D>("Dinoflagellate2");
// planktonImages[2,2] = Content.Load<Texture2D>("Dinoflagellate3"); // This is actually a diatom, don't use for now
planktonImages[3] = new Texture2D[3];
planktonImages[3][0] = Content.Load<Texture2D>("Diatom1"); // PhyGrp5
planktonImages[3][1] = Content.Load<Texture2D>("Diatom2");
planktonImages[3][2] = Content.Load<Texture2D>("Diatom3");
//planktonImages[3,3] = Content.Load<Texture2D>("Dinoflagellate3"); // This is actually a diatom, don't use for now
// Load continent textures
continentsImage = Content.Load<Texture2D>("Continents02");
// Load fonts
smallFont = Content.Load<SpriteFont>("Explo16");
mediumFont = Content.Load<SpriteFont>("Explo20");
largeFont = Content.Load<SpriteFont>("Explo32");
extraLargeFont = Content.Load<SpriteFont>("Explo48");
// Load movie images into the texture collection
//movieFrames = new Texture2D[1000];
//for (int i = 1; i <= 1000; i++)
//{
// movieFrames[i - 1] = Content.Load<Texture2D>(i.ToString("D4"));
//}
// Create the mask texture
CreateMaskTexture();
// Load movie file
if (Settings.MOVIE_BLUE_WATER)
{
if (Settings.MOVIE_BLUE_WATER_SATURATED)
video = Content.Load<Video>("overviewMovieBlueSaturated");
else
video = Content.Load<Video>("overviewMovieBlue");
}
else
{
if (Settings.MOVIE_SLOWER)
video = Content.Load<Video>("overviewMovieSlow");
else
video = Content.Load<Video>("overviewMovieFast");
}
videoPlayer = new VideoPlayer();
videoPlayer.IsLooped = true;
videoPlayer.Play(video);
// Initialize timeline
this.timeline = new Timeline(GraphicsDevice, video, videoPlayer);
//this.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// Unload any non ContentManager content here
this.readyForInput = false;
//Debug.WriteLine("Disconnecting TUIO client...");
//tuioClient.removeAllTuioListeners();
tuioClient.disconnect();
//Debug.WriteLine("TUIO client connected: " + tuioClient.isConnected());
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
bool keyboardIsReady = true;
private int lastCheckedGameTime = 0;
protected override void Update(GameTime gameTime)
{
// Update this object's gameTime
PlanktonPopulations.gameTime = gameTime;
// Get current movie frame as a texture
currentFrame = videoPlayer.GetTexture();
//currentFrame = (Texture2D)movieFrames[timestep-1];
// Ensure at least one frame with no keys pressed before accepting further keyboard input
if (Keyboard.GetState().GetPressedKeys().Count() == 0)
keyboardIsReady = true;
else if (keyboardIsReady)
{
keyboardIsReady = false;
// Exit if escape key is pressed
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
this.UnloadContent();
this.Exit();
}
// Toggle fullscreen if alt+enter or F is pressed
if (Keyboard.GetState().IsKeyDown(Keys.F) || ((Keyboard.GetState().IsKeyDown(Keys.LeftAlt) || (Keyboard.GetState().IsKeyDown(Keys.RightAlt))) && Keyboard.GetState().IsKeyDown(Keys.Enter)))
{
graphicsDeviceManager.ToggleFullScreen();
//CreateMaskTexture();
//Game1.maskTexture.SetData<Color>(Game1.maskTextureArray);
}
// Toggle showing circle contents if space key is pressed
//if (Keyboard.GetState().IsKeyDown(Keys.Space))
//{
// if (showZoomedCircleContents)
// showZoomedCircleContents = false;
// else
// showZoomedCircleContents = true;
//}
// Toggle touch-only if T key is pressed
if (Keyboard.GetState().IsKeyDown(Keys.T))
{
if (Settings.TOUCHONLY)
{
Settings.TOUCHONLY = false;
}
else
{
Settings.TOUCHONLY = true;
}
this.CreateMaskTexture();
this.InitializeCircles();
}
// Move something using keyboard arrows
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
//myForm.Location = new System.Drawing.Point(myForm.Location.X, myForm.Location.Y - 1);
ArrowsOffset.Y--;
Debug.WriteLine("ArrowsOffset: " + ArrowsOffset.X + ", " + ArrowsOffset.Y);
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
//myForm.Location = new System.Drawing.Point(myForm.Location.X, myForm.Location.Y + 1);
ArrowsOffset.Y++;
Debug.WriteLine("ArrowsOffset: " + ArrowsOffset.X + ", " + ArrowsOffset.Y);
}
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
//myForm.Location = new System.Drawing.Point(myForm.Location.X - 1, myForm.Location.Y);
ArrowsOffset.X--;
Debug.WriteLine("ArrowsOffset: " + ArrowsOffset.X + ", " + ArrowsOffset.Y);
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
//myForm.Location = new System.Drawing.Point(myForm.Location.X + 1, myForm.Location.Y);
ArrowsOffset.X++;
Debug.WriteLine("ArrowsOffset: " + ArrowsOffset.X + ", " + ArrowsOffset.Y);
}
// DEBUG
if (Keyboard.GetState().IsKeyDown(Keys.E))
{
throw new Exception("You pushed E!");
}
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
if (Settings.SHOW_TOUCHES)
{
Settings.SHOW_TOUCHES = false;
Settings.SHOW_HITBOXES = false;
}
else
{
Settings.SHOW_TOUCHES = true;
Settings.SHOW_HITBOXES = true;
}
}
}
// Recreate the mask texture if the content is lost (for example, if switched to fullscreen)
if (maskTarget.IsContentLost)
CreateMaskTexture();
if (Settings.INPUT_USE_MOUSE)
processMouseInput(gameTime);
processTuioObjects(gameTime);
ConsumeCursorAndHandEvents();
if (zoomsOpen)
{
// Update each zoomed circle
foreach (ZoomCircle zoomedCircle in zoomedCircles.Values)
{
zoomedCircle.LoadDataAt((int)zoomedCircle.position.X, (int)zoomedCircle.position.Y, this.GetMovieTimestamp(), this.GetMovieTimestampRaw());
}
// Update each temperature tool
foreach (TempTool tempTool in tempTools)
{
tempTool.Update(this.GetMovieTimestamp(), movieDestination);
}
// Update each nutrient tool
foreach (NutrientTool nutrientTool in nutrientTools)
{
nutrientTool.Update(this.GetMovieTimestamp(), movieDestination);
}
}
// Update all ZoomedCircles
//if (!maskCreated)
//{
// CreateMaskTexture();
// maskCreated = true;
// maskTexture.GetData<Color>(Game1.maskTextureArray, 0, maskTextureArray.Length);
//}
//CreateMaskTexture();
//maskTexture.GetData<Color>(Game1.maskTextureArray, 0, maskTextureArray.Length);
//maskTexture.SetData<Color>(maskTextureArray);
foreach (ZoomCircle zoomedCircle in zoomedCircles.Values)
{
zoomedCircle.Update(gameTime);
// Update readout textures
foreach (Readout readout in zoomedCircle.readoutList)
{
// Retrieve appropriate images
Texture2D empty = readoutImages[readout.dataName][0];
Texture2D full = readoutImages[readout.dataName][1];
// Calculate how full the readout display should be
double readoutRatio = (readout.value - readout.min) / (readout.max - readout.min);
//double scaleFactor = (double)DASHBOARD_READOUT_SIZE / (double)empty.Height;
int bottomSourcePixels = (int)(readoutRatio * (readout.top - readout.bottom) + readout.bottom);
int topSourcePixels = empty.Height - bottomSourcePixels;
//int bottomDestPixels = bottomSourcePixels;
//int topDestPixels = empty.Height - bottomDestPixels;
//Debug.WriteLine("{0}: {1}", readout.dataName, bottomDestPixelsRaw);
// Create rectangles representing readout sources and destinations
//Rectangle destTop = new Rectangle((int)(readoutPosition.X - DASHBOARD_READOUT_SIZE / 2), (int)(readoutPosition.Y - DASHBOARD_READOUT_SIZE / 2), (int)DASHBOARD_READOUT_SIZE, (int)topDestPixels);
//Rectangle destBottom = new Rectangle((int)(readoutPosition.X - DASHBOARD_READOUT_SIZE / 2), (int)(readoutPosition.Y - DASHBOARD_READOUT_SIZE / 2 + topDestPixels), (int)DASHBOARD_READOUT_SIZE, (int)bottomDestPixels);
Rectangle sourceTop = new Rectangle(0, 0, empty.Width, topSourcePixels);
Rectangle sourceBottom = new Rectangle(0, topSourcePixels, empty.Width, bottomSourcePixels);
// Draw top and bottom parts of readout
//spriteBatch.Draw(empty, destTop, sourceTop, Color.White);
//spriteBatch.Draw(full, destBottom, sourceBottom, Color.White);
// Center readout position
//Vector2 drawPosition = readoutPosition - new Vector2(DASHBOARD_READOUT_SIZE / 2, DASHBOARD_READOUT_SIZE / 2);
GraphicsDevice.SetRenderTarget(readout.target);
GraphicsDevice.Clear(Color.Transparent);
spriteBatch.Begin();
spriteBatch.Draw(empty, Vector2.Zero, sourceTop, Color.White);
spriteBatch.Draw(full, new Vector2(0, topSourcePixels), sourceBottom, Color.White);
spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
readout.texture = (Texture2D)readout.target;
}
}
this.timeline.Update();
base.Update(gameTime);
}
/// <summary>
/// Use TUIO cursors (touches) to move TuioObjects. Only runs if touch_only is true, in the [Input] section of Settings.txt.
/// </summary>
/// <param name="gameTime"></param>
private ConcurrentDictionary<long, TuioObject> draggedObjects = new ConcurrentDictionary<long, TuioObject>(); // key is ID of dragging cursor's session ID
private ConcurrentDictionary<long, Vector2> dragDiffVectors = new ConcurrentDictionary<long, Vector2>(); // key is ID of dragging cursor's session ID
private ConcurrentDictionary<long, TuioObject> freeObjects = new ConcurrentDictionary<long, TuioObject>(); // key is TuioObject symbol ID
private void processTouchOnlyUpdateEvent(TuioCursor tcur)
{
lock (touchOnlyLock)
{
if (draggedObjects.ContainsKey(tcur.getSessionID()))
{
// Move the currently dragged TuioObject according to its dragging cursor
Vector2 tcurPosition = new Vector2(tcur.getScreenX(Settings.RESOLUTION_X), tcur.getScreenY(Settings.RESOLUTION_Y));
TuioObject tobj = draggedObjects[tcur.getSessionID()];
Vector2 dragDiffVector = dragDiffVectors[tcur.getSessionID()];
Vector2 newObjPos = new Vector2((tcurPosition.X - dragDiffVector.X) / (float)Settings.RESOLUTION_X, (tcurPosition.Y - dragDiffVector.Y) / (float)Settings.RESOLUTION_Y);
// Don't let objects move off screen
if (newObjPos.X >= 0 && newObjPos.X <= 1 && newObjPos.Y >= 0 && newObjPos.Y <= 1)
{
//tobj.update(TuioTime.getSessionTime(), (tcurPosition.X - dragDiffVector.X) / (float)Settings.RESOLUTION_X, (tcurPosition.Y - dragDiffVector.Y) / (float)Settings.RESOLUTION_Y, tcur.getXSpeed(), tcur.getYSpeed(), tcur.getMotionAccel());
//tobj.update(TuioTime.getSessionTime(), newObjPos.X, newObjPos.Y);
tobj.update(TuioTime.getSessionTime(), newObjPos.X, newObjPos.Y, tcur.getXSpeed(), tcur.getYSpeed(), tcur.getMotionAccel());
draggedObjects.AddOrUpdate(tobj.getSymbolID(), tobj, updateTObj);
//tuioObjects.AddOrUpdate(tobj.getSymbolID(), tobj, updateTObj);
this.updateTuioObject(tobj);
Debug.WriteLine("Moving " + tobj.getSymbolID() + " with " + tcur.getSessionID() + " at speed " + tcur.getXSpeed() + ", " + tcur.getYSpeed());
}
/*
// Update corresponding ZoomCircle
// Calculate speed from x and y speeds
float totalSpeed = (float)Math.Sqrt(Math.Pow(tobj.getXSpeed() * Settings.RESOLUTION_X, 2) + Math.Pow(tobj.getYSpeed() * Settings.RESOLUTION_Y, 2));
// Ignore implausibly high values because Multitaction is giving us speeds of "Infinity" every couple updates
if (totalSpeed > 1000f)
totalSpeed = 0f;
//Debug.WriteLine(totalSpeed);
zoomedCircles[tobj.getSymbolID()].MoveTo(tobj.getScreenX(Settings.RESOLUTION_X), tobj.getScreenY(Settings.RESOLUTION_Y), tobj.getAngleDegrees(), this.GetMovieTimestamp(), this.GetMovieTimestampRaw(), totalSpeed);
*/
}
}
}
private void processTouchOnlyRemoveEvent(long id)
{
lock (touchOnlyLock)
{
// Release a dragged TuioObject when the touch point is removed
if (draggedObjects.ContainsKey(id))
{
TuioObject outTobj;
draggedObjects.TryRemove(id, out outTobj);
Vector2 outVector;
dragDiffVectors.TryRemove(id, out outVector);
// Set object speed to 0
outTobj.update(outTobj.getX(), outTobj.getY(), 0, 0, 0);
this.updateTuioObject(outTobj);
if (freeObjects.TryAdd(outTobj.getSymbolID(), outTobj))
Debug.WriteLine("Releasing " + outTobj.getSymbolID() + " with " + id);
else
Debug.WriteLine("Failed to add back to freeObjects in processTouchOnlyRemoveEvent()");
}
// Check if currently dragged TuioObjects are still being dragged; if not, release them
/*
foreach (long tcurSessionID in draggedObjects.Keys)
{
bool contains = false;
foreach (TuioCursor tcur in tuioCursorsCopy)
{
if (tcur.getSessionID() == tcurSessionID)
{
contains = true;
}
}
if (!contains)
{
freeObjects.Add(draggedObjects[tcurSessionID]);
draggedObjects.Remove(tcurSessionID);
dragDiffVectors.Remove(tcurSessionID);
Debug.WriteLine("Releasing " + tcurSessionID);
}
}
*/
}
}
/// <summary>
/// Check if this newly added cursor should start dragging an object
/// </summary>
/// <param name="tcur"></param>
private static Object touchOnlyLock = new Object();
private void processTouchOnlyAddEvent(TuioCursor tcur)
{
lock (touchOnlyLock)
{
// Make sure touch point isn't already dragging something else
if (!draggedObjects.Keys.Contains(tcur.getSessionID()))
{
// Find the closest touched freeObject to this touch point
Vector2 tcurStartPos = new Vector2(tcur.getPath()[0].getScreenX(Settings.RESOLUTION_X), tcur.getPath()[0].getScreenY(Settings.RESOLUTION_Y));
float closestDistance = float.PositiveInfinity;
TuioObject closestTobj = null;
foreach (TuioObject tobj in freeObjects.Values)
//foreach (TuioObject tobj in tuioObjects.Values)
{
if (!draggedObjects.ContainsKey(tobj.getSessionID()))
{
Vector2 tobjPosition = new Vector2(tobj.getScreenX(Settings.RESOLUTION_X), tobj.getScreenY(Settings.RESOLUTION_Y));
float distance = Vector2.Distance(tcurStartPos, tobjPosition);
if (distance < closestDistance && distance < Settings.CIRCLE_RADIUS)
{
// Found a closer freeObject
closestDistance = distance;
closestTobj = tobj;
}
}
}
if (closestTobj != null)
{
Vector2 closestTobjPosition = new Vector2(closestTobj.getScreenX(Settings.RESOLUTION_X), closestTobj.getScreenY(Settings.RESOLUTION_Y));
draggedObjects.TryAdd(tcur.getSessionID(), closestTobj);
dragDiffVectors.TryAdd(tcur.getSessionID(), tcurStartPos - closestTobjPosition);
TuioObject outTobj;
freeObjects.TryRemove(closestTobj.getSymbolID(), out outTobj);
Debug.WriteLine("Grabbing " + closestTobj.getSymbolID() + " with " + +tcur.getSessionID());
}
}
}
}
/// <summary>
/// Use mouse input to simulate TUIO object input. Only runs if mouse_input is true, in the [Input] section of Settings.txt.
/// </summary>
/// <param name="gameTime"></param>
private TuioObject draggedObject;
private TuioCursor draggedCursor;
private Vector2 dragDiffVector;
private void processMouseInput(GameTime gameTime)
{
MouseState mouseState = Mouse.GetState();
float mouseX = (float)mouseState.X / (float)Settings.RESOLUTION_X;
float mouseY = (float)mouseState.Y / (float)Settings.RESOLUTION_Y;
// If left button not clicked, release objects and cursors
if (mouseState.LeftButton != ButtonState.Pressed)
{
draggedCursor = null;
draggedObject = null;
dragDiffVector = Vector2.Zero;
tuioCursors.Clear();
/*
// Update objects
foreach (TuioObject tobj in tuioObjects.Values) {
this.updateTuioObject(new TuioObject(new TuioTime(), tobj.getSessionID(), tobj.getSymbolID(),tobj.getX()+0.00001f,tobj.getY(),tobj.getAngle()));
}
*/
}
else if (mouseState.LeftButton == ButtonState.Pressed)
{
// If we're already dragging an object or cursor, just update that object's position
if (draggedCursor != null)
{
draggedCursor.update(TuioTime.getSessionTime(), mouseX, mouseY);
this.updateTuioCursor(draggedCursor);
return;
}
else if (draggedObject != null)
{
draggedObject.update(TuioTime.getSessionTime(), mouseX - dragDiffVector.X / (float)Settings.RESOLUTION_X, mouseY - dragDiffVector.Y / (float)Settings.RESOLUTION_Y);
//draggedObject.update(new TuioTime(), mouseX, mouseY);
this.updateTuioObject(draggedObject);
return;
}
// If click is on a guide tab, make it a cursor
TuioCursor tcur = new TuioCursor(TuioTime.getSessionTime(), 0, 0, mouseX, mouseY);
ZoomCircle outZoomCircle;
string outButtonName;
this.WouldPushButton(tcur, out outZoomCircle, out outButtonName);
if (outZoomCircle != null)
{
draggedCursor = tcur;
this.addTuioCursor(tcur);
return;
}
// If click is on an open zoomCircle, start dragging its object
// Find nearest object
TuioObject closestTobj = null;
float closestDistance = 1000f;
Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);
Vector2 tobjPosition = new Vector2();
Vector2 closestDiffVector = Vector2.Zero;
foreach (TuioObject tobj in tuioObjects.Values)
{
tobjPosition.X = tobj.getScreenX(Settings.RESOLUTION_X);
tobjPosition.Y = tobj.getScreenY(Settings.RESOLUTION_Y);
Vector2 diffVector = Vector2.Subtract(mousePosition, tobjPosition);
float distance = diffVector.Length();
if (closestTobj == null)
{
closestDistance = distance;
closestTobj = tobj;
closestDiffVector = diffVector;
}
else if (distance < closestDistance)
{
closestDistance = distance;
closestTobj = tobj;
closestDiffVector = diffVector;
}
}
if (closestTobj != null)
{
if (closestDistance <= Settings.CIRCLE_RADIUS)
{
// On left click, start dragging it
if (mouseState.LeftButton == ButtonState.Pressed)
{
draggedObject = closestTobj;
dragDiffVector = closestDiffVector;
return;
}
}
}
// If we're not maxed out on objects, place a new object and start dragging it
if (tuioObjects.Count < Settings.MAX_CIRCLES)
{
// Find a new id that's not taken yet
int id = 0;
while (tuioObjects.Keys.Contains(id))
{
id++;
}
TuioObject tobj = new TuioObject(new TuioTime(), rand.Next(), id, mouseX, mouseY, 0f);
draggedObject = tobj;
this.addTuioObject(tobj);
return;
}
// Otherwise, it's a cursor
draggedCursor = tcur;
this.addTuioCursor(tcur);
return;
}
// If right clicked on a circle, remove it
if (mouseState.RightButton == ButtonState.Pressed)
{
TuioObject closestTobj = null;
float closestDistance = 1000f;
Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);
Vector2 tobjPosition = new Vector2();
foreach (TuioObject tobj in tuioObjects.Values)
{
tobjPosition.X = tobj.getScreenX(Settings.RESOLUTION_X);
tobjPosition.Y = tobj.getScreenY(Settings.RESOLUTION_Y);
Vector2 diffVector = Vector2.Subtract(mousePosition, tobjPosition);
float distance = diffVector.Length();
if (closestTobj == null)
{
closestDistance = distance;
closestTobj = tobj;
}
else if (distance < closestDistance)
{
closestDistance = distance;
closestTobj = tobj;
}
}
if (closestTobj != null)
{
if (closestDistance <= Settings.CIRCLE_RADIUS)
{
this.removeTuioObject(closestTobj);
return;
}
}
}
// If middle clicked on a circle, flip the orientation
if (mouseState.MiddleButton == ButtonState.Pressed)
{
TuioObject closestTobj = null;
float closestDistance = 1000f;
Vector2 mousePosition = new Vector2(mouseState.X, mouseState.Y);
Vector2 tobjPosition = new Vector2();
foreach (TuioObject tobj in tuioObjects.Values)
{
tobjPosition.X = tobj.getScreenX(Settings.RESOLUTION_X);
tobjPosition.Y = tobj.getScreenY(Settings.RESOLUTION_Y);
Vector2 diffVector = Vector2.Subtract(mousePosition, tobjPosition);
float distance = diffVector.Length();
if (closestTobj == null)
{
closestDistance = distance;
closestTobj = tobj;
}
else if (distance < closestDistance)
{
closestDistance = distance;
closestTobj = tobj;
}
}
if (closestTobj != null)
{
if (closestDistance <= Settings.CIRCLE_RADIUS)
{
if (this.zoomedCircles[closestTobj.getSymbolID()].AttachedGuide.IsUpsideDown)
this.zoomedCircles[closestTobj.getSymbolID()].AttachedGuide.IsUpsideDown = false;
else
this.zoomedCircles[closestTobj.getSymbolID()].AttachedGuide.IsUpsideDown = true;
return;
}
}
}
}
/// <summary>
/// Update game state based on TUIO object inputs.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
private void processTuioObjects(GameTime gameTime)
{
// Tag ZoomedCircles without corresponding tuioObjects as expiring, or unexpire those that do have corresponding tuioObjects
foreach (long id in zoomedCircles.Keys)
{
if (!tuioObjects.Keys.Contains(id))
{
if (!zoomedCircles[id].isExpiring)
// Only set this when circle first starts expiring
zoomedCircles[id].expirationStartTime = gameTime.TotalGameTime.TotalMilliseconds;
zoomedCircles[id].isExpiring = true;
}
else
{
zoomedCircles[id].isExpiring = false;
}
// Remove expired ZoomedCircles
if (zoomedCircles[id].isExpiring && (gameTime.TotalGameTime.TotalMilliseconds - zoomedCircles[id].expirationStartTime > Settings.CROSSHAIRS_RING_UP_DELAY_TIME + Settings.CROSSHAIRS_ON_RING_UP_ZOOM_TIME))
{
ZoomCircle removedCircle;
if (zoomedCircles.TryRemove(id, out removedCircle))
{
removedCircle.ReturnPlankton();
}
}
}
// NOTE: This section not needed any more because ZoomCircles now manage their own state AND video no longer pauses when circles are open