VirtualGamePadGame.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5. using MonoGame.Framework.Devices.Sensors;
  6. namespace VirtualGamePad
  7. {
  8. public class VirtualGamePadGame : Game
  9. {
  10. GraphicsDeviceManager graphics;
  11. SpriteBatch spriteBatch;
  12. Texture2D gamepadTexture, caracter;
  13. Vector2 position = new Vector2();
  14. Color caracterColor = Color.White;
  15. SpriteFont font;
  16. public VirtualGamePadGame()
  17. {
  18. graphics = new GraphicsDeviceManager(this);
  19. Content.RootDirectory = "Content";
  20. }
  21. protected override void Initialize()
  22. {
  23. base.Initialize();
  24. }
  25. protected override void LoadContent()
  26. {
  27. spriteBatch = new SpriteBatch(GraphicsDevice);
  28. gamepadTexture = Content.Load<Texture2D>("gamepad");
  29. caracter = Content.Load<Texture2D>("monogameicon");
  30. font = Content.Load<SpriteFont>("font");
  31. // Set the virtual GamePad
  32. /* TODO ButtonDefinition BButton = new ButtonDefinition();
  33. BButton.Texture = texture;
  34. BButton.Position = new Vector2(200,150);
  35. BButton.Type = Buttons.B;
  36. BButton.TextureRect = new Rectangle(72,77,36,36);
  37. ButtonDefinition AButton = new ButtonDefinition();
  38. AButton.Texture = texture;
  39. AButton.Position = new Vector2(150,150);
  40. AButton.Type = Buttons.A;
  41. AButton.TextureRect = new Rectangle(73,114,36,36);
  42. GamePad.ButtonsDefinitions.Add(BButton);
  43. GamePad.ButtonsDefinitions.Add(AButton);
  44. ThumbStickDefinition thumbStick = new ThumbStickDefinition();
  45. thumbStick.Position = new Vector2(200,200);
  46. thumbStick.Texture = texture;
  47. thumbStick.TextureRect = new Rectangle(2,2,68,68);
  48. GamePad.LeftThumbStickDefinition = thumbStick;*/
  49. }
  50. protected override void Update(GameTime gameTime)
  51. {
  52. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  53. this.Exit();
  54. if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
  55. caracterColor = Color.Green;
  56. if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
  57. caracterColor = Color.Red;
  58. GamePadState gamepadStatus = GamePad.GetState(PlayerIndex.One);
  59. position.Y += (int)(gamepadStatus.ThumbSticks.Left.Y * -4);
  60. position.X += (int)(gamepadStatus.ThumbSticks.Left.X * 4);
  61. // right
  62. if (position.X + caracter.Width > Window.ClientBounds.Width)
  63. {
  64. position.X = Window.ClientBounds.Width - caracter.Width;
  65. }
  66. // bottom
  67. if (position.Y + caracter.Height > Window.ClientBounds.Height)
  68. {
  69. position.Y = Window.ClientBounds.Height - caracter.Height;
  70. }
  71. // left
  72. if (position.X < 0)
  73. {
  74. position.X = 0;
  75. }
  76. // top
  77. if (position.Y < 0)
  78. {
  79. position.Y = 0;
  80. }
  81. base.Update(gameTime);
  82. }
  83. protected override void Draw(GameTime gameTime)
  84. {
  85. graphics.GraphicsDevice.Clear(Color.MonoGameOrange);
  86. spriteBatch.Begin();
  87. spriteBatch.Draw(caracter, position, caracterColor);
  88. spriteBatch.DrawString(font, GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.ToString(),Vector2.One,Color.Black);
  89. //spriteBatch.DrawString(font,Accelerometer.GetState().Acceleration.ToString(),new Vector2(1,40),Color.Black);
  90. // Draw the virtual GamePad
  91. // TODO GamePad.Draw(gameTime,spriteBatch);
  92. spriteBatch.End();
  93. base.Draw(gameTime);
  94. }
  95. }
  96. }