Advertisement
DSHFJGHLKG

Untitled

Dec 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include "InputSystem.h"
  2. #include <Windows.h>
  3.  
  4. InputSystem::InputSystem()
  5. {
  6. }
  7.  
  8. InputSystem::~InputSystem()
  9. {
  10. }
  11.  
  12. void InputSystem::update()
  13. {
  14.     POINT current_mouse_pos = {};
  15.     ::GetCursorPos(&current_mouse_pos);
  16.  
  17.     if (m_first_time)
  18.     {
  19.         m_old_mouse_pos = Point(current_mouse_pos.x, current_mouse_pos.y);
  20.         m_first_time = false;
  21.     }
  22.  
  23.     if (current_mouse_pos.x != m_old_mouse_pos.m_x || current_mouse_pos.y != m_old_mouse_pos.m_y)
  24.     {
  25.         //THERE IS MOUSE MOVE EVENT
  26.         std::unordered_set<InputListener*>::iterator it = m_set_listeners.begin();
  27.  
  28.         while (it != m_set_listeners.end())
  29.         {
  30.             (*it)->onMouseMove(Point(current_mouse_pos.x - m_old_mouse_pos.m_x, current_mouse_pos.y - m_old_mouse_pos.m_y));
  31.             ++it;
  32.         }
  33.     }
  34.     m_old_mouse_pos = Point(current_mouse_pos.x, current_mouse_pos.y);
  35.  
  36.     if (::GetKeyboardState(m_keys_state))
  37.     {
  38.         for (unsigned int i = 0; i < 256; i++)
  39.         {
  40.             // KEY IS DOWN
  41.             if (m_keys_state[i] & 0x80)
  42.             {
  43.                 std::unordered_set<InputListener*>::iterator it = m_set_listeners.begin();
  44.  
  45.                 while (it != m_set_listeners.end())
  46.                 {
  47.                     if (i == VK_LBUTTON)
  48.                     {
  49.                         if (m_keys_state[i] != m_old_keys_state[i])
  50.                             (*it)->onLeftMouseDown(Point(current_mouse_pos.x, current_mouse_pos.y));
  51.                     }
  52.                     else if (i == VK_RBUTTON)
  53.                     {
  54.                         if (m_keys_state[i] != m_old_keys_state[i])
  55.                             (*it)->onRightMouseDown(Point(current_mouse_pos.x, current_mouse_pos.y));
  56.                     }
  57.                     else
  58.                         (*it)->onKeyDown(i);
  59.  
  60.                     ++it;
  61.                 }
  62.             }
  63.             else // KEY IS UP
  64.             {
  65.                 if (m_keys_state[i] != m_old_keys_state[i])
  66.                 {
  67.                     std::unordered_set<InputListener*>::iterator it = m_set_listeners.begin();
  68.  
  69.                     while (it != m_set_listeners.end())
  70.                     {
  71.                         if (i == VK_LBUTTON)
  72.                             (*it)->onLeftMouseUp(Point(current_mouse_pos.x, current_mouse_pos.y));
  73.                         else if (i == VK_RBUTTON)
  74.                             (*it)->onRightMouseUp(Point(current_mouse_pos.x, current_mouse_pos.y));
  75.                         else
  76.                             (*it)->onKeyUp(i);
  77.  
  78.                         ++it;
  79.                     }
  80.                 }
  81.  
  82.             }
  83.  
  84.         }
  85.         // store current keys state to old keys state buffer
  86.         ::memcpy(m_old_keys_state, m_keys_state, sizeof(unsigned char) * 256);
  87.     }
  88. }
  89.  
  90. void InputSystem::addListener(InputListener * listener)
  91. {
  92.     m_set_listeners.insert(listener);
  93. }
  94.  
  95. void InputSystem::removeListener(InputListener * listener)
  96. {
  97.     m_set_listeners.erase(listener);
  98. }
  99.  
  100. InputSystem * InputSystem::get()
  101. {
  102.     static InputSystem system;
  103.     return &system;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement