Advertisement
konalisp

mouse stuff

Nov 6th, 2017
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.19 KB | None | 0 0
  1. //---
  2.  
  3.         public Rectangle CalculateRenderTargetPosition()
  4.         {
  5.             Rectangle rec = new Rectangle(0, 0, 0, 0);
  6.             Single preferredAspect = Globals.RenderSize.Width / (Single)Globals.RenderSize.Height;
  7.             Single outputAspect = Globals.WindowSize.Width / (Single)Globals.WindowSize.Height;
  8.             if (outputAspect <= preferredAspect)
  9.             {
  10.                 // output is taller than it is wider, bars on top/bottom
  11.                 Int32 presentHeight = (Int32)((Globals.WindowSize.Width / preferredAspect) + 0.5f);
  12.                 Int32 barHeight = (Globals.WindowSize.Height - presentHeight) / 2;
  13.                 rec = new Rectangle(0, barHeight, Globals.WindowSize.Width, presentHeight);
  14.             }
  15.             else
  16.             {
  17.                 // output is wider than it is tall, bars left/right
  18.                 Int32 presentWidth = (Int32)((Globals.WindowSize.Height * preferredAspect) + 0.5f);
  19.                 Int32 barWidth = (Globals.WindowSize.Width - presentWidth) / 2;
  20.                 rec = new Rectangle(barWidth, 0, presentWidth, Globals.WindowSize.Height);
  21.             }
  22.             return rec;
  23.         }
  24.  
  25. //---
  26.  
  27.         private void CalculateMousePosition()
  28.         {
  29.             //using same mouse code from http://www.david-gouveia.com/portfolio/2d-camera-with-parallax-scrolling-in-xna/
  30.             Matrix cam = Globals.Camera.GetViewMatrix();
  31.             MouseState ms = Mouse.GetState();
  32.             Vector2 pos = new Vector2(0,0);
  33.            
  34.             if (Globals.WindowSize > Globals.RenderSize)
  35.             {
  36.                 Rectangle r = Globals.Draw.CalculateRenderTargetPosition();
  37.                 Single aspect = Globals.WindowSize.Width / (Single)Globals.WindowSize.Height;
  38.                
  39.                 pos = Vector2.Transform(new Vector2(
  40.                         (ms.X - r.X) / aspect,
  41.                         (ms.Y - r.Y) / aspect),
  42.                     Matrix.Invert(cam));
  43.             }
  44.             else if (Globals.WindowSize == Globals.RenderSize)
  45.             {
  46.                 pos = Vector2.Transform(new Vector2(ms.X, ms.Y, Matrix.Invert(cam));
  47.             }
  48.             this.MousePosition = pos;
  49.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement