using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace BallanceTasEditor.Utils { /// /// 原始的TAS帧结构,与二进制结构保持一致。 /// [StructLayout(LayoutKind.Sequential)] public struct RawTasFrame { /// /// 该帧的持续时间(以秒为单位)。 /// public float TimeDelta; /// /// 该帧的按键组合。 /// public uint KeyFlags; } /// /// 描述TAS文件中一帧的结构。 /// public class TasFrame { /// /// 以指定的FPS,无任何按键初始化当前帧。 /// public TasFrame(uint fps = 60) { m_TimeDelta = FpsConverter.ToDelta(fps); m_KeyFlags = 0; } /// /// 从原始TAS数据初始化。 /// /// 要用来初始化的原始数据。 public TasFrame(RawTasFrame raw) { m_TimeDelta = raw.TimeDelta; m_KeyFlags = raw.KeyFlags; } /// /// 转换为原始TAS数据。 /// /// 转换后的原始TAS数据。 public RawTasFrame ToRaw() { return new RawTasFrame() { TimeDelta = m_TimeDelta, KeyFlags = m_KeyFlags }; } /// /// 原位转换为原始TAS数据。 /// /// 以引用传递的原始TAS数据。 public void ToImplaceRaw(ref RawTasFrame raw) { raw.TimeDelta = m_TimeDelta; raw.KeyFlags = m_KeyFlags; } /// /// 该帧的持续时间(以秒为单位)。 /// private float m_TimeDelta; /// /// 该帧的按键组合。 /// private uint m_KeyFlags; /// /// 获取帧时间Delta。 /// /// 获取到的帧时间Delta。 public float GetTimeDelta() { return m_TimeDelta; } /// /// 设置帧时间Delta。 /// /// 要设置的帧时间Delta。 public void SetTimeDelta(float delta) { m_TimeDelta = delta; } /// /// 判断按键是否被按下。 /// /// 要检查的按键。 /// true表示被按下,否则为false。 public bool IsKeyPressed(TasKey key) { return (m_KeyFlags & (1u << (int)key)) != 0; } /// /// 设置按键状态。 /// /// 要设置的按键。 /// true表示设置为按下,否则为松开。 public void SetKeyPressed(TasKey key, bool pressed = true) { if (pressed) m_KeyFlags |= (1u << (int)key); else m_KeyFlags &= ~(1u << (int)key); } /// /// 反转按键状态。 /// /// 要反转的按键。 public void FlipKeyPressed(TasKey key) { m_KeyFlags ^= (1u << (int)key); } /// /// 获取或设置Up键的按下状态。 /// public bool KeyUpPressed { get { return IsKeyPressed(TasKey.KeyUp); } set { SetKeyPressed(TasKey.KeyUp, value); } } /// /// 获取或设置Down键的按下状态。 /// public bool KeyDownPressed { get { return IsKeyPressed(TasKey.KeyDown); } set { SetKeyPressed(TasKey.KeyDown, value); } } /// /// 获取或设置Left键的按下状态。 /// public bool KeyLeftPressed { get { return IsKeyPressed(TasKey.KeyLeft); } set { SetKeyPressed(TasKey.KeyLeft, value); } } /// /// 获取或设置Right键的按下状态。 /// public bool KeyRightPressed { get { return IsKeyPressed(TasKey.KeyRight); } set { SetKeyPressed(TasKey.KeyRight, value); } } /// /// 获取或设置Shift键的按下状态。 /// public bool KeyShiftPressed { get { return IsKeyPressed(TasKey.KeyShift); } set { SetKeyPressed(TasKey.KeyShift, value); } } /// /// 获取或设置Space键的按下状态。 /// public bool KeySpacePressed { get { return IsKeyPressed(TasKey.KeySpace); } set { SetKeyPressed(TasKey.KeySpace, value); } } /// /// 获取或设置Q键的按下状态。 /// public bool KeyQPressed { get { return IsKeyPressed(TasKey.KeyQ); } set { SetKeyPressed(TasKey.KeyQ, value); } } /// /// 获取或设置Esc键的按下状态。 /// public bool KeyEscPressed { get { return IsKeyPressed(TasKey.KeyEsc); } set { SetKeyPressed(TasKey.KeyEsc, value); } } /// /// 获取或设置回车键的按下状态。 /// public bool KeyEnterPressed { get { return IsKeyPressed(TasKey.KeyEnter); } set { SetKeyPressed(TasKey.KeyEnter, value); } } } /// /// 描述TAS文件中的可能的按键。 /// public enum TasKey : int { KeyUp = 0, KeyDown = 1, KeyLeft = 2, KeyRight = 3, KeyShift = 4, KeySpace = 5, KeyQ = 6, KeyEsc = 7, KeyEnter = 8, } }