📄 gui.cs
字号:
using System;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Blockers
{
/// <summary>
/// Will contain a UI Screen.
/// </summary>
public abstract class UiScreen : IDisposable
{
#region Constants
// Constants
public static readonly Vector3 ObjectCenter = new Vector3();
public static readonly int SpriteColor = unchecked((int)0xffffffff);
protected const int SmallButtonWidth = 128;
protected const int SmallButtonHeight = 64;
protected const int LargeButtonWidth = 256;
protected const int LargeButtonHeight = 64;
#endregion
#region Instance Data
protected Sprite renderSprite = null;
protected Texture backgroundTexture = null;
protected Rectangle backgroundSource;
// Screen size
protected int screenWidth = 0;
protected int screenHeight = 0;
// Should the object be centered?
private bool isCentered = false;
protected Vector3 centerUpper;
#endregion
#region Creation
/// <summary>
/// Create a new UI Screen
/// </summary>
public UiScreen(Device device, int width, int height)
{
// Create the sprite object
renderSprite = new Sprite(device);
// Hook the device events to 'fix' the sprite if needed
device.DeviceLost += new EventHandler(OnDeviceLost);
device.DeviceReset += new EventHandler(OnDeviceReset);
StoreTexture(null, width, height, false);
}
/// <summary>
/// Store the texture for the background
/// </summary>
protected void StoreTexture(Texture background, int width, int height,
bool centerTexture)
{
// Store the background texture
backgroundTexture = background;
if (backgroundTexture != null)
{
// Get the background texture
using (Surface s = backgroundTexture.GetSurfaceLevel(0))
{
SurfaceDescription desc = s.Description;
backgroundSource = new Rectangle(0, 0,
desc.Width, desc.Height);
}
}
// Store the width/height
screenWidth = width;
screenHeight = height;
// Store the centered texture
isCentered = centerTexture;
if (isCentered)
{
centerUpper = new Vector3((float)(width - backgroundSource.Width) / 2.0f,
(float)(height - backgroundSource.Height) / 2.0f, 0.0f);
}
}
#endregion
#region Device Events
private void OnDeviceLost(object sender, EventArgs e)
{
// The device has been lost, make sure the sprite cleans itself up
if (renderSprite != null)
renderSprite.OnLostDevice();
}
private void OnDeviceReset(object sender, EventArgs e)
{
// The device has been reset, make sure the sprite fixes itself up
if (renderSprite != null)
renderSprite.OnResetDevice();
}
#endregion
#region Draw
/// <summary>
/// Start drawing with this sprite
/// </summary>
protected void BeginSprite()
{
renderSprite.Begin(SpriteFlags.AlphaBlend);
}
/// <summary>
/// Staop drawing with this sprite
/// </summary>
protected void EndSprite()
{
renderSprite.End();
}
/// <summary>
/// Render the button in the correct state
/// </summary>
public virtual void Draw()
{
// Render the background if it exists
if (backgroundTexture != null)
{
if (isCentered)
{
// Render to the screen centered
renderSprite.Draw(backgroundTexture, backgroundSource,
ObjectCenter, centerUpper, SpriteColor);
}
else
{
// Scale the background to the right size
renderSprite.Transform = Matrix.Scaling(
(float)screenWidth / (float)backgroundSource.Width,
(float)screenHeight / (float)backgroundSource.Height,
0);
// Render it to the screen
renderSprite.Draw(backgroundTexture, backgroundSource,
ObjectCenter, ObjectCenter, SpriteColor);
// Reset the transform
renderSprite.Transform = Matrix.Identity;
}
}
}
#endregion
#region IDisposable Members
/// <summary>
/// Cleanup in case dispose isn't called
/// </summary>
~UiScreen()
{
Dispose();
}
/// <summary>
/// Cleanup any resources
/// </summary>
public virtual void Dispose()
{
GC.SuppressFinalize(this);
// Dispose the sprite
if (renderSprite != null)
{
renderSprite.Dispose();
}
renderSprite = null;
}
#endregion
}
/// <summary>
/// Will hold a 'button' that will be rendered via DX
/// </summary>
public class UiButton
{
#region Instance Data
private Sprite renderSprite = null;
private Texture buttonTextureOff = null;
private Texture buttonTextureOn = null;
private Rectangle onSource;
private Rectangle offSource;
private Vector3 location;
private bool isButtonOn = false;
private Rectangle buttonRect;
// The click event
public event EventHandler Click;
#endregion
#region Creation
/// <summary>
/// Create a new instance of the Button class using an existing sprite
/// </summary>
public UiButton(Sprite sprite, Texture on, Texture off,
Rectangle rectOn, Rectangle rectOff, Point buttonLocation)
{
// Store the sprite object
renderSprite = sprite;
// Store the textures
buttonTextureOff = off;
buttonTextureOn = on;
// Rectangles
onSource = rectOn;
offSource = rectOff;
// Location
location = new Vector3(buttonLocation.X, buttonLocation.Y, 0);
// Create a rectangle based on the location and size
buttonRect = new Rectangle((int)location.X, (int)location.Y,
onSource.Width, onSource.Height);
}
#endregion
#region Mouse Events
/// <summary>
/// Update the button if the mouse is over it
/// </summary>
public void OnMouseMove(int x, int y)
{
// Determine if the button is on or not
isButtonOn = buttonRect.Contains(x, y);
}
/// <summary>
/// See if the user clicked the button
/// </summary>
public void OnMouseClick(int x, int y)
{
// Determine if the button is pressed
if(buttonRect.Contains(x, y))
{
if (Click != null)
Click(this, EventArgs.Empty);
}
}
#endregion
#region Draw
/// <summary>
/// Render the button in the correct state
/// </summary>
public void Draw()
{
if (isButtonOn)
{
renderSprite.Draw(buttonTextureOn, onSource, UiScreen.ObjectCenter,
location, UiScreen.SpriteColor);
}
else
{
renderSprite.Draw(buttonTextureOff, offSource, UiScreen.ObjectCenter,
location, UiScreen.SpriteColor);
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -