Emulated Wiimote: Added customizable controls for the Wiimote and the Nunchuck

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2259 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
John Peterson
2009-02-15 18:23:42 +00:00
parent c49f969563
commit a8e35e976a
16 changed files with 525 additions and 204 deletions

View File

@ -1,189 +0,0 @@
// Copyright (C) 2003-2008 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "stdafx.h"
#include "DirectInputBase.h"
DInput::DInput()
: g_pDI(NULL),
g_pKeyboard(NULL)
{}
DInput::~DInput()
{
Free();
}
void DInput::DIKToString(unsigned int keycode, char *keyStr)
{
switch(keycode) {
case DIK_RETURN:
sprintf(keyStr, "Enter");
break;
case DIK_UP:
sprintf(keyStr, "Up");
break;
case DIK_DOWN:
sprintf(keyStr, "Down");
break;
case DIK_LEFT:
sprintf(keyStr, "Left");
break;
case DIK_RIGHT:
sprintf(keyStr, "Right");
break;
case DIK_HOME:
strcpy(keyStr, "Home");
break;
case DIK_END:
strcpy(keyStr, "End");
break;
case DIK_INSERT:
strcpy(keyStr, "Ins");
break;
case DIK_DELETE:
strcpy(keyStr, "Del");
break;
case DIK_PGUP:
strcpy(keyStr, "PgUp");
break;
case DIK_PGDN:
strcpy(keyStr, "PgDn");
break;
case DIK_NUMPAD0:
strcpy(keyStr, "Num 0");
break;
case DIK_NUMPAD1:
strcpy(keyStr, "Num 1");
break;
case DIK_NUMPAD2:
strcpy(keyStr, "Num 2");
break;
case DIK_NUMPAD3:
strcpy(keyStr, "Num 3");
break;
case DIK_NUMPAD4:
strcpy(keyStr, "Num 4");
break;
case DIK_NUMPAD5:
strcpy(keyStr, "Num 5");
break;
case DIK_NUMPAD6:
strcpy(keyStr, "Num 6");
break;
case DIK_NUMPAD7:
strcpy(keyStr, "Num 7");
break;
case DIK_NUMPAD8:
strcpy(keyStr, "Num 8");
break;
case DIK_NUMPAD9:
strcpy(keyStr, "Num 9");
break;
case DIK_NUMPADSLASH:
strcpy(keyStr, "Num /");
break;
default:
GetKeyNameText(keycode << 16, keyStr, 64);
break;
}
}
HRESULT DInput::Init(HWND hWnd)
{
HRESULT hr;
DWORD dwCoopFlags;
dwCoopFlags = DISCL_FOREGROUND | DISCL_NOWINKEY;
// Create a DInput object
if (FAILED(hr = DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID* *)&g_pDI, NULL)))
{
MessageBox(0, "Direct Input Create Failed", 0, MB_ICONERROR);
return(hr);
}
if (FAILED(hr = g_pDI->CreateDevice(GUID_SysKeyboard, &g_pKeyboard, NULL)))
{
MessageBox(0, "Couldn't access keyboard", 0, MB_ICONERROR);
Free();
return(hr);
}
g_pKeyboard->SetDataFormat(&c_dfDIKeyboard);
g_pKeyboard->SetCooperativeLevel(hWnd, dwCoopFlags);
g_pKeyboard->Acquire();
return(S_OK);
}
void DInput::Free()
{
if (g_pKeyboard)
{
g_pKeyboard->Unacquire();
g_pKeyboard->Release();
g_pKeyboard = 0;
}
if (g_pDI)
{
g_pDI->Release();
g_pDI = 0;
}
}
// Desc: Read the input device's state when in immediate mode and display it.
HRESULT DInput::Read()
{
HRESULT hr;
if (NULL == g_pKeyboard)
{
return(S_OK);
}
// Get the input's device state, and put the state in dims
ZeroMemory(diks, sizeof(diks));
hr = g_pKeyboard->GetDeviceState(sizeof(diks), diks);
//for (int i=0; i<256; i++)
// if (diks[i])MessageBox(0,"DSFJDKSF|",0,0);
if (FAILED(hr))
{
// DirectInput may be telling us that the input stream has been
// interrupted. We aren't tracking any state between polls, so
// we don't have any special reset that needs to be done.
// We just re-acquire and try again.
// If input is lost then acquire and keep trying
hr = g_pKeyboard->Acquire();
while (hr == DIERR_INPUTLOST)
{
hr = g_pKeyboard->Acquire();
}
// hr may be DIERR_OTHERAPPHASPRIO or other errors. This
// may occur when the app is minimized or in the process of
// switching, so just try again later
return(S_OK);
}
return(S_OK);
}

View File

@ -1,44 +0,0 @@
// Copyright (C) 2003-2008 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#ifndef _DIRECTINPUTBASE_H
#define _DIRECTINPUTBASE_H
class DInput
{
public:
DInput();
~DInput();
static void DInput::DIKToString(unsigned int keycode, char *keyStr);
HRESULT Init(HWND hWnd);
void Free();
HRESULT Read();
BYTE diks[256]; // DirectInput keyboard state buffer
private:
LPDIRECTINPUT8 g_pDI; // The DirectInput object
LPDIRECTINPUTDEVICE8 g_pKeyboard; // The keyboard device
};
#endif

View File

@ -21,7 +21,7 @@
#ifdef _WIN32
#include "XInput.h"
#include "../DirectInputBase.h"
#include "../../../../Core/InputCommon/Src/DirectInputBase.h" // Core
DInput m_dinput;
#endif
@ -70,7 +70,7 @@ ConfigDialog::ConfigDialog(wxWindow *parent, wxWindowID id, const wxString &titl
#ifdef _WIN32
m_dinput.Init((HWND)parent);
#endif
clickedButton = NULL;
ClickedButton = NULL;
CreateGUIControls();
Fit();
}
@ -79,28 +79,36 @@ ConfigDialog::~ConfigDialog()
{
}
//////////////////////////////////////////////////////////////////////////////////////////
// Create input button controls
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
inline void AddControl(wxPanel *pan, wxButton **button, wxStaticBoxSizer *sizer,
const char *name, int ctl, int controller)
{
wxBoxSizer *hButton = new wxBoxSizer(wxHORIZONTAL);
char keyStr[10] = {0};
// Add the label
hButton->Add(new wxStaticText(pan, 0, wxString::FromAscii(name),
wxDefaultPosition, wxDefaultSize), 0,
wxALIGN_CENTER_VERTICAL|wxALL);
// Give it the mapped key name
#ifdef _WIN32
DInput::DIKToString(pad[controller].keyForControl[ctl], keyStr);
#elif defined(HAVE_X11) && HAVE_X11
XKeyToString(pad[controller].keyForControl[ctl], keyStr);
#endif
// Add the button to its sizer
*button = new wxButton(pan, ctl, wxString::FromAscii(keyStr),
wxDefaultPosition, wxDefaultSize, wxWANTS_CHARS);
hButton->Add(*button, 0, wxALIGN_RIGHT|wxALL);
sizer->Add(hButton, 0, wxALIGN_RIGHT|wxALL);
}
////////////////////////////////////
void ConfigDialog::CreateGUIControls()
{
@ -307,8 +315,9 @@ void ConfigDialog::OnClose(wxCloseEvent& event)
void ConfigDialog::OnKeyDown(wxKeyEvent& event)
{
if(clickedButton != NULL)
if(ClickedButton != NULL)
{
// Get the selected notebook page
int page = m_Notebook->GetSelection();
#ifdef _WIN32
@ -318,23 +327,45 @@ void ConfigDialog::OnKeyDown(wxKeyEvent& event)
if(m_dinput.diks[i])
{
char keyStr[10] = {0};
pad[page].keyForControl[clickedButton->GetId()] = i;
// Save the mapped key, the wxButtons have the Id 0 to 21
pad[page].keyForControl[ClickedButton->GetId()] = i;
// Get the key name
DInput::DIKToString(i, keyStr);
clickedButton->SetLabel(wxString::FromAscii(keyStr));
ClickedButton->SetLabel(wxString::FromAscii(keyStr));
break;
}
}
#elif defined(HAVE_X11) && HAVE_X11
pad[page].keyForControl[clickedButton->GetId()] = wxCharCodeWXToX(event.GetKeyCode());
clickedButton->SetLabel(wxString::Format(_T("%c"), event.GetKeyCode()));
pad[page].keyForControl[ClickedButton->GetId()] = wxCharCodeWXToX(event.GetKeyCode());
ClickedButton->SetLabel(wxString::Format(_T("%c"), event.GetKeyCode()));
#endif
clickedButton->Disconnect();
ClickedButton->Disconnect();
}
clickedButton = NULL;
// Reset
ClickedButton = NULL;
event.Skip();
}
// We have clicked a button
void ConfigDialog::OnButtonClick(wxCommandEvent& event)
{
// Check if the Space key was set to solve the Space key problem
if (m_dinput.diks[DIK_SPACE]) { m_dinput.diks[DIK_SPACE] = 0; return; }
// If we come here again before any key was set
if(ClickedButton) ClickedButton->SetLabel(oldLabel);
// Save the old button label so we can reapply it if necessary
ClickedButton = (wxButton *)event.GetEventObject();
oldLabel = ClickedButton->GetLabel();
ClickedButton->SetLabel(_("Press Key"));
// Connect EVT_KEY_DOWN to OnKeyDown()
ClickedButton->Connect(wxID_ANY, wxEVT_KEY_DOWN,
wxKeyEventHandler(ConfigDialog::OnKeyDown),
(wxObject*)NULL, this);
}
void ConfigDialog::OnCloseClick(wxCommandEvent& event)
{
Close();
@ -385,20 +416,6 @@ void ConfigDialog::ControllerSettingsChanged(wxCommandEvent& event)
}
}
void ConfigDialog::OnButtonClick(wxCommandEvent& event)
{
if(clickedButton)
{
clickedButton->SetLabel(oldLabel);
}
clickedButton = (wxButton *)event.GetEventObject();
oldLabel = clickedButton->GetLabel();
clickedButton->SetLabel(_("Press Key"));
clickedButton->Connect(wxID_ANY, wxEVT_KEY_DOWN,
wxKeyEventHandler(ConfigDialog::OnKeyDown),
(wxObject*)NULL, this);
}
void ConfigDialog::DllAbout(wxCommandEvent& event)
{
wxString message;

View File

@ -129,7 +129,7 @@ class ConfigDialog : public wxDialog
void DllAbout(wxCommandEvent& event);
int keyPress;
wxButton *clickedButton;
wxButton *ClickedButton;
wxString oldLabel;
};

View File

@ -36,7 +36,7 @@
#ifdef _WIN32
#include "XInput.h"
#include "DirectInputBase.h"
#include "../../../Core/InputCommon/Src/DirectInputBase.h" // Core
DInput dinput;
//#elif defined(USE_SDL) && USE_SDL
@ -615,6 +615,8 @@ void SetDllGlobals(PLUGIN_GLOBALS* _pPluginGlobals) {}
void DllConfig(HWND _hParent)
{
//Console::Open(70, 5000);
LoadConfig();
#ifdef _WIN32
wxWindow win;