recording: Significant refactor on VirtualPad implementation

This commit is contained in:
Tyler Wilding 2018-11-21 23:59:17 -05:00 committed by lightningterror
parent d7074503d8
commit 270f7fd905
5 changed files with 157 additions and 226 deletions

View File

@ -39,87 +39,27 @@ void PadData::logPadData(u8 port, u16 bufCount, u8 buf[512]) {
}
}
std::vector<wxString> split(const wxString &s, char delim) {
std::vector<wxString> elems;
wxString item;
for (char ch : s) {
if (ch == delim) {
if (!item.empty())
elems.push_back(item);
item.clear();
}
else {
item += ch;
}
}
if (!item.empty())
elems.push_back(item);
return elems;
}
void deserializeConvert(u8 & n, wxString s)
int* PadData::getNormalButtons(int port) const
{
try {
n = std::stoi(s.ToStdString(), NULL, 16);
int buttons[PadDataNormalButtonCount];
for (int i = 0; i < PadDataNormalButtonCount; i++)
{
buttons[i] = getNormalButton(port, PadDataNormalButton(i));
}
catch (std::invalid_argument e) {/*none*/ }
catch (std::out_of_range e) {/*none*/ }
return buttons;
}
wxString PadData::serialize()const
void PadData::setNormalButtons(int port, int* buttons)
{
if (!fExistKey)return L"";
wxString s = wxString::Format(L"%X", buf[0][0]);
for (int i = 1; i < ArraySize(buf[0]); i++)
for (int i = 0; i < PadDataNormalButtonCount; i++)
{
s += wxString::Format(L",%X", buf[0][i]);
}
for (int i = 0; i < ArraySize(buf[1]); i++)
{
s += wxString::Format(L",%X", buf[1][i]);
}
return s;
}
void PadData::deserialize(wxString s)
{
std::vector<wxString> v = split(s, L',');
if (v.size() != 12)return;
for (int i = 0; i < 6; i++)
{
deserializeConvert(buf[0][i], v[i]);
}
for (int i = 0; i < 6; i++)
{
deserializeConvert(buf[1][i], v[6 + i]);
}
fExistKey = true;
}
//=====================================
// normal key
//=====================================
std::map<wxString, int> PadData::getNormalKeys(int port)const
{
std::map<wxString, int> key;
for (int i = 0; i < PadDataNormalKeysSize; i++)
{
key.insert(std::map<wxString, int>::value_type(PadDataNormalKeys[i], getNormalButton(port, PadDataNormalKeys[i])));
}
return key;
}
void PadData::setNormalKeys(int port, std::map<wxString, int> key)
{
for (auto it = key.begin(); it != key.end(); ++it)
{
setNormalButton(port, it->first, it->second);
setNormalButton(port, PadDataNormalButton(i), buttons[i]);
}
}
void PadData::setNormalButton(int port, wxString button, int fpushed)
void PadData::setNormalButton(int port, PadDataNormalButton button, int fpushed)
{
if (port < 0 || 1 < port)return;
if (port < 0 || 1 < port)
return;
wxByte keybit[2];
getKeyBit(keybit, button);
int pressureByteIndex = getPressureByte(button);
@ -149,9 +89,10 @@ void PadData::setNormalButton(int port, wxString button, int fpushed)
}
}
int PadData::getNormalButton(int port, wxString button)const
int PadData::getNormalButton(int port, PadDataNormalButton button) const
{
if (port < 0 || 1 < port)return false;
if (port < 0 || 1 < port)
return false;
wxByte keybit[2];
getKeyBit(keybit, button);
int pressureByteIndex = getPressureByte(button);
@ -176,101 +117,110 @@ int PadData::getNormalButton(int port, wxString button)const
return 0;
}
void PadData::getKeyBit(wxByte keybit[2], wxString button)const
void PadData::getKeyBit(wxByte keybit[2], PadDataNormalButton button) const
{
// TODO - switch statement or search array?
if (button == L"up") { keybit[0] = 0b00010000; keybit[1] = 0b00000000; }
else if (button == L"left") { keybit[0] = 0b10000000; keybit[1] = 0b00000000; }
else if (button == L"right") { keybit[0] = 0b00100000; keybit[1] = 0b00000000; }
else if (button == L"down") { keybit[0] = 0b01000000; keybit[1] = 0b00000000; }
else if (button == L"start") { keybit[0] = 0b00001000; keybit[1] = 0b00000000; }
else if (button == L"select") { keybit[0] = 0b00000001; keybit[1] = 0b00000000; }
else if (button == L"cross") { keybit[0] = 0b00000000; keybit[1] = 0b01000000; }
else if (button == L"circle") { keybit[0] = 0b00000000; keybit[1] = 0b00100000; }
else if (button == L"square") { keybit[0] = 0b00000000; keybit[1] = 0b10000000; }
else if (button == L"triangle") { keybit[0] = 0b00000000; keybit[1] = 0b00010000; }
else if (button == L"l1") { keybit[0] = 0b00000000; keybit[1] = 0b00000100; }
else if (button == L"l2") { keybit[0] = 0b00000000; keybit[1] = 0b00000001; }
else if (button == L"l3") { keybit[0] = 0b00000010; keybit[1] = 0b00000000; }
else if (button == L"r1") { keybit[0] = 0b00000000; keybit[1] = 0b00001000; }
else if (button == L"r2") { keybit[0] = 0b00000000; keybit[1] = 0b00000010; }
else if (button == L"r3") { keybit[0] = 0b00000100; keybit[1] = 0b00000000; }
else
{
keybit[0] = 0;
keybit[1] = 0;
}
if (button == UP) { keybit[0] = 0b00010000; keybit[1] = 0b00000000; }
else if (button == LEFT) { keybit[0] = 0b10000000; keybit[1] = 0b00000000; }
else if (button == RIGHT) { keybit[0] = 0b00100000; keybit[1] = 0b00000000; }
else if (button == DOWN) { keybit[0] = 0b01000000; keybit[1] = 0b00000000; }
else if (button == START) { keybit[0] = 0b00001000; keybit[1] = 0b00000000; }
else if (button == SELECT) { keybit[0] = 0b00000001; keybit[1] = 0b00000000; }
else if (button == CROSS) { keybit[0] = 0b00000000; keybit[1] = 0b01000000; }
else if (button == CIRCLE) { keybit[0] = 0b00000000; keybit[1] = 0b00100000; }
else if (button == SQUARE) { keybit[0] = 0b00000000; keybit[1] = 0b10000000; }
else if (button == TRIANGLE) { keybit[0] = 0b00000000; keybit[1] = 0b00010000; }
else if (button == L1) { keybit[0] = 0b00000000; keybit[1] = 0b00000100; }
else if (button == L2) { keybit[0] = 0b00000000; keybit[1] = 0b00000001; }
else if (button == L3) { keybit[0] = 0b00000010; keybit[1] = 0b00000000; }
else if (button == R1) { keybit[0] = 0b00000000; keybit[1] = 0b00001000; }
else if (button == R2) { keybit[0] = 0b00000000; keybit[1] = 0b00000010; }
else if (button == R3) { keybit[0] = 0b00000100; keybit[1] = 0b00000000; }
else { keybit[0] = 0; keybit[1] = 0; }
}
// just returns an index for the buffer to set the pressure byte
// returns -1 if it is a button that does not support pressure sensitivty
int PadData::getPressureByte(wxString button)const
// Returns an index for the buffer to set the pressure byte
// Returns -1 if it is a button that does not support pressure sensitivty
int PadData::getPressureByte(PadDataNormalButton button) const
{
// button order
// Pressure Byte Order
// R - L - U - D - Tri - Sqr - Circle - Cross - L1 - R1 - L2 - R2
// TODO - switch statement or search array?
if (button == L"up") { return 2; }
else if (button == L"left") { return 1; }
else if (button == L"right") { return 0; }
else if (button == L"down") { return 3; }
else if (button == L"cross") { return 6; }
else if (button == L"circle") { return 5; }
else if (button == L"square") { return 7; }
else if (button == L"triangle") { return 4; }
else if (button == L"l1") { return 8; }
else if (button == L"l2") { return 10; }
else if (button == L"r1") { return 9; }
else if (button == L"r2") { return 11; }
if (button == UP)
return 2;
else if (button == LEFT)
return 1;
else if (button == RIGHT)
return 0;
else if (button == DOWN)
return 3;
else if (button == CROSS)
return 6;
else if (button == CIRCLE)
return 5;
else if (button == SQUARE)
return 7;
else if (button == TRIANGLE)
return 4;
else if (button == L1)
return 8;
else if (button == L2)
return 10;
else if (button == R1)
return 9;
else if (button == R2)
return 11;
else
{
return -1;
}
}
//=====================================
// analog key
//=====================================
std::map<wxString, int> PadData::getAnalogKeys(int port)const
int* PadData::getAnalogVectors(int port) const
{
std::map<wxString, int> key;
for (int i = 0; i < PadDataAnalogKeysSize; i++)
int vectors[PadDataAnalogVectorCount];
for (int i = 0; i < PadDataAnalogVectorCount; i++)
{
key.insert(std::map<wxString, int>::value_type(PadDataAnalogKeys[i], getAnalogButton(port, PadDataAnalogKeys[i])));
vectors[i] = getAnalogVector(port, PadDataAnalogVector(i));
}
return key;
return vectors;
}
void PadData::setAnalogKeys(int port, std::map<wxString, int> key)
void PadData::setAnalogVectors(int port, int* vectors)
{
for (auto it = key.begin(); it != key.end(); ++it)
for (int i = 0; i < PadDataAnalogVectorCount; i++)
{
setAnalogButton(port, it->first, it->second);
setAnalogVector(port, PadDataAnalogVector(i), vectors[i]);
}
}
void PadData::setAnalogButton(int port, wxString button, int push)
void PadData::setAnalogVector(int port, PadDataAnalogVector vector, int val)
{
if (port < 0 || 1 < port)return;
if (push < 0)push = 0;
else if (push > 255)push = 255;
if (port < 0 || 1 < port)
return;
if (val < 0)
val = 0;
else if (val > 255)
val = 255;
if (button == L"l_analog_x") { buf[port][4] = push; }
else if (button == L"l_analog_y") { buf[port][5] = push; }
else if (button == L"r_analog_x") { buf[port][2] = push; }
else if (button == L"r_analog_y") { buf[port][3] = push; }
buf[port][getAnalogVectorByte(vector)] = val;
}
int PadData::getAnalogButton(int port, wxString button)const
int PadData::getAnalogVector(int port, PadDataAnalogVector vector) const
{
if (port < 0 || 1 < port)return 0;
int val = 127;
if (button == L"l_analog_x") { val = buf[port][4]; }
else if (button == L"l_analog_y") { val = buf[port][5]; }
else if (button == L"r_analog_x") { val = buf[port][2]; }
else if (button == L"r_analog_y") { val = buf[port][3]; }
return val;
if (port < 0 || 1 < port)
return 0;
return buf[port][getAnalogVectorByte(vector)];
}
// Returns an index for the buffer to set the analog's vector
int PadData::getAnalogVectorByte(PadDataAnalogVector vector) const
{
// Vector Byte Ordering
// RX - RY - LX - LY
if (vector == LEFT_ANALOG_X)
return 4;
else if (vector == LEFT_ANALOG_Y)
return 5;
else if (vector == RIGHT_ANALOG_X)
return 2;
else
return 3;
}

View File

@ -2,80 +2,69 @@
#include <map>
#define PadDataNormalKeysSize 16
const wxString PadDataNormalKeys[PadDataNormalKeysSize] =
#define PadDataNormalButtonCount 16
enum PadDataNormalButton
{
"up",
"right",
"left",
"down",
"cross",
"circle",
"square",
"triangle",
"l1",
"l2",
"r1",
"r2",
"l3",
"r3",
"select",
"start"
UP,
RIGHT,
LEFT,
DOWN,
CROSS,
CIRCLE,
SQUARE,
TRIANGLE,
L1,
L2,
R1,
R2,
L3,
R3,
SELECT,
START
};
#define PadDataAnalogKeysSize 4
const wxString PadDataAnalogKeys[PadDataAnalogKeysSize] =
#define PadDataAnalogVectorCount 4
enum PadDataAnalogVector
{
"l_analog_x",
"l_analog_y",
"r_analog_x",
"r_analog_y"
LEFT_ANALOG_X,
LEFT_ANALOG_Y,
RIGHT_ANALOG_X,
RIGHT_ANALOG_Y
};
//----------------------------
// Pad info
//----------------------------
struct PadData
{
public:
PadData();
~PadData() {}
public:
bool fExistKey = false;
u8 buf[2][18];
public:
// Prints controlller data every frame to the Controller Log filter, disabled by default
static void logPadData(u8 port, u16 bufCount, u8 buf[512]);
wxString serialize()const;
void deserialize(wxString s);
//------------------------------------------
// normalKey
//------------------------------------------
std::map<wxString, int> getNormalKeys(int port)const;
void setNormalKeys(int port, std::map<wxString, int> key);
//------------------------------------------
// analogKey 0~255
// max left/up : 0
// neutral : 127
// max right/down : 255
//------------------------------------------
std::map<wxString, int> getAnalogKeys(int port)const;
void setAnalogKeys(int port, std::map<wxString, int> key);
// Normal Buttons
int* getNormalButtons(int port) const;
void setNormalButtons(int port, int* buttons);
// Analog Vectors
// max left/up : 0
// neutral : 127
// max right/down : 255
int* getAnalogVectors(int port) const;
// max left/up : 0
// neutral : 127
// max right/down : 255
void setAnalogVectors(int port, int* vector);
private:
void setNormalButton(int port, wxString button, int pressure);
int getNormalButton(int port, wxString button)const;
void getKeyBit(wxByte keybit[2], wxString button)const;
int getPressureByte(wxString button)const;
void setAnalogButton(int port, wxString button, int push);
int getAnalogButton(int port, wxString button)const;
void setNormalButton(int port, PadDataNormalButton button, int pressure);
int getNormalButton(int port, PadDataNormalButton button) const;
void getKeyBit(wxByte keybit[2], PadDataNormalButton button) const;
int getPressureByte(PadDataNormalButton button) const;
void setAnalogVector(int port, PadDataAnalogVector vector, int val);
int getAnalogVector(int port, PadDataAnalogVector vector) const;
int getAnalogVectorByte(PadDataAnalogVector vector) const;
};

View File

@ -40,18 +40,18 @@ void RecordingInputManager::ControllerInterrupt(u8 & data, u8 & port, u16 & BufC
}
}
void RecordingInputManager::SetButtonState(int port, wxString button, int pressure)
void RecordingInputManager::SetButtonState(int port, PadDataNormalButton button, int pressure)
{
auto normalKeys = pad.getNormalKeys(port);
normalKeys.at(button) = pressure;
pad.setNormalKeys(port, normalKeys);
int* buttons = pad.getNormalButtons(port);
buttons[button] = pressure;
pad.setNormalButtons(port, buttons);
}
void RecordingInputManager::UpdateAnalog(int port, wxString key, int value)
void RecordingInputManager::UpdateAnalog(int port, PadDataAnalogVector vector, int value)
{
auto analogKeys = pad.getAnalogKeys(port);
analogKeys.at(key) = value;
pad.setAnalogKeys(port, analogKeys);
int* vectors = pad.getAnalogVectors(port);
vectors[vector] = value;
pad.setAnalogVectors(port, vectors);
}
void RecordingInputManager::SetVirtualPadReading(int port, bool read)

View File

@ -8,17 +8,15 @@ public:
RecordingInputManager();
void ControllerInterrupt(u8 &data, u8 &port, u16 & BufCount, u8 buf[]);
// Handles normal keys
void SetButtonState(int port, wxString button, int pressure);
void SetButtonState(int port, PadDataNormalButton button, int pressure);
// Handles analog sticks
void UpdateAnalog(int port, wxString key, int value);
void UpdateAnalog(int port, PadDataAnalogVector vector, int value);
void SetVirtualPadReading(int port, bool read);
protected:
PadData pad;
bool virtualPad[2];
};
extern RecordingInputManager g_RecordingInput;

View File

@ -155,7 +155,7 @@ void VirtualPad::OnButtonPress(wxCommandEvent & event)
pressure = 255;
}
}
g_RecordingInput.SetButtonState(controllerPort, PadDataNormalKeys[buttonId], pressure);
g_RecordingInput.SetButtonState(controllerPort, PadDataNormalButton(buttonId), pressure);
}
}
@ -177,7 +177,7 @@ void VirtualPad::OnPressureChange(wxSpinEvent & event)
if (event.IsChecked()) {
pressure = buttonsPressure[spinnerId]->GetValue();
}
g_RecordingInput.SetButtonState(controllerPort, PadDataNormalKeys[spinnerId], pressure);
g_RecordingInput.SetButtonState(controllerPort, PadDataNormalButton(spinnerId), pressure);
}
}
@ -202,7 +202,7 @@ void VirtualPad::OnAnalogSliderChange(wxCommandEvent & event)
analogVals[sliderId]->SetValue(event.GetInt() * -1);
}
g_RecordingInput.UpdateAnalog(controllerPort, PadDataAnalogKeys[sliderId], event.GetInt() + 127);
g_RecordingInput.UpdateAnalog(controllerPort, PadDataAnalogVector(sliderId), event.GetInt() + 127);
}
}
@ -219,14 +219,8 @@ void VirtualPad::OnAnalogValChange(wxSpinEvent & event)
}
if (spinnerId != -1)
{
if (spinnerId % 2 == 0)
{
analogVals[spinnerId]->SetValue(event.GetInt());
}
else {
analogVals[spinnerId]->SetValue(event.GetInt() * -1);
}
g_RecordingInput.UpdateAnalog(controllerPort, PadDataAnalogKeys[spinnerId], event.GetInt() + 127);
analogVals[spinnerId]->SetValue(event.GetInt());
g_RecordingInput.UpdateAnalog(controllerPort, PadDataAnalogVector(spinnerId), event.GetInt() + 127);
}
}