Game1.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using System.Windows.Forms;
  12. using MonoMac.AppKit;
  13. using MonoMac.Foundation;
  14. namespace MouseGetStateAndIsMouseVisibleTester
  15. {
  16. public class Game1 : Microsoft.Xna.Framework.Game
  17. {
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20. public TextManager aTm;
  21. public InputManager cIm;
  22. private SpriteFont sfStandard;
  23. public MouseState mousestatus;
  24. public Texture2D t2dUiCursor;
  25. public Object aObjects;
  26. bool bWindowIsCentered = false;
  27. bool bFullScreen = false;
  28. public Game1()
  29. {
  30. graphics = new GraphicsDeviceManager(this);
  31. Content.RootDirectory = "Content";
  32. cIm = new InputManager(this);
  33. Window.AllowUserResizing = true;
  34. // Subscribe to the game window's ClientSizeChanged event.
  35. Window.ClientSizeChanged += Window_ClientSizeChanged;
  36. }
  37. void Window_ClientSizeChanged( object sender, EventArgs e )
  38. {
  39. // Make changes to handle the new window size.
  40. Console.WriteLine("Window size changed " + Window.ClientBounds);
  41. }
  42. protected override void Initialize()
  43. {
  44. // TODO: Add your initialization logic here
  45. base.Initialize();
  46. }
  47. protected override void LoadContent()
  48. {
  49. // Create a new SpriteBatch, which can be used to draw textures.
  50. spriteBatch = new SpriteBatch(GraphicsDevice);
  51. Services.AddService(typeof(SpriteBatch), spriteBatch);
  52. sfStandard = Content.Load<SpriteFont>("fntStandard");
  53. t2dUiCursor = Content.Load<Texture2D>("UiCursor");
  54. aTm = new TextManager(this, sfStandard);
  55. Components.Add(aTm);
  56. aObjects = new Object(this, ref t2dUiCursor);
  57. Components.Add(aObjects);
  58. aObjects.pos.X = 200;
  59. aObjects.pos.Y = 200;
  60. //this.Window.Window.StyleMask = this.Window.Window.StyleMask | NSWindowStyle.Miniaturizable;
  61. }
  62. protected override void UnloadContent()
  63. {
  64. // TODO: Unload any non ContentManager content here
  65. }
  66. public void Toggle()
  67. {
  68. graphics.ToggleFullScreen();
  69. if(bFullScreen)
  70. {
  71. bFullScreen = false;
  72. }//if
  73. else
  74. {
  75. bFullScreen = true;
  76. }//else
  77. }//Toggle
  78. public int GetBackBufferWidth()
  79. {
  80. return graphics.PreferredBackBufferWidth;
  81. }//GetBackBufferWidth
  82. public int GetBackBufferHeight()
  83. {
  84. return graphics.PreferredBackBufferHeight;
  85. }//GetBackBufferWidth
  86. public String GetStyleMask()
  87. {
  88. return this.Window.Window.StyleMask.ToString();
  89. }//GetStyleMask
  90. protected override void Update(GameTime gameTime)
  91. {
  92. mousestatus = Mouse.GetState();
  93. cIm.InputHandler(mousestatus, gameTime);
  94. aObjects.pos.X = mousestatus.X;
  95. aObjects.pos.Y = mousestatus.Y;
  96. base.Update(gameTime);
  97. }
  98. protected override void Draw(GameTime gameTime)
  99. {
  100. GraphicsDevice.Clear(Color.CornflowerBlue);
  101. spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
  102. base.Draw(gameTime);
  103. spriteBatch.End();
  104. }
  105. public void CenterWindow()
  106. {
  107. if(bFullScreen) return;
  108. int index;
  109. int upperBound;
  110. float fScreenWidth, fScreenHeight, fNewX, fNewY, fWindowWidth, fWindowHeight, fTitleBarHeight;
  111. Screen[] screens = Screen.AllScreens;
  112. fScreenWidth = fScreenHeight = 0;
  113. upperBound = screens.GetUpperBound(0);
  114. for (index = 0; index <= upperBound; index++)
  115. {
  116. if (screens[index].Primary)
  117. {
  118. fScreenWidth = (float)screens[index].Bounds.Width;
  119. fScreenHeight = (float)screens[index].Bounds.Height;
  120. index = upperBound;
  121. }//if
  122. }//for
  123. fWindowWidth = graphics.PreferredBackBufferWidth;
  124. fWindowHeight = graphics.PreferredBackBufferHeight;
  125. fNewX = (fScreenWidth - fWindowWidth) / 2;
  126. fNewY = (fScreenHeight - fWindowHeight) / 2;
  127. fTitleBarHeight = this.Window.Window.Frame.Height - fWindowHeight;
  128. System.Drawing.PointF pfLocation = new System.Drawing.PointF(fNewX,fNewY);
  129. System.Drawing.PointF pfSize = new System.Drawing.PointF(fWindowWidth, fWindowHeight + fTitleBarHeight);
  130. System.Drawing.SizeF sfSize = new System.Drawing.SizeF(pfSize);
  131. System.Drawing.RectangleF rectTemp = new System.Drawing.RectangleF(pfLocation, sfSize);
  132. this.Window.Window.SetFrame(rectTemp, true);
  133. }//CenterWindow
  134. }
  135. }