Files
XCEngine/MVS/Music fluctuations/source/myinput/input.cpp

75 lines
1.8 KiB
C++

#include"Input.h"
#include<iostream>
Input::Input()
{
keyDownID = KeyDown.AddListener(std::bind(&Input::OnKeyDown, this, std::placeholders::_1));
keyUpID = KeyUp.AddListener(std::bind(&Input::OnKeyUp, this, std::placeholders::_1));
mouseButtonDownID = MouseButtonDown.AddListener(std::bind(&Input::OnMouseButtonDown, this, std::placeholders::_1));
mouseButtonUpID = MouseButtonUp.AddListener(std::bind(&Input::OnMouseButtonUp, this, std::placeholders::_1));
mouseMovingID = MouseMoving.AddListener(std::bind(&Input::OnMouseMove, this, std::placeholders::_1, std::placeholders::_2));
}
Input::~Input()
{
KeyDown.RemoveListener(keyDownID);
KeyUp.RemoveListener(keyUpID);
MouseButtonDown.RemoveListener(mouseButtonDownID);
MouseButtonUp.RemoveListener(mouseButtonUpID);
}
void Input::HandelInput(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYUP:
KeyUp.Invoke(wParam);
break;
case WM_KEYDOWN:
KeyDown.Invoke(wParam);
break;
}
}
void Input::HandelMouseInput(int p_x, int p_y)
{
MouseMove.Invoke(p_x - m_mousePosition.first, p_y - m_mousePosition.second);
MouseMoving.Invoke(p_x, p_y);
}
void Input::OnKeyDown(int p_key)
{
downKeys.push_back((dKey)p_key);
if (p_key == (int)dKey::KEY_SPACE) { ClearEvents(); }
}
void Input::OnKeyUp(int p_key)
{
downKeys.erase(std::remove(downKeys.begin(), downKeys.end(), (dKey)p_key), downKeys.end());
}
void Input::OnMouseButtonDown(int p_mouseButton)
{
//downKeys.push_back((dKey)p_mouseButton);
}
void Input::OnMouseButtonUp(int p_mouseButton)
{
}
void Input::OnMouseMove(int p_x, int p_y)
{
m_mousePosition = std::make_pair(p_x, p_y);
}
bool Input::IsKeyDown(dKey p_key)
{
return std::find(downKeys.begin(), downKeys.end(), p_key) != downKeys.end();
}
void Input::ClearEvents()
{
downKeys.clear();
}