File: WindowsGameWindow.cs

package info (click to toggle)
monogame 2.5.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,060 kB
  • ctags: 10,325
  • sloc: cs: 65,996; xml: 591; makefile: 22; ansic: 8
file content (622 lines) | stat: -rw-r--r-- 23,040 bytes parent folder | download | duplicates (2)
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
#region License
/*
Microsoft Public License (Ms-PL)
XnaTouch - Copyright © 2009 The XnaTouch Team

All rights reserved.

This license governs use of the accompanying software. If you use the software, you accept this license. If you do not
accept the license, do not use the software.

1. Definitions
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under 
U.S. copyright law.

A "contribution" is the original software, or any additions or changes to the software.
A "contributor" is any person that distributes its contribution under this license.
"Licensed patents" are a contributor's patent claims that read directly on its contribution.

2. Grant of Rights
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, 
each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, 
each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.

3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, 
your patent license from such contributor to the software ends automatically.
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution 
notices that are present in the software.
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including 
a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object 
code form, you may only do so under a license that complies with this license.
(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
purpose and non-infringement.
*/
#endregion License

#region Using Statements
using System;
using System.ComponentModel;
using Microsoft.Xna.Framework.Input;
using OpenTK;
using OpenTK.Graphics;
using System.Collections.Generic;


#endregion Using Statements

namespace Microsoft.Xna.Framework
{
    public static class KeyExtensions
    {
        public static Microsoft.Xna.Framework.Input.Keys ToXna(this OpenTK.Input.Key key)
        {
            switch (key)
            {
                case OpenTK.Input.Key.A:
                    return Input.Keys.A;
                    
                case OpenTK.Input.Key.AltLeft:
                    return Input.Keys.LeftAlt;
                    
                case OpenTK.Input.Key.AltRight:
                    return Input.Keys.RightAlt;
                    
                case OpenTK.Input.Key.B:
                    return Input.Keys.B;
                    
                case OpenTK.Input.Key.Back:
                    return Input.Keys.Back;
                    
                case OpenTK.Input.Key.BackSlash:
                    return Input.Keys.OemBackslash;
                    
                case OpenTK.Input.Key.BracketLeft:
                    return Input.Keys.OemOpenBrackets;
                    
                case OpenTK.Input.Key.BracketRight:
                    return Input.Keys.OemCloseBrackets;
                    
                case OpenTK.Input.Key.C:
                    return Input.Keys.C;
                    
                case OpenTK.Input.Key.CapsLock:
                    return Input.Keys.CapsLock;
                    
                case OpenTK.Input.Key.Clear:
                    return Input.Keys.OemClear;
                    
                case OpenTK.Input.Key.Comma:
                    return Input.Keys.OemComma;
                    
                case OpenTK.Input.Key.ControlLeft:
                    return Input.Keys.LeftControl;
                    
                case OpenTK.Input.Key.ControlRight:
                    return Input.Keys.RightControl;
                    
                case OpenTK.Input.Key.D:
                    return Input.Keys.D;
                    
                case OpenTK.Input.Key.Delete:
                    return Input.Keys.Delete;
                    
                case OpenTK.Input.Key.Down:
                    return Input.Keys.Down;
                    
                case OpenTK.Input.Key.E:
                    return Input.Keys.E;
                    
                case OpenTK.Input.Key.End:
                    return Input.Keys.End;
                    
                case OpenTK.Input.Key.Enter:
                    return Input.Keys.Enter;
                    
                case OpenTK.Input.Key.Escape:
                    return Input.Keys.Escape;
                    
                case OpenTK.Input.Key.F:
                    return Input.Keys.F;
                    
                case OpenTK.Input.Key.F1:
                    return Input.Keys.F1;
                    
                case OpenTK.Input.Key.F10:
                    return Input.Keys.F10;
                    
                case OpenTK.Input.Key.F11:
                    return Input.Keys.F11;
                    
                case OpenTK.Input.Key.F12:
                    return Input.Keys.F12;
                    
                case OpenTK.Input.Key.F13:
                    return Input.Keys.F13;
                    
                case OpenTK.Input.Key.F14:
                    return Input.Keys.F14;
                    
                case OpenTK.Input.Key.F15:
                    return Input.Keys.F15;
                    
                case OpenTK.Input.Key.F16:
                    return Input.Keys.F16;
                    
                case OpenTK.Input.Key.F17:
                    return Input.Keys.F17;
                    
                case OpenTK.Input.Key.F18:
                    return Input.Keys.F18;
                    
                case OpenTK.Input.Key.F19:
                    return Input.Keys.F19;
                    
                case OpenTK.Input.Key.F2:
                    return Input.Keys.F2;
                    
                case OpenTK.Input.Key.F20:
                    return Input.Keys.F20;
                    
                case OpenTK.Input.Key.F21:
                    return Input.Keys.F21;
                    
                case OpenTK.Input.Key.F22:
                    return Input.Keys.F22;
                    
                case OpenTK.Input.Key.F23:
                    return Input.Keys.F23;
                    
                case OpenTK.Input.Key.F24:
                    return Input.Keys.F24;
                    
                case OpenTK.Input.Key.F25:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F26:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F27:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F28:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F29:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F3:
                    return Input.Keys.F3;
                    
                case OpenTK.Input.Key.F30:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F31:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F32:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F33:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F34:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F35:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.F4:
                    return Input.Keys.F4;
                    
                case OpenTK.Input.Key.F5:
                    return Input.Keys.F5;
                    
                case OpenTK.Input.Key.F6:
                    return Input.Keys.F6;
                    
                case OpenTK.Input.Key.F7:
                    return Input.Keys.F7;
                    
                case OpenTK.Input.Key.F8:
                    return Input.Keys.F8;
                    
                case OpenTK.Input.Key.F9:
                    return Input.Keys.F9;
                    
                case OpenTK.Input.Key.G:
                    return Input.Keys.G;
                    
                case OpenTK.Input.Key.H:
                    return Input.Keys.H;
                    
                case OpenTK.Input.Key.Home:
                    return Input.Keys.Home;
                    
                case OpenTK.Input.Key.I:
                    return Input.Keys.I;
                    
                case OpenTK.Input.Key.Insert:
                    return Input.Keys.Insert;
                    
                case OpenTK.Input.Key.J:
                    return Input.Keys.J;
                    
                case OpenTK.Input.Key.K:
                    return Input.Keys.K;
                    
                case OpenTK.Input.Key.Keypad0:
                    return Input.Keys.NumPad0;
                    
                case OpenTK.Input.Key.Keypad1:
                    return Input.Keys.NumPad1;
                    
                case OpenTK.Input.Key.Keypad2:
                    return Input.Keys.NumPad2;
                    
                case OpenTK.Input.Key.Keypad3:
                    return Input.Keys.NumPad3;
                    
                case OpenTK.Input.Key.Keypad4:
                    return Input.Keys.NumPad4;
                    
                case OpenTK.Input.Key.Keypad5:
                    return Input.Keys.NumPad5;
                    
                case OpenTK.Input.Key.Keypad6:
                    return Input.Keys.NumPad6;
                    
                case OpenTK.Input.Key.Keypad7:
                    return Input.Keys.NumPad7;
                    
                case OpenTK.Input.Key.Keypad8:
                    return Input.Keys.NumPad8;
                    
                case OpenTK.Input.Key.Keypad9:
                    return Input.Keys.NumPad9;
                    
                case OpenTK.Input.Key.KeypadAdd:
                    return Input.Keys.Add;
                    
                case OpenTK.Input.Key.KeypadDecimal:
                    return Input.Keys.Decimal;
                    
                case OpenTK.Input.Key.KeypadDivide:
                    return Input.Keys.Divide;
                    
                case OpenTK.Input.Key.KeypadEnter:
                    return Input.Keys.Enter;
                    
                case OpenTK.Input.Key.KeypadMinus:
                    return Input.Keys.OemMinus;
                    
                case OpenTK.Input.Key.KeypadMultiply:
                    return Input.Keys.Multiply;
                    
                case OpenTK.Input.Key.L:
                    return Input.Keys.L;
                    
                case OpenTK.Input.Key.LShift:
                    return Input.Keys.LeftShift;
                    
                case OpenTK.Input.Key.LWin:
                    return Input.Keys.LeftWindows;
                    
                case OpenTK.Input.Key.Left:
                    return Input.Keys.Left;
                    
                case OpenTK.Input.Key.M:
                    return Input.Keys.M;
                    
                case OpenTK.Input.Key.Minus:
                    return Input.Keys.OemMinus;
                    
                case OpenTK.Input.Key.N:
                    return Input.Keys.N;
                    
                case OpenTK.Input.Key.NumLock:
                    return Input.Keys.NumLock;
                    
                case OpenTK.Input.Key.Number0:
                    return Input.Keys.D0;
                    
                case OpenTK.Input.Key.Number1:
                    return Input.Keys.D1;
                    
                case OpenTK.Input.Key.Number2:
                    return Input.Keys.D2;
                    
                case OpenTK.Input.Key.Number3:
                    return Input.Keys.D3;
                    
                case OpenTK.Input.Key.Number4:
                    return Input.Keys.D4;
                    
                case OpenTK.Input.Key.Number5:
                    return Input.Keys.D5;
                    
                case OpenTK.Input.Key.Number6:
                    return Input.Keys.D6;
                    
                case OpenTK.Input.Key.Number7:
                    return Input.Keys.D7;
                    
                case OpenTK.Input.Key.Number8:
                    return Input.Keys.D8;
                    
                case OpenTK.Input.Key.Number9:
                    return Input.Keys.D9;
                    
                case OpenTK.Input.Key.O:
                    return Input.Keys.O;
                    
                case OpenTK.Input.Key.P:
                    return Input.Keys.P;
                    
                case OpenTK.Input.Key.PageDown:
                    return Input.Keys.PageDown;
                    
                case OpenTK.Input.Key.PageUp:
                    return Input.Keys.PageUp;
                    
                case OpenTK.Input.Key.Pause:
                    return Input.Keys.Pause;
                    
                case OpenTK.Input.Key.Period:
                    return Input.Keys.OemPeriod;
                    
                case OpenTK.Input.Key.Plus:
                    return Input.Keys.OemPlus;
                    
                case OpenTK.Input.Key.PrintScreen:
                    return Input.Keys.PrintScreen;
                    
                case OpenTK.Input.Key.Q:
                    return Input.Keys.Q;
                    
                case OpenTK.Input.Key.Quote:
                    return Input.Keys.OemQuotes;
                    
                case OpenTK.Input.Key.R:
                    return Input.Keys.R;
                    
                case OpenTK.Input.Key.Right:
                    return Input.Keys.Right;

                case OpenTK.Input.Key.RShift:
                    return Input.Keys.RightShift;

                case OpenTK.Input.Key.RWin:
                    return Input.Keys.RightWindows;
                    
                case OpenTK.Input.Key.S:
                    return Input.Keys.S;
                    
                case OpenTK.Input.Key.ScrollLock:
                    return Input.Keys.Scroll;
                    
                case OpenTK.Input.Key.Semicolon:
                    return Input.Keys.OemSemicolon;
                    
                case OpenTK.Input.Key.Slash:
                    return Input.Keys.OemQuestion;
                    
                case OpenTK.Input.Key.Sleep:
                    return Input.Keys.Sleep;
                    
                case OpenTK.Input.Key.Space:
                    return Input.Keys.Space;
                    
                case OpenTK.Input.Key.T:
                    return Input.Keys.T;
                    
                case OpenTK.Input.Key.Tab:
                    return Input.Keys.Tab;
                    
                case OpenTK.Input.Key.Tilde:
                    return Input.Keys.OemTilde;
                    
                case OpenTK.Input.Key.U:
                    return Input.Keys.U;
                    
                case OpenTK.Input.Key.Unknown:
                    return Input.Keys.None;
                    
                case OpenTK.Input.Key.Up:
                    return Input.Keys.Up;
                    
                case OpenTK.Input.Key.V:
                    return Input.Keys.V;
                    
                case OpenTK.Input.Key.W:
                    return Input.Keys.W;
                    
                case OpenTK.Input.Key.X:
                    return Input.Keys.X;
                    
                case OpenTK.Input.Key.Y:
                    return Input.Keys.Y;
                    
                case OpenTK.Input.Key.Z:
                    return Input.Keys.Z;
                    
                default:
                    return Input.Keys.None;
                    
            }
        }
    }

    internal class WindowsGameWindow : GameWindow
    {
		private Rectangle clientBounds;
		private GameTime _updateGameTime;
        private GameTime _drawGameTime;
        private DateTime _lastUpdate;
		private DateTime _now;
        private static List<Microsoft.Xna.Framework.Input.Keys> keys = new List<Input.Keys>();

        public static List<Microsoft.Xna.Framework.Input.Keys> Keys
        {
            get
            {
                return keys;
            }
        }

        internal Game Game;
        internal OpenTK.GameWindow OpenTkGameWindow { get; private set; }

        public WindowsGameWindow() 
        {
            Initialize();
        }

        private void Initialize()
        {
            OpenTkGameWindow = new OpenTK.GameWindow();            
            OpenTkGameWindow.RenderFrame += OnRenderFrame;
            OpenTkGameWindow.Closing += new EventHandler<CancelEventArgs>(OpenTkGameWindow_Closing);
            OpenTkGameWindow.Resize += OnResize;
            OpenTkGameWindow.Keyboard.KeyDown += new EventHandler<OpenTK.Input.KeyboardKeyEventArgs>(Keyboard_KeyDown);
            OpenTkGameWindow.Keyboard.KeyUp += new EventHandler<OpenTK.Input.KeyboardKeyEventArgs>(Keyboard_KeyUp);
            clientBounds = new Rectangle(0, 0, OpenTkGameWindow.Width, OpenTkGameWindow.Height);
			
			// mouse
			// TODO review this when opentk 1.1 is released
			Mouse.UpdateMouseInfo(OpenTkGameWindow.Mouse);

            // Initialize GameTime
            _updateGameTime = new GameTime();
            _drawGameTime = new GameTime();

            // Initialize _lastUpdate
            _lastUpdate = DateTime.Now;

            //Default no resizing
            AllowUserResizing = false;
        }

        void OpenTkGameWindow_Closing(object sender,CancelEventArgs e)
        {        	
        	Game.Exit();
        }

        void Keyboard_KeyUp(object sender, OpenTK.Input.KeyboardKeyEventArgs e)
        {
            if (keys.Contains(e.Key.ToXna())) keys.Remove(e.Key.ToXna());
        }

        void Keyboard_KeyDown(object sender, OpenTK.Input.KeyboardKeyEventArgs e)
        {
            if (!keys.Contains(e.Key.ToXna())) keys.Add(e.Key.ToXna());
        }

        // This method should only be called when necessary like when the Guide is displayed
        internal void ClearKeyCacheState()
        {
            keys.Clear();
        }

        
        #region GameWindow Methods

        private void OnResize(object sender, EventArgs e)
        {
            Game.GraphicsDevice.SizeChanged(OpenTkGameWindow.ClientRectangle.Width, OpenTkGameWindow.ClientRectangle.Height);
            OnClientSizeChanged();
        }

        private void OnRenderFrame(object sender, FrameEventArgs e)
        {
            if (GraphicsContext.CurrentContext == null || GraphicsContext.CurrentContext.IsDisposed)
                return;

            //Should not happen at all..
            if (!GraphicsContext.CurrentContext.IsCurrent)
                OpenTkGameWindow.MakeCurrent();

            if (Game != null) {
                _now = DateTime.Now;
		_updateGameTime.Update(_now - _lastUpdate);
            	Game.DoUpdate(_updateGameTime);

                _drawGameTime.Update(_now - _lastUpdate);
                _lastUpdate = _now;
                Game.DoDraw(_drawGameTime);
            }

            OpenTkGameWindow.SwapBuffers();
        }

		#endregion

        public override IntPtr Handle
        {
            get { return IntPtr.Zero; }
        }

        public override string ScreenDeviceName 
		{
			get { return string.Empty; }
		}

		public override Rectangle ClientBounds 
		{
			get 
			{
				return clientBounds;
			}
		}

        protected override void SetTitle(string title)
        {
            OpenTkGameWindow.Title = title;
        }

        public new string Title
        {
            get { return OpenTkGameWindow.Title; }
            set { SetTitle(value); }
        }

        private bool _allowUserResizing;
        public override bool AllowUserResizing
        {
            get { return _allowUserResizing; }
            set
            {
                _allowUserResizing = value;

                if (_allowUserResizing)
                    OpenTkGameWindow.WindowBorder = WindowBorder.Resizable;
                else OpenTkGameWindow.WindowBorder = WindowBorder.Fixed;
            }
        }

		public override DisplayOrientation CurrentOrientation 
		{
            get { return DisplayOrientation.LandscapeLeft; }
		}

        protected internal override void SetSupportedOrientations(DisplayOrientation orientations)
        {
            // Do nothing.  Desktop platforms don't do orientations.
        }

        public override void BeginScreenDeviceChange(bool willBeFullScreen)
        {
           
        }

        public override void EndScreenDeviceChange(string screenDeviceName, int clientWidth, int clientHeight)
        {
           
        }
    }
}