-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathGame.cs
1190 lines (1001 loc) · 42.3 KB
/
Game.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.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace XNALara
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game : Microsoft.Xna.Framework.Game
{
public string ReleaseProductName {
get { return "XNALara"; }
}
public string ReleaseVersion {
get { return "9.7.7"; }
}
public string ReleaseDate {
get { return "July 2011"; }
}
public static readonly Color DefaultBackgroundColor = new Color(72, 72, 72);
public const int DefaultCanvasWidth = 700;
public const int DefaultCanvasHeight = 700;
private System.Windows.Forms.Form gameForm;
private GraphicsDeviceManager graphicsDeviceManager;
private ShaderProfile maxVertexShaderProfile;
private ShaderProfile maxPixelShaderProfile;
private bool supportsShadersV3;
private bool forceShadersV3;
private bool enableAddModelMultiSelect = true;
private bool renderLogo = false;
private CameraTurnTable camera;
private CameraTurnTableState cameraSavedState;
private TextureManager textureManager;
private Renderer renderer;
private HUD hud;
private List<Armature> armatures = new List<Armature>();
private BoneSelector boneSelector;
private Point mouseAnchor;
private int mouseWheel = 0;
private bool mouseLeftDragging;
private bool mouseRightDragging;
private bool disableBoneSelection;
private bool boneTransformActive = false;
private Color backgroundColor = DefaultBackgroundColor;
private BackgroundImage backgroundImage = null;
private bool displayTextures = true;
private bool enableBumpMaps = true;
private bool displayWireframe = false;
private bool backFaceCulling = false;
private bool alwaysForceCulling = false;
private bool displayBones = true;
private bool displayBoneNames = true;
private bool displayGround = true;
private bool useAlternativeReflection = false;
private bool displaySkyDome = false;
private float skyDomeRotation = 0;
private float skyDomeElevation = 0;
private float light1AngleHorizontal;
private float light1AngleVertical;
private Vector3 light1Direction;
private float light1Intensity;
private Color light1Color;
private float light1ShadowDepth;
private float light2AngleHorizontal;
private float light2AngleVertical;
private Vector3 light2Direction;
private float light2Intensity;
private Color light2Color;
private float light2ShadowDepth;
private float light3AngleHorizontal;
private float light3AngleVertical;
private Vector3 light3Direction;
private float light3Intensity;
private Color light3Color;
private float light3ShadowDepth;
private float postProcessBrightness;
private float postProcessGamma;
private float postProcessContrast;
private float postProcessSaturation;
private ControlGUI controlGUI;
private bool hasFocus = false;
private bool forceExit = false;
private SplashForm splash;
private Timer timerSplash;
public Game() {
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
this.Window.Title = string.Format("{0} {1} by Dusan Pavlicek (c) {2}",
ReleaseProductName, ReleaseVersion, ReleaseDate);
this.Window.AllowUserResizing = true;
this.Window.ClientSizeChanged += new EventHandler(WindowSizeChanged);
Content.RootDirectory = "Content";
graphicsDeviceManager = new GraphicsDeviceManager(this);
graphicsDeviceManager.PreferMultiSampling = true;
graphicsDeviceManager.PreparingDeviceSettings +=
new EventHandler<PreparingDeviceSettingsEventArgs>(OnPreparingDeviceSettings);
gameForm = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(this.Window.Handle);
gameForm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(OnFormClosing);
gameForm.Icon = System.Drawing.Icon.FromHandle(Properties.Resources.XNALaraIcon22.GetHicon());
gameForm.ShowIcon = true;
splash = new SplashForm();
splash.Owner = gameForm;
splash.TopMost = true;
splash.Show();
splash.Update();
timerSplash = new Timer(SplashTimerTimeout, null, 2000, System.Threading.Timeout.Infinite);
}
private void SplashTimerTimeout(object state) {
timerSplash.Dispose();
timerSplash = null;
if (this.splash.InvokeRequired) {
this.splash.Invoke(new CloseSplashDelegate(CloseSplash));
}
else {
CloseSplash();
}
}
private delegate void CloseSplashDelegate();
private void CloseSplash() {
if (splash != null) {
splash.Hide();
splash.Dispose();
splash = null;
}
if (controlGUI != null) {
controlGUI.TopMost = controlGUI.PreferredAlwaysOnTop;
}
}
public System.Drawing.Size CanvasSize {
set {
gameForm.WindowState = System.Windows.Forms.FormWindowState.Normal;
System.Drawing.Rectangle screenSize = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
if (value.Width < screenSize.Width && value.Height < screenSize.Height) {
graphicsDeviceManager.PreferredBackBufferWidth = value.Width;
graphicsDeviceManager.PreferredBackBufferHeight = value.Height;
graphicsDeviceManager.ApplyChanges();
WindowSizeChanged();
camera.FieldOfViewHorizontal = camera.FieldOfViewHorizontal;
}
else {
gameForm.WindowState = System.Windows.Forms.FormWindowState.Maximized;
bool savedTopMost = controlGUI.TopMost;
controlGUI.TopMost = false;
System.Windows.Forms.MessageBox.Show("Requested canvas size is larger than the screen.", "Warning!",
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
controlGUI.TopMost = savedTopMost;
}
}
get {
return new System.Drawing.Size(this.Window.ClientBounds.Width, this.Window.ClientBounds.Height);
}
}
public System.Windows.Forms.Form GameForm {
get { return gameForm; }
}
public HUD HUD {
get { return hud; }
}
public bool HasFocus {
set { hasFocus = value; }
get { return hasFocus; }
}
public TextureManager TextureManager {
get { return textureManager; }
}
public Renderer Renderer {
get { return renderer; }
}
public ShaderProfile MaxVertexShaderProfile {
get { return maxVertexShaderProfile; }
}
public ShaderProfile MaxPixelShaderProfile {
get { return maxPixelShaderProfile; }
}
public bool SupportsShadersV3 {
get { return supportsShadersV3; }
}
public bool ForceShadersV3 {
set { forceShadersV3 = value; }
get { return forceShadersV3; }
}
public bool EnableAddModelMultiSelect {
set { enableAddModelMultiSelect = value; }
get { return enableAddModelMultiSelect; }
}
public bool RenderLogo {
set { renderLogo = value; }
get { return renderLogo; }
}
public CameraTurnTable Camera {
get { return camera; }
}
public ControlGUI ControlGUI {
get { return controlGUI; }
}
public BoneSelector BoneSelector {
get { return boneSelector; }
}
public Color BackgroundColor {
set { backgroundColor = value; }
get { return backgroundColor; }
}
public BackgroundImage BackgroundImage {
set { backgroundImage = value; }
get { return backgroundImage; }
}
public bool DisplayBones {
set { displayBones = value; }
get { return displayBones; }
}
public bool DisplayBoneNames {
set { displayBoneNames = value; }
get { return displayBoneNames; }
}
public bool DisplayTextures {
set { displayTextures = value; }
get { return displayTextures; }
}
public bool EnableBumpMaps {
set { enableBumpMaps = value; }
get { return enableBumpMaps; }
}
public bool DisplayWireframe {
set { displayWireframe = value; }
get { return displayWireframe; }
}
public bool BackFaceCulling {
set { backFaceCulling = value; }
get { return backFaceCulling; }
}
public bool AlwaysForceCulling {
set { alwaysForceCulling = value; }
get { return alwaysForceCulling; }
}
public bool DisplayGround {
set { displayGround = value; }
get { return displayGround; }
}
public bool UseAlternativeReflection {
set { useAlternativeReflection = value; }
get { return useAlternativeReflection; }
}
public bool DisplaySkyDome {
set { displaySkyDome = value; }
get { return displaySkyDome; }
}
public float SkyDomeRotation {
set { skyDomeRotation = value; }
get { return skyDomeRotation; }
}
public float SkyDomeElevation {
set { skyDomeElevation = value; }
get { return skyDomeElevation; }
}
public float Light1AngleHorizontal {
set {
light1AngleHorizontal = value;
light1Direction =
CalculateLightDirection(
light1AngleHorizontal,
light1AngleVertical);
}
get {
return light1AngleHorizontal;
}
}
public float Light1AngleVertical {
set {
light1AngleVertical = value;
light1Direction =
CalculateLightDirection(
light1AngleHorizontal,
light1AngleVertical);
}
get {
return light1AngleVertical;
}
}
public Vector3 Light1Direction {
set {
light1Direction = value;
light1Direction.Normalize();
ExtractLightAngles(
light1Direction,
out light1AngleHorizontal,
out light1AngleVertical);
}
get {
return light1Direction;
}
}
public float Light1Intensity {
set { light1Intensity = value; }
get { return light1Intensity; }
}
public Color Light1Color {
set { light1Color = value; }
get { return light1Color; }
}
public float Light1ShadowDepth {
set {
light1ShadowDepth = value;
if (light1ShadowDepth < 0) {
light1ShadowDepth = 0;
}
if (light1ShadowDepth > 1) {
light1ShadowDepth = 1;
}
}
get { return light1ShadowDepth; }
}
public float Light2AngleHorizontal {
set {
light2AngleHorizontal = value;
light2Direction =
CalculateLightDirection(
light2AngleHorizontal,
light2AngleVertical);
}
get {
return light2AngleHorizontal;
}
}
public float Light2AngleVertical {
set {
light2AngleVertical = value;
light2Direction =
CalculateLightDirection(
light2AngleHorizontal,
light2AngleVertical);
}
get {
return light2AngleVertical;
}
}
public Vector3 Light2Direction {
set {
light2Direction = value;
light2Direction.Normalize();
ExtractLightAngles(
light2Direction,
out light2AngleHorizontal,
out light2AngleVertical);
}
get {
return light2Direction;
}
}
public float Light2Intensity {
set { light2Intensity = value; }
get { return light2Intensity; }
}
public Color Light2Color {
set { light2Color = value; }
get { return light2Color; }
}
public float Light2ShadowDepth {
set {
light2ShadowDepth = value;
if (light2ShadowDepth < 0) {
light2ShadowDepth = 0;
}
if (light2ShadowDepth > 1) {
light2ShadowDepth = 1;
}
}
get { return light2ShadowDepth; }
}
public float Light3AngleHorizontal {
set {
light3AngleHorizontal = value;
light3Direction =
CalculateLightDirection(
light3AngleHorizontal,
light3AngleVertical);
}
get {
return light3AngleHorizontal;
}
}
public float Light3AngleVertical {
set {
light3AngleVertical = value;
light3Direction =
CalculateLightDirection(
light3AngleHorizontal,
light3AngleVertical);
}
get {
return light3AngleVertical;
}
}
public Vector3 Light3Direction {
set {
light3Direction = value;
light3Direction.Normalize();
ExtractLightAngles(
light3Direction,
out light3AngleHorizontal,
out light3AngleVertical);
}
get {
return light3Direction;
}
}
public float Light3Intensity {
set { light3Intensity = value; }
get { return light3Intensity; }
}
public Color Light3Color {
set { light3Color = value; }
get { return light3Color; }
}
public float Light3ShadowDepth {
set {
light3ShadowDepth = value;
if (light3ShadowDepth < 0) {
light3ShadowDepth = 0;
}
if (light3ShadowDepth > 1) {
light3ShadowDepth = 1;
}
}
get { return light3ShadowDepth; }
}
public float PostProcessBrightness {
get { return postProcessBrightness; }
set { postProcessBrightness = value; }
}
public float PostProcessGamma {
get { return postProcessGamma; }
set { postProcessGamma = value; }
}
public float PostProcessContrast {
get { return postProcessContrast; }
set { postProcessContrast = value; }
}
public float PostProcessSaturation {
get { return postProcessSaturation; }
set { postProcessSaturation = value; }
}
public bool IsPostProcessingActive {
get {
return
postProcessBrightness != 1.0f ||
postProcessGamma != 1.0f ||
postProcessContrast != 1.0f ||
postProcessSaturation != 1.0f;
}
}
private Vector3 CalculateLightDirection(float lightAngleHorizontal, float lightAngleVertical) {
double t = MathHelper.ToRadians(lightAngleHorizontal);
double f = Math.PI / 2 - MathHelper.ToRadians(lightAngleVertical);
double x = Math.Cos(t) * Math.Sin(f);
double y = Math.Sin(t) * Math.Sin(f);
double z = Math.Cos(f);
return -new Vector3((float)x, (float)z, (float)-y);
}
private void ExtractLightAngles(Vector3 lightDirection, out float lightAngleHorizontal, out float lightAngleVertical) {
double x = -lightDirection.X;
double y = lightDirection.Z;
double z = -lightDirection.Y;
double t = Math.Atan2(y, x);
double f = Math.PI / 2 - Math.Acos(z / lightDirection.Length());
lightAngleHorizontal = MathHelper.ToDegrees((float)t);
lightAngleVertical = MathHelper.ToDegrees((float)f);
}
public void HandleItemAdded(Item item) {
Model model = item.Model;
armatures.Add(model.Armature);
renderer.AddItem(item);
boneSelector.AddModel(model);
}
public void HandleItemRemoved(Item item) {
Model model = item.Model;
armatures.Remove(model.Armature);
renderer.RemoveItem(item);
boneSelector.RemoveModel(model);
foreach (Mesh mesh in model.Meshes) {
textureManager.HandleMeshRemoved(mesh);
}
}
private void WindowSizeChanged(object sender, EventArgs e) {
WindowSizeChanged();
}
private void WindowSizeChanged() {
Rectangle rect = this.Window.ClientBounds;
if (boneSelector != null) {
boneSelector.HandleWindowSizeChanged(rect.Width, rect.Height);
}
if (backgroundImage != null) {
backgroundImage.HandleWindowResized(rect.Width, rect.Height);
}
}
private void OnPreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e) {
GraphicsAdapter adapter = e.GraphicsDeviceInformation.Adapter;
GraphicsDeviceCapabilities caps = adapter.GetCapabilities(DeviceType.Hardware);
maxVertexShaderProfile = caps.MaxVertexShaderProfile;
maxPixelShaderProfile = caps.MaxPixelShaderProfile;
supportsShadersV3 = (maxVertexShaderProfile >= ShaderProfile.VS_3_0 && maxPixelShaderProfile >= ShaderProfile.PS_3_0);
SurfaceFormat format = adapter.CurrentDisplayMode.Format;
PresentationParameters presentParams = e.GraphicsDeviceInformation.PresentationParameters;
if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.EightSamples)) {
presentParams.MultiSampleQuality = 0;
presentParams.MultiSampleType = MultiSampleType.EightSamples;
return;
}
if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.FourSamples)) {
presentParams.MultiSampleQuality = 0;
presentParams.MultiSampleType = MultiSampleType.FourSamples;
return;
}
if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.TwoSamples)) {
presentParams.MultiSampleQuality = 0;
presentParams.MultiSampleType = MultiSampleType.TwoSamples;
return;
}
}
/// <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() {
graphicsDeviceManager.PreferredBackBufferWidth = DefaultCanvasWidth;
graphicsDeviceManager.PreferredBackBufferHeight = DefaultCanvasHeight;
graphicsDeviceManager.IsFullScreen = false;
graphicsDeviceManager.ApplyChanges();
this.IsFixedTimeStep = false;
this.IsMouseVisible = true;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent() {
CheckContentDirectory();
textureManager = new TextureManager(this);
camera = new CameraTurnTable(this.GraphicsDevice, this.Window);
ResetCamera(camera);
ModelLoader modelLoader = new ModelLoader(this);
boneSelector = new BoneSelector(this);
ResetLights();
ForceShadersV3 = LightingParamsDialog.DefaultForceShadersV3;
ResetPostProcessParams();
renderer = new Renderer(this);
renderer.Init();
hud = new HUD(this);
controlGUI = new ControlGUI(this);
if (splash == null) {
controlGUI.TopMost = controlGUI.PreferredAlwaysOnTop;
}
controlGUI.Show();
}
public void ResetCamera(CameraTurnTable camera) {
camera.SetClippingPlanes(0.05f, 150.0f);
camera.FieldOfViewHorizontal = MathHelper.ToRadians(CameraParamsDialog.DefaultCameraFOV);
camera.Target = CameraParamsDialog.DefaultCameraTarget;
camera.Distance = CameraParamsDialog.DefaultCameraDistance;
camera.SetRotation(MathHelper.ToRadians(CameraParamsDialog.DefaultCameraRotationHoriz), MathHelper.ToRadians(CameraParamsDialog.DefaultCameraRotationVert));
}
public void ResetLights() {
Light1Direction = LightingParamsDialog.DefaultLight1Direction;
Light1Intensity = LightingParamsDialog.DefaultLight1Intensity;
Light1Color = LightingParamsDialog.DefaultLight1Color;
Light1ShadowDepth = LightingParamsDialog.DefaultLight1ShadowDepth;
Light2Direction = LightingParamsDialog.DefaultLight2Direction;
Light2Intensity = LightingParamsDialog.DefaultLight2Intensity;
Light2Color = LightingParamsDialog.DefaultLight2Color;
Light2ShadowDepth = LightingParamsDialog.DefaultLight2ShadowDepth;
Light3Direction = LightingParamsDialog.DefaultLight3Direction;
Light3Intensity = LightingParamsDialog.DefaultLight3Intensity;
Light3Color = LightingParamsDialog.DefaultLight3Color;
Light3ShadowDepth = LightingParamsDialog.DefaultLight3ShadowDepth;
}
public void ResetPostProcessParams() {
PostProcessBrightness = PostProcessParamsDialog.DefaultBrightness;
PostProcessGamma = PostProcessParamsDialog.DefaultGamma;
PostProcessContrast = PostProcessParamsDialog.DefaultContrast;
PostProcessSaturation = PostProcessParamsDialog.DefaultSaturation;
}
private void CheckContentDirectory() {
string currDir = Directory.GetCurrentDirectory();
string contentDir = currDir + @"\Content";
if (!Directory.Exists(contentDir)) {
System.Windows.Forms.MessageBox.Show(
"Directory \"Content\" not found.\n" +
"Current directory: " + currDir + "\n",
"Error!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
Environment.Exit(0);
}
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent() {
}
/// <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>
protected override void Update(GameTime gameTime) {
float deltaMilliseconds = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (hasFocus) {
ProcessMouse(deltaMilliseconds);
ProcessKeyboard(deltaMilliseconds);
}
foreach (Armature armature in armatures) {
armature.UpdateBoneMatrices();
}
hud.Update(gameTime);
base.Update(gameTime);
}
private void ProcessMouse(float deltaMilliseconds) {
MouseState mouseState = Mouse.GetState();
KeyboardState keyboardState = Keyboard.GetState();
bool axisKeyPressed = keyboardState.IsKeyDown(Keys.NumPad1) ||
keyboardState.IsKeyDown(Keys.NumPad2) ||
keyboardState.IsKeyDown(Keys.NumPad3) ||
keyboardState.IsKeyDown(Keys.Q) ||
keyboardState.IsKeyDown(Keys.W) ||
keyboardState.IsKeyDown(Keys.E);
bool ctrlKeyPressed = keyboardState.IsKeyDown(Keys.LeftControl) ||
keyboardState.IsKeyDown(Keys.RightControl);
bool shiftKeyPressed = keyboardState.IsKeyDown(Keys.LeftShift) ||
keyboardState.IsKeyDown(Keys.RightShift);
bool altKeyPressed = keyboardState.IsKeyDown(Keys.LeftAlt) ||
keyboardState.IsKeyDown(Keys.RightAlt);
if (axisKeyPressed ||
ctrlKeyPressed ||
shiftKeyPressed ||
altKeyPressed) {
disableBoneSelection = true;
}
else {
if (!mouseLeftDragging) {
disableBoneSelection = false;
}
}
if (mouseState.LeftButton == ButtonState.Pressed) {
if (displayBones && !disableBoneSelection) {
Armature.Bone selectedBone = boneSelector.SelectedBone;
if (selectedBone != null) {
controlGUI.HandleBoneSelectedIn3D(selectedBone);
}
}
if (!mouseLeftDragging) {
mouseLeftDragging = true;
mouseAnchor = new Point(mouseState.X, mouseState.Y);
}
}
else {
mouseLeftDragging = false;
disableBoneSelection = false;
if (boneTransformActive) {
boneTransformActive = false;
controlGUI.UndoSaveInterrupt();
}
}
if (mouseState.RightButton == ButtonState.Pressed) {
if (!mouseRightDragging) {
mouseRightDragging = true;
mouseAnchor = new Point(mouseState.X, mouseState.Y);
}
}
else {
mouseRightDragging = false;
}
if (mouseLeftDragging && mouseRightDragging) {
int dx = mouseState.X - mouseAnchor.X;
int dy = mouseState.Y - mouseAnchor.Y;
mouseAnchor = new Point(mouseState.X, mouseState.Y);
if (!controlGUI.IsCameraLocked) {
camera.Target -= (camera.Left * dx - camera.Up * dy) * 0.005f;
}
return;
}
if (mouseLeftDragging) {
int dx = mouseState.X - mouseAnchor.X;
int dy = mouseState.Y - mouseAnchor.Y;
mouseAnchor = new Point(mouseState.X, mouseState.Y);
if (axisKeyPressed) {
Armature.Bone selectedBone = controlGUI.SelectedBone;
if (selectedBone != null) {
boneTransformActive = true;
if (keyboardState.IsKeyDown(Keys.NumPad1) ||
keyboardState.IsKeyDown(Keys.Q)) {
float angle = controlGUI.GetBoneRotationX(selectedBone);
controlGUI.HandleBoneRotationXChanged(selectedBone, angle + (dx + dy) * 0.5f);
}
if (keyboardState.IsKeyDown(Keys.NumPad2) ||
keyboardState.IsKeyDown(Keys.W)) {
float angle = controlGUI.GetBoneRotationY(selectedBone);
controlGUI.HandleBoneRotationYChanged(selectedBone, angle + (dx + dy) * 0.5f);
}
if (keyboardState.IsKeyDown(Keys.NumPad3) ||
keyboardState.IsKeyDown(Keys.E)) {
float angle = controlGUI.GetBoneRotationZ(selectedBone);
controlGUI.HandleBoneRotationZChanged(selectedBone, angle + (dx + dy) * 0.5f);
}
}
return;
}
if (ctrlKeyPressed) {
Item selectedItem = controlGUI.SelectedItem;
if (selectedItem != null) {
Vector3 point;
if (ProjectMouseToGround(mouseState.X, mouseState.Y, out point)) {
Armature armature = selectedItem.Model.Armature;
float height = armature.WorldTranslation.Y;
armature.WorldTranslation = new Vector3(point.X, height, point.Z);
controlGUI.HandlePositionChanged(armature.WorldTranslation);
}
}
return;
}
if (!controlGUI.IsCameraLocked) {
if (shiftKeyPressed) {
camera.Target -= (camera.Left * dx - camera.Up * dy) * 0.005f;
return;
}
float rotationHorizontal = camera.RotationHorizontal - dx * 0.01f;
float rotationVertical = camera.RotationVertical + dy * 0.01f;
camera.SetRotation(rotationHorizontal, rotationVertical);
}
}
if (mouseRightDragging) {
if (!controlGUI.IsCameraLocked) {
int dy = mouseState.Y - mouseAnchor.Y;
mouseAnchor = new Point(mouseState.X, mouseState.Y);
if (!shiftKeyPressed) {
camera.Distance += dy * 0.01f;
}
else {
camera.Target -= camera.Forward * dy * 0.005f;
}
}
}
if (!mouseLeftDragging && !mouseRightDragging) {
boneSelector.HandleMouseMoved(mouseState.X, mouseState.Y);
}
if (mouseWheel != mouseState.ScrollWheelValue) {
if (!controlGUI.IsCameraLocked) {
int delta = mouseWheel - mouseState.ScrollWheelValue;
camera.Distance += delta * 0.002f;
}
mouseWheel = mouseState.ScrollWheelValue;
}
}
private void ProcessKeyboard(float deltaMilliseconds) {
KeyboardState keyboardState = Keyboard.GetState();
KeyboardEventHandler.ProcessKeyboardState(keyboardState);
if (KeyboardEventHandler.HasKeyBeenPressed(Keys.NumPad0)) {
Armature.Bone selectedBone = controlGUI.SelectedBone;
if (selectedBone != null) {
controlGUI.HandleBoneRotationXChanged(selectedBone, 0);
controlGUI.HandleBoneRotationYChanged(selectedBone, 0);
controlGUI.HandleBoneRotationZChanged(selectedBone, 0);
}
}
bool ctrlKeyPressed = keyboardState.IsKeyDown(Keys.LeftControl) ||
keyboardState.IsKeyDown(Keys.RightControl);
bool shiftKeyPressed = keyboardState.IsKeyDown(Keys.LeftShift) ||
keyboardState.IsKeyDown(Keys.RightShift);
bool altKeyPressed = keyboardState.IsKeyDown(Keys.LeftAlt) ||
keyboardState.IsKeyDown(Keys.RightAlt);
if (keyboardState.IsKeyDown(Keys.Up) ||
keyboardState.IsKeyDown(Keys.Down) ||
keyboardState.IsKeyDown(Keys.Left) ||
keyboardState.IsKeyDown(Keys.Right)) {
Item selectedItem = controlGUI.SelectedItem;
if (selectedItem != null) {
Armature armature = selectedItem.Model.Armature;
float speed = shiftKeyPressed ? 3e-3f : 1e-3f;
if (!altKeyPressed) {
Vector3 dir = Vector3.Zero;
if (keyboardState.IsKeyDown(Keys.Up)) {
dir += DetermineMovementVector(Keys.Up);
}
if (keyboardState.IsKeyDown(Keys.Down)) {
dir += DetermineMovementVector(Keys.Down);
}
if (keyboardState.IsKeyDown(Keys.Left)) {
dir += DetermineMovementVector(Keys.Left);
}
if (keyboardState.IsKeyDown(Keys.Right)) {
dir += DetermineMovementVector(Keys.Right);
}
armature.WorldTranslation += dir * speed;
controlGUI.HandlePositionChanged(armature.WorldTranslation);
}
else {
float dir = 0;
if (keyboardState.IsKeyDown(Keys.Up)) {
dir += 1;
}
if (keyboardState.IsKeyDown(Keys.Down)) {
dir -= 1;
}
float height = armature.WorldTranslation.Y;
height += dir * speed;
controlGUI.HandleHeightChanged(height);
}
}
}
if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F2)) {
cameraSavedState = camera.CameraState;
hud.Message = "camera state saved";
}
if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F3)) {
if (cameraSavedState != null) {
camera.CameraState = cameraSavedState;
hud.Message = "camera state loaded";
}
}
if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F5)) {
controlGUI.QuickSaveImage();
}
if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F8)) {
controlGUI.ReloadTextures();
}
if (KeyboardEventHandler.HasKeyBeenPressed(Keys.F12)) {
if (controlGUI.IsDisposed) {
controlGUI = new ControlGUI(this);
}
controlGUI.Show();