簡體   English   中英

向 XNA 游戲添加類似輸入框的控件

[英]Adding inputbox-like control to XNA game

我希望我的游戲有正常的文本輸入,但使用純 XNA 似乎很不愉快。

早些時候我發現了這段代碼,它讓我在我的游戲中使用MessageBox ,安全地暫停它的執行並顯示一條消息:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern uint MessageBox(IntPtr hWnd, String text, String caption, uint type);

是否有類似的東西可以將InputBox功能添加到我的游戲中,最好不要中斷(暫停)游戲?

啊,文本輸入 - 我最近有這方面的經驗。

問題

通常, Keyboard.GetKeyboardState()在獲取文本輸入方面很糟糕,這有很多原因,其中一些是:

  • 你必須編寫一個巨大的開關來檢測按下了什么鍵
  • 您必須手動檢測是否將字母大寫(Shift 或 CapsLock)
  • 您必須破譯那些OemPeriod的鍵(如在測試中)以查看它們的實際位置,並將它們映射到特定值。
  • 無法檢測/使用鍵盤布局或鍵盤語言
  • 如果按鍵被按下,您必須實現自己的計時重復機制

問題的第二部分是檢測您的哪個文本框(或一般的 UI 控件)當前正在接收此輸入,因為您不希望所有框在您鍵入時接收文本。

第三,您需要在指定的邊界內繪制 TextBox,您還可能想要繪制插入符號(閃爍的垂直位置指示器)、當前選擇(如果您想到目前為止實現它)、表示框,以及突出顯示(使用鼠標)或選中(具有焦點)狀態的紋理。

第四,您必須手動實現復制粘貼功能。


速記

您可能不需要所有這些功能,因為我不需要它們。 您只需要簡單的輸入和檢測鍵,例如 Enter 或 Tab,以及鼠標點擊。 也許也貼。

解決方案

問題是(至少當我們談論 Windows 時,而不是 X-Box 或 WP7 時),操作系統已經擁有從鍵盤實現您需要的一切所需的機制:

  • 根據當前的鍵盤布局和語言給出字符
  • 自動處理重復輸入(在按鍵被按住的情況下)
  • 自動大寫並提供特殊字符

我用來獲取鍵盤輸入的解決方案,我已經復制了這個 Gamedev.net 論壇帖子 它是下面的代碼,您只需要將其復制粘貼到一個 .cs 文件中,您將永遠不必再次打開該文件。

它用於從您的鍵盤接收本地化輸入,您需要做的就是在您的Game.Initialize()覆蓋方法中初始化它(通過使用 Game.Window),並連接到事件以接收您想要的任何地方的輸入像。

您需要將PresentationCore (PresentationCore.dll) 添加到您的引用中才能使用此代碼( System.Windows.Input命名空間需要)。 這適用於 .NET 4.0 和 .NET 4.0 Client Profile。

事件輸入

using System;
using System.Runtime.InteropServices;   
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Text;
using System.Windows.Input;

namespace EventInput
{

    public class KeyboardLayout
    {
        const uint KLF_ACTIVATE = 1; //activate the layout
        const int KL_NAMELENGTH = 9; // length of the keyboard buffer
        const string LANG_EN_US = "00000409";
        const string LANG_HE_IL = "0001101A";

        [DllImport("user32.dll")]
        private static extern long LoadKeyboardLayout(
              string pwszKLID,  // input locale identifier
              uint Flags       // input locale identifier options
              );

        [DllImport("user32.dll")]
        private static extern long GetKeyboardLayoutName(
              System.Text.StringBuilder pwszKLID  //[out] string that receives the name of the locale identifier
              );

        public static string getName()
        {
            System.Text.StringBuilder name = new System.Text.StringBuilder(KL_NAMELENGTH);
            GetKeyboardLayoutName(name);
            return name.ToString();
        }
    }

    public class CharacterEventArgs : EventArgs
    {
        private readonly char character;
        private readonly int lParam;

        public CharacterEventArgs(char character, int lParam)
        {
            this.character = character;
            this.lParam = lParam;
        }

        public char Character
        {
            get { return character; }
        }

        public int Param
        {
            get { return lParam; }
        }

        public int RepeatCount
        {
            get { return lParam & 0xffff; }
        }

        public bool ExtendedKey
        {
            get { return (lParam & (1 << 24)) > 0; }
        }

        public bool AltPressed
        {
            get { return (lParam & (1 << 29)) > 0; }
        }

        public bool PreviousState
        {
            get { return (lParam & (1 << 30)) > 0; }
        }

        public bool TransitionState
        {
            get { return (lParam & (1 << 31)) > 0; }
        }
    }

    public class KeyEventArgs : EventArgs
    {
        private Keys keyCode;

        public KeyEventArgs(Keys keyCode)
        {
            this.keyCode = keyCode;
        }

        public Keys KeyCode
        {
            get { return keyCode; }
        }
    }

    public delegate void CharEnteredHandler(object sender, CharacterEventArgs e);
    public delegate void KeyEventHandler(object sender, KeyEventArgs e);

    public static class EventInput
    {
        /// <summary>
        /// Event raised when a character has been entered.
        /// </summary>
        public static event CharEnteredHandler CharEntered;

        /// <summary>
        /// Event raised when a key has been pressed down. May fire multiple times due to keyboard repeat.
        /// </summary>
        public static event KeyEventHandler KeyDown;

        /// <summary>
        /// Event raised when a key has been released.
        /// </summary>
        public static event KeyEventHandler KeyUp;

        delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

        static bool initialized;
        static IntPtr prevWndProc;
        static WndProc hookProcDelegate;
        static IntPtr hIMC;

        //various Win32 constants that we need
        const int GWL_WNDPROC = -4;
        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int WM_CHAR = 0x102;
        const int WM_IME_SETCONTEXT = 0x0281;
        const int WM_INPUTLANGCHANGE = 0x51;
        const int WM_GETDLGCODE = 0x87;
        const int WM_IME_COMPOSITION = 0x10f;
        const int DLGC_WANTALLKEYS = 4;

        //Win32 functions that we're using
        [DllImport("Imm32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr ImmGetContext(IntPtr hWnd);

        [DllImport("Imm32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);


        /// <summary>
        /// Initialize the TextInput with the given GameWindow.
        /// </summary>
        /// <param name="window">The XNA window to which text input should be linked.</param>
        public static void Initialize(GameWindow window)
        {
            if (initialized)
                throw new InvalidOperationException("TextInput.Initialize can only be called once!");

            hookProcDelegate = new WndProc(HookProc);
            prevWndProc = (IntPtr)SetWindowLong(window.Handle, GWL_WNDPROC,
                (int)Marshal.GetFunctionPointerForDelegate(hookProcDelegate));

            hIMC = ImmGetContext(window.Handle);
            initialized = true;
        }

        static IntPtr HookProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
        {
            IntPtr returnCode = CallWindowProc(prevWndProc, hWnd, msg, wParam, lParam);

            switch (msg)
            {
                case WM_GETDLGCODE:
                    returnCode = (IntPtr)(returnCode.ToInt32() | DLGC_WANTALLKEYS);
                    break;

                case WM_KEYDOWN:
                    if (KeyDown != null)
                        KeyDown(null, new KeyEventArgs((Keys)wParam));
                    break;

                case WM_KEYUP:
                    if (KeyUp != null)
                        KeyUp(null, new KeyEventArgs((Keys)wParam));
                    break;

                case WM_CHAR:
                    if (CharEntered != null)
                        CharEntered(null, new CharacterEventArgs((char)wParam, lParam.ToInt32()));
                    break;

                case WM_IME_SETCONTEXT:
                    if (wParam.ToInt32() == 1)
                        ImmAssociateContext(hWnd, hIMC);
                    break;

                case WM_INPUTLANGCHANGE:
                    ImmAssociateContext(hWnd, hIMC);
                    returnCode = (IntPtr)1;
                    break;
            }

            return returnCode;
        }
    }
}

現在您已經可以按原樣使用它(通過訂閱EventInput.CharEntered事件),並使用邏輯來檢測將輸入發送到何處。


KeyboardDispatcher, IKeyboardSubscriber

我所做的是創建一個類KeyboardDispatcher ,它通過具有IKeyboardSubscriber類型的屬性來處理鍵盤輸入的分派,它將接收到的輸入發送到該屬性。 這個想法是您將此屬性設置為要接收輸入的 UI 控件。

定義如下:

public interface IKeyboardSubscriber
{
    void RecieveTextInput(char inputChar);
    void RecieveTextInput(string text);
    void RecieveCommandInput(char command);
    void RecieveSpecialInput(Keys key);

    bool Selected { get; set; } //or Focused
}

public class KeyboardDispatcher
{
    public KeyboardDispatcher(GameWindow window)
    {
        EventInput.EventInput.Initialize(window);
        EventInput.EventInput.CharEntered += new EventInput.CharEnteredHandler(EventInput_CharEntered);
        EventInput.EventInput.KeyDown += new EventInput.KeyEventHandler(EventInput_KeyDown);
    }

    void EventInput_KeyDown(object sender, EventInput.KeyEventArgs e)
    {
        if (_subscriber == null)
            return;

        _subscriber.RecieveSpecialInput(e.KeyCode);
    }

    void EventInput_CharEntered(object sender, EventInput.CharacterEventArgs e)
    {
        if (_subscriber == null)
            return;
        if (char.IsControl(e.Character))
        {
            //ctrl-v
            if (e.Character == 0x16)
            {
                //XNA runs in Multiple Thread Apartment state, which cannot recieve clipboard
                Thread thread = new Thread(PasteThread);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
                _subscriber.RecieveTextInput(_pasteResult);
            }
            else
            {
                _subscriber.RecieveCommandInput(e.Character);
            }
        }
        else
        {
            _subscriber.RecieveTextInput(e.Character);
        }
    }

    IKeyboardSubscriber _subscriber;
    public IKeyboardSubscriber Subscriber
    {
        get { return _subscriber; }
        set
        {
            if (_subscriber != null)
                _subscriber.Selected = false;
            _subscriber = value;
            if(value!=null)
                value.Selected = true;
        }
    }

    //Thread has to be in Single Thread Apartment state in order to receive clipboard
    string _pasteResult = "";
    [STAThread]
    void PasteThread()
    {
        if (Clipboard.ContainsText())
        {
            _pasteResult = Clipboard.GetText();
        }
        else
        {
            _pasteResult = "";
        }
    }
}

用法相當簡單,實例化KeyboardDispatcher ,即在Game.Initialize()並保持對它的引用(這樣您就可以在選定的 [focused] 控件之間切換),並將使用IKeyboardSubscriber接口的類傳遞給它,例如您的TextBox


文本框

接下來是您的實際控制權。 現在我最初編寫了一個相當復雜的框,它使用渲染目標將文本渲染為紋理,以便我可以移動它(如果文本比框大),但是經過很多痛苦之后我把它報廢並做了一個非常簡單的版本。 隨意改進它!

public delegate void TextBoxEvent(TextBox sender);

public class TextBox : IKeyboardSubscriber
{
    Texture2D _textBoxTexture;
    Texture2D _caretTexture;

    SpriteFont _font;

    public int X { get; set; }
    public int Y { get; set; }
    public int Width { get; set; }
    public int Height { get; private set; }

    public bool Highlighted { get; set; }

    public bool PasswordBox { get; set; }

    public event TextBoxEvent Clicked;

    string _text = "";
    public String Text
    {
        get
        {
            return _text;
        }
        set
        {
            _text = value;
            if (_text == null)
                _text = "";

            if (_text != "")
            {
                //if you attempt to display a character that is not in your font
                //you will get an exception, so we filter the characters
                //remove the filtering if you're using a default character in your spritefont
                String filtered = "";
                foreach (char c in value)
                {
                    if (_font.Characters.Contains(c))
                        filtered += c;
                }

                _text = filtered;

                while (_font.MeasureString(_text).X > Width)
                {
                    //to ensure that text cannot be larger than the box
                    _text = _text.Substring(0, _text.Length - 1);
                }
            }
        }
    }

    public TextBox(Texture2D textBoxTexture, Texture2D caretTexture, SpriteFont font)
    {
        _textBoxTexture = textBoxTexture;
        _caretTexture = caretTexture;
        _font = font;           

        _previousMouse = Mouse.GetState();
    }

    MouseState _previousMouse;
    public void Update(GameTime gameTime)
    {
        MouseState mouse = Mouse.GetState();
        Point mousePoint = new Point(mouse.X, mouse.Y);

        Rectangle position = new Rectangle(X, Y, Width, Height);
        if (position.Contains(mousePoint))
        {
            Highlighted = true;
            if (_previousMouse.LeftButton == ButtonState.Released && mouse.LeftButton == ButtonState.Pressed)
            {
                if (Clicked != null)
                    Clicked(this);
            }
        }
        else
        {
            Highlighted = false;
        }
    }

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        bool caretVisible = true;

        if ((gameTime.TotalGameTime.TotalMilliseconds % 1000) < 500)
            caretVisible = false;
        else
            caretVisible = true;

        String toDraw = Text;

        if (PasswordBox)
        {
            toDraw = "";
            for (int i = 0; i < Text.Length; i++)
                toDraw += (char) 0x2022; //bullet character (make sure you include it in the font!!!!)
        } 

        //my texture was split vertically in 2 parts, upper was unhighlighted, lower was highlighted version of the box
        spriteBatch.Draw(_textBoxTexture, new Rectangle(X, Y, Width, Height), new Rectangle(0, Highlighted ? (_textBoxTexture.Height / 2) : 0, _textBoxTexture.Width, _textBoxTexture.Height / 2), Color.White);



        Vector2 size = _font.MeasureString(toDraw);

        if (caretVisible && Selected)
            spriteBatch.Draw(_caretTexture, new Vector2(X + (int)size.X + 2, Y + 2), Color.White); //my caret texture was a simple vertical line, 4 pixels smaller than font size.Y

        //shadow first, then the actual text
        spriteBatch.DrawString(_font, toDraw, new Vector2(X, Y) + Vector2.One, Color.Black);
        spriteBatch.DrawString(_font, toDraw, new Vector2(X, Y), Color.White);
    }


    public void RecieveTextInput(char inputChar)
    {
        Text = Text + inputChar;
    }
    public void RecieveTextInput(string text)
    {
        Text = Text + text;
    }
    public void RecieveCommandInput(char command)
    {
        switch (command)
        {
            case '\b': //backspace
                if (Text.Length > 0)
                    Text = Text.Substring(0, Text.Length - 1);
                break;
            case '\r': //return
                if (OnEnterPressed != null)
                    OnEnterPressed(this);
                break;
            case '\t': //tab
                if (OnTabPressed != null)
                    OnTabPressed(this);
                break;
            default:
                break;
        }
    }
    public void RecieveSpecialInput(Keys key)
    {

    }

    public event TextBoxEvent OnEnterPressed;
    public event TextBoxEvent OnTabPressed;

    public bool Selected
    {
        get;
        set;
    }
}

實例化TextBox ,不要忘記在實例上設置XYWidth (!!!) 值( Height由字體自動設置)。

我用於盒子的紋理是文本框紋理 (未突出顯示有漸變,在黑色背景上看起來不錯:))

要顯示框,請調用實例上的.Draw()方法(在您的Game.Draw()方法中),spritebatch 已經啟動(調用SpriteBatch.Begin() !!!)。 對於您顯示的每個框,如果您希望它接收鼠標輸入,您應該調用.Update()方法。

當您希望特定實例接收鍵盤輸入時,請使用您的KeyboardDispatcher實例來訂閱它,例如:

_keyboardDispatcher.Subscriber = _usernameTextBox;

您可以使用文本框上的ClickTabEnter事件來切換訂閱者(我建議這樣做,因為當您可以通過 Tab 鍵瀏覽 UI 並單擊以進行選擇時,它會給 UI 帶來非常好的感覺)。


尚未解決的問題

Ofc,我已經談到了一些我沒有實現的功能,例如如果文本比框寬,框能夠平移文本,移動插入符號的能力(插入文本,而不僅僅是附加),以選擇和復制文本等。

這些問題你可以通過輕到中等的努力解決,我敢肯定,但在你做之前,問問自己:

我真的需要嗎?

寫過幾次這樣的代碼后,我會說在 XNA 中編寫一個基本的文本框並不難。 你定義一個用背景顏色填充的矩形,一個代表用戶輸入內容的字符串,並在矩形內使用 Spritebatch.DrawString() 顯示字符串! 使用 SpriteFont.MeasureString(),您可以隨心所欲地對齊文本,在文本被禁止時將文本換行到下一行,等等。

然后您查看每個更新的 Keyboard.GetState() 並檢查已按下哪些鍵。 這可能是最大的問題,因為如果用戶輸入速度很快,你會錯過一些按鍵——游戲每秒只更新這么多次。 這個問題在互聯網上被廣泛記錄,並且有解決方案,例如這里

另一種選擇是使用預制的 XNA GUI 組件,例如您使用Nuclex框架獲得的組件

好吧,最簡單的方法如下(從我的角度來看;])

using TextboxInputTest.Textbox.TextInput;
private TextboxInput _inputTextBox

那么我建議啟用鼠標(將其設置為可見)

IsMouseVisible = true;

現在你需要初始化文本框本身

this._inputTextBox = new TextboxInput(this, "background_box", "Arial");

這代表游戲,就是這個(懷疑你需要改變它)

background_box 是你想用來顯示的圖片的名稱(afaik,這個沒有默認選項)

Arial 是你想要使用的字體(不要忘記你必須將它添加到游戲的內容中

設置框的位置

this._inputTextBox.Position = new Vector2(100,100);

作為最后一步,您必須將框添加到組件數組

Components.Add(this._inputTextBox);

您可能想編輯許多功能,為此,我建議使用 IntelliSense

編輯:我的錯,對不起,我經常使用它們,我完全忘記了這一點;] 提前說,你看到的不是我的作品

http://www.4shared.com/file/RVqzHWk0/TextboxInput.html

希望它有所幫助。

問候,

雷萊斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM