input doesn't produce action when imgui has focus (may cause crash; will be fixed soon)

This commit is contained in:
Lendemor 2018-04-10 00:26:33 +02:00
parent ace1eacb24
commit 7ca3fc15ea
5 changed files with 170 additions and 46 deletions

View File

@ -12,6 +12,7 @@ set(EXEC_SRC_LIST src/main.cpp)
#set compilation option #set compilation option
set(IS_LIBRARY True) set(IS_LIBRARY True)
set(USE_IMGUI True) set(USE_IMGUI True)
set(USE_CEREAL True)
set(SFML_MODULES window system) set(SFML_MODULES window system)
set(CMAKE_TEMPLATE_PATH "../CMakeTemplate") set(CMAKE_TEMPLATE_PATH "../CMakeTemplate")

View File

@ -72,6 +72,11 @@ void Input::updateEvents(){
Action action; Action action;
bool focus_on_imgui = ImGui::IsWindowFocused(ImGuiFocusedFlags_AnyWindow);
if(focus_on_imgui)
std::cout << "focused" << std::endl;
/* event-parsing loop */ /* event-parsing loop */
while(m_window->pollEvent(event)) while(m_window->pollEvent(event))
{ {
@ -90,52 +95,78 @@ void Input::updateEvents(){
m_mouseGrabbed = m_mouseWasGrabbed; m_mouseGrabbed = m_mouseWasGrabbed;
break; break;
case sf::Event::TextEntered: case sf::Event::TextEntered:
c = (char) event.text.unicode; if (focus_on_imgui)
sf::Utf32::encodeAnsi(event.text.unicode,std::back_inserter(m_buffer)); io.AddInputCharacter(static_cast<ImWchar>(event.text.unicode));
io.AddInputCharacter(static_cast<ImWchar>(event.text.unicode)); else
sf::Utf32::encodeAnsi(event.text.unicode,std::back_inserter(m_buffer));
break; break;
case sf::Event::KeyPressed: case sf::Event::KeyPressed:
action = kb.getPressedAction(input::KEYBOARD,event.key.code); if(focus_on_imgui)
if(!action.isNull()) {
m_actions.push_back(action); io.KeysDown[event.key.code] = true;
m_heldKeys.push_back(event.key.code); io.KeyCtrl = event.key.control;
io.KeysDown[event.key.code] = true; io.KeyShift = event.key.shift;
io.KeyCtrl = event.key.control; io.KeyAlt = event.key.alt;
io.KeyShift = event.key.shift; }
io.KeyAlt = event.key.alt; else
{
action = kb.getPressedAction(input::KEYBOARD,event.key.code);
if(!action.isNull())
m_actions.push_back(action);
m_heldKeys.push_back(event.key.code);
}
break; break;
case sf::Event::KeyReleased: case sf::Event::KeyReleased:
action = kb.getReleasedAction(input::KEYBOARD,event.key.code); if (focus_on_imgui)
if(!action.isNull()) {
m_actions.push_back(action); io.KeysDown[event.key.code] = false;
releaseHeldKeys(event.key.code); io.KeyCtrl = event.key.control;
io.KeysDown[event.key.code] = false; io.KeyShift = event.key.shift;
io.KeyCtrl = event.key.control; io.KeyAlt = event.key.alt;
io.KeyShift = event.key.shift; }
io.KeyAlt = event.key.alt; else
{
action = kb.getReleasedAction(input::KEYBOARD,event.key.code);
if(!action.isNull())
m_actions.push_back(action);
releaseHeldKeys(event.key.code);
}
break; break;
case sf::Event::MouseWheelScrolled: case sf::Event::MouseWheelScrolled:
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel) if (!focus_on_imgui)
m_delta_vertical_scroll = event.mouseWheelScroll.delta; {
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
m_delta_vertical_scroll = event.mouseWheelScroll.delta;
}
break; break;
case sf::Event::MouseWheelMoved: case sf::Event::MouseWheelMoved:
io.MouseWheel += static_cast<float>(event.mouseWheel.delta); if (focus_on_imgui)
io.MouseWheel += static_cast<float>(event.mouseWheel.delta);
break; break;
case sf::Event::MouseButtonPressed: case sf::Event::MouseButtonPressed:
action = kb.getPressedAction(input::MOUSE,event.mouseButton.button); if (!focus_on_imgui)
if(!action.isNull()) {
m_actions.push_back(action); action = kb.getPressedAction(input::MOUSE,event.mouseButton.button);
m_heldMouseButtons.push_back(event.mouseButton.button); if(!action.isNull())
m_actions.push_back(action);
m_heldMouseButtons.push_back(event.mouseButton.button);
}
break; break;
case sf::Event::MouseButtonReleased: case sf::Event::MouseButtonReleased:
action = kb.getReleasedAction(input::MOUSE,event.mouseButton.button); if(!focus_on_imgui)
if(!action.isNull()) {
m_actions.push_back(action); action = kb.getReleasedAction(input::MOUSE,event.mouseButton.button);
releaseHeldMouseButton(event.mouseButton.button); if(!action.isNull())
m_actions.push_back(action);
releaseHeldMouseButton(event.mouseButton.button);
}
break; break;
case sf::Event::MouseMoved: case sf::Event::MouseMoved:
m_mouse_position = sf::Mouse::getPosition(*m_window); m_mouse_position = sf::Mouse::getPosition(*m_window);
io.MousePos = ImVec2(m_mouse_position.x, m_mouse_position.y); if (focus_on_imgui)
{
io.MousePos = ImVec2(m_mouse_position.x, m_mouse_position.y);
}
break; break;
case sf::Event::MouseEntered: case sf::Event::MouseEntered:
// action MouseEntered // action MouseEntered

View File

@ -5,6 +5,27 @@
#include "keybindings.h" #include "keybindings.h"
#include <iostream> #include <iostream>
//#define ACTION_PROPERTY_CODE AbstractProperty::NB_PROPERTY_TYPES + 1000
/* Function used for serialization of Action
*
*/
/*
template <> std::ostream& Property<Action>::saveBinary(std::ostream& os)
{
return os.write((char*)&m_value.action, sizeof(Action));
// return os.write((char*)&m_value.source, sizeof(int));
// return os.write((char*)&m_value.controller_id, sizeof(int));
}
template <> std::ostream& Property<Action>::saveAscii(std::ostream& os) { return os;}// << m_value; }
template <> std::istream& Property<Action>::loadBinary(std::istream& is) { return is.read((char*)&m_value, sizeof(Action)); }
template <> std::istream& Property<Action>::loadAscii(std::istream& is) { return is;}// >> m_value; }
template <> AbstractProperty::PropertyType Property<Action>::getStaticPropertyType() { return AbstractProperty::REFERENCE; }
*/
//INIT_SERIALIZABLE(Action)
//INIT_SERIALIZABLE(Binding)
/* Implementation of IKeysMap class /* Implementation of IKeysMap class
* @author: Thomas Brandého * @author: Thomas Brandého
* @info: This class register all the association between key and action. * @info: This class register all the association between key and action.
@ -22,10 +43,29 @@ std::vector<Binding> IKeysMap::getBindings(Action action) const {
return bindings; return bindings;
} }
Action Action::getNull(){ Action Action::getNull()
return {NO_ACTION,input::NONE}; {
Action a;
a.action = NO_ACTION;
a.source = input::NONE;
return a;
} }
Action& Action::operator=(const Action& newAction)
{
action = newAction.action;
source = newAction.source;
controller_id = newAction.controller_id;
}
Binding& Binding::operator=(const Binding newBinding)
{
action = newBinding.action;
key = newBinding.key;
type = newBinding.type;
}
/* Implementation of KeyBindings class /* Implementation of KeyBindings class
* @author: Thomas Brandého * @author: Thomas Brandého
* @info: This class map a list of action with the associated keys, for a quick access in-game. * @info: This class map a list of action with the associated keys, for a quick access in-game.

View File

@ -7,6 +7,9 @@
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include "SparrowSerializer/serializable.h"
#include <cereal/archives/json.hpp>
#include <unordered_map> #include <unordered_map>
#define NO_KEY -1 #define NO_KEY -1
@ -18,9 +21,10 @@ enum Controller{BUTTON_A,BUTTON_B,BUTTON_X,BUTTON_Y,BUTTON_LB,BUTTON_RB,BUTTON_S
enum Axis{LEFT_JOYSTICK_HORIZONTAL,LEFT_JOYSTICK_VERTICAL,LT_RT,RIGHT_JOYSTICK_VERTICAL,RIGHT_JOYSTICK_HORIZONTAL,DPAD_HORIZONTAL=6,DPAD_VERTICAL}; enum Axis{LEFT_JOYSTICK_HORIZONTAL,LEFT_JOYSTICK_VERTICAL,LT_RT,RIGHT_JOYSTICK_VERTICAL,RIGHT_JOYSTICK_HORIZONTAL,DPAD_HORIZONTAL=6,DPAD_VERTICAL};
} }
struct Action { struct Action
{
int action; int action;
input::Source source; int source;
int controller_id; int controller_id;
static Action getNull(); static Action getNull();
bool isNull(){ bool isNull(){
@ -29,22 +33,62 @@ struct Action {
friend bool operator ==(const Action& action1, const Action& action2){ friend bool operator ==(const Action& action1, const Action& action2){
return (action1.action == action2.action) && (action1.source == action2.source); return (action1.action == action2.action) && (action1.source == action2.source);
} }
Action& operator=(const Action& newAction);
Action(){}
Action(std::initializer_list<int> c){
assert(c.size() <= 3);
int array[3];
std::copy(c.begin(),c.end(),array);
switch (c.size()) {
case 3:
controller_id = array[2];
case 2:
source = array[1];
case 1:
action = array[0];
break;
default:
break;
}
}
template <class Archive>
void serialize(Archive & archive)
{
archive(action,source,controller_id);
}
template <class Archive>
void load(Archive & archive)
{
archive >> action >> source >> controller_id;
}
}; };
struct Binding{ struct Binding// : public Serializable
{
Action action; Action action;
int key; // PROPERTY(Action,action)
int type; int key;
// P_INT(key)
int type;
// P_INT(type)
Binding& operator=(const Binding newBinding);
//SERIALIZABLE(Binding,CAST(action),CAST(key),CAST(type))
}; };
class IKeysMap{ class IKeysMap// : public Serializable
{
public: public:
enum {PRESSED, RELEASED, HOLD}; enum {PRESSED, RELEASED, HOLD};
IKeysMap(); IKeysMap();
std::vector<Binding> getBindings(Action action) const; std::vector<Binding> getBindings(Action action) const;
std::vector<Binding>& data(){return keys;} std::vector<Binding>& data(){return keys;}
protected: protected:
std::vector<Binding> keys; std::vector<Binding> keys;
// P_VECTOR_(Binding, keys)
// SERIALIZABLE(IKeysMap,CAST(keys))
}; };
class Context { class Context {

View File

@ -10,7 +10,7 @@ public:
TestKeysMap(){ TestKeysMap(){
keys.push_back ({{LEFT_CLICK,input::MOUSE},sf::Mouse::Left,PRESSED} ); keys.push_back ({{LEFT_CLICK,input::MOUSE},sf::Mouse::Left,PRESSED} );
keys.push_back ({{RIGHT_CLICK,input::MOUSE},sf::Mouse::Right,PRESSED} ); keys.push_back ({{RIGHT_CLICK,input::MOUSE},sf::Mouse::Right,PRESSED} );
keys.push_back ({{ACTION,input::CONTROLLER},sf::Joystick::X,PRESSED} ); keys.push_back ({{ACTION,input::CONTROLLER},input::Controller::BUTTON_X,PRESSED} );
} }
static std::vector<Action> getTestContext() static std::vector<Action> getTestContext()
@ -36,19 +36,27 @@ int main()
myInput.addContext(context); myInput.addContext(context);
myInput.setCurrentContext("test"); myInput.setCurrentContext("test");
cereal::JSONOutputArchive output(std::cout);
while(!myInput.isCloseRequested()) while(!myInput.isCloseRequested())
{ {
myInput.updateEvents(); myInput.updateEvents();
for (Action action : myInput.getActions()) for (Action action : myInput.getActions())
{ {
if (action.action == TestKeysMap::ACTION && action.source == input::CONTROLLER && action.controller_id == 0){ // if (action.action == TestKeysMap::ACTION && action.source == input::CONTROLLER && action.controller_id == 0){
std::cout << "Controllers connected: " << myInput.getControllersConnected().size(); // std::cout << "Controllers connected: " << myInput.getControllersConnected().size();
// for(auto controller : myInput.getControllersConnected()) if(action.action != NO_ACTION){
// std::cout << controller << " "; output( cereal::make_nvp("data", action) );
std::cout << std::endl; std::cout << std::endl;
} }
// for(auto controller : myInput.getControllersConnected())
// std::cout << controller << " ";
//
// }
} }
} }
/* IKeysMap* keymap = new IKeysMap("test", 1); /* IKeysMap* keymap = new IKeysMap("test", 1);
// keymap->setKeyBinding(ACTION_2,0,sf::Keyboard::Z); // keymap->setKeyBinding(ACTION_2,0,sf::Keyboard::Z);