XNA Mouse Interface Component
Friday, September 15 2006
This being friday evening with the wife out and the kids in Bed I have had a small ammount of time to play some more with the XNA Systems. With some of the projects that I am moving with I needed to have a basic UI System, so I have started to put one together. The first part was to get a mouse interface running. I know that in the furture if I did want to migrate my code for the Xbox 360 I would not be able to use the mouse. But for the moment this interface gives me the choice of developing on my Laptop when I can, and with the use of the component system I should then just be able to swap it out for the Controller Interface (Comming soon).
Now the code I have is pretty simple but I will be expanding on it, if others have some more ideas on it please post so that I can grow the interface component.
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Components;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
namespace MouseTest
{
public partial class MouseInterface : Microsoft.Xna.Framework.GameComponent
{
public MouseInterface()
{
InitializeComponent();
}
private Texture2D mousePointerTexture;
private SpriteBatch uiSpriteBatch;
private Vector2 mousePosition;
public Vector2 MousePosition
{
get { return mousePosition; }
set { mousePosition = value; }
}
private string mousePointer;
public string MousePointer
{
get { return mousePointer; }
set { mousePointer = value; }
}
private ButtonState middleButton;
public ButtonState MiddleButton
{
get { return middleButton; }
set { middleButton = value; }
}
private ButtonState leftButton;
public ButtonState LeftButton
{
get { return leftButton; }
set { leftButton = value; }
}
private ButtonState rightButton;
public ButtonState RightButton
{
get { return rightButton; }
set { rightButton = value; }
}
public override void Start()
{
// TODO: Add your start up code here
this.LoadResources();
}
public override void Update()
{
// TODO: Add your update code here
MouseState ms = Mouse.GetState();
this.mousePosition = new Vector2(ms.X, ms.Y);
this.leftButton = ms.LeftButton;
this.rightButton = ms.RightButton;
this.middleButton = ms.MiddleButton;
}
public override void Draw()
{
// TODO: Add your drawing code here
// Draw the UI Components.
this.uiSpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
this.uiSpriteBatch.Draw(this.mousePointerTexture, this.mousePosition, Color.White);
this.uiSpriteBatch.End();
}
protected virtual void LoadResources()
{
this.mousePointerTexture =
Texture2D.FromFile(((GraphicsComponent)Game.GameComponents[0]).GraphicsDevice, this.mousePointer);
this.uiSpriteBatch = new SpriteBatch(((GraphicsComponent)Game.GameComponents[0]).GraphicsDevice);
}
}
}
For those using it you should see how it works... Just drop it on assign the filename to the Mouse pointer in the component, and you are off.
Similar Posts
- XNA Game Component - Custom Mouse Pointer
- Custom Mouse Pointer in XNA
- Drawing a Basic Background in XNA
