XNA可编辑文本字段

时间:2010-12-16 14:57:46

标签: c# user-interface xna

我知道如何在XNA中绘制文本,但我正在实现一个级别设计器,它将要求用户在UI中输入文本。我在Google上遇到了一些小问题,但无法找到如何实现可编辑的文本字段。框架中没有内置任何内容吗?

5 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

您是否考虑过embedding XNA in WinForms为您的游戏实施关卡编辑器?

答案 2 :(得分:1)

如果只需要一种简单的方法输入少量文本就可以创建一个类kbHandler

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;

namespace CodeName
{
public class KbHandler
{
    private Keys[] lastPressedKeys;
    public string tekst = "";

    public KbHandler()
    {
        lastPressedKeys = new Keys[0];
    }

    public void Update()
    {
        KeyboardState kbState = Keyboard.GetState();
        Keys[] pressedKeys = kbState.GetPressedKeys();

        //check if any of the previous update's keys are no longer pressed
        foreach (Keys key in lastPressedKeys)
        {
            if (!pressedKeys.Contains(key))
                OnKeyUp(key);
        }

        //check if the currently pressed keys were already pressed
        foreach (Keys key in pressedKeys)
        {
            if (!lastPressedKeys.Contains(key))
                OnKeyDown(key);
        }

        //save the currently pressed keys so we can compare on the next update
        lastPressedKeys = pressedKeys;
    }

//Create your own   
private void OnKeyDown(Keys key)
    {
        switch (key)
        {
            case Keys.D0:
                tekst += "0";
                break;
            case Keys.D1:
                tekst += "1";
                break;
            case Keys.D2:
                tekst += "2";
                break;
            case Keys.D3:
                tekst += "3";
                break;
            case Keys.D4:
                tekst += "4";
                break;
            case Keys.D5:
                tekst += "5";
                break;
            case Keys.D6:
                tekst += "6";
                break;
            case Keys.D7:
                tekst += "7";
                break;
            case Keys.D8:
                tekst += "8";
                break;
            case Keys.D9:
                tekst += "9";
                break;
            case Keys.NumPad0:
                tekst += "0";
                break;
            case Keys.NumPad1:
                tekst += "1";
                break;
            case Keys.NumPad2:
                tekst += "2";
                break;
            case Keys.NumPad3:
                tekst += "3";
                break;
            case Keys.NumPad4:
                tekst += "4";
                break;
            case Keys.NumPad5:
                tekst += "5";
                break;
            case Keys.NumPad6:
                tekst += "6";
                break;
            case Keys.NumPad7:
                tekst += "7";
                break;
            case Keys.NumPad8:
                tekst += "8";
                break;
            case Keys.NumPad9:
                tekst += "9";
                break;
            case Keys.OemPeriod:
                tekst += ".";
                break;
            case Keys.Back:
                if (tekst.Length > 0)
                {
                    tekst = tekst.Remove(tekst.Length - 1, 1);
                }                    
                break;

        }

    }

    private void OnKeyUp(Keys key)
    {
        //do stuff
    }
}
}

在更新循环中

kb.Update();
string text = kb.tekst;

这是一种非常容易和脏的方法

答案 3 :(得分:0)

您必须创建用户可以单击的区域。当他们这样做时,您需要捕获按键。

答案 4 :(得分:0)

为了与此线程中的其他一些建议一起使用,您还可以使用WPF并将XNA渲染为xaml中的自定义用户控件。 Nick Gravelyn有一篇很棒的博客文章,关于如何在这里做到这一点:
http://blogs.msdn.com/b/nicgrave/archive/2010/07/25/rendering-with-xna-framework-4-0-inside-of-a-wpf-application.aspx

关于这个的重要部分是你可以使用像MVVM这样的技术编写你的编辑器: - )