Advertisement
szymski

SFML Input Manager

Jul 23rd, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5.  
  6. namespace Piss_of_Motch
  7. {
  8.     public class Input
  9.     {
  10.         enum KeyState { None, Down, Up }
  11.  
  12.         Dictionary<SFML.Window.Keyboard.Key, KeyState> keys = new Dictionary<SFML.Window.Keyboard.Key, KeyState>();
  13.         Dictionary<int, KeyState> mouseButtons = new Dictionary<int, KeyState>();
  14.  
  15.         public void KeyPressed(object sender, EventArgs e)
  16.         {
  17.             SetKey(((SFML.Window.KeyEventArgs)e).Code, KeyState.Down);
  18.         }
  19.  
  20.         public void KeyReleased(object sender, EventArgs e)
  21.         {
  22.             SetKey(((SFML.Window.KeyEventArgs)e).Code, KeyState.Up);
  23.         }
  24.  
  25.         public void MouseButtonPressed(object sender, EventArgs e)
  26.         {
  27.             SetMouseButton((int)((SFML.Window.MouseButtonEventArgs)e).Button, KeyState.Down);
  28.         }
  29.  
  30.         public void MouseButtonReleased(object sender, EventArgs e)
  31.         {
  32.             SetMouseButton((int)((SFML.Window.MouseButtonEventArgs)e).Button, KeyState.Up);
  33.         }
  34.  
  35.         void SetKey(SFML.Window.Keyboard.Key key, KeyState state)
  36.         {
  37.             if (keys.ContainsKey(key)) keys[key] = state;
  38.             else keys.Add(key, state);
  39.         }
  40.  
  41.         void SetMouseButton(int key, KeyState state)
  42.         {
  43.             if (mouseButtons.ContainsKey(key)) mouseButtons[key] = state;
  44.             else mouseButtons.Add(key, state);
  45.         }
  46.  
  47.         KeyState _GetKey(SFML.Window.Keyboard.Key key)
  48.         {
  49.             if (keys.ContainsKey(key)) return keys[key];
  50.             else return KeyState.None;
  51.         }
  52.  
  53.         KeyState _GetMouseButton(int button)
  54.         {
  55.             if (mouseButtons.ContainsKey(button)) return mouseButtons[button];
  56.             else return KeyState.None;
  57.         }
  58.  
  59.         public void Update()
  60.         {
  61.             keys.Clear();
  62.             mouseButtons.Clear();
  63.         }
  64.  
  65.         public static bool GetKeyDown(SFML.Window.Keyboard.Key key)
  66.         {
  67.             return (Game.input._GetKey(key) == KeyState.Down);
  68.         }
  69.  
  70.         public static bool GetKey(SFML.Window.Keyboard.Key key)
  71.         {
  72.             return SFML.Window.Keyboard.IsKeyPressed(key);
  73.         }
  74.  
  75.         public static bool GetKeyUp(SFML.Window.Keyboard.Key key)
  76.         {
  77.             return (Game.input._GetKey(key) == KeyState.Up);
  78.         }
  79.  
  80.         public static bool GetMouseButtonDown(int button)
  81.         {
  82.             return (Game.input._GetMouseButton(button) == KeyState.Down);
  83.         }
  84.  
  85.         public static bool GetMouseButton(int button)
  86.         {
  87.             return SFML.Window.Mouse.IsButtonPressed((SFML.Window.Mouse.Button)Enum.ToObject(typeof(SFML.Window.Mouse.Button), button));
  88.         }
  89.  
  90.         public static bool GetMouseButtonUp(int button)
  91.         {
  92.             return (Game.input._GetMouseButton(button) == KeyState.Up);
  93.         }
  94.  
  95.         public static Vector2 mousePosition
  96.         {
  97.             get
  98.             {
  99.                 SFML.Window.Vector2i vec = SFML.Window.Mouse.GetPosition(Game.graphics.window);
  100.                 return new Vector2(vec.X, vec.Y);
  101.             }
  102.         }
  103.  
  104.         public static Vector2 worldMousePosition
  105.         {
  106.             get
  107.             {
  108.                 SFML.Window.Vector2f vec = Game.graphics.window.MapPixelToCoords(SFML.Window.Mouse.GetPosition(Game.graphics.window));
  109.                 return new Vector2(vec.X, vec.Y);
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement