Minor joystick event handler finding refactor.

Remove the `evthandler` member and `Attach()` method from `wxSDLJoy`.

Add the `GetJoyEventHandler()` method to `MainFrame`, which returns the
window in focus, or the panel if the option for joystick background
input is enabled.

Remove the handler parameter in `wxSDLJoy::CreateAndSendEvent()` and
call `wxGetApp().frame->GetJoyEventHandler()` to get the handler.

Signed-off-by: Rafael Kitover <rkitover@gmail.com>
This commit is contained in:
Rafael Kitover 2020-08-29 20:45:55 +00:00
parent 891b1c35e4
commit 546c9d3a11
4 changed files with 34 additions and 33 deletions

View File

@ -3,7 +3,6 @@
#include "wx/sdljoy.h"
#include "SDL.h"
#include <SDL_events.h>
#include <wx/window.h>
#include "../common/range.hpp"
#include "../common/contains.h"
@ -16,7 +15,6 @@ DEFINE_EVENT_TYPE(wxEVT_SDLJOY)
wxSDLJoy::wxSDLJoy()
: wxTimer()
, evthandler(nullptr)
{
// Start up joystick if not already started
// FIXME: check for errors
@ -41,15 +39,12 @@ static int16_t axisval(int16_t x)
return 0;
}
void wxSDLJoy::CreateAndSendEvent(wxEvtHandler* handler, unsigned short joy, unsigned short ctrl_type, unsigned short ctrl_idx, short ctrl_val, short prev_val)
void wxSDLJoy::CreateAndSendEvent(unsigned short joy, unsigned short ctrl_type, unsigned short ctrl_idx, short ctrl_val, short prev_val)
{
if (!handler) {
GameArea *panel = wxGetApp().frame->GetPanel();
if (panel && allowJoystickBackgroundInput)
handler = panel->GetEventHandler();
else
return;
}
auto handler = wxGetApp().frame->GetJoyEventHandler();
if (!handler)
return;
wxSDLJoyEvent *ev = new wxSDLJoyEvent(wxEVT_SDLJOY);
ev->joy = joy;
@ -63,7 +58,6 @@ void wxSDLJoy::CreateAndSendEvent(wxEvtHandler* handler, unsigned short joy, uns
void wxSDLJoy::Poll()
{
wxEvtHandler* handler = evthandler ? evthandler : wxWindow::FindFocus();
SDL_Event e;
bool got_event = false;
@ -84,7 +78,7 @@ void wxSDLJoy::Poll()
auto prev_val = joystate[joy].button[but];
if (val != prev_val) {
CreateAndSendEvent(handler, joy, WXSDLJOY_BUTTON, but, val, prev_val);
CreateAndSendEvent(joy, WXSDLJOY_BUTTON, but, val, prev_val);
joystate[joy].button[but] = val;
@ -109,7 +103,7 @@ void wxSDLJoy::Poll()
auto prev_val = joystate[joy].axis[axis];
if (val != prev_val) {
CreateAndSendEvent(handler, joy, WXSDLJOY_AXIS, axis, val, prev_val);
CreateAndSendEvent(joy, WXSDLJOY_AXIS, axis, val, prev_val);
joystate[joy].axis[axis] = val;
@ -171,7 +165,7 @@ void wxSDLJoy::Poll()
auto prev_val = joystate[joy].button[but];
if (val != prev_val) {
CreateAndSendEvent(handler, joy, WXSDLJOY_BUTTON, but, val, prev_val);
CreateAndSendEvent(joy, WXSDLJOY_BUTTON, but, val, prev_val);
joystate[joy].button[but] = val;
@ -196,7 +190,7 @@ void wxSDLJoy::Poll()
auto prev_val = joystate[joy].axis[axis];
if (val != prev_val) {
CreateAndSendEvent(handler, joy, WXSDLJOY_AXIS, axis, val, prev_val);
CreateAndSendEvent(joy, WXSDLJOY_AXIS, axis, val, prev_val);
joystate[joy].axis[axis] = val;
@ -266,7 +260,7 @@ void wxSDLJoy::Poll()
auto state = SDL_GameControllerGetButton(joy.second.dev, static_cast<SDL_GameControllerButton>(but));
if (last_state != state) {
CreateAndSendEvent(handler, joy.first, WXSDLJOY_BUTTON, but, state, last_state);
CreateAndSendEvent(joy.first, WXSDLJOY_BUTTON, but, state, last_state);
joy.second.button[but] = state;
@ -279,7 +273,7 @@ void wxSDLJoy::Poll()
auto prev_val = joy.second.axis[axis];
if (val != prev_val) {
CreateAndSendEvent(handler, joy.first, WXSDLJOY_AXIS, axis, val, prev_val);
CreateAndSendEvent(joy.first, WXSDLJOY_AXIS, axis, val, prev_val);
joy.second.axis[axis] = val;
@ -293,7 +287,7 @@ void wxSDLJoy::Poll()
auto state = SDL_JoystickGetButton(joy.second.dev, but);
if (last_state != state) {
CreateAndSendEvent(handler, joy.first, WXSDLJOY_BUTTON, but, state, last_state);
CreateAndSendEvent(joy.first, WXSDLJOY_BUTTON, but, state, last_state);
joy.second.button[but] = state;
@ -306,7 +300,7 @@ void wxSDLJoy::Poll()
auto prev_val = joy.second.axis[axis];
if (val != prev_val) {
CreateAndSendEvent(handler, joy.first, WXSDLJOY_AXIS, axis, val, prev_val);
CreateAndSendEvent(joy.first, WXSDLJOY_AXIS, axis, val, prev_val);
joy.second.axis[axis] = val;
@ -350,13 +344,6 @@ void wxSDLJoy::DisconnectController(uint8_t joy)
}
}
wxEvtHandler* wxSDLJoy::Attach(wxEvtHandler* handler)
{
wxEvtHandler* prev = evthandler;
evthandler = handler;
return prev;
}
void wxSDLJoy::Add(int8_t joy_n)
{
if (joy_n < 0) {

View File

@ -4,8 +4,7 @@
// This is my own SDL-based joystick handler, since wxJoystick is brain-dead.
// It's geared towards keyboard emulation
// To use, create a wxSDLJoy object Add() the joysticks you want to monitor and
// Attach() the target window to receive events.
// To use, create a wxSDLJoy object Add() the joysticks you want to monitor.
//
// The target window will receive EVT_SDLJOY events of type wxSDLJoyEvent.
@ -47,9 +46,6 @@ struct wxSDLJoyState {
class wxSDLJoy : public wxTimer {
public:
wxSDLJoy();
// send events to this handler
// If NULL (default), send to window with keyboard focus
wxEvtHandler* Attach(wxEvtHandler*);
// add another joystick to the list of polled sticks
// -1 == add all
// If joy > # of joysticks, it is ignored
@ -75,13 +71,12 @@ protected:
void Notify();
void ConnectController(uint8_t joy);
void DisconnectController(uint8_t joy);
void CreateAndSendEvent(wxEvtHandler* handler, unsigned short joy, unsigned short ctrl_type, unsigned short ctrl_idx, short ctrl_val, short prev_val);
void CreateAndSendEvent(unsigned short joy, unsigned short ctrl_type, unsigned short ctrl_idx, short ctrl_val, short prev_val);
const uint8_t POLL_TIME_MS = 10;
private:
std::unordered_map<uint8_t, wxSDLJoyState> joystate;
wxEvtHandler* evthandler;
bool add_all = false, rumbling = false;
wxLongLong last_poll = wxGetUTCTimeMillis();

View File

@ -963,6 +963,23 @@ bool MainFrame::IsJoyPollTimerRunning()
return jpoll->IsRunning();
}
wxEvtHandler* MainFrame::GetJoyEventHandler()
{
auto focused_window = wxWindow::FindFocus();
if (focused_window)
return focused_window;
auto panel = GetPanel();
if (!panel)
return nullptr;
if (allowJoystickBackgroundInput)
return panel->GetEventHandler();
return nullptr;
}
void MainFrame::enable_menus()
{
for (int i = 0; i < ncmds; i++)

View File

@ -328,6 +328,8 @@ public:
void StopJoyPollTimer();
bool IsJoyPollTimerRunning();
wxEvtHandler* GetJoyEventHandler();
// required for building from xrc
DECLARE_DYNAMIC_CLASS(MainFrame);
// required for event handling