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) { int* PadData::getNormalButtons(int port) const
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)
{ {
try { int buttons[PadDataNormalButtonCount];
n = std::stoi(s.ToStdString(), NULL, 16); for (int i = 0; i < PadDataNormalButtonCount; i++)
{
buttons[i] = getNormalButton(port, PadDataNormalButton(i));
} }
catch (std::invalid_argument e) {/*none*/ } return buttons;
catch (std::out_of_range e) {/*none*/ }
} }
void PadData::setNormalButtons(int port, int* buttons)
wxString PadData::serialize()const
{ {
if (!fExistKey)return L""; for (int i = 0; i < PadDataNormalButtonCount; i++)
wxString s = wxString::Format(L"%X", buf[0][0]);
for (int i = 1; i < ArraySize(buf[0]); i++)
{ {
s += wxString::Format(L",%X", buf[0][i]); setNormalButton(port, PadDataNormalButton(i), buttons[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);
} }
} }
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]; wxByte keybit[2];
getKeyBit(keybit, button); getKeyBit(keybit, button);
int pressureByteIndex = getPressureByte(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]; wxByte keybit[2];
getKeyBit(keybit, button); getKeyBit(keybit, button);
int pressureByteIndex = getPressureByte(button); int pressureByteIndex = getPressureByte(button);
@ -176,101 +117,110 @@ int PadData::getNormalButton(int port, wxString button)const
return 0; 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 == UP) { keybit[0] = 0b00010000; keybit[1] = 0b00000000; }
if (button == L"up") { keybit[0] = 0b00010000; keybit[1] = 0b00000000; } else if (button == LEFT) { keybit[0] = 0b10000000; keybit[1] = 0b00000000; }
else if (button == L"left") { keybit[0] = 0b10000000; keybit[1] = 0b00000000; } else if (button == RIGHT) { keybit[0] = 0b00100000; keybit[1] = 0b00000000; }
else if (button == L"right") { keybit[0] = 0b00100000; keybit[1] = 0b00000000; } else if (button == DOWN) { keybit[0] = 0b01000000; keybit[1] = 0b00000000; }
else if (button == L"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 == L"start") { keybit[0] = 0b00001000; keybit[1] = 0b00000000; } else if (button == CROSS) { keybit[0] = 0b00000000; keybit[1] = 0b01000000; }
else if (button == L"select") { keybit[0] = 0b00000001; keybit[1] = 0b00000000; } else if (button == CIRCLE) { keybit[0] = 0b00000000; keybit[1] = 0b00100000; }
else if (button == SQUARE) { keybit[0] = 0b00000000; keybit[1] = 0b10000000; }
else if (button == L"cross") { keybit[0] = 0b00000000; keybit[1] = 0b01000000; } else if (button == TRIANGLE) { keybit[0] = 0b00000000; keybit[1] = 0b00010000; }
else if (button == L"circle") { keybit[0] = 0b00000000; keybit[1] = 0b00100000; } else if (button == L1) { keybit[0] = 0b00000000; keybit[1] = 0b00000100; }
else if (button == L"square") { keybit[0] = 0b00000000; keybit[1] = 0b10000000; } else if (button == L2) { keybit[0] = 0b00000000; keybit[1] = 0b00000001; }
else if (button == L"triangle") { keybit[0] = 0b00000000; keybit[1] = 0b00010000; } else if (button == L3) { keybit[0] = 0b00000010; keybit[1] = 0b00000000; }
else if (button == R1) { keybit[0] = 0b00000000; keybit[1] = 0b00001000; }
else if (button == L"l1") { keybit[0] = 0b00000000; keybit[1] = 0b00000100; } else if (button == R2) { keybit[0] = 0b00000000; keybit[1] = 0b00000010; }
else if (button == L"l2") { keybit[0] = 0b00000000; keybit[1] = 0b00000001; } else if (button == R3) { keybit[0] = 0b00000100; keybit[1] = 0b00000000; }
else if (button == L"l3") { keybit[0] = 0b00000010; keybit[1] = 0b00000000; } else { keybit[0] = 0; keybit[1] = 0; }
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;
}
} }
// just returns an index for the buffer to set the pressure byte // Returns an index for the buffer to set the pressure byte
// returns -1 if it is a button that does not support pressure sensitivty // Returns -1 if it is a button that does not support pressure sensitivty
int PadData::getPressureByte(wxString button)const int PadData::getPressureByte(PadDataNormalButton button) const
{ {
// button order // Pressure Byte Order
// R - L - U - D - Tri - Sqr - Circle - Cross - L1 - R1 - L2 - R2 // R - L - U - D - Tri - Sqr - Circle - Cross - L1 - R1 - L2 - R2
// TODO - switch statement or search array? if (button == UP)
if (button == L"up") { return 2; } return 2;
else if (button == L"left") { return 1; } else if (button == LEFT)
else if (button == L"right") { return 0; } return 1;
else if (button == L"down") { return 3; } else if (button == RIGHT)
return 0;
else if (button == L"cross") { return 6; } else if (button == DOWN)
else if (button == L"circle") { return 5; } return 3;
else if (button == L"square") { return 7; } else if (button == CROSS)
else if (button == L"triangle") { return 4; } return 6;
else if (button == CIRCLE)
else if (button == L"l1") { return 8; } return 5;
else if (button == L"l2") { return 10; } else if (button == SQUARE)
else if (button == L"r1") { return 9; } return 7;
else if (button == L"r2") { return 11; } 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 else
{
return -1; return -1;
}
} }
//===================================== int* PadData::getAnalogVectors(int port) const
// analog key
//=====================================
std::map<wxString, int> PadData::getAnalogKeys(int port)const
{ {
std::map<wxString, int> key; int vectors[PadDataAnalogVectorCount];
for (int i = 0; i < PadDataAnalogKeysSize; i++) 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 (port < 0 || 1 < port)
if (push < 0)push = 0; return;
else if (push > 255)push = 255; if (val < 0)
val = 0;
else if (val > 255)
val = 255;
if (button == L"l_analog_x") { buf[port][4] = push; } buf[port][getAnalogVectorByte(vector)] = val;
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; }
} }
int PadData::getAnalogButton(int port, wxString button)const int PadData::getAnalogVector(int port, PadDataAnalogVector vector) const
{ {
if (port < 0 || 1 < port)return 0; if (port < 0 || 1 < port)
int val = 127; return 0;
if (button == L"l_analog_x") { val = buf[port][4]; }
else if (button == L"l_analog_y") { val = buf[port][5]; } return buf[port][getAnalogVectorByte(vector)];
else if (button == L"r_analog_x") { val = buf[port][2]; } }
else if (button == L"r_analog_y") { val = buf[port][3]; }
return val; // 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> #include <map>
#define PadDataNormalKeysSize 16 #define PadDataNormalButtonCount 16
const wxString PadDataNormalKeys[PadDataNormalKeysSize] = enum PadDataNormalButton
{ {
"up", UP,
"right", RIGHT,
"left", LEFT,
"down", DOWN,
"cross", CROSS,
"circle", CIRCLE,
"square", SQUARE,
"triangle", TRIANGLE,
"l1", L1,
"l2", L2,
"r1", R1,
"r2", R2,
"l3", L3,
"r3", R3,
"select", SELECT,
"start" START
}; };
#define PadDataAnalogKeysSize 4 #define PadDataAnalogVectorCount 4
const wxString PadDataAnalogKeys[PadDataAnalogKeysSize] = enum PadDataAnalogVector
{ {
"l_analog_x", LEFT_ANALOG_X,
"l_analog_y", LEFT_ANALOG_Y,
"r_analog_x", RIGHT_ANALOG_X,
"r_analog_y" RIGHT_ANALOG_Y
}; };
//----------------------------
// Pad info
//----------------------------
struct PadData struct PadData
{ {
public: public:
PadData(); PadData();
~PadData() {} ~PadData() {}
public:
bool fExistKey = false; bool fExistKey = false;
u8 buf[2][18]; u8 buf[2][18];
public:
// Prints controlller data every frame to the Controller Log filter, disabled by default // Prints controlller data every frame to the Controller Log filter, disabled by default
static void logPadData(u8 port, u16 bufCount, u8 buf[512]); static void logPadData(u8 port, u16 bufCount, u8 buf[512]);
wxString serialize()const;
void deserialize(wxString s);
//------------------------------------------ // Normal Buttons
// normalKey int* getNormalButtons(int port) const;
//------------------------------------------ void setNormalButtons(int port, int* buttons);
std::map<wxString, int> getNormalKeys(int port)const;
void setNormalKeys(int port, std::map<wxString, int> key);
//------------------------------------------ // Analog Vectors
// analogKey 0~255
// max left/up : 0 // max left/up : 0
// neutral : 127 // neutral : 127
// max right/down : 255 // max right/down : 255
//------------------------------------------ int* getAnalogVectors(int port) const;
std::map<wxString, int> getAnalogKeys(int port)const; // max left/up : 0
void setAnalogKeys(int port, std::map<wxString, int> key); // neutral : 127
// max right/down : 255
void setAnalogVectors(int port, int* vector);
private: private:
void setNormalButton(int port, wxString button, int pressure); void setNormalButton(int port, PadDataNormalButton button, int pressure);
int getNormalButton(int port, wxString button)const; int getNormalButton(int port, PadDataNormalButton button) const;
void getKeyBit(wxByte keybit[2], wxString button)const; void getKeyBit(wxByte keybit[2], PadDataNormalButton button) const;
int getPressureByte(wxString button)const; int getPressureByte(PadDataNormalButton button) const;
void setAnalogButton(int port, wxString button, int push);
int getAnalogButton(int port, wxString 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); int* buttons = pad.getNormalButtons(port);
normalKeys.at(button) = pressure; buttons[button] = pressure;
pad.setNormalKeys(port, normalKeys); 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); int* vectors = pad.getAnalogVectors(port);
analogKeys.at(key) = value; vectors[vector] = value;
pad.setAnalogKeys(port, analogKeys); pad.setAnalogVectors(port, vectors);
} }
void RecordingInputManager::SetVirtualPadReading(int port, bool read) void RecordingInputManager::SetVirtualPadReading(int port, bool read)

View File

@ -8,17 +8,15 @@ public:
RecordingInputManager(); RecordingInputManager();
void ControllerInterrupt(u8 &data, u8 &port, u16 & BufCount, u8 buf[]); void ControllerInterrupt(u8 &data, u8 &port, u16 & BufCount, u8 buf[]);
// Handles normal keys // Handles normal keys
void SetButtonState(int port, wxString button, int pressure); void SetButtonState(int port, PadDataNormalButton button, int pressure);
// Handles analog sticks // 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); void SetVirtualPadReading(int port, bool read);
protected: protected:
PadData pad; PadData pad;
bool virtualPad[2]; bool virtualPad[2];
}; };
extern RecordingInputManager g_RecordingInput; extern RecordingInputManager g_RecordingInput;

View File

@ -155,7 +155,7 @@ void VirtualPad::OnButtonPress(wxCommandEvent & event)
pressure = 255; 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()) { if (event.IsChecked()) {
pressure = buttonsPressure[spinnerId]->GetValue(); 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); analogVals[sliderId]->SetValue(event.GetInt() * -1);
} }
g_RecordingInput.UpdateAnalog(controllerPort, PadDataAnalogKeys[sliderId], event.GetInt() + 127); g_RecordingInput.UpdateAnalog(controllerPort, PadDataAnalogVector(sliderId), event.GetInt() + 127);
} }
} }
@ -218,15 +218,9 @@ void VirtualPad::OnAnalogValChange(wxSpinEvent & event)
} }
} }
if (spinnerId != -1) if (spinnerId != -1)
{
if (spinnerId % 2 == 0)
{ {
analogVals[spinnerId]->SetValue(event.GetInt()); analogVals[spinnerId]->SetValue(event.GetInt());
} g_RecordingInput.UpdateAnalog(controllerPort, PadDataAnalogVector(spinnerId), event.GetInt() + 127);
else {
analogVals[spinnerId]->SetValue(event.GetInt() * -1);
}
g_RecordingInput.UpdateAnalog(controllerPort, PadDataAnalogKeys[spinnerId], event.GetInt() + 127);
} }
} }