结构化输入管理

时间:2017-11-06 13:25:06

标签: opengl input opentk

我通过opentk使用opengl,我必须为实体实现控制组件。 opentk处理输入的方式是它将键盘键和鼠标按钮存储在不同的枚举中。理想情况下,我希望我的输入是这样的:

class Control
{
    ActionsEnum Action;
    KeyClass Key;
}

这样我就可以把它放在一个列表中,并且" foreach"通过它,以便我可以轻松确定是否按下了一个键,如果是,它应该执行哪个动作(moveX,jump,attack等)

由于我无法找到使其工作的方法,我只能想到一个存储多个不同控件类型列表的类,这将是一个混乱:

class Controls
    {
        public List<KeyboardInput>          KeyboardInputs          { get; private set; }
        public List<KeyboardRangeInput>     KeyboardRangeInputs     { get; private set; }
        public List<MouseInput>             MouseInputs             { get; private set; }
        public List<MouseRangeInput>        MouseRangeInputs        { get; private set; }
        public List<MouseMoveRangeInput>    MouseMoveRangeInputs    { get; private set; }

        public Controls
        {
            //don't initialize lists since that would take up memory
        }

        public void AddKeyboardInput(KeyboardInput keyInput)
        {
            if(KeyboardInputs == null)
            {
                KeyboardInputs = new List<KeyboardInput>();
            }
            KeyboardInputs.Add(keyInput);
        }

        public void AddKeyboardRangeInput(KeyboardRangeInput keyInput)
        {
            if(KeyboardRangeInputs == null)
            {
                KeyboardRangeInputs = new List<KeyboardRangeInput>();
            }
            KeyboardRangeInputs.Add(keyInput);
        }

        //and so on
    }

如果有人处理过这样的问题,请分享您的经验,任何建议都会非常有用:)

1 个答案:

答案 0 :(得分:0)

我想我的解决方案是实现自定义&#34; isactivated&#34;允许我检查输入内容是否被按下的方法,无论它是鼠标还是键盘输入,这里是未来任何人的代码。

我将ControlInput保存在一个列表中并通过它调用&#34; IsActivated&#34;功能,如果它被激活,我会读取它应该触发和处理的动作。

public enum ControlAction
{
    MoveXPos, MoveXNeg,
    MoveYPos, MoveYNeg,
    MoveZPos, MoveZNeg,
    ScaleXPos, ScaleXNeg,
    ScaleYPos, ScaleYNeg,
    ScaleZPos, ScaleZNeg,
    RotateXPos, RotateXNeg,
    RotateYPos, RotateYNeg,
    RotateZPos, RotateZNeg
}
abstract public class ControlInput
{
    public ControlAction Action { get; private set; }
    public ControlInput(ControlAction action)
    {
        this.Action = action;
    }
    abstract public bool IsActivated();
}

public class KeyboardInput : ControlInput
{
    private Key keyToActivate;
    public KeyboardInput(Key key, ControlAction action) : base( action )
    {
        keyToActivate = key;
    }
    public override bool IsActivated()
    {
        return Input.KeyboardKeys.IsKeyDown( keyToActivate );
    }
}
public class MouseButtonInput : ControlInput
{
    private MouseButton buttonToActivate;
    public MouseButtonInput(MouseButton button, ControlAction action) : base( action )
    {
        this.buttonToActivate = button;
    }
    public override bool IsActivated()
    {
        return Input.MouseButtons.IsButtonDown( buttonToActivate );
    }
}