Лекция 3: Установка XNA XNA -001-Install Game XNA 4.0 with Visual Studio 2010 (Cài đặt XNA 4.0 How to build a Pacman game in XNA - 2. Tutorial-- C# XNA 4.0 Accepting User Input from Keyboard - Duration: 2:48.
Автор книг: «Разработка компьютерных игр для Windows в XNA Game Studio Express», «Инструментальные средства программирования и отладки шейдеров в DirectX и OpenGL», «DirectX 9. Уроки программирования на С++», «Symbian OS.
Adding inputbox- like control to XNA game. Ah, the text input - I have very recent experience with this. Problem. Usually, Keyboard. Get. Keyboard. State() sucks at getting text input, and that is for many reasons, some of them being: You have to code a HUGE switch to detect what key had been pressed. You have to manually detect whether to capitalize letters (Shift or Caps. Lock)You have to decipher those Oem.
Period- like keys (as in test) to see where they actually are, and map them to specific values. There is no way to detect/use keyboard layout or keyboard language. You have to implement own mechanism for timing repetition in case of key being held down.
Chapter 8. Input For most programmers and designers, user input often takes a back seat to rendering in the priority list. But user input is far more important to gameplay. - Selection from XNA® Game Studio 4.0 for Xbox 360® Developers [Book].. Overview of User Input and Input Devices. Working with Multitouch and Accelerometer Input. Topics that provide guidance on writing code to support multitouch and accelerometer input for XNA Game Studio. Working with Text and Keyboard Input..
Second part of the problem is detecting which of your Text. Boxes (or UI controls in general) is currently receiving this input, since you don't want all of your boxes to receive text as you type. Third, you need to draw the Text.
Microsoft's XNA Game Studio Express gives you powerful game tools, and the ability to create games for both Windows and Xbox 360. In other words, gathering user input using XNA Game Studio Express is a snap.. Могу ли я использовать XNA Game Studio Express или XNA Framework XNA Framework также использует другие технологии такие как XACT и X/ Input. Презентация с доклада Павла Морозова «Игры «на коленке». XNA Game Studio». Скачать pdf | Скачать исходники.
Box in specified bounds, and you could also want to draw the caret (the blinking vertical position indicator), the current selection (if you want to go so far to implement it), the texture that represents the box, and the textures for highlighted (with mouse) or selected (has focus) state. Fourth, you have to manually implement copy- paste features. Quick note. You probably don't need all these features, as I didn't need them. You'd just want simple input, and detection for keys such as enter or tab, as well as mouse click.
Maybe also paste. Solution. The thing is (at least when we talk about Windows, not X- Box or WP7), the operating system already has the mechanisms necessary to implement everything you need from your keyboard: Gives characters based on current keyboard layout and language. Automatically handles repeating input (in case of key being held down)Automatically capitalizes and provides special characters. Solution I use for getting keyboard input, I've copied off this Gamedev. It is the code below, and you just need to copy- paste it into a . It is used for receiving localized input from your keyboard, and all you need to do is initialize it in your Game. Initialize() override method (by using Game.
Window), and hook up to the events to receive input anywhere you'd like. You need to add Presentation. Core (Presentation. Core. dll) to your references in order to use this code (needed for System. Windows. Input namespace). This works for . NET 4. NET 4. 0 Client Profile.
Event. Inputusing System. System. Runtime. Interop. Services. using Microsoft. Xna. Framework. using Microsoft. Xna. Framework. Input.
System. Text. using System. Windows. Input. namespace Event. Input. public class Keyboard. Layout. const uint KLF_ACTIVATE = 1; //activate the layout. KL_NAMELENGTH = 9; // length of the keyboard buffer.
LANG_EN_US = "0. 00. LANG_HE_IL = "0. 00.
A". [Dll. Import("user. Load. Keyboard. Layout(. KLID, // input locale identifier. Flags // input locale identifier options. Dll. Import("user. Get. Keyboard. Layout. Name(. System. Text.
String. Builder pwsz. KLID //[out] string that receives the name of the locale identifier. Name(). System. Text. String. Builder name = new System. Text. String. Builder(KL_NAMELENGTH). Get. Keyboard. Layout. Name(name). return name.
To. String(). public class Character. Event. Args : Event. Args. private readonly char character. Param. public Character. Event. Args(char character, int l.
Param). this. character = character. Param = l. Param. Character. get { return character; }.
Param. get { return l. Param; }. public int Repeat. Count. get { return l.
Param & 0xffff; }. Extended. Key. get { return (l. Param & (1 < < 2. Alt. Pressed. get { return (l. Param & (1 < < 2. Previous. State. get { return (l.
Param & (1 < < 3. Transition. State. Param & (1 < < 3. Key. Event. Args : Event.
Args. private Keys key. Code. public Key.
Event. Args(Keys key. Code). this. key. Code = key. Code.
Keys Key. Code. get { return key. Code; }. public delegate void Char. Entered. Handler(object sender, Character. Event. Args e). public delegate void Key. Event. Handler(object sender, Key. Event. Args e). public static class Event. Input. /// < summary>.
Event raised when a character has been entered. Char. Entered. Handler Char. Entered. /// < summary>.
Event raised when a key has been pressed down. May fire multiple times due to keyboard repeat. Key. Event. Handler Key. Down. /// < summary>. Event raised when a key has been released. Key. Event. Handler Key.
Up. delegate Int. Ptr Wnd. Proc(Int. Ptr h. Wnd, uint msg, Int. Ptr w. Param, Int. Ptr l. Param). static bool initialized. Int. Ptr prev. Wnd. Proc. static Wnd.
Proc hook. Proc. Delegate. Int. Ptr h. IMC. //various Win. GWL_WNDPROC = - 4. WM_KEYDOWN = 0x. 10. WM_KEYUP = 0x. 10. WM_CHAR = 0x. 10.
WM_IME_SETCONTEXT = 0x. WM_INPUTLANGCHANGE = 0x. WM_GETDLGCODE = 0x. WM_IME_COMPOSITION = 0x. DLGC_WANTALLKEYS = 4.
Win. 32 functions that we're using. Dll. Import("Imm. Char. Set = Char.
Set. Unicode)]. static extern Int. Ptr Imm. Get. Context(Int. Ptr h. Wnd). [Dll. Import("Imm. 32. dll", Char. Set = Char. Set. Unicode)]. Int. Ptr Imm. Associate.
Context(Int. Ptr h. Wnd, Int. Ptr h. IMC). Dll. Import("user. Char. Set = Char. Set. Unicode)]. static extern Int.
Ptr Call. Window. Proc(Int. Ptr lp. Prev. Wnd. Func, Int. Ptr h. Wnd, uint Msg, Int.
Ptr w. Param, Int. Ptr l. Param). [Dll.
Import("user. 32. Char. Set = Char.
Set. Unicode)]. static extern int Set. Window. Long(Int. Ptr h. Wnd, int n. Index, int dw. New. Long). /// < summary>.
Initialize the Text. Input with the given Game. Window. /// < /summary>.
The XNA window to which text input should be linked.< /param>. Initialize(Game. Window window). Invalid. Operation. Exception("Text. Input.
Initialize can only be called once!"). Proc. Delegate = new Wnd. Proc(Hook. Proc). Wnd. Proc = (Int. Ptr)Set. Window. Long(window. Handle, GWL_WNDPROC. Marshal. Get. Function.
Pointer. For. Delegate(hook. Proc. Delegate)). IMC = Imm. Get. Context(window. Handle). initialized = true. Int. Ptr Hook. Proc(Int.
Ptr h. Wnd, uint msg, Int. Ptr w. Param, Int. Ptr l. Param). Int. Ptr return. Code = Call. Window. Proc(prev.
Wnd. Proc, h. Wnd, msg, w. Param, l. Param).
WM_GETDLGCODE. return. Code = (Int. Ptr)(return.
Code. To. Int. 32() | DLGC_WANTALLKEYS). WM_KEYDOWN. if (Key. Down != null). Key.
Down(null, new Key. Event. Args((Keys)w. Param)). break. case WM_KEYUP. Key. Up != null). Key. Up(null, new Key. Event. Args((Keys)w.
Param)). break. case WM_CHAR. Char. Entered != null). Char. Entered(null, new Character.
Event. Args((char)w. Param, l. Param. To. Int. 32())). break. WM_IME_SETCONTEXT. Param. To. Int. 32() == 1). Imm. Associate. Context(h. Wnd, h. IMC). break.
WM_INPUTLANGCHANGE. Imm. Associate. Context(h. Wnd, h. IMC). return. Code = (Int. Ptr)1. Code. Now you could already use this as it is (by subscribing to Event. Input. Char. Entered event), and use logic to detect where to send your input. Keyboard. Dispatcher, IKeyboard.
Subscriber. What I did was create a class Keyboard. Dispatcher, which handles the dispatching of keyboard input by way of having a property of type IKeyboard. Subscriber to which it sends received input. The idea is that you set this property to that UI control that you want to receive input. Definitions are as follows: public interface IKeyboard. Subscriber. void Recieve.
Text. Input(char input. Char). void Recieve. Text. Input(string text). Recieve. Command. Input(char command). Recieve. Special. Input(Keys key). bool Selected { get; set; } //or Focused.
Keyboard. Dispatcher. Keyboard. Dispatcher(Game. Window window). Event. Input. Event. Input. Initialize(window). Event. Input. Event.
Input. Char. Entered += new Event. Input. Char. Entered. Handler(Event. Input_Char. Entered). Event. Input. Event. Input. Key. Down += new Event.
Input. Key. Event. Handler(Event. Input_Key. Down). void Event. Input_Key. Down(object sender, Event. Input. Key. Event. Args e). if (_subscriber == null).
Recieve. Special. Input(e. Key. Code). Event. Input_Char. Entered(object sender, Event. Input. Character. Event. Args e). if (_subscriber == null).
Is. Control(e. Character)). Character == 0x. 16). XNA runs in Multiple Thread Apartment state, which cannot recieve clipboard. Thread thread = new Thread(Paste. Thread). thread. Set. Apartment. State(Apartment.
State. STA). thread. Start(). thread. Join(). Recieve. Text. Input(_paste. Result). else. _subscriber.
Recieve. Command. Input(e. Character). Recieve. Text. Input(e. Character). IKeyboard. Subscriber _subscriber. IKeyboard. Subscriber Subscriber.
Selected = false. Selected = true. //Thread has to be in Single Thread Apartment state in order to receive clipboard. Result = "". [STAThread]. Paste. Thread(). if (Clipboard.
Contains. Text()). Result = Clipboard. Get. Text(). _paste. Result = "". Usage is fairly simple, instantiate Keyboard. Dispatcher, i. e. Game. Initialize() and keep a reference to it (so you can switch between selected [focused] controls), and pass it a class that uses the IKeyboard.
Subscriber interface, such as your Text. Box. Text. Box. Next up is your actual control. Now I've originally programed a fairly complicated box that used render targets to render the text to a texture so I could move it around (if text was larger than the box), but then after a lot of pain i scrapped it and made a really simple version. Feel free to improve it! Text. Box. Event(Text. Box sender). public class Text.
Box : IKeyboard. Subscriber. Texture. 2D _text. Box. Texture. Texture. D _caret. Texture. Sprite. Font _font. X { get; set; }. public int Y { get; set; }. Width { get; set; }.
Height { get; private set; }. Highlighted { get; set; }. Password. Box { get; set; }. Text. Box. Event Clicked. String Text. return _text.
String filtered = "". Characters. Contains(c)). Measure. String(_text). X > Width). {. Substring(0, _text. Length - 1). }. public Text.
Box(Texture. 2D text. Box. Texture, Texture. D caret. Texture, Sprite.
Font font). _text. Box. Texture = text. Box. Texture. _caret.
Texture = caret. Texture. Mouse = Mouse. Get. State(). Mouse. State _previous.
Mouse. public void Update(Game. Time game. Time). Mouse. State mouse = Mouse.
Get. State(). Point mouse.