📄 uiscreens.cs
字号:
using System;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Blockers
{
/// <summary>
/// The main selection screen for the game
/// </summary>
public class MainUiScreen : UiScreen, IDisposable
{
#region Instance Data
private Texture buttonTextures = null;
private Texture messageTexture = null;
private UiButton newButton = null;
private UiButton exitButton = null;
// Events
public event EventHandler NewGame;
public event EventHandler Quit;
#endregion
#region Creation
/// <summary>
/// Create the main menu screen
/// </summary>
public MainUiScreen(Device device, int width, int height)
: base(device, width, height)
{
// Create the texture for the background
messageTexture = TextureLoader.FromFile(device, GameEngine.MediaPath + "MainMenu.png");
// Mark the background texture as centered
StoreTexture(messageTexture, width, height, false);
// Create the textures for the buttons
buttonTextures = TextureLoader.FromFile(device, GameEngine.MediaPath
+ "MainButtons.png");
// Create the main menu buttons now
// Create the new game button
newButton = new UiButton(renderSprite, buttonTextures, buttonTextures,
new Rectangle(0,LargeButtonHeight * 1,
LargeButtonWidth, LargeButtonHeight),
new Rectangle(0,0,
LargeButtonWidth, LargeButtonHeight),
new Point((width - LargeButtonWidth) / 2,
(height - (LargeButtonHeight * 4)) / 2));
newButton.Click += new EventHandler(OnNewButton);
// Create the new game button
exitButton = new UiButton(renderSprite, buttonTextures,
buttonTextures, new Rectangle(0,LargeButtonHeight * 3,
LargeButtonWidth, LargeButtonHeight),
new Rectangle(0,LargeButtonHeight * 2,
LargeButtonWidth, LargeButtonHeight),
new Point((width - LargeButtonWidth) / 2,
(height - (LargeButtonHeight * 2)) / 2));
exitButton.Click += new EventHandler(OnExitButton);
}
#endregion
#region Rendering
/// <summary>
/// Render the main ui screen
/// </summary>
public override void Draw()
{
// Start rendering sprites
base.BeginSprite();
// Draw the background
base.Draw();
// Now the buttons
newButton.Draw();
exitButton.Draw();
// You're done rendering sprites
base.EndSprite();
}
#endregion
#region Button Click Event Handlers
/// <summary>
/// Fired when the new button is clicked
/// </summary>
private void OnNewButton(object sender, EventArgs e)
{
if (NewGame != null)
NewGame(this, e);
}
/// <summary>
/// Fired when the exit button is clicked
/// </summary>
private void OnExitButton(object sender, EventArgs e)
{
if (Quit != null)
Quit(this, e);
}
#endregion
#region Winform Events
/// <summary>
/// Update the buttons if the mouse is over it
/// </summary>
public void OnMouseMove(int x, int y)
{
newButton.OnMouseMove(x, y);
exitButton.OnMouseMove(x, y);
}
/// <summary>
/// See if the user clicked the buttons
/// </summary>
public void OnMouseClick(int x, int y)
{
newButton.OnMouseClick(x, y);
exitButton.OnMouseClick(x, y);
}
/// <summary>
/// Called when a key is pressed
/// </summary>
public void OnKeyPress(System.Windows.Forms.Keys key)
{
switch(key)
{
case Keys.Escape:
// Escape is the same as pressing quit
OnExitButton(this, EventArgs.Empty);
break;
case Keys.Enter:
// Enter is the same as starting a new game
OnNewButton(this, EventArgs.Empty);
break;
}
}
#endregion
#region IDisposable Members
/// <summary>
/// Cleanup in case dispose isn't called
/// </summary>
~MainUiScreen()
{
Dispose();
}
/// <summary>
/// Cleanup any resources
/// </summary>
public override void Dispose()
{
GC.SuppressFinalize(this);
if (messageTexture != null)
{
messageTexture.Dispose();
}
if (buttonTextures != null)
{
buttonTextures.Dispose();
}
buttonTextures = null;
messageTexture = null;
base.Dispose();
}
#endregion
}
/// <summary>
/// The screen to select the Loopy character
/// </summary>
public class SelectLoopyScreen : UiScreen, IDisposable
{
#region Instance Data
// Constants
private static readonly Vector3 CameraPos = new Vector3(0, 20.0f, -85.0f);
private static readonly Vector3 CameraTarget = new Vector3(0,20,0);
private static readonly Vector3 CameraUp = new Vector3(0, 1.0f, 0);
private Texture buttonTextures = null;
private Texture messageTexture = null;
private UiButton okButton = null;
private UiButton leftButton = null;
private UiButton rightButton = null;
private Mesh loopyMesh = null;
private Texture loopyTexture = null;
private Material loopyMaterial;
private Device storedDevice = null;
private PlayerColor loopyColor = PlayerColor.Min;
// Events
public event EventHandler Selected;
#endregion
#region Creation
/// <summary>
/// Create the selection screen
/// </summary>
/// <param name="device"></param>
public SelectLoopyScreen(Device device, Texture buttons, int width, int height)
: base(device, width, height)
{
// Store the device for texture creation
storedDevice = device;
// Store the textures for the buttons
buttonTextures = buttons;
// Create the loopy mesh
loopyMesh = Mesh.FromFile(GameEngine.MediaPath + "Loopy.x", MeshFlags.Managed, device);
// Create the loop texture
CreateTexture(device);
// Create a material
loopyMaterial = new Material();
loopyMaterial.Diffuse = loopyMaterial.Ambient = Color.White;
// Create the texture for the background
messageTexture = TextureLoader.FromFile(device, GameEngine.MediaPath + "Select.png");
// Create the ok button
okButton = new UiButton(renderSprite, buttonTextures, buttonTextures,
new Rectangle(SmallButtonWidth,SmallButtonHeight * 3,
SmallButtonWidth,SmallButtonHeight),
new Rectangle(0,SmallButtonHeight * 3,
SmallButtonWidth,SmallButtonHeight),
new Point((width - SmallButtonWidth) / 2,
height - (SmallButtonHeight * 2)));
okButton.Click += new EventHandler(OnSelectButton);
// Create the yes/no buttons
leftButton = new UiButton(renderSprite, buttonTextures, buttonTextures,
new Rectangle(SmallButtonWidth,0,SmallButtonWidth,SmallButtonHeight),
new Rectangle(0,0,SmallButtonWidth,SmallButtonHeight),
new Point(SmallButtonWidth / 2,
height - (SmallButtonHeight * 2)));
leftButton.Click += new EventHandler(OnLeftButton);
rightButton = new UiButton(renderSprite, buttonTextures, buttonTextures,
new Rectangle(SmallButtonWidth,SmallButtonHeight * 1,
SmallButtonWidth,SmallButtonHeight),
new Rectangle(0,SmallButtonHeight * 1,
SmallButtonWidth,SmallButtonHeight),
new Point(width - (SmallButtonWidth + (SmallButtonWidth / 2)),
height - (SmallButtonHeight * 2)));
rightButton.Click += new EventHandler(OnRightButton);
StoreTexture(messageTexture, width, height / 2, true);
}
/// <summary>
/// Create the correct loopy texture
/// </summary>
private void CreateTexture(Device device)
{
if (loopyTexture != null)
{
// Destroy and recreate a new texture
loopyTexture.Dispose();
}
loopyTexture = TextureLoader.FromFile(device,
GetTextureFileFromColor(loopyColor));
}
/// <summary>
/// Return the texture filename for the given player color
/// </summary>
private static string GetTextureFileFromColor(PlayerColor color)
{
switch(color)
{
case PlayerColor.Blue:
return GameEngine.MediaPath + "LoopyBlue.bmp";
case PlayerColor.Green:
return GameEngine.MediaPath + "LoopyGreen.bmp";
case PlayerColor.Purple:
return GameEngine.MediaPath + "LoopyPurple.bmp";
case PlayerColor.Red:
return GameEngine.MediaPath + "LoopyRed.bmp";
case PlayerColor.Yellow:
return GameEngine.MediaPath + "LoopyYellow.bmp";
case PlayerColor.Gray:
return GameEngine.MediaPath + "LoopyGray.bmp";
default:
throw new ArgumentException("color", "An invalid color was passed in.");
}
}
#endregion
#region Rendering
/// <summary>
/// Render the buttons
/// </summary>
public void Draw(float totalTime)
{
// Render Loopy
storedDevice.Transform.View = Matrix.LookAtLH(
CameraPos, CameraTarget, CameraUp);
storedDevice.Transform.World = Matrix.RotationY(totalTime * 2);
storedDevice.SetTexture(0, loopyTexture);
storedDevice.Material = loopyMaterial;
loopyMesh.DrawSubset(0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -