joystick update ready to use
This commit is contained in:
parent
f3b1d4ceb5
commit
d949a58a8a
@ -59,16 +59,11 @@ void Input::updateEvents(){
|
|||||||
m_last_mouse_position = sf::Vector2i(m_window->getSize())/2;
|
m_last_mouse_position = sf::Vector2i(m_window->getSize())/2;
|
||||||
break;
|
break;
|
||||||
case sf::Event::LostFocus:
|
case sf::Event::LostFocus:
|
||||||
if(m_mouseGrabbed){
|
m_mouseWasGrabbed = m_mouseGrabbed;
|
||||||
m_mouseWasGrabbed = true;
|
m_mouseGrabbed = false;
|
||||||
m_mouseGrabbed = false;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case sf::Event::GainedFocus:
|
case sf::Event::GainedFocus:
|
||||||
if(m_mouseWasGrabbed){
|
m_mouseGrabbed = m_mouseWasGrabbed;
|
||||||
m_mouseGrabbed = true;
|
|
||||||
m_mouseWasGrabbed = false;
|
|
||||||
}
|
|
||||||
//Nothing to add here because the window already keep the state of the focus
|
//Nothing to add here because the window already keep the state of the focus
|
||||||
break;
|
break;
|
||||||
case sf::Event::TextEntered:
|
case sf::Event::TextEntered:
|
||||||
@ -114,7 +109,8 @@ void Input::updateEvents(){
|
|||||||
sf::Mouse::setPosition(m_last_mouse_position,*m_window);
|
sf::Mouse::setPosition(m_last_mouse_position,*m_window);
|
||||||
break;
|
break;
|
||||||
case sf::Event::JoystickButtonPressed:
|
case sf::Event::JoystickButtonPressed:
|
||||||
action = kb.getPressedAction(input::CONTROLLER,event.joystickButton.button); // + idjoy * joystick::buttoncount
|
std::cout << event.joystickButton.button << std::endl;
|
||||||
|
action = kb.getPressedAction(input::CONTROLLER,event.joystickButton.button);
|
||||||
if(!action.isNull()){
|
if(!action.isNull()){
|
||||||
action.controller_id = event.joystickButton.joystickId;
|
action.controller_id = event.joystickButton.joystickId;
|
||||||
m_actions.push_back(action);
|
m_actions.push_back(action);
|
||||||
@ -122,7 +118,7 @@ void Input::updateEvents(){
|
|||||||
m_heldJoystickButtons[action.controller_id].push_back(event.joystickButton.button);
|
m_heldJoystickButtons[action.controller_id].push_back(event.joystickButton.button);
|
||||||
break;
|
break;
|
||||||
case sf::Event::JoystickButtonReleased:
|
case sf::Event::JoystickButtonReleased:
|
||||||
action = kb.getReleasedAction(input::CONTROLLER, event.joystickButton.button); // + idjoy * joystick::buttoncount
|
action = kb.getReleasedAction(input::CONTROLLER, event.joystickButton.button);
|
||||||
if(!action.isNull()){
|
if(!action.isNull()){
|
||||||
action.controller_id = event.joystickButton.joystickId;
|
action.controller_id = event.joystickButton.joystickId;
|
||||||
m_actions.push_back(action);
|
m_actions.push_back(action);
|
||||||
@ -130,34 +126,14 @@ void Input::updateEvents(){
|
|||||||
releaseHeldJoystickButton(event.joystickButton.button,action.controller_id);
|
releaseHeldJoystickButton(event.joystickButton.button,action.controller_id);
|
||||||
break;
|
break;
|
||||||
case sf::Event::JoystickMoved:
|
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;
|
break;
|
||||||
case sf::Event::JoystickConnected:
|
case sf::Event::JoystickConnected:
|
||||||
{
|
|
||||||
m_heldJoystickButtons[event.joystickConnect.joystickId] = std::vector<int>();
|
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;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
case sf::Event::JoystickDisconnected:
|
case sf::Event::JoystickDisconnected:
|
||||||
{
|
|
||||||
// sf::Joystick::Identification id = sf::Joystick::getIdentification(event.joystickConnect.joystickId);
|
|
||||||
m_heldJoystickButtons.erase(event.joystickConnect.joystickId);
|
m_heldJoystickButtons.erase(event.joystickConnect.joystickId);
|
||||||
// std::cout << "Joystick "<< event.joystickConnect.joystickId << " disconnected :" << id.name.toAnsiString() << std::endl;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if(m_mouseGrabbed)
|
if(m_mouseGrabbed)
|
||||||
sf::Mouse::setPosition(m_last_mouse_position, *m_window);
|
sf::Mouse::setPosition(m_last_mouse_position, *m_window);
|
||||||
@ -254,16 +230,19 @@ void Input::setMouseGrabbed(bool isGrabbed)
|
|||||||
|
|
||||||
std::vector<int> Input::getControllersConnected() const{
|
std::vector<int> Input::getControllersConnected() const{
|
||||||
std::vector<int> v;
|
std::vector<int> v;
|
||||||
|
std::cout << "pliplop " << m_heldJoystickButtons.size();
|
||||||
for(auto &controller : m_heldJoystickButtons)
|
for(auto &controller : m_heldJoystickButtons)
|
||||||
v.push_back(controller.first);
|
v.push_back(controller.first);
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Input::getAxisPosition(int device_id, int axis_id)
|
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 sf::Joystick::getAxisPosition(device_id,axis_id);
|
||||||
return m_joystick_position[device_id][axis_id];
|
// if(m_joystick_position.count(device_id) && m_joystick_position[device_id].count(axis_id))
|
||||||
else
|
// return m_joystick_position[device_id][axis_id];
|
||||||
return 0;
|
// else
|
||||||
|
// return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring Input::getText()
|
std::wstring Input::getText()
|
||||||
|
265
src/input.cpp.autosave
Normal file
265
src/input.cpp.autosave
Normal file
@ -0,0 +1,265 @@
|
|||||||
|
/**
|
||||||
|
* @author: Thomas Brandého
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <SFML/Window.hpp>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
Input::Input(sf::Window *w) :
|
||||||
|
m_window(w),
|
||||||
|
m_closeRequested(false),
|
||||||
|
m_mouseGrabbed(false)
|
||||||
|
{
|
||||||
|
m_heldKeys = std::vector<int>();
|
||||||
|
m_heldMouseButtons = std::vector<int>();
|
||||||
|
m_actions = std::vector<Action>();
|
||||||
|
m_window->setKeyRepeatEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::setKeysMap(IKeysMap km){
|
||||||
|
m_keysmap = km;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::updateEvents(){
|
||||||
|
sf::Event event;
|
||||||
|
KeyBindings kb;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
/* reset variables */
|
||||||
|
m_closeRequested = false;
|
||||||
|
m_hasBeenResized = false;
|
||||||
|
m_delta_vertical_scroll = 0;
|
||||||
|
m_buffer.clear();
|
||||||
|
|
||||||
|
if(m_mouseGrabbed)
|
||||||
|
m_last_mouse_position = sf::Vector2i(m_window->getSize())/2;
|
||||||
|
else
|
||||||
|
m_last_mouse_position = m_mouse_position;
|
||||||
|
|
||||||
|
/* global affectation */
|
||||||
|
kb = m_keybindings[m_current_context];
|
||||||
|
m_actions.clear();
|
||||||
|
|
||||||
|
Action action;
|
||||||
|
|
||||||
|
/* event-parsing loop */
|
||||||
|
while(m_window->pollEvent(event))
|
||||||
|
{
|
||||||
|
switch(event.type){
|
||||||
|
case sf::Event::Closed:
|
||||||
|
m_closeRequested = true;
|
||||||
|
break;
|
||||||
|
case sf::Event::Resized:
|
||||||
|
m_hasBeenResized = true;
|
||||||
|
if(m_mouseGrabbed)
|
||||||
|
m_last_mouse_position = sf::Vector2i(m_window->getSize())/2;
|
||||||
|
break;
|
||||||
|
case sf::Event::LostFocus:
|
||||||
|
m_mouseWasGrabbed = m_mouseGrabbed;
|
||||||
|
m_mouseGrabbed = false;
|
||||||
|
break;
|
||||||
|
case sf::Event::GainedFocus:
|
||||||
|
m_mouseGrabbed = m_mouseWasGrabbed;
|
||||||
|
//Nothing to add here because the window already keep the state of the focus
|
||||||
|
break;
|
||||||
|
case sf::Event::TextEntered:
|
||||||
|
c = (char) event.text.unicode;
|
||||||
|
sf::Utf32::encodeAnsi(event.text.unicode,std::back_inserter(m_buffer));
|
||||||
|
break;
|
||||||
|
case sf::Event::KeyPressed:
|
||||||
|
action = kb.getPressedAction(input::KEYBOARD,event.key.code);
|
||||||
|
if(!action.isNull())
|
||||||
|
m_actions.push_back(action);
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
case sf::Event::MouseWheelScrolled:
|
||||||
|
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
|
||||||
|
m_delta_vertical_scroll = event.mouseWheelScroll.delta;
|
||||||
|
break;
|
||||||
|
case sf::Event::MouseButtonPressed:
|
||||||
|
action = kb.getPressedAction(input::MOUSE,event.mouseButton.button);
|
||||||
|
if(!action.isNull())
|
||||||
|
m_actions.push_back(action);
|
||||||
|
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);
|
||||||
|
releaseHeldMouseButton(event.mouseButton.button);
|
||||||
|
break;
|
||||||
|
case sf::Event::MouseMoved:
|
||||||
|
m_mouse_position = sf::Mouse::getPosition(*m_window);
|
||||||
|
break;
|
||||||
|
case sf::Event::MouseEntered:
|
||||||
|
// action MouseEntered
|
||||||
|
break;
|
||||||
|
case sf::Event::MouseLeft:
|
||||||
|
if(m_mouseGrabbed)
|
||||||
|
sf::Mouse::setPosition(m_last_mouse_position,*m_window);
|
||||||
|
break;
|
||||||
|
case sf::Event::JoystickButtonPressed:
|
||||||
|
std::cout << event.joystickButton.button << std::endl;
|
||||||
|
action = kb.getPressedAction(input::CONTROLLER,event.joystickButton.button);
|
||||||
|
if(!action.isNull()){
|
||||||
|
action.controller_id = event.joystickButton.joystickId;
|
||||||
|
m_actions.push_back(action);
|
||||||
|
}
|
||||||
|
m_heldJoystickButtons[action.controller_id].push_back(event.joystickButton.button);
|
||||||
|
break;
|
||||||
|
case sf::Event::JoystickButtonReleased:
|
||||||
|
action = kb.getReleasedAction(input::CONTROLLER, event.joystickButton.button);
|
||||||
|
if(!action.isNull()){
|
||||||
|
action.controller_id = event.joystickButton.joystickId;
|
||||||
|
m_actions.push_back(action);
|
||||||
|
}
|
||||||
|
releaseHeldJoystickButton(event.joystickButton.button,action.controller_id);
|
||||||
|
break;
|
||||||
|
case sf::Event::JoystickMoved:
|
||||||
|
break;
|
||||||
|
case sf::Event::JoystickConnected:
|
||||||
|
m_heldJoystickButtons[event.joystickConnect.joystickId] = std::vector<int>();
|
||||||
|
break;
|
||||||
|
case sf::Event::JoystickDisconnected:
|
||||||
|
m_heldJoystickButtons.erase(event.joystickConnect.joystickId);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(m_mouseGrabbed)
|
||||||
|
sf::Mouse::setPosition(m_last_mouse_position, *m_window);
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
return m_actions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* context-related functions */
|
||||||
|
void Input::updateKeyBindings(){
|
||||||
|
m_keybindings.clear();
|
||||||
|
for (auto iter= m_contexts.begin(); iter != m_contexts.end(); ++iter)
|
||||||
|
m_keybindings[iter->getName()]= KeyBindings(*iter,m_keysmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* window-related function */
|
||||||
|
|
||||||
|
bool Input::isCloseRequested() const
|
||||||
|
{
|
||||||
|
return m_closeRequested;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Input::isResized() const
|
||||||
|
{
|
||||||
|
return m_hasBeenResized;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* keyboard-related functions */
|
||||||
|
|
||||||
|
bool Input::isKeyPressed(int key) const
|
||||||
|
{
|
||||||
|
return sf::Keyboard::isKeyPressed((sf::Keyboard::Key) key);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec2 Input::getDeltaPosition() const
|
||||||
|
{
|
||||||
|
sf::Vector2i delta = m_mouse_position - m_last_mouse_position;
|
||||||
|
return glm::vec2(delta.x,delta.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
float Input::getDeltaVerticalScroll() const
|
||||||
|
{
|
||||||
|
return m_delta_vertical_scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Input::setMouseGrabbed(bool isGrabbed)
|
||||||
|
{
|
||||||
|
m_mouseGrabbed = isGrabbed;
|
||||||
|
if(isGrabbed)
|
||||||
|
m_last_mouse_position = sf::Vector2i(m_window->getSize())/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> Input::getControllersConnected() const{
|
||||||
|
std::vector<int> v;
|
||||||
|
std::cout << "pliplop " << m_heldJoystickButtons.size();
|
||||||
|
for(auto &controller : m_heldJoystickButtons)
|
||||||
|
v.push_back(controller.first);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
float Input::getAxisPosition(int device_id, int axis_id)
|
||||||
|
{
|
||||||
|
return sf::Joystick::getAxisPosition(device_id,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()
|
||||||
|
{
|
||||||
|
return m_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------ */
|
||||||
|
|
||||||
|
void Input::test()
|
||||||
|
{
|
||||||
|
/* KeyBindings kb = m_keybindings[m_current_context];
|
||||||
|
int action;
|
||||||
|
action = kb.getPressedAction(sf::Keyboard::I);
|
||||||
|
if (action != NO_ACTION)
|
||||||
|
std::cerr << action << std::endl;
|
||||||
|
action = kb.getPressedAction(sf::Keyboard::O);
|
||||||
|
if (action != NO_ACTION)
|
||||||
|
std::cerr << action << std::endl;*/
|
||||||
|
}
|
@ -41,8 +41,12 @@ int main()
|
|||||||
myInput.updateEvents();
|
myInput.updateEvents();
|
||||||
for (Action action : myInput.getActions())
|
for (Action action : myInput.getActions())
|
||||||
{
|
{
|
||||||
if (action.action == TestKeysMap::ACTION)
|
if (action.action == TestKeysMap::ACTION && action.source == input::CONTROLLER && action.controller_id == 0){
|
||||||
std::cout << "Plop" << action.source << std::endl;
|
std::cout << "Controllers connected: " << myInput.getControllersConnected().size();
|
||||||
|
// for(auto controller : myInput.getControllersConnected())
|
||||||
|
// std::cout << controller << " ";
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* IKeysMap* keymap = new IKeysMap("test", 1);
|
/* IKeysMap* keymap = new IKeysMap("test", 1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user