18 public static double DoubleClickInterval = 0.2;
19 public static float ScrollSpeed = 0.6f;
23 public MouseState Mouse;
24 public bool MouseDown;
25 public bool DoubleClick;
27 public bool MouseHeld;
30 public Vector2 MousePosition;
31 public Vector2 MousePositionDif;
32 public bool MouseMoved;
34 public bool SomethingHappened;
37 public bool ClickConsumed;
39 public KeyboardState Keyboard;
40 public Keys[] HeldKeys =
new Keys[0];
41 public Keys[] PressedKeys =
new Keys[0];
42 public Keys[] UnpressedKeys =
new Keys[0];
43 public bool SomeKeyHeld;
44 public bool SomeKeyPressed;
45 public bool SomeKeyUnpressed;
46 public TextInputEventArgs[] WindowTextInputEvents;
47 public TextInputEventArgs[] WindowKeyDownEvents;
48 public bool SomeWindowEvents;
52 private double PrevMouseDownTiming;
53 private int PrevScrollWheelValue;
54 private MouseState PrevMouseState;
55 private Vector2 PrevMousePosition;
56 private Keys[] PrevHeldKeys =
new Keys[0];
57 private Queue<TextInputEventArgs> WindowTextInputQueue =
new Queue<TextInputEventArgs>(10);
58 private Queue<TextInputEventArgs> WindowKeyDownQueue =
new Queue<TextInputEventArgs>(10);
61 public bool MouseInputHandled {
get;
set; }
63 public void Scan(
double totalTime)
65 MouseInputHandled =
false;
67 ScanKeyboard(totalTime);
70 private void ScanMouse(
double totalTime)
72 ClickConsumed =
false;
74 Mouse = Microsoft.Xna.Framework.Input.Mouse.GetState();
76 MouseDown = PrevMouseState.LeftButton == ButtonState.Released && Mouse.LeftButton == ButtonState.Pressed;
77 MouseUp = PrevMouseState.LeftButton == ButtonState.Pressed && Mouse.LeftButton == ButtonState.Released;
78 MouseHeld = Mouse.LeftButton == ButtonState.Pressed;
80 PrevMousePosition = MousePosition;
81 MousePosition =
new Vector2(Mouse.Position.X, Mouse.Position.Y);
82 MousePositionDif = MousePosition - PrevMousePosition;
83 MouseMoved = MousePositionDif != Vector2.Zero;
85 Scroll = (Mouse.ScrollWheelValue - PrevScrollWheelValue) * ScrollSpeed;
86 PrevScrollWheelValue = Mouse.ScrollWheelValue;
87 Scrolled = Scroll != 0;
93 if (totalTime - PrevMouseDownTiming < DoubleClickInterval)
98 PrevMouseDownTiming = totalTime;
101 SomethingHappened = MouseHeld || MouseUp || MouseDown || MouseMoved || Scrolled;
103 PrevMouseState = Mouse;
106 private void ScanKeyboard(
double totalTime)
108 Keyboard = Microsoft.Xna.Framework.Input.Keyboard.GetState();
109 HeldKeys = Keyboard.GetPressedKeys();
110 SomeKeyHeld = HeldKeys.Length > 0;
112 PressedKeys = HeldKeys.Except(PrevHeldKeys).ToArray();
113 UnpressedKeys = PrevHeldKeys.Except(HeldKeys).ToArray();
115 SomeKeyPressed = PressedKeys.Length > 0;
116 SomeKeyUnpressed = UnpressedKeys.Length > 0;
118 PrevHeldKeys = HeldKeys;
120 WindowTextInputEvents = WindowTextInputQueue.ToArray();
121 WindowTextInputQueue.Clear();
123 WindowKeyDownEvents = WindowKeyDownQueue.ToArray();
124 WindowKeyDownQueue.Clear();
127 SomeWindowEvents = WindowTextInputEvents.Length > 0 || WindowKeyDownEvents.Length > 0;
132 CUI.OnWindowKeyDown += (e) => WindowKeyDownQueue.Enqueue(e);
133 CUI.OnWindowTextInput += (e) => WindowTextInputQueue.Enqueue(e);