Gute Tag liebe XNA Freunde
Hiermit Release ich für euch die Xbox360 & Keyboard Controll Funktion.
Es ist schon alles perfekt eingestellt. Ihr müsst es nur noch an eurem
XNA Game anpassen.
Hiermit Release ich für euch die Xbox360 & Keyboard Controll Funktion.
Es ist schon alles perfekt eingestellt. Ihr müsst es nur noch an eurem
XNA Game anpassen.
Quellcode
- /*//---------------------------------------------------//
- * GameControl.cs *
- * Microsoft XNA Game Platform *
- * Adventure of Kanaarh *
- * Copyright (C) Jok3r *
- * Copyright (C) Kevin Kaymak. All rights reserved *
- *///--------------------------------------------------//
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Input;
- namespace AdventureofK
- {
- public static class GameControl
- {
- public enum Action
- {
- MainMenu,
- Ok,
- Back,
- CharackterManagment,
- Exit,
- TakeView,
- DropUnEquip,
- MoveUp,
- MoveDown,
- MoveLeft,
- MoveRight,
- CursorUp,
- CursorDown,
- DecreaseAmount,
- IncreaseAmount,
- PageLeft,
- PageRight,
- TargetUp,
- TargetDown,
- ActiveLeft,
- ActiveRight,
- TotalActionCount,
- }
- private static readonly string[] actionNames =
- {
- "Haupt Menü",
- "Ok",
- "Zurück",
- "Charackter Auswahl",
- "Spiel Beenden",
- "Nehmen/Ansehen",
- "Drop/Unequip",
- "Move - Up",
- "Move - Down",
- "Move - Right",
- "Move Cursor - Up",
- "Move Cursor - Down",
- "Decrease Amount",
- "Increase Amount",
- "Page Left",
- "Page Right",
- "Target Up",
- "Target Down",
- "Select Char Left",
- "Select Char Right",
- };
- public static string GetActionName(Action action)
- {
- int index = (int)action;
- if ((index < 0) || (index > actionNames.Length))
- {
- throw new ArgumentException("action");
- }
- return actionNames[index];
- }
- public enum GamePadButtons
- {
- Start,
- Back,
- A,
- B,
- X,
- Y,
- Up,
- Down,
- Left,
- Right,
- LeftSchoulder,
- RightSchoulder,
- LeftTrigger,
- RightTrigger,
- }
- public class ActionMap
- {
- public List<GamePadButtons> gamePadButtons = new List<GamePadButtons>();
- public List<Keys> keyboardKeys = new List<Keys>();
- }
- const float analogLimit = 0.5f;
- public static KeyboardState currentKeyboardState;
- public static KeyboardState CurrentKeyboardState
- {
- get { return currentKeyboardState; }
- }
- private static KeyboardState previousKeyboardState;
- public static bool IsKeyPressed(Keys key)
- {
- return currentKeyboardState.IsKeyDown(key);
- }
- public static bool IsKeyTriggered(Keys key)
- {
- return (currentKeyboardState.IsKeyDown(key)) &&
- (!previousKeyboardState.IsKeyDown(key));
- }
- //GamePad Controlls
- private static GamePadState currentGamePadState;
- private static GamePadState CurrentGamePadState
- {
- get { return currentGamePadState; }
- }
- private static GamePadState previousGamePadState;
- public static bool IsGamePadStartPressed()
- {
- return (currentGamePadState.Buttons.Start == ButtonState.Pressed);
- }
- public static bool IsGamePadBackPressed()
- {
- return (currentGamePadState.Buttons.Back == ButtonState.Pressed);
- }
- public static bool IsGamePadAPressed()
- {
- return (currentGamePadState.Buttons.A == ButtonState.Pressed);
- }
- public static bool IsGamePadBPressed()
- {
- return (currentGamePadState.Buttons.B == ButtonState.Pressed);
- }
- public static bool IsGamePadXPressed()
- {
- return (currentGamePadState.Buttons.X == ButtonState.Pressed);
- }
- public static bool IsGamePadYkPressed()
- {
- return (currentGamePadState.Buttons.Y == ButtonState.Pressed);
- }
- public static bool IsGamePadLeftShoulderPressed()
- {
- return (currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed);
- }
- public static bool IsGamePadRightShoulderPressed()
- {
- return (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed);
- }
- public static bool IsGamePadDPadDownPressed()
- {
- return (currentGamePadState.DPad.Down == ButtonState.Pressed);
- }
- public static bool IsGamePadDPadUpPressed()
- {
- return (currentGamePadState.DPad.Up == ButtonState.Pressed);
- }
- public static bool IsGamePadDPadLeftPressed()
- {
- return (currentGamePadState.DPad.Left == ButtonState.Pressed);
- }
- public static bool IsGamePadDPadRightPressed()
- {
- return (currentGamePadState.DPad.Right == ButtonState.Pressed);
- }
- public static bool IsGamePadLeftTriggerPressed()
- {
- return (currentGamePadState.Triggers.Left > analogLimit);
- }
- public static bool IsGamePadRightTriggerPressed()
- {
- return (currentGamePadState.Triggers.Right > analogLimit);
- }
- public static bool IsGamePadLeftStickUpPressed()
- {
- return (currentGamePadState.ThumbSticks.Left.Y > analogLimit);
- }
- public static bool IsGamePadLeftStickDownPressed()
- {
- return (-1f * currentGamePadState.ThumbSticks.Left.Y > analogLimit);
- }
- public static bool IsGamePadLeftStickLeftPressed()
- {
- return (-1f * currentGamePadState.ThumbSticks.Left.X > analogLimit);
- }
- public static bool IsGamePadLeftStickRightPressed()
- {
- return (currentGamePadState.ThumbSticks.Left.X > analogLimit);
- }
- private static bool IsGamePadButtonsPressed(GamePadButtons gamePadKey)
- {
- switch (gamePadKey)
- {
- case GamePadButtons.Start:
- return IsGamePadStartPressed();
- case GamePadButtons.Back:
- return IsGamePadBackPressed();
- case GamePadButtons.A:
- return IsGamePadAPressed();
- case GamePadButtons.B:
- return IsGamePadBPressed();
- case GamePadButtons.X:
- return IsGamePadXPressed();
- case GamePadButtons.LeftSchoulder:
- return IsGamePadLeftShoulderPressed();
- case GamePadButtons.RightSchoulder:
- return IsGamePadRightShoulderPressed();
- case GamePadButtons.LeftTrigger:
- return IsGamePadLeftTriggerPressed();
- case GamePadButtons.RightTrigger:
- return IsGamePadRightTriggerPressed();
- case GamePadButtons.Up:
- return IsGamePadDPadUpPressed() ||
- IsGamePadLeftStickUpPressed();
- case GamePadButtons.Down:
- return IsGamePadDPadDownPressed() ||
- IsGamePadLeftStickDownPressed();
- case GamePadButtons.Left:
- return IsGamePadDPadLeftPressed() ||
- IsGamePadLeftStickLeftPressed();
- case GamePadButtons.Right:
- return IsGamePadDPadRightPressed() ||
- IsGamePadLeftStickRightPressed();
- }
- return false;
- }
- //Gamepad Triggers
- public static bool IsGamePadStartTriggered()
- {
- return ((currentGamePadState.Buttons.Start == ButtonState.Pressed) &&
- (previousGamePadState.Buttons.Start == ButtonState.Released));
- }
- public static bool IsGamePadBackTriggered()
- {
- return ((currentGamePadState.Buttons.Back == ButtonState.Pressed) &&
- (previousGamePadState.Buttons.Back == ButtonState.Released));
- }
- public static bool IsGamePadATriggered()
- {
- return ((currentGamePadState.Buttons.A == ButtonState.Pressed) &&
- (previousGamePadState.Buttons.A == ButtonState.Released));
- }
- public static bool IsGamePadBTriggered()
- {
- return ((currentGamePadState.Buttons.B == ButtonState.Pressed) &&
- (previousGamePadState.Buttons.B == ButtonState.Released));
- }
- public static bool IsGamePadXTriggered()
- {
- return ((currentGamePadState.Buttons.X == ButtonState.Pressed) &&
- (previousGamePadState.Buttons.X == ButtonState.Released));
- }
- public static bool IsGamePadYTriggered()
- {
- return ((currentGamePadState.Buttons.Y == ButtonState.Pressed) &&
- (previousGamePadState.Buttons.Y == ButtonState.Released));
- }
- public static bool IsGamePadLeftShoulderTriggered()
- {
- return ((currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed) &&
- (previousGamePadState.Buttons.LeftShoulder == ButtonState.Released));
- }
- public static bool IsGamePadRightShoulderTriggered()
- {
- return ((currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed) &&
- (previousGamePadState.Buttons.RightShoulder == ButtonState.Released));
- }
- public static bool IsGamePadDPatUpTriggered()
- {
- return ((currentGamePadState.DPad.Up == ButtonState.Pressed) &&
- (previousGamePadState.DPad.Up == ButtonState.Released));
- }
- public static bool IsGamePadDPatDownTriggered()
- {
- return ((currentGamePadState.DPad.Down == ButtonState.Pressed) &&
- (previousGamePadState.DPad.Down == ButtonState.Released));
- }
- public static bool IsGamePadDPatLeftTriggered()
- {
- return ((currentGamePadState.DPad.Left == ButtonState.Pressed) &&
- (previousGamePadState.DPad.Left == ButtonState.Released));
- }
- public static bool IsGamePadDPatRightTriggered()
- {
- return ((currentGamePadState.DPad.Right == ButtonState.Pressed) &&
- (previousGamePadState.DPad.Right == ButtonState.Released));
- }
- public static bool IsGamePadLeftTriggerTriggered()
- {
- return ((currentGamePadState.Triggers.Left > analogLimit) &&
- (previousGamePadState.Triggers.Left > analogLimit));
- }
- public static bool IsGamePadRightTriggerTriggered()
- {
- return ((currentGamePadState.Triggers.Right > analogLimit) &&
- (previousGamePadState.Triggers.Right > analogLimit));
- }
- public static bool IsGamePadLeftStickUpTriggered()
- {
- return ((currentGamePadState.ThumbSticks.Left.Y > analogLimit) &&
- (previousGamePadState.ThumbSticks.Left.Y > analogLimit));
- }
- public static bool IsGamePadLeftStickDownTriggered()
- {
- return ((-1f * currentGamePadState.ThumbSticks.Left.Y > analogLimit) &&
- (-1f * previousGamePadState.ThumbSticks.Left.Y > analogLimit));
- }
- public static bool IsGamePadLeftStickLeftTriggered()
- {
- return ((-1f * currentGamePadState.ThumbSticks.Left.X > analogLimit) &&
- (-1f * previousGamePadState.ThumbSticks.Left.X > analogLimit));
- }
- public static bool IsGamePadLeftStickRightTriggered()
- {
- return ((currentGamePadState.ThumbSticks.Left.X > analogLimit) &&
- (previousGamePadState.ThumbSticks.Left.X > analogLimit));
- }
- private static bool IsGamePadButtonTriggered(GamePadButtons gamePadKey)
- {
- switch (gamePadKey)
- {
- case GamePadButtons.Start:
- return IsGamePadStartTriggered();
- case GamePadButtons.Back:
- return IsGamePadBackTriggered();
- case GamePadButtons.A:
- return IsGamePadATriggered();
- case GamePadButtons.B:
- return IsGamePadBTriggered();
- case GamePadButtons.X:
- return IsGamePadXTriggered();
- case GamePadButtons.Y:
- return IsGamePadYTriggered();
- case GamePadButtons.LeftSchoulder:
- return IsGamePadLeftShoulderTriggered();
- case GamePadButtons.RightSchoulder:
- return IsGamePadRightShoulderTriggered();
- case GamePadButtons.LeftTrigger:
- return IsGamePadLeftTriggerTriggered();
- case GamePadButtons.RightTrigger:
- return IsGamePadRightTriggerTriggered();
- case GamePadButtons.Up:
- return IsGamePadDPatUpTriggered() ||
- IsGamePadLeftStickUpTriggered();
- case GamePadButtons.Down:
- return IsGamePadDPatDownTriggered() ||
- IsGamePadLeftStickDownTriggered();
- case GamePadButtons.Left:
- return IsGamePadDPatLeftTriggered() ||
- IsGamePadLeftStickLeftTriggered();
- case GamePadButtons.Right:
- return IsGamePadDPatRightTriggered() ||
- IsGamePadLeftStickRightTriggered();
- }
- return false;
- }
- //Action Mapping Game
- private static ActionMap[] actionMaps;
- public static ActionMap[] ActionMaps
- {
- get { return actionMaps; }
- }
- private static void ResetActionMaps()
- {
- actionMaps = new ActionMap[(int)Action.TotalActionCount];
- actionMaps[(int)Action.MainMenu] = new ActionMap();
- actionMaps[(int)Action.MainMenu].keyboardKeys.Add(Keys.Tab);
- actionMaps[(int)Action.MainMenu].gamePadButtons.Add(GamePadButtons.Start);
- actionMaps[(int)Action.Ok] = new ActionMap();
- actionMaps[(int)Action.Ok].keyboardKeys.Add(Keys.Enter);
- actionMaps[(int)Action.Ok].gamePadButtons.Add(GamePadButtons.A);
- actionMaps[(int)Action.Back] = new ActionMap();
- actionMaps[(int)Action.Back].keyboardKeys.Add(Keys.Escape);
- actionMaps[(int)Action.Back].gamePadButtons.Add(GamePadButtons.B);
- actionMaps[(int)Action.CharackterManagment] = new ActionMap();
- actionMaps[(int)Action.CharackterManagment].keyboardKeys.Add(Keys.Space);
- actionMaps[(int)Action.CharackterManagment].gamePadButtons.Add(GamePadButtons.Y);
- actionMaps[(int)Action.Exit] = new ActionMap();
- actionMaps[(int)Action.Exit].keyboardKeys.Add(Keys.Escape);
- actionMaps[(int)Action.Exit].gamePadButtons.Add(GamePadButtons.Back);
- actionMaps[(int)Action.TakeView] = new ActionMap();
- actionMaps[(int)Action.TakeView].keyboardKeys.Add(Keys.LeftControl);
- actionMaps[(int)Action.TakeView].gamePadButtons.Add(GamePadButtons.Y);
- actionMaps[(int)Action.DropUnEquip] = new ActionMap();
- actionMaps[(int)Action.DropUnEquip].keyboardKeys.Add(Keys.D);
- actionMaps[(int)Action.DropUnEquip].gamePadButtons.Add(GamePadButtons.X);
- actionMaps[(int)Action.MoveUp] = new ActionMap();
- actionMaps[(int)Action.MoveUp].keyboardKeys.Add(Keys.Up);
- actionMaps[(int)Action.MoveUp].gamePadButtons.Add(GamePadButtons.Up);
- actionMaps[(int)Action.MoveDown] = new ActionMap();
- actionMaps[(int)Action.MoveDown].keyboardKeys.Add(Keys.Down);
- actionMaps[(int)Action.MoveDown].gamePadButtons.Add(GamePadButtons.Down);
- actionMaps[(int)Action.MoveLeft] = new ActionMap();
- actionMaps[(int)Action.MoveLeft].keyboardKeys.Add(Keys.Left);
- actionMaps[(int)Action.MoveLeft].gamePadButtons.Add(GamePadButtons.Left);
- actionMaps[(int)Action.MoveRight] = new ActionMap();
- actionMaps[(int)Action.MoveRight].keyboardKeys.Add(Keys.Right);
- actionMaps[(int)Action.MoveRight].gamePadButtons.Add(GamePadButtons.Right);
- actionMaps[(int)Action.CursorUp] = new ActionMap();
- actionMaps[(int)Action.CursorUp].keyboardKeys.Add(Keys.Up);
- actionMaps[(int)Action.CursorUp].gamePadButtons.Add(GamePadButtons.Up);
- actionMaps[(int)Action.CursorDown] = new ActionMap();
- actionMaps[(int)Action.CursorDown].keyboardKeys.Add(Keys.Down);
- actionMaps[(int)Action.CursorDown].gamePadButtons.Add(GamePadButtons.Down);
- actionMaps[(int)Action.DecreaseAmount] = new ActionMap();
- actionMaps[(int)Action.DecreaseAmount].keyboardKeys.Add(Keys.Left);
- actionMaps[(int)Action.DecreaseAmount].gamePadButtons.Add(GamePadButtons.Left);
- actionMaps[(int)Action.IncreaseAmount] = new ActionMap();
- actionMaps[(int)Action.IncreaseAmount].keyboardKeys.Add(Keys.Right);
- actionMaps[(int)Action.IncreaseAmount].gamePadButtons.Add(GamePadButtons.Right);
- actionMaps[(int)Action.PageLeft] = new ActionMap();
- actionMaps[(int)Action.PageLeft].keyboardKeys.Add(Keys.LeftShift);
- actionMaps[(int)Action.PageLeft].gamePadButtons.Add(GamePadButtons.LeftTrigger);
- actionMaps[(int)Action.PageRight] = new ActionMap();
- actionMaps[(int)Action.PageRight].keyboardKeys.Add(Keys.RightShift);
- actionMaps[(int)Action.PageRight].gamePadButtons.Add(GamePadButtons.RightTrigger);
- actionMaps[(int)Action.TargetUp] = new ActionMap();
- actionMaps[(int)Action.TargetUp].keyboardKeys.Add(Keys.Up);
- actionMaps[(int)Action.TargetUp].gamePadButtons.Add(GamePadButtons.Up);
- actionMaps[(int)Action.TargetDown] = new ActionMap();
- actionMaps[(int)Action.TargetDown].keyboardKeys.Add(Keys.Down);
- actionMaps[(int)Action.TargetDown].gamePadButtons.Add(GamePadButtons.Down);
- actionMaps[(int)Action.ActiveLeft] = new ActionMap();
- actionMaps[(int)Action.ActiveLeft].keyboardKeys.Add(Keys.Left);
- actionMaps[(int)Action.ActiveLeft].gamePadButtons.Add(GamePadButtons.Left);
- actionMaps[(int)Action.ActiveRight] = new ActionMap();
- actionMaps[(int)Action.ActiveRight].keyboardKeys.Add(Keys.Right);
- actionMaps[(int)Action.ActiveRight].gamePadButtons.Add(GamePadButtons.Right);
- }
- public static bool IsActionPressed(Action action)
- {
- return IsActionMapPressed(actionMaps[(int)action]);
- }
- public static bool IsActionTriggered(Action action)
- {
- return IsActionMapTriggered(actionMaps[(int)action]);
- }
- private static bool IsActionMapPressed(ActionMap actionMap)
- {
- for (int i = 0; i < actionMap.keyboardKeys.Count; i++)
- {
- if (IsKeyPressed(actionMap.keyboardKeys[i]))
- {
- return true;
- }
- }
- if (currentGamePadState.IsConnected)
- {
- for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
- {
- if (IsGamePadButtonsPressed(actionMap.gamePadButtons[i]))
- {
- return true;
- }
- }
- }
- return false;
- }
- private static bool IsActionMapTriggered(ActionMap actionMap)
- {
- for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
- {
- if (IsGamePadButtonTriggered(actionMap.gamePadButtons[i]))
- {
- return true;
- }
- }
- if (currentGamePadState.IsConnected)
- {
- for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
- {
- if (IsGamePadButtonTriggered(actionMap.gamePadButtons[i]))
- {
- return true;
- }
- }
- }
- return false;
- }
- public static void Initialize()
- {
- ResetActionMaps();
- }
- public static void Update()
- {
- //Update Keyboard
- previousKeyboardState = currentKeyboardState;
- currentKeyboardState = Keyboard.GetState();
- //Update Gamepade
- previousGamePadState = currentGamePadState;
- currentGamePadState = GamePad.GetState(PlayerIndex.One);
- }
- }
- }
Ihr müsst nur euren Namespace anpassen.
Auserdem müsst ihr in eurer Game.cs folgende dinge adden.
im feld und im feld
und schon könnt ihr euren Xbox Controller benutzen und eure
Tastaturbelegung anpassen!
Für diesen Code habe ich ca 2 Tage harte Arbeit gebraucht.
Es sind:
-600 Zeilen
-493 Absätze
-20.564 Zeichen
-1.143 Wörter
Ich werde schauen das ich noch mehr solche Dateien Releasen werde, da ich
ja weniger Zeit habe ein paar XNA Tutorials zu schreiben.
Wer Support brauch bei diesem Code, kann sich gerne an mich wenden.
Auserdem müsst ihr in eurer Game.cs folgende dinge adden.
im feld und im feld
Tastaturbelegung anpassen!
Für diesen Code habe ich ca 2 Tage harte Arbeit gebraucht.
Es sind:
-600 Zeilen
-493 Absätze
-20.564 Zeichen
-1.143 Wörter
Ich werde schauen das ich noch mehr solche Dateien Releasen werde, da ich
ja weniger Zeit habe ein paar XNA Tutorials zu schreiben.
Wer Support brauch bei diesem Code, kann sich gerne an mich wenden.