📄 input.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace SceneryComponent.Components
{
public delegate void KeyboardEventDelegate(KeyboardEventArgs e);
public class KeyboardEventArgs : EventArgs
{
public readonly Keys Key;
public KeyboardEventArgs(Keys key)
: base()
{
Key = key;
}
}
public class Input : GameComponent
{
private bool m_Active = false;
private Viewport m_Viewport;
private List<Keys> m_PressedKeys = new List<Keys>();
public event KeyboardEventDelegate KeyDown;
public event KeyboardEventDelegate KeyUp;
public bool Active
{
get
{
return m_Active;
}
set
{
if (m_Active != value)
{
m_Active = value;
Mouse.SetPosition(CenterX, CenterY);
}
}
}
public int CenterX
{
get
{
return m_Viewport.Width / 2;
}
}
public int CenterY
{
get
{
return m_Viewport.Height / 2;
}
}
public int X
{
get
{
if (m_Active)
{
return CenterX - Mouse.GetState().X;
}
return 0;
}
}
public int Y
{
get
{
if (m_Active)
{
return CenterY - Mouse.GetState().Y;
}
return 0;
}
}
public Input(Game game, Viewport viewport):base(game)
{
m_Viewport = viewport;
}
public bool ReadKey(Keys key)
{
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(key))
{
if (!m_PressedKeys.Contains(key))
{
m_PressedKeys.Add(key);
if (KeyDown != null)
{
KeyDown(new KeyboardEventArgs(key));
}
}
return true;
}
else
{
if (m_PressedKeys.Contains(key))
{
m_PressedKeys.Remove(key);
if (KeyUp != null)
{
KeyUp(new KeyboardEventArgs(key));
}
}
return false;
}
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
if (m_Active)
{
Mouse.SetPosition(CenterX, CenterY);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -