问题
I know how to draw text in XNA but I am implementing a level designer that will require the user to enter text into a UI. I have had a little scoot around on Google but couldn't find how to implement an editable text field. Is there nothing built into the framework for this?
回答1:
you can use some sort of gui library like http://nuclexframework.codeplex.com
or use winforms http://create.msdn.com/en-US/education/catalog/sample/winforms_series_1
回答2:
Have you considered implementing a level editor for your game by embedding XNA in WinForms?
回答3:
If just need a simple way to enter a small amount of text create a class 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
}
}
}
And in the update loop
kb.Update();
string text = kb.tekst;
This is a really easy and dirty way to do it
回答4:
You will have to create your own area that the user can click into. When they do you need to capture the key presses.
回答5:
To go along with some of the other suggestions on this thread, you could also use WPF and just render the XNA to a custom user control in xaml. There's a great blog post by Nick Gravelyn on how to do this here:
http://blogs.msdn.com/b/nicgrave/archive/2010/07/25/rendering-with-xna-framework-4-0-inside-of-a-wpf-application.aspx
The great part about this is that you can use techniques like MVVM then to write your editor :-)
来源:https://stackoverflow.com/questions/4462088/xna-editable-text-field