Advertisement
ccyoulater

Mouse Off Set Problem

Feb 15th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.58 KB | None | 0 0
  1.  
  2. -------Game1.cs-----------
  3.  
  4.  
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. using System.Collections.Generic;
  9.  
  10. namespace PhysicsStudio_XNA
  11. {
  12.     /// <summary>
  13.     /// This is the main type for your game.
  14.     /// </summary>
  15.     public class Game1 : Game
  16.     {
  17.         GraphicsDeviceManager graphics;
  18.         SpriteBatch spriteBatch;
  19.         SpriteFont spriteFont;
  20.  
  21.         public const int WINDOW_HEIGHT = 800;
  22.         public const int WINDOW_WIDTH = 600;
  23.  
  24.         public int tree;
  25.  
  26.         public TitleScreen titleScreen;
  27.         public SATDemo satDemo;
  28.         public SeparatingAxisTest separatingAxisTest;
  29.         public SATWithAABB sATWithAABB;
  30.  
  31.         //Mouse Position Test
  32.         public MouseState MouseTestState;
  33.         static int CounterTest;
  34.    
  35.  
  36.  
  37.  
  38.         GameState currentState;
  39.  
  40.         public static Dictionary<string, Texture2D> m_textureLibrary = new Dictionary<string, Texture2D>();
  41.         public static Dictionary<string, SpriteFont> m_fontLibrary = new Dictionary<string, SpriteFont>();
  42.  
  43.  
  44.  
  45.         public Game1()
  46.         {
  47.             graphics = new GraphicsDeviceManager(this);
  48.             Content.RootDirectory = "Content";
  49.             //graphics.IsFullScreen = true;
  50.            
  51.             graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
  52.             graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
  53.  
  54.  
  55.  
  56.         }
  57.  
  58.         /// <summary>
  59.         /// Allows the game to perform any initialization it needs to before starting to run.
  60.         /// This is where it can query for any required services and load any non-graphic
  61.         /// related content.  Calling base.Initialize will enumerate through any components
  62.         /// and initialize them as well.
  63.         /// </summary>
  64.         protected override void Initialize()
  65.         {
  66.             Mouse.WindowHandle = this.Window.Handle;
  67.             //enable the mousepointer
  68.             IsMouseVisible = true;
  69.             currentState = GameState.TitleScreen;
  70.             //sets the windows mouse handle to client bounds handle
  71.            
  72.            
  73.          
  74.  
  75.             //Button.
  76.  
  77.             base.Initialize();
  78.         }
  79.  
  80.         public void RequestSATDemo()
  81.         {
  82.             currentState = GameState.RequestSATDemo;
  83.  
  84.         }
  85.  
  86.         /// <summary>
  87.         /// LoadContent will be called once per game and is the place to load
  88.         /// all of your content.
  89.         /// </summary>
  90.         protected override void LoadContent()
  91.         {
  92.             // Create a new SpriteBatch, which can be used to draw textures.
  93.             spriteBatch = new SpriteBatch(GraphicsDevice);
  94.  
  95.  
  96.             m_textureLibrary.Add("Pixel", Content.Load<Texture2D>("White_Pixel"));
  97.             m_fontLibrary.Add("Font", Content.Load<SpriteFont>("MotorwerkOblique"));
  98.  
  99.             titleScreen = new TitleScreen();
  100.             satDemo = new SATDemo();
  101.             separatingAxisTest = new SeparatingAxisTest();
  102.             sATWithAABB = new SATWithAABB();
  103.  
  104.  
  105.             // TODO: use this.Content to load your game content here
  106.         }
  107.  
  108.         /// <summary>
  109.         /// UnloadContent will be called once per game and is the place to unload
  110.         /// game-specific content.
  111.         /// </summary>
  112.  
  113.  
  114.         public void RequestSeparatingAxisTest()
  115.         {
  116.             currentState = GameState.SeparatingAxisTest;
  117.         }
  118.  
  119.         public void RequestSATWithAABB()
  120.         {
  121.             currentState = GameState.SATWithAABB;
  122.         }
  123.         /// <summary>
  124.         /// Allows the game to run logic such as updating the world,
  125.         /// checking for collisions, gathering input, and playing audio.
  126.         /// </summary>
  127.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  128.         protected override void Update(GameTime gameTime)
  129.         {
  130.  
  131.             MouseTestState = Mouse.GetState();
  132.             switch (currentState)
  133.             {
  134.                 case GameState.TitleScreen:
  135.                     {
  136.                         titleScreen.Update(gameTime);
  137.                         break;
  138.                     }
  139.                 case GameState.SeparatingAxisTest:
  140.                     {
  141.                         separatingAxisTest.Update(gameTime);
  142.                         break;
  143.                     }
  144.                 case GameState.SATWithAABB:
  145.                     {
  146.                         sATWithAABB.Update(gameTime);
  147.                         break;
  148.                     }
  149.                 case GameState.Exit:
  150.                     {
  151.                         Exit();
  152.                         break;
  153.                     }
  154.                 default:
  155.                     {
  156.                         titleScreen.Update(gameTime);
  157.                         break;
  158.                     }
  159.             }
  160.  
  161.  
  162.  
  163.             base.Update(gameTime);
  164.         }
  165.  
  166.         /// <summary>
  167.         /// This is called when the game should draw itself.
  168.         /// </summary>
  169.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  170.         protected override void Draw(GameTime gameTime)
  171.         {
  172.             spriteBatch.Begin();
  173.            
  174.             spriteBatch.DrawString(m_fontLibrary["Font"], MouseTestState.ToString(), new Vector2(0, 0), Color.White);
  175.             switch (currentState)
  176.             {
  177.                 case GameState.TitleScreen:
  178.                     {
  179.                         titleScreen.Draw(spriteBatch, spriteFont);
  180.                         break;
  181.                     }
  182.                 case GameState.SeparatingAxisTest:
  183.                     {
  184.                         separatingAxisTest.Draw(gameTime, spriteBatch);
  185.                         break;
  186.                     }
  187.                 case GameState.SATWithAABB:
  188.                     {
  189.                         sATWithAABB.Draw(gameTime, spriteBatch);
  190.                         break;
  191.                     }
  192.                 case GameState.Exit:
  193.                     {
  194.                         Exit();
  195.                         break;
  196.                     }
  197.                 default:
  198.                     {
  199.                         titleScreen.Update(gameTime);
  200.                         break;
  201.                     }
  202.             }
  203.  
  204.             spriteBatch.End();
  205.  
  206.             // TODO: Add your drawing code here
  207.  
  208.             base.Draw(gameTime);
  209.         }
  210.     }
  211. }
  212.  
  213.  
  214. ----------TitleScreen.cs
  215.  
  216.  
  217. using Microsoft.Xna.Framework;
  218. using Microsoft.Xna.Framework.Graphics;
  219. using System;
  220. using System.Collections.Generic;
  221. using System.Linq;
  222. using System.Text;
  223.  
  224. namespace PhysicsStudio_XNA
  225. {
  226.     public class TitleScreen : Screen
  227.     {
  228.  
  229.         List<Button> buttonList = new List<Button>();
  230.         public Menu mainMenu;
  231.         public TitleScreen()
  232.         {
  233.  
  234.  
  235.             mainMenu = new Menu(new Vector2(200, 100), buttonList, 0);
  236.  
  237.  
  238.             buttonList.Add(new PushButton("Separating Axis Test"));
  239.             buttonList.Add(new PushButton("SAT With AABB"));
  240.             buttonList.Add(new PushButton("Awesome"));
  241.             buttonList.Add(new PushButton("Awesomere"));
  242.             buttonList.Add(new PushButton("Awesomere"));
  243.  
  244.         }
  245.  
  246.         public override void Initialize()
  247.         {
  248.  
  249.         }
  250.  
  251.         public override void Update(GameTime gametime)
  252.         {
  253.             mainMenu.Update(gametime);
  254.         }
  255.  
  256.         public void Draw(SpriteBatch sB, SpriteFont sF)
  257.         {
  258.             mainMenu.Draw(sB, sF);
  259.  
  260.         }
  261.  
  262.     }
  263. }
  264.  
  265.  
  266. ------------PushButton.cs---------
  267.  
  268.  
  269. using Microsoft.Xna.Framework;
  270. using Microsoft.Xna.Framework.Graphics;
  271. using Microsoft.Xna.Framework.Input;
  272. using System;
  273. using System.Collections.Generic;
  274. using System.Linq;
  275. using System.Text;
  276.  
  277. namespace PhysicsStudio_XNA
  278. {
  279.     public class PushButton : Button
  280.     {
  281.         #region Fields
  282.  
  283.         string m_text;
  284.         SpriteFont m_font;
  285.         Color m_static, m_onClick, m_onHover;
  286.         Texture2D m_sprite2D, m_onClick2D;
  287.  
  288.  
  289.  
  290.         static public int Pbuttoncount;
  291.  
  292.         //click processing
  293.         bool m_clickedInside =  false,
  294.              m_releasedInside = false,
  295.              m_OnClicked =      false,
  296.              selected =         false;
  297.  
  298.  
  299.  
  300.  
  301.  
  302.         //drawing
  303.  
  304.  
  305.         Rectangle drawRectangle;
  306.  
  307.  
  308.  
  309.         #endregion
  310.  
  311.  
  312.  
  313.         #region Constructers
  314.         // construter for simple black text PushButton
  315.         public PushButton(string Text)
  316.         {
  317.  
  318.             m_text = Text;
  319.  
  320.             drawRectangle = new Rectangle((int)Menu.m_position.X, (int)Menu.m_position.Y + (15 * Pbuttoncount), 200, 15);
  321.             ButtonRegion = new Rectangle((int)Position.X, (int)Position.Y, 200, 15);
  322.             Pbuttoncount++;
  323.         }
  324.  
  325.         // Constructer that uses colors for Static, OnClick And Onhover
  326.         public PushButton(Rectangle ButtonRegion, SpriteFont Font, string Text, Color Static, Color OnClick, Color OnHover)
  327.         {
  328.             m_buttonRegion = ButtonRegion;
  329.             m_font = Font;
  330.             m_text = Text;
  331.             m_static = Static;
  332.             m_onClick = OnClick;
  333.             m_onHover = OnHover;
  334.             // drawRectangle = ButtonPosition(m_buttonRegion);
  335.  
  336.         }
  337.         // Constructer that uses Textures for Static, OnClick And Onhover
  338.         public PushButton(Rectangle ButtonRegion, Texture2D Sprite2D, Texture2D OnClick2D)
  339.         {
  340.             m_buttonRegion = ButtonRegion;
  341.             m_sprite2D = Sprite2D;
  342.             m_onClick2D = OnClick2D;
  343.             //drawRectangle = ButtonPosition(m_buttonRegion);
  344.         }
  345.         #endregion
  346.  
  347.  
  348.  
  349.  
  350.  
  351.         #region Private Methods
  352.  
  353.  
  354.  
  355.         //public Rectangle ButtonPosition(Rectangle MenuRegion)
  356.         //{
  357.  
  358.         //    return new Rectangle();
  359.         //}
  360.  
  361.         #endregion
  362.  
  363.         #region Public Methods
  364.  
  365.         public override void Update(GameTime gameTime)
  366.         {
  367.      
  368.             MouseState currentMouse = Mouse.GetState();
  369.  
  370.             selected = MouseState(drawRectangle, currentMouse);
  371.             m_clickedInside = ClickInside(currentMouse, m_lastMouseState);
  372.             ReleaseInside(currentMouse, m_lastMouseState);
  373.  
  374.             if (selected && m_clickedInside && m_releasedInside)
  375.             {
  376.                 //fire off some event to let our user know that we had a click event.
  377.                 m_OnClicked = true;
  378.             }
  379.             else
  380.             {
  381.                 m_OnClicked = false;
  382.             }
  383.  
  384.             m_lastMouseState = currentMouse;
  385.         }
  386.  
  387.         public override void Draw(SpriteBatch spriteBatch, SpriteFont spriteFont, int buttonCount, Vector2 Position)
  388.         {
  389.  
  390.             spriteBatch.Draw(Game1.m_textureLibrary["Pixel"], new Rectangle((int)Position.X + 10, (int)(Position.Y + 15 * buttonCount), 180, 15), Color.Wheat);
  391.  
  392.  
  393.             if (selected)
  394.             {
  395.                 spriteBatch.DrawString(Game1.m_fontLibrary["Font"], m_text, new Vector2(Position.X + 15, Position.Y + 15 * buttonCount), Color.Orange);
  396.             }
  397.             else
  398.             {
  399.                 spriteBatch.DrawString(Game1.m_fontLibrary["Font"], m_text, new Vector2(Position.X + 15, Position.Y + 15 * buttonCount), Color.Black);
  400.             }
  401.  
  402.  
  403.         }
  404.         #endregion
  405.  
  406.     }
  407. }
  408.  
  409.  
  410.  
  411.  
  412. -------Menu.cs---------
  413.  
  414.  
  415.  
  416. using Microsoft.Xna.Framework;
  417. using Microsoft.Xna.Framework.Graphics;
  418. using System;
  419. using System.Collections.Generic;
  420. using System.Linq;
  421. using System.Text;
  422.  
  423. namespace PhysicsStudio_XNA
  424. {
  425.     public class Menu
  426.     {
  427.  
  428.         #region Fields
  429.  
  430.         List<Button> m_buttonList;
  431.         float m_transparency;
  432.         public int n = 0;
  433.         public Rectangle buttonRegion, m_menuRegion, m_dimensions;
  434.         static public Vector2 m_position;
  435.         int m_WINDOW_HEIGHT = Game1.WINDOW_HEIGHT;
  436.         int m_WINDOW_WIDTH = Game1.WINDOW_WIDTH;
  437.         private Game1 m_managerClass;
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.         #endregion
  445.  
  446.         #region Constructer
  447.  
  448.         public Menu(Vector2 Position, List<Button> ButtonList, float Transparency)
  449.         {
  450.             m_position = Position;
  451.             m_buttonList = ButtonList;
  452.             m_transparency = Transparency;
  453.             m_managerClass = new Game1();
  454.  
  455.  
  456.  
  457.  
  458.  
  459.         }
  460.  
  461.         #endregion
  462.  
  463.  
  464.         #region Properties
  465.  
  466.         public Rectangle MenuRegion
  467.         {
  468.             get { return m_menuRegion; }
  469.             set { m_menuRegion = value; }
  470.         }
  471.  
  472.         static public Vector2 Position
  473.         {
  474.             get { return m_position; }
  475.         }
  476.         #endregion
  477.  
  478.  
  479.  
  480.         #region Public Methods
  481.  
  482.  
  483.  
  484.  
  485.         public void Update(GameTime gametime)
  486.         {
  487.  
  488.  
  489.             for (int i = 0; i < m_buttonList.Count; i++)
  490.             {
  491.                 m_buttonList[i].Update(gametime);
  492.                 if (m_buttonList[0].OnClicked)
  493.                 {
  494.                     SeperatingAxisTest();
  495.                 }
  496.             }
  497.  
  498.  
  499.         }
  500.  
  501.         public void Draw(SpriteBatch sB, SpriteFont sF)
  502.         {
  503.  
  504.  
  505.  
  506.  
  507.             sB.Draw(Game1.m_textureLibrary["Pixel"], new Rectangle((int)m_position.X - 5, (int)m_position.Y - 10, (m_buttonList[0].ButtonRegion.Width + 10), (m_buttonList[0].ButtonRegion.Height * m_buttonList.Count) + 20), Color.Blue);
  508.             for (int i = 0; i < m_buttonList.Count; i++)
  509.             {
  510.                 m_buttonList[i].Draw(sB, sF, i, new Vector2(Position.X, Position.Y));
  511.             }
  512.  
  513.         }
  514.  
  515.         #endregion
  516.  
  517.         #region Protected Methods
  518.  
  519.         #endregion
  520.  
  521.         #region Private Methods
  522.  
  523.         private void SeperatingAxisTest()
  524.         {
  525.             m_managerClass.RequestSeparatingAxisTest();
  526.         }
  527.         #endregion
  528.  
  529.  
  530.  
  531.  
  532.     }
  533. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement