diff --git a/Source/Core/Core/Src/EventHandler.cpp b/Source/Core/Core/Src/EventHandler.cpp index c93e16d2ad..a6a6bb76da 100644 --- a/Source/Core/Core/Src/EventHandler.cpp +++ b/Source/Core/Core/Src/EventHandler.cpp @@ -15,6 +15,20 @@ bool EventHandler::RegisterEventListener(listenFuncPtr func, Keys key) { return true; } +bool EventHandler::RemoveEventListener(Keys key) { + if (key.inputType == KeyboardInput) { + if (! keys[key.keyCode][key.mods]) + return false; + keys[key.keyCode][key.mods] = NULL; + } else if (key.inputType == MouseInput) { + if (! mouse[key.mouseButton]) + return false; + mouse[key.mouseButton] = NULL; + } + + return true; +} + void EventHandler::Update() { for (unsigned int i = 0; i < eventQueue.size();i++) { sf::Event ev = eventQueue.front(); @@ -45,9 +59,9 @@ bool EventHandler::TestEvent (Keys k, sf::Event e) } // Taken from wxw source code -int EventHandler::wxCharCodeWXToSF(int id) +sf::Key::Code EventHandler::wxCharCodeToSF(int id) { - int sfKey; + sf::Key::Code sfKey; switch (id) { // case WXK_CANCEL: sfKey = sf::Key::Cancel; break; @@ -121,13 +135,18 @@ int EventHandler::wxCharCodeWXToSF(int id) case WXK_F15: sfKey = sf::Key::F15; break; // case WXK_NUMLOCK: sfKey = sf::Key::Num_Lock; break; // case WXK_SCROLL: sfKey = sf::Key::Scroll_Lock; break; - default: sfKey = id <= 255 ? id : 0; + default: + if ((id >= 'a' && id <= 'z') || + (id >= '0' && id <= '9')) + sfKey = (sf::Key::Code)id; + else + sfKey = sf::Key::Count; // Invalid key } return sfKey; } -void EventHandler::SFKeyToString(unsigned int keycode, char *keyStr) { +void EventHandler::SFKeyToString(sf::Key::Code keycode, char *keyStr) { switch (keycode) { /* case sf::Key::A = 'a': sprintf(keyStr, "UP"); break; case sf::Key::B = 'b': sprintf(keyStr, "UP"); break; diff --git a/Source/Core/Core/Src/EventHandler.h b/Source/Core/Core/Src/EventHandler.h index 0141f46802..1de00ecb75 100644 --- a/Source/Core/Core/Src/EventHandler.h +++ b/Source/Core/Core/Src/EventHandler.h @@ -35,11 +35,14 @@ private: std::queue eventQueue; public: bool RegisterEventListener(listenFuncPtr func, Keys key); + bool RemoveEventListener(Keys key); void Update(); bool addEvent(sf::Event *e); static bool TestEvent (Keys k, sf::Event e); - static int wxCharCodeWXToSF(int id); - static void SFKeyToString(unsigned int keycode, char *keyStr); + static sf::Key::Code wxCharCodeToSF(int id); + static void SFKeyToString(sf::Key::Code keycode, char *keyStr); }; +extern EventHandler *eventHandler; + #endif diff --git a/Source/Plugins/Plugin_PadSimpleEvnt/Src/GUI/ConfigDlg.cpp b/Source/Plugins/Plugin_PadSimpleEvnt/Src/GUI/ConfigDlg.cpp index 8a95c371a8..7cb7c29d07 100644 --- a/Source/Plugins/Plugin_PadSimpleEvnt/Src/GUI/ConfigDlg.cpp +++ b/Source/Plugins/Plugin_PadSimpleEvnt/Src/GUI/ConfigDlg.cpp @@ -195,12 +195,13 @@ void ConfigDialog::OnKeyDown(wxKeyEvent& event) if(clickedButton != NULL) { int page = m_Notebook->GetSelection(); - int sfcode = EventHandler::wxCharCodeWXToSF(event.GetKeyCode()); + sf::Key::Code sfcode = EventHandler::wxCharCodeToSF(event.GetKeyCode()); char sfstr[100]; EventHandler::SFKeyToString(sfcode, sfstr); - pad[page].keyForControl[clickedButton->GetId()] = sfcode; - clickedButton->SetLabel(wxString::FromAscii(sfstr)); + // pad[page].keyForControl[clickedButton->GetId()] = sfcode; + if (registerKey(page, clickedButton->GetId(), sfcode)) + clickedButton->SetLabel(wxString::FromAscii(sfstr)); clickedButton->Disconnect(); } diff --git a/Source/Plugins/Plugin_PadSimpleEvnt/Src/PadSimple.cpp b/Source/Plugins/Plugin_PadSimpleEvnt/Src/PadSimple.cpp index 9a06244fdf..4e9199809f 100644 --- a/Source/Plugins/Plugin_PadSimpleEvnt/Src/PadSimple.cpp +++ b/Source/Plugins/Plugin_PadSimpleEvnt/Src/PadSimple.cpp @@ -83,6 +83,37 @@ const SPADStatus& PlayRecord() } +bool registerKey(int nPad, int id, sf::Key::Code code, int mods) { + + Keys key, oldKey; + + key.inputType = KeyboardInput; + key.keyCode = code; + key.mods = mods; + + if (!eventHandler->EventHandler::RegisterEventListener(ParseKeyEvent, key)) { + char codestr[100]; + EventHandler::SFKeyToString(code, codestr); + PanicAlert("Failed to register %s, might be already in use", codestr); + return false; + } + + + if (pad[nPad].keyForControl[id] != 0) { + + oldKey.inputType = KeyboardInput; + oldKey.keyCode = pad[nPad].keyForControl[id]; + oldKey.mods = mods; + + // Might be not be registered yet + eventHandler->EventHandler::RemoveEventListener(oldKey); + } + + pad[nPad].keyForControl[id] = code; + + return true; +} + void LoadRecord() { FILE* pStream = fopen("c:\\pad-record.bin", "rb"); @@ -195,39 +226,39 @@ void PAD_Shutdown() SaveConfig(); } -void ParseKeyEvent(sf::Event ev) -{ +bool ParseKeyEvent(sf::Event ev) { + return true; } void PAD_GetStatus(u8 _numPAD, SPADStatus* _pPADStatus) { - // Check if all is okay - if ((_pPADStatus == NULL)) - { - return; - } + // Check if all is okay + if ((_pPADStatus == NULL)) { + return; + } #ifdef RECORD_REPLAY - *_pPADStatus = PlayRecord(); - return; + *_pPADStatus = PlayRecord(); + return; #endif - - const int base = 0x80; - // Clear pad - memset(_pPADStatus, 0, sizeof(SPADStatus)); - - _pPADStatus->stickY = base; - _pPADStatus->stickX = base; - _pPADStatus->substickX = base; - _pPADStatus->substickY = base; - _pPADStatus->button |= PAD_USE_ORIGIN; - - _pPADStatus->err = PAD_ERR_NONE; - + + const int base = 0x80; + // Clear pad + memset(_pPADStatus, 0, sizeof(SPADStatus)); + + _pPADStatus->stickY = base; + _pPADStatus->stickX = base; + _pPADStatus->substickX = base; + _pPADStatus->substickY = base; + _pPADStatus->button |= PAD_USE_ORIGIN; + + _pPADStatus->err = PAD_ERR_NONE; + int stickvalue = (KeyStatus[CTL_HALFPRESS]) ? 40 : 100; int triggervalue = (KeyStatus[CTL_HALFPRESS]) ? 100 : 255; + int sensevalue = (KeyStatus[CTL_HALFPRESS]) ? 100 : 255; if (KeyStatus[CTL_MAINLEFT]){_pPADStatus->stickX -= stickvalue;} if (KeyStatus[CTL_MAINUP]){_pPADStatus->stickY += stickvalue;} @@ -246,12 +277,12 @@ void PAD_GetStatus(u8 _numPAD, SPADStatus* _pPADStatus) if (KeyStatus[CTL_A]) { _pPADStatus->button |= PAD_BUTTON_A; - _pPADStatus->analogA = 255; + _pPADStatus->analogA = sensevalue; } if (KeyStatus[CTL_B]) { _pPADStatus->button |= PAD_BUTTON_B; - _pPADStatus->analogB = 255; + _pPADStatus->analogB = sensevalue; } if (KeyStatus[CTL_X]){_pPADStatus->button |= PAD_BUTTON_X;} @@ -327,7 +358,7 @@ void LoadConfig() sf::Key::F, sf::Key::H, sf::Key::LShift, //halfpress - sf::Key::P + sf::Key::P // mic }; IniFile file; @@ -340,9 +371,12 @@ void LoadConfig() file.Get(SectionName, "Attached", &pad[i].bAttached, i==0); file.Get(SectionName, "DisableOnBackground", &pad[i].bDisable, false); for (int x = 0; x < NUMCONTROLS; x++) { - file.Get(SectionName, controlNames[x], - &pad[i].keyForControl[x], - (i==0)?defaultKeyForControl[x]:0); + int key; + file.Get(SectionName, controlNames[x], + &key, (i==0)?defaultKeyForControl[x]:0); + + pad[i].keyForControl[x] = (sf::Key::Code)key; + } } } diff --git a/Source/Plugins/Plugin_PadSimpleEvnt/Src/PadSimple.h b/Source/Plugins/Plugin_PadSimpleEvnt/Src/PadSimple.h index 8afa399eee..f3cf6fb62c 100644 --- a/Source/Plugins/Plugin_PadSimpleEvnt/Src/PadSimple.h +++ b/Source/Plugins/Plugin_PadSimpleEvnt/Src/PadSimple.h @@ -52,12 +52,14 @@ enum struct SPads { bool bAttached; // Pad is "attached" to the gamecube/wii bool bDisable; // Disabled when dolphin isn't in focus - unsigned int keyForControl[NUMCONTROLS];// Keyboard mapping + sf::Key::Code keyForControl[NUMCONTROLS];// Keyboard mapping }; extern SPads pad[]; void LoadConfig(); void SaveConfig(); +bool registerKey(int nPad, int id, sf::Key::Code code, int mods = 0); +bool ParseKeyEvent(sf::Event ev); #endif