Advertisement
Azeranth

InputEventDispatcher

Jan 8th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | None | 0 0
  1.         private Dictionary<Keys, KeyDownEventHandler> keyDownEvents = new Dictionary<Keys, KeyDownEventHandler>();
  2.         private Dictionary<Keys, KeyUpEventHandler> keyUpEvents = new Dictionary<Keys, KeyUpEventHandler>();
  3.         private Dictionary<Keys, KeyHeldDelegate> keyHeldDelegates = new Dictionary<Keys, KeyHeldDelegate>();
  4.         private Dictionary<InputAxes, AxisDelegate> axisDelegates = new Dictionary<InputAxes, AxisDelegate>();
  5.         private KeyboardState previousKeyboardState = new KeyboardState();
  6.         private KeyboardState currentKeyboardState = new KeyboardState();
  7.         private MouseState previousMouseState;
  8.         private MouseState currentMouseState;
  9.         private Vector2 previousMousePosition = Vector2.Zero;
  10.         private Vector2 currentMousePosition = Vector2.Zero;
  11.         private Vector2 deltaMousePosition = Vector2.Zero;
  12.  
  13.         private Dictionary<InputAxes, Axis> axes = new Dictionary<InputAxes, Axis>();
  14.         private Vector2 viewportDimensions = Vector2.Zero;
  15.         private bool siezeMouse = true;
  16.  
  17.         private Vector2 target = new Vector2(200,200);
  18.  
  19.         public Dictionary<InputAxes, Axis> Axes { get => axes; set => axes = value; }
  20.         public Vector2 ViewportDimensions { get => viewportDimensions; set => viewportDimensions = value; }
  21.         public bool SiezeMouse { get => siezeMouse; set => siezeMouse = value; }
  22.  
  23.         public void SubscribeKeyDownEvent(Keys key, KeyDownEventHandler keyDownEventHandler)
  24.         {
  25.             KeyDownEventHandler foo;
  26.             if (keyDownEvents.TryGetValue(key, out foo))
  27.             {
  28.                 keyDownEvents[key] += keyDownEventHandler;
  29.             }
  30.             else
  31.             {
  32.                 keyDownEvents.Add(key, keyDownEventHandler);
  33.             }
  34.         }
  35.         public void SubscribeKeyUpEvent(Keys key, KeyUpEventHandler keyUpEventHandler)
  36.         {
  37.             KeyUpEventHandler foo;
  38.             if (keyUpEvents.TryGetValue(key, out foo))
  39.             {
  40.                 keyUpEvents[key] += keyUpEventHandler;
  41.             }
  42.             else
  43.             {
  44.                 keyUpEvents.Add(key, keyUpEventHandler);
  45.             }
  46.         }
  47.         public void SubscribeKeyHeldDelegate(Keys key, KeyHeldDelegate keyHeldDelegate)
  48.         {
  49.             KeyHeldDelegate foo;
  50.             if (keyHeldDelegates.TryGetValue(key, out foo))
  51.             {
  52.                 keyHeldDelegates[key] += keyHeldDelegate;
  53.             }
  54.             else
  55.             {
  56.                 keyHeldDelegates.Add(key, keyHeldDelegate);
  57.             }
  58.         }
  59.         public void SubscribeAxisDelegegate(InputAxes axis, AxisDelegate axisDelegate)
  60.         {
  61.             AxisDelegate foo;
  62.             if (axisDelegates.TryGetValue(axis, out foo))
  63.             {
  64.                 axisDelegates[axis] += axisDelegate;
  65.             }
  66.             else
  67.             {
  68.                 axisDelegates.Add(axis, axisDelegate);
  69.             }
  70.         }
  71.  
  72.         public bool ToggleSiezeMouse()
  73.         {
  74.             SiezeMouse = !SiezeMouse;
  75.             return SiezeMouse;
  76.         }
  77.         public void Start()
  78.         {
  79.             Axes.Add(InputAxes.MoveForward, new Axis
  80.             {
  81.                 weights = new List<Func<float>>
  82.                 {
  83.                     delegate() { return currentKeyboardState.IsKeyDown(Keys.W) ? 1f : 0f; },
  84.                     delegate() { return currentKeyboardState.IsKeyDown(Keys.S) ? -1f : 0f; }
  85.                 }
  86.             });
  87.             Axes.Add(InputAxes.MoveRight, new Axis
  88.             {
  89.                 weights = new List<Func<float>>
  90.                 {
  91.                     delegate() { return currentKeyboardState.IsKeyDown(Keys.A) ? 1f : 0f; },
  92.                     delegate() { return currentKeyboardState.IsKeyDown(Keys.D) ? -1f : 0f; }
  93.                 }
  94.             });
  95.             Axes.Add(InputAxes.MoveUp, new Axis
  96.             {
  97.                 weights = new List<Func<float>>
  98.                 {
  99.                     delegate() { return currentKeyboardState.IsKeyDown(Keys.Space) ? 1f : 0f; },
  100.                     delegate() { return currentKeyboardState.IsKeyDown(Keys.LeftShift) ? -1f : 0f; }
  101.                 }
  102.             });
  103.             Axes.Add(InputAxes.MouseY, new Axis
  104.             {
  105.                 weights = new List<Func<float>>
  106.                 {
  107.                     delegate() { return deltaMousePosition.Y; }
  108.                 }
  109.             });
  110.             Axes.Add(InputAxes.MouseX, new Axis
  111.             {
  112.                 weights = new List<Func<float>>
  113.                 {
  114.                     delegate() { return deltaMousePosition.X; }
  115.                 }
  116.             });
  117.         }
  118.         public void Update(float deltaTime)
  119.         {
  120.             previousKeyboardState = currentKeyboardState;
  121.             currentKeyboardState = Keyboard.GetState();
  122.  
  123.             previousMouseState = currentMouseState;
  124.             currentMouseState = Mouse.GetState();
  125.  
  126.             previousMousePosition = previousMouseState.Position.ToVector2() / viewportDimensions;
  127.             currentMousePosition = currentMouseState.Position.ToVector2() / viewportDimensions;
  128.             deltaMousePosition = previousMousePosition - currentMousePosition;
  129.  
  130.             foreach (var entry in keyDownEvents)
  131.             {
  132.                 if (currentKeyboardState.IsKeyDown(entry.Key) && !previousKeyboardState.IsKeyDown(entry.Key))
  133.                 {
  134.                     entry.Value();
  135.                 }
  136.             }
  137.             foreach (var entry in keyUpEvents)
  138.             {
  139.                 if (!currentKeyboardState.IsKeyDown(entry.Key) && previousKeyboardState.IsKeyDown(entry.Key))
  140.                 {
  141.                     entry.Value();
  142.                 }
  143.             }
  144.             foreach (var entry in keyHeldDelegates)
  145.             {
  146.                 if (currentKeyboardState.IsKeyDown(entry.Key) && previousKeyboardState.IsKeyDown(entry.Key))
  147.                 {
  148.                     entry.Value(deltaTime);
  149.                 }
  150.             }
  151.             foreach (var entry in axisDelegates)
  152.             {
  153.                 if (Axes.ContainsKey(entry.Key))
  154.                     entry.Value(Axes[entry.Key].GetValue(), deltaTime);
  155.             }
  156.  
  157.             if(siezeMouse)
  158.             {
  159.                 Mouse.SetPosition((int)viewportDimensions.X / 2, (int)viewportDimensions.Y / 2);
  160.                 currentMouseState = Mouse.GetState();
  161.             }
  162.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement