added getter for joystick position
This commit is contained in:
parent
afecb9596b
commit
f3b1d4ceb5
106
src/input.cpp
106
src/input.cpp
@ -14,7 +14,8 @@ Input::Input(sf::Window *w) :
|
||||
m_closeRequested(false),
|
||||
m_mouseGrabbed(false)
|
||||
{
|
||||
m_heldkeys = std::vector<sf::Keyboard::Key>();
|
||||
m_heldKeys = std::vector<int>();
|
||||
m_heldMouseButtons = std::vector<int>();
|
||||
m_actions = std::vector<Action>();
|
||||
m_window->setKeyRepeatEnabled(false);
|
||||
}
|
||||
@ -44,16 +45,6 @@ void Input::updateEvents(){
|
||||
m_actions.clear();
|
||||
|
||||
Action action;
|
||||
/*
|
||||
sf::Joystick::X;
|
||||
sf::Joystick::Y;
|
||||
sf::Joystick::Z;
|
||||
sf::Joystick::R;
|
||||
sf::Joystick::U;
|
||||
sf::Joystick::V;
|
||||
sf::Joystick::PovX;
|
||||
sf::Joystick::PovY;
|
||||
*/
|
||||
|
||||
/* event-parsing loop */
|
||||
while(m_window->pollEvent(event))
|
||||
@ -68,7 +59,16 @@ void Input::updateEvents(){
|
||||
m_last_mouse_position = sf::Vector2i(m_window->getSize())/2;
|
||||
break;
|
||||
case sf::Event::LostFocus:
|
||||
if(m_mouseGrabbed){
|
||||
m_mouseWasGrabbed = true;
|
||||
m_mouseGrabbed = false;
|
||||
}
|
||||
break;
|
||||
case sf::Event::GainedFocus:
|
||||
if(m_mouseWasGrabbed){
|
||||
m_mouseGrabbed = true;
|
||||
m_mouseWasGrabbed = false;
|
||||
}
|
||||
//Nothing to add here because the window already keep the state of the focus
|
||||
break;
|
||||
case sf::Event::TextEntered:
|
||||
@ -79,13 +79,13 @@ void Input::updateEvents(){
|
||||
action = kb.getPressedAction(input::KEYBOARD,event.key.code);
|
||||
if(!action.isNull())
|
||||
m_actions.push_back(action);
|
||||
// m_heldkeys.push_back(event.key.code);
|
||||
m_heldKeys.push_back(event.key.code);
|
||||
break;
|
||||
case sf::Event::KeyReleased:
|
||||
action = kb.getReleasedAction(input::KEYBOARD,event.key.code);
|
||||
if(!action.isNull())
|
||||
m_actions.push_back(action);
|
||||
// releaseHeldKeys(event.key.code);
|
||||
releaseHeldKeys(event.key.code);
|
||||
break;
|
||||
case sf::Event::MouseWheelScrolled:
|
||||
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
|
||||
@ -95,13 +95,13 @@ void Input::updateEvents(){
|
||||
action = kb.getPressedAction(input::MOUSE,event.mouseButton.button);
|
||||
if(!action.isNull())
|
||||
m_actions.push_back(action);
|
||||
// m_heldkeys.push_back((sf::Keyboard::Key) (event.mouseButton.button));
|
||||
m_heldMouseButtons.push_back(event.mouseButton.button);
|
||||
break;
|
||||
case sf::Event::MouseButtonReleased:
|
||||
action = kb.getReleasedAction(input::MOUSE,event.mouseButton.button);
|
||||
if(!action.isNull())
|
||||
m_actions.push_back(action);
|
||||
// releaseHeldKeys((sf::Keyboard::Key) (event.mouseButton.button));
|
||||
releaseHeldMouseButton(event.mouseButton.button);
|
||||
break;
|
||||
case sf::Event::MouseMoved:
|
||||
m_mouse_position = sf::Mouse::getPosition(*m_window);
|
||||
@ -110,7 +110,8 @@ void Input::updateEvents(){
|
||||
// action MouseEntered
|
||||
break;
|
||||
case sf::Event::MouseLeft:
|
||||
//action MouseLeft
|
||||
if(m_mouseGrabbed)
|
||||
sf::Mouse::setPosition(m_last_mouse_position,*m_window);
|
||||
break;
|
||||
case sf::Event::JoystickButtonPressed:
|
||||
action = kb.getPressedAction(input::CONTROLLER,event.joystickButton.button); // + idjoy * joystick::buttoncount
|
||||
@ -118,7 +119,7 @@ void Input::updateEvents(){
|
||||
action.controller_id = event.joystickButton.joystickId;
|
||||
m_actions.push_back(action);
|
||||
}
|
||||
m_heldkeys.push_back((sf::Keyboard::Key) (event.joystickButton.button));
|
||||
m_heldJoystickButtons[action.controller_id].push_back(event.joystickButton.button);
|
||||
break;
|
||||
case sf::Event::JoystickButtonReleased:
|
||||
action = kb.getReleasedAction(input::CONTROLLER, event.joystickButton.button); // + idjoy * joystick::buttoncount
|
||||
@ -126,23 +127,34 @@ void Input::updateEvents(){
|
||||
action.controller_id = event.joystickButton.joystickId;
|
||||
m_actions.push_back(action);
|
||||
}
|
||||
releaseHeldKeys((sf::Keyboard::Key) (event.joystickButton.button));
|
||||
releaseHeldJoystickButton(event.joystickButton.button,action.controller_id);
|
||||
break;
|
||||
case sf::Event::JoystickMoved:
|
||||
switch(event.joystickMove.axis){
|
||||
case 6:
|
||||
case 7:
|
||||
event.joystickMove.position;
|
||||
break;
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
// i'll handle that later
|
||||
std::cout << "Joystick axis " << event.joystickMove.axis << " of controller " << event.joystickMove.joystickId << " moved at" << event.joystickMove.position << std::endl;
|
||||
break;
|
||||
case sf::Event::JoystickConnected:
|
||||
{
|
||||
sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId);
|
||||
std::cout << "Joystick "<< event.joystickConnect.joystickId << " connected :" << id.name.toAnsiString() << std::endl;
|
||||
m_heldJoystickButtons[event.joystickConnect.joystickId] = std::vector<int>();
|
||||
// sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId);
|
||||
// std::cout << "Joystick "<< event.joystickConnect.joystickId << " connected :" << id.name.toAnsiString() << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
case sf::Event::JoystickDisconnected:
|
||||
{
|
||||
sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId);
|
||||
std::cout << "Joystick "<< event.joystickConnect.joystickId << " disconnected :" << id.name.toAnsiString() << std::endl;
|
||||
// sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId);
|
||||
m_heldJoystickButtons.erase(event.joystickConnect.joystickId);
|
||||
// std::cout << "Joystick "<< event.joystickConnect.joystickId << " disconnected :" << id.name.toAnsiString() << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -150,10 +162,21 @@ void Input::updateEvents(){
|
||||
if(m_mouseGrabbed)
|
||||
sf::Mouse::setPosition(m_last_mouse_position, *m_window);
|
||||
|
||||
for (auto key: m_heldkeys)
|
||||
for (auto key: m_heldKeys)
|
||||
m_actions.push_back(kb.getHoldAction(input::KEYBOARD,key));
|
||||
|
||||
for (auto button: m_heldMouseButtons)
|
||||
m_actions.push_back(kb.getHoldAction(input::MOUSE,button));
|
||||
|
||||
for (auto heldJoystickButton : m_heldJoystickButtons)
|
||||
{
|
||||
// m_actions.push_back(kb.getHoldAction(key));
|
||||
}
|
||||
for (auto button: heldJoystickButton.second)
|
||||
{
|
||||
action = kb.getHoldAction(input::CONTROLLER,button);
|
||||
action.controller_id = heldJoystickButton.first;
|
||||
m_actions.push_back(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Action> Input::getActions()
|
||||
@ -187,13 +210,25 @@ bool Input::isKeyPressed(int key) const
|
||||
return sf::Keyboard::isKeyPressed((sf::Keyboard::Key) key);
|
||||
}
|
||||
|
||||
void Input::releaseHeldKeys(sf::Keyboard::Key keycode){
|
||||
for(auto iter = m_heldkeys.begin();iter < m_heldkeys.end();)
|
||||
iter = *iter == keycode ? m_heldkeys.erase(iter) : iter+1;
|
||||
void Input::releaseHeldKeys(int keycode)
|
||||
{
|
||||
for(auto iter = m_heldKeys.begin();iter < m_heldKeys.end();)
|
||||
iter = *iter == keycode ? m_heldKeys.erase(iter) : iter+1;
|
||||
}
|
||||
|
||||
void Input::releaseHeldMouseButton(int buttoncode)
|
||||
{
|
||||
for(auto iter = m_heldMouseButtons.begin();iter<m_heldMouseButtons.end();)
|
||||
iter = *iter == buttoncode ? m_heldMouseButtons.erase(iter) : iter+1;
|
||||
}
|
||||
|
||||
void Input::releaseHeldJoystickButton(int buttoncode,int controller_id)
|
||||
{
|
||||
for(auto iter = m_heldJoystickButtons[controller_id].begin();iter<m_heldJoystickButtons[controller_id].end();)
|
||||
iter = *iter == buttoncode ? m_heldJoystickButtons[controller_id].erase(iter) : iter+1;
|
||||
}
|
||||
|
||||
/* mouse-related functions */
|
||||
|
||||
glm::vec2 Input::getPosition() const
|
||||
{
|
||||
return glm::vec2(m_mouse_position.x,m_mouse_position.y);
|
||||
@ -217,9 +252,18 @@ void Input::setMouseGrabbed(bool isGrabbed)
|
||||
m_last_mouse_position = sf::Vector2i(m_window->getSize())/2;
|
||||
}
|
||||
|
||||
float getAxisPosition(int device_id, int axis_id)
|
||||
{
|
||||
std::vector<int> Input::getControllersConnected() const{
|
||||
std::vector<int> v;
|
||||
for(auto &controller : m_heldJoystickButtons)
|
||||
v.push_back(controller.first);
|
||||
}
|
||||
|
||||
float Input::getAxisPosition(int device_id, int axis_id)
|
||||
{
|
||||
if(m_joystick_position.count(device_id) && m_joystick_position[device_id].count(axis_id))
|
||||
return m_joystick_position[device_id][axis_id];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::wstring Input::getText()
|
||||
|
23
src/input.h
23
src/input.h
@ -7,6 +7,7 @@
|
||||
#include "keybindings.h"
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include "textbuffer.h"
|
||||
#include "glm/vec2.hpp"
|
||||
@ -28,15 +29,23 @@ private:
|
||||
std::vector<Context> m_contexts;
|
||||
std::unordered_map<std::string,KeyBindings> m_keybindings;
|
||||
|
||||
/* keyboard-related variables */
|
||||
std::vector<sf::Keyboard::Key> m_heldkeys;
|
||||
void releaseHeldKeys(sf::Keyboard::Key keycode);
|
||||
/* keyboard */
|
||||
std::vector<int> m_heldKeys;
|
||||
void releaseHeldKeys(int keycode);
|
||||
|
||||
/* mouse-related variables */
|
||||
/* mouse */
|
||||
sf::Vector2i m_mouse_position;
|
||||
sf::Vector2i m_last_mouse_position;
|
||||
float m_delta_vertical_scroll;
|
||||
bool m_mouseGrabbed;
|
||||
bool m_mouseWasGrabbed;
|
||||
std::vector<int> m_heldMouseButtons;
|
||||
void releaseHeldMouseButton(int buttoncode);
|
||||
|
||||
/* joystick */
|
||||
std::map<int,std::map<int,float>> m_joystick_position;
|
||||
std::map<int,std::vector<int>> m_heldJoystickButtons;
|
||||
void releaseHeldJoystickButton(int buttoncode, int controller_id);
|
||||
|
||||
/* text-related variable */
|
||||
std::wstring m_buffer;
|
||||
@ -63,9 +72,7 @@ public:
|
||||
bool isKeyPressed(int key) const;
|
||||
|
||||
/* mouse-related function */
|
||||
//sf::Vector2i getPosition() const;
|
||||
glm::vec2 getPosition() const;
|
||||
//sf::Vector2i getDeltaPosition() const;
|
||||
glm::vec2 getDeltaPosition() const;
|
||||
float getDeltaVerticalScroll() const;
|
||||
|
||||
@ -74,6 +81,10 @@ public:
|
||||
void toggleMouseGrab() { setMouseGrabbed(!m_mouseGrabbed); }
|
||||
bool isMouseGrabbed() const { return m_mouseGrabbed; }
|
||||
|
||||
/* joystick-related function */
|
||||
std::vector<int> getControllersConnected() const;
|
||||
float getAxisPosition(int device_id, int axis_id);
|
||||
|
||||
/* text-related function */
|
||||
std::wstring getText();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user