1 using System.Collections.Generic;
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Input;
6 using System.Runtime.InteropServices;
25 public readonly Keys
Key;
33 if (name ==
null) { name =
GetName(); }
45 this.Key = keyBinding;
60 if (
Key == Keys.None) {
return false; }
86 if (
Key == Keys.None) {
return false; }
111 return this == keyOrMouse;
131 return !(b is
null) && a.
Key.Equals(b.
Key);
143 return keyOrMouse.
Key == key;
148 return !(keyOrMouse == key);
153 return keyOrMouse == key;
158 return keyOrMouse != key;
163 return keyOrMouse.
MouseButton == mb && keyOrMouse.
Key == Keys.None;
168 return !(keyOrMouse == mb);
173 return keyOrMouse == mb;
178 return keyOrMouse != mb;
186 return Key.ToString();
194 var hashCode =
int.MinValue;
195 hashCode = hashCode * -1521134295 +
Key.GetHashCode();
196 hashCode = hashCode * -1521134295 + EqualityComparer<int?>.Default.GetHashCode((
int)
MouseButton);
204 return Key.ToString().Substring(1, 1);
215 return TextManager.Get($
"Input.{MouseButton}");
220 return Key.ToString();
227 static MouseState mouseState, oldMouseState;
228 static MouseState latestMouseState;
229 static KeyboardState keyboardState, oldKeyboardState;
231 static double timeSincePrimaryClick;
232 static Point lastPrimaryClickPosition;
234 static double timeSinceSecondaryClick;
235 static Point lastSecondaryClickPosition;
237 const float DoubleClickDelay = 0.4f;
243 static bool primaryDoubleClicked;
244 static bool secondaryDoubleClicked;
246 static bool allowInput;
247 static bool wasWindowActive;
249 public static readonly List<Keys>
NumberKeys =
new List<Keys> { Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9 };
252 [DllImport(
"user32.dll")]
253 static extern int GetSystemMetrics(
int smIndex);
254 private const int SM_SWAPBUTTON = 23;
258 return GetSystemMetrics(SM_SWAPBUTTON) != 0;
272 get {
return new Vector2(mouseState.Position.X, mouseState.Position.Y); }
277 get {
return new Vector2(latestMouseState.Position.X, latestMouseState.Position.Y); }
289 return AllowInput ?
MousePosition -
new Vector2(oldMouseState.X, oldMouseState.Y) : Vector2.Zero;
293 private static bool AllowInput
302 get {
return keyboardState; }
307 get {
return oldKeyboardState; }
312 get {
return AllowInput ? mouseState.ScrollWheelValue - oldMouseState.ScrollWheelValue : 0; }
318 return AllowInput && mouseState.LeftButton == ButtonState.Pressed;
324 oldMouseState.LeftButton == ButtonState.Released &&
325 mouseState.LeftButton == ButtonState.Pressed;
330 return AllowInput && mouseState.LeftButton == ButtonState.Released;
336 return (AllowInput &&
337 oldMouseState.LeftButton == ButtonState.Pressed
338 && mouseState.LeftButton == ButtonState.Released);
343 return AllowInput && mouseState.RightButton == ButtonState.Pressed;
349 oldMouseState.RightButton == ButtonState.Released &&
350 mouseState.RightButton == ButtonState.Pressed;
355 return AllowInput && mouseState.RightButton == ButtonState.Released;
360 return (AllowInput &&
361 oldMouseState.RightButton == ButtonState.Pressed
362 && mouseState.RightButton == ButtonState.Released);
367 return (AllowInput &&
368 oldMouseState.MiddleButton == ButtonState.Pressed
369 && mouseState.MiddleButton == ButtonState.Released);
374 return AllowInput && mouseState.MiddleButton == ButtonState.Pressed;
379 return (AllowInput &&
380 oldMouseState.XButton1 == ButtonState.Pressed
381 && mouseState.XButton1 == ButtonState.Released);
386 return AllowInput && mouseState.XButton1 == ButtonState.Pressed;
391 return (AllowInput &&
392 oldMouseState.XButton2 == ButtonState.Pressed
393 && mouseState.XButton2 == ButtonState.Released);
398 return AllowInput && mouseState.XButton2 == ButtonState.Pressed;
413 return AllowInput && primaryDoubleClicked;
418 return AllowInput && secondaryDoubleClicked;
423 return AllowInput && GameSettings.CurrentConfig.KeyMap.Bindings[inputType].IsHit();
428 return AllowInput && GameSettings.CurrentConfig.KeyMap.Bindings[inputType].IsDown();
433 return AllowInput && !GameSettings.CurrentConfig.KeyMap.Bindings[inputType].IsDown();
438 return AllowInput && oldKeyboardState.IsKeyUp(button) && keyboardState.IsKeyDown(button);
443 if (index == -1)
return false;
444 return AllowInput && GameSettings.CurrentConfig.InventoryKeyMap.Bindings[index].IsHit();
449 return AllowInput && keyboardState.IsKeyDown(button);
452 public static bool KeyUp(Keys button)
454 return AllowInput && keyboardState.IsKeyUp(button);
476 public static void Update(
double deltaTime)
478 timeSincePrimaryClick += deltaTime;
479 timeSinceSecondaryClick += deltaTime;
483 wasWindowActive =
false;
488 if (!wasWindowActive)
490 wasWindowActive =
true;
500 oldMouseState = mouseState;
501 mouseState = latestMouseState;
504 oldKeyboardState = keyboardState;
505 keyboardState = Keyboard.GetState();
510 primaryDoubleClicked =
false;
513 primaryDoubleClicked = UpdateDoubleClicking(ref lastPrimaryClickPosition, ref timeSincePrimaryClick);
518 lastPrimaryClickPosition = mouseState.Position;
521 secondaryDoubleClicked =
false;
524 secondaryDoubleClicked = UpdateDoubleClicking(ref lastSecondaryClickPosition, ref timeSinceSecondaryClick);
529 lastSecondaryClickPosition = mouseState.Position;
532 bool UpdateDoubleClicking(ref Point lastClickPosition, ref
double timeSinceClick)
534 bool doubleClicked =
false;
535 float dist = (mouseState.Position - lastClickPosition).ToVector2().Length();
539 doubleClicked =
true;
540 timeSinceClick = DoubleClickDelay;
542 else if (timeSinceClick < DoubleClickDelay)
544 lastClickPosition = mouseState.Position;
548 timeSinceClick = 0.0;
551 return doubleClicked;
559 latestMouseState = Mouse.GetState();
static int GraphicsHeight
LocalizedString GetName()
override string ToString()
KeyOrMouse(MouseButton mouseButton)
override int GetHashCode()
KeyOrMouse(Keys keyBinding)
static bool operator==(KeyOrMouse a, KeyOrMouse b)
override bool Equals(object obj)
static bool operator!=(KeyOrMouse a, KeyOrMouse b)
object Call(string name, params object[] args)