CrabUI
Loading...
Searching...
No Matches
CUIInput.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6using Barotrauma;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
10
11namespace CrabUI
12{
13 /// <summary>
14 /// Containing a snapshot of current mouse and keyboard state
15 /// </summary>
16 public class CUIInput
17 {
18 public static double DoubleClickInterval = 0.2;
19 public static float ScrollSpeed = 0.6f;
20
21
22
23 public MouseState Mouse;
24 public bool MouseDown;
25 public bool DoubleClick;
26 public bool MouseUp;
27 public bool MouseHeld;
28 public float Scroll;
29 public bool Scrolled;
30 public Vector2 MousePosition;
31 public Vector2 MousePositionDif;
32 public bool MouseMoved;
33 //TODO split into sh mouse and sh keyboard
34 public bool SomethingHappened;
35
36 //HACK rethink, this is too hacky
37 public bool ClickConsumed;
38
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;
49
50
51 //-------------- private stuff
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);
59
60 //HACK super hacky solution to block input from one CUIMainComponent to another
61 public bool MouseInputHandled { get; set; }
62
63 public void Scan(double totalTime)
64 {
65 MouseInputHandled = false;
66 ScanMouse(totalTime);
67 ScanKeyboard(totalTime);
68 }
69
70 private void ScanMouse(double totalTime)
71 {
72 ClickConsumed = false;
73
74 Mouse = Microsoft.Xna.Framework.Input.Mouse.GetState();
75
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;
79
80 PrevMousePosition = MousePosition;
81 MousePosition = new Vector2(Mouse.Position.X, Mouse.Position.Y);
82 MousePositionDif = MousePosition - PrevMousePosition;
83 MouseMoved = MousePositionDif != Vector2.Zero;
84
85 Scroll = (Mouse.ScrollWheelValue - PrevScrollWheelValue) * ScrollSpeed;
86 PrevScrollWheelValue = Mouse.ScrollWheelValue;
87 Scrolled = Scroll != 0;
88
89 DoubleClick = false;
90
91 if (MouseDown)
92 {
93 if (totalTime - PrevMouseDownTiming < DoubleClickInterval)
94 {
95 DoubleClick = true;
96 }
97
98 PrevMouseDownTiming = totalTime;
99 }
100
101 SomethingHappened = MouseHeld || MouseUp || MouseDown || MouseMoved || Scrolled;
102
103 PrevMouseState = Mouse;
104 }
105
106 private void ScanKeyboard(double totalTime)
107 {
108 Keyboard = Microsoft.Xna.Framework.Input.Keyboard.GetState();
109 HeldKeys = Keyboard.GetPressedKeys();
110 SomeKeyHeld = HeldKeys.Length > 0;
111
112 PressedKeys = HeldKeys.Except(PrevHeldKeys).ToArray();
113 UnpressedKeys = PrevHeldKeys.Except(HeldKeys).ToArray();
114
115 SomeKeyPressed = PressedKeys.Length > 0;
116 SomeKeyUnpressed = UnpressedKeys.Length > 0;
117
118 PrevHeldKeys = HeldKeys;
119
120 WindowTextInputEvents = WindowTextInputQueue.ToArray();
121 WindowTextInputQueue.Clear();
122
123 WindowKeyDownEvents = WindowKeyDownQueue.ToArray();
124 WindowKeyDownQueue.Clear();
125
126
127 SomeWindowEvents = WindowTextInputEvents.Length > 0 || WindowKeyDownEvents.Length > 0;
128 }
129
130 public CUIInput()
131 {
132 CUI.OnWindowKeyDown += (e) => WindowKeyDownQueue.Enqueue(e);
133 CUI.OnWindowTextInput += (e) => WindowTextInputQueue.Enqueue(e);
134 }
135
136 }
137}
Containing a snapshot of current mouse and keyboard state.
Definition CUIInput.cs:17