JS joypad API improvements.

This commit is contained in:
harry 2024-02-14 06:28:26 -05:00
parent 446763b232
commit f85f93c5bd
2 changed files with 60 additions and 64 deletions

View File

@ -102,7 +102,7 @@ ColorScriptObject::~ColorScriptObject()
//---------------------------------------------------- //----------------------------------------------------
int JoypadScriptObject::numInstances = 0; int JoypadScriptObject::numInstances = 0;
JoypadScriptObject::JoypadScriptObject(int playerIdx) JoypadScriptObject::JoypadScriptObject(int playerIdx, bool immediate)
: QObject() : QObject()
{ {
numInstances++; numInstances++;
@ -110,7 +110,20 @@ JoypadScriptObject::JoypadScriptObject(int playerIdx)
moveToThread(QApplication::instance()->thread()); moveToThread(QApplication::instance()->thread());
if ( (playerIdx < 0) || (playerIdx >= MAX_JOYPAD_PLAYERS) )
{
QString msg = "Error: Joypad player index (" + QString::number(playerIdx) + ") is out of bounds!\n";
auto* engine = FCEU::JSEngine::getCurrent();
if (engine != nullptr)
{
engine->throwError(QJSValue::RangeError, msg);
}
playerIdx = 0;
}
player = playerIdx; player = playerIdx;
refreshData(immediate);
} }
//---------------------------------------------------- //----------------------------------------------------
JoypadScriptObject::~JoypadScriptObject() JoypadScriptObject::~JoypadScriptObject()
@ -119,33 +132,35 @@ JoypadScriptObject::~JoypadScriptObject()
//printf("JoypadScriptObject %p Destructor: %i\n", this, numInstances); //printf("JoypadScriptObject %p Destructor: %i\n", this, numInstances);
} }
//---------------------------------------------------- //----------------------------------------------------
void JoypadScriptObject::readData() void JoypadScriptObject::refreshData(bool immediate)
{ {
uint8_t buttons = joy[player]; if (immediate)
{
uint32_t gpData = GetGamepadPressedImmediate();
uint8_t buttons = gpData >> (player * 8);
a = (buttons & 0x01) ? true : false; a = (buttons & 0x01) ? true : false;
b = (buttons & 0x02) ? true : false; b = (buttons & 0x02) ? true : false;
select = (buttons & 0x04) ? true : false; select = (buttons & 0x04) ? true : false;
start = (buttons & 0x08) ? true : false; start = (buttons & 0x08) ? true : false;
up = (buttons & 0x10) ? true : false; up = (buttons & 0x10) ? true : false;
down = (buttons & 0x20) ? true : false; down = (buttons & 0x20) ? true : false;
left = (buttons & 0x40) ? true : false; left = (buttons & 0x40) ? true : false;
right = (buttons & 0x80) ? true : false; right = (buttons & 0x80) ? true : false;
} }
//---------------------------------------------------- else
void JoypadScriptObject::readDataPhy() {
{ uint8_t buttons = joy[player];
uint32_t gpData = GetGamepadPressedImmediate();
uint8_t buttons = gpData >> (player * 8);
a = (buttons & 0x01) ? true : false; a = (buttons & 0x01) ? true : false;
b = (buttons & 0x02) ? true : false; b = (buttons & 0x02) ? true : false;
select = (buttons & 0x04) ? true : false; select = (buttons & 0x04) ? true : false;
start = (buttons & 0x08) ? true : false; start = (buttons & 0x08) ? true : false;
up = (buttons & 0x10) ? true : false; up = (buttons & 0x10) ? true : false;
down = (buttons & 0x20) ? true : false; down = (buttons & 0x20) ? true : false;
left = (buttons & 0x40) ? true : false; left = (buttons & 0x40) ? true : false;
right = (buttons & 0x80) ? true : false; right = (buttons & 0x80) ? true : false;
}
} }
//---------------------------------------------------- //----------------------------------------------------
//---- EMU State Object //---- EMU State Object
@ -844,17 +859,6 @@ InputScriptObject::InputScriptObject(QObject* parent)
{ {
script = qobject_cast<QtScriptInstance*>(parent); script = qobject_cast<QtScriptInstance*>(parent);
engine = script->getEngine(); engine = script->getEngine();
for (int i=0; i<MAX_NUM_JOYPADS; i++)
{
joypad[i].qObj = new JoypadScriptObject(i);
joypad[i].jsObj = engine->newQObject( joypad[i].qObj );
//#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
// QJSEngine::setObjectOwnership( joypad[i].qObj, QJSEngine::CppScriptOwnership);
//#endif
}
} }
//---------------------------------------------------- //----------------------------------------------------
InputScriptObject::~InputScriptObject() InputScriptObject::~InputScriptObject()
@ -863,21 +867,15 @@ InputScriptObject::~InputScriptObject()
//---------------------------------------------------- //----------------------------------------------------
QJSValue InputScriptObject::readJoypad(int player, bool immediate) QJSValue InputScriptObject::readJoypad(int player, bool immediate)
{ {
if ( (player < 0) || (player >= MAX_NUM_JOYPADS) ) JoypadScriptObject* joypadObj = new JoypadScriptObject(player, immediate);
{
QString msg = "Error: Joypad player index (" + QString::number(player) + ") is out of bounds!\n"; QJSValue jsObj = engine->newQObject( joypadObj );
engine->throwError(QJSValue::RangeError, msg);
player = 0; #if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
} QJSEngine::setObjectOwnership( joypadObj, QJSEngine::JavaScriptOwnership);
if (immediate) #endif
{
joypad[player].qObj->readDataPhy(); return jsObj;
}
else
{
joypad[player].qObj->readData();
}
return joypad[player].jsObj;
} }
//---------------------------------------------------- //----------------------------------------------------
//---- Memory Script Object //---- Memory Script Object
@ -1490,6 +1488,9 @@ int QtScriptInstance::initEngine()
QJSValue jsColorMetaObject = engine->newQMetaObject(&JS::ColorScriptObject::staticMetaObject); QJSValue jsColorMetaObject = engine->newQMetaObject(&JS::ColorScriptObject::staticMetaObject);
engine->globalObject().setProperty("Color", jsColorMetaObject); engine->globalObject().setProperty("Color", jsColorMetaObject);
QJSValue jsJoypadMetaObject = engine->newQMetaObject(&JS::JoypadScriptObject::staticMetaObject);
engine->globalObject().setProperty("Joypad", jsJoypadMetaObject);
QJSValue jsEmuStateMetaObject = engine->newQMetaObject(&JS::EmuStateScriptObject::staticMetaObject); QJSValue jsEmuStateMetaObject = engine->newQMetaObject(&JS::EmuStateScriptObject::staticMetaObject);
engine->globalObject().setProperty("EmuState", jsEmuStateMetaObject); engine->globalObject().setProperty("EmuState", jsEmuStateMetaObject);

View File

@ -116,14 +116,12 @@ class JoypadScriptObject: public QObject
Q_PROPERTY(bool start READ getStart) Q_PROPERTY(bool start READ getStart)
Q_PROPERTY(bool a READ getA) Q_PROPERTY(bool a READ getA)
Q_PROPERTY(bool b READ getB) Q_PROPERTY(bool b READ getB)
Q_PROPERTY(int player READ getPlayer) Q_PROPERTY(int player READ getPlayer WRITE setPlayer)
public: public:
JoypadScriptObject(int playerIdx); Q_INVOKABLE JoypadScriptObject(int playerIdx = 0, bool immediate = false);
~JoypadScriptObject(); ~JoypadScriptObject();
void readData(); static constexpr int MAX_JOYPAD_PLAYERS = 4;
void readDataPhy();
private: private:
bool up = false; bool up = false;
bool down = false; bool down = false;
@ -137,6 +135,7 @@ private:
static int numInstances; static int numInstances;
public slots: public slots:
Q_INVOKABLE void refreshData(bool immediate = false);
Q_INVOKABLE bool getUp(){ return up; } Q_INVOKABLE bool getUp(){ return up; }
Q_INVOKABLE bool getDown(){ return down; } Q_INVOKABLE bool getDown(){ return down; }
Q_INVOKABLE bool getLeft(){ return left; } Q_INVOKABLE bool getLeft(){ return left; }
@ -145,7 +144,9 @@ public slots:
Q_INVOKABLE bool getStart(){ return start; } Q_INVOKABLE bool getStart(){ return start; }
Q_INVOKABLE bool getA(){ return a; } Q_INVOKABLE bool getA(){ return a; }
Q_INVOKABLE bool getB(){ return b; } Q_INVOKABLE bool getB(){ return b; }
Q_INVOKABLE int maxPlayers(){ return MAX_JOYPAD_PLAYERS; }
Q_INVOKABLE int getPlayer(){ return player; } Q_INVOKABLE int getPlayer(){ return player; }
Q_INVOKABLE void setPlayer(int newPlayerIdx){ player = newPlayerIdx; }
}; };
class EmuStateScriptObject: public QObject class EmuStateScriptObject: public QObject
@ -352,20 +353,14 @@ public:
void setEngine(FCEU::JSEngine* _engine){ engine = _engine; } void setEngine(FCEU::JSEngine* _engine){ engine = _engine; }
void setDialog(QScriptDialog_t* _dialog){ dialog = _dialog; } void setDialog(QScriptDialog_t* _dialog){ dialog = _dialog; }
static constexpr int MAX_NUM_JOYPADS = 4;
private: private:
FCEU::JSEngine* engine = nullptr; FCEU::JSEngine* engine = nullptr;
QScriptDialog_t* dialog = nullptr; QScriptDialog_t* dialog = nullptr;
QtScriptInstance* script = nullptr; QtScriptInstance* script = nullptr;
struct
{
JoypadScriptObject* qObj;
QJSValue jsObj;
} joypad[MAX_NUM_JOYPADS];
public slots: public slots:
Q_INVOKABLE QJSValue readJoypad(int player, bool immediate = false); Q_INVOKABLE QJSValue readJoypad(int player = 0, bool immediate = false);
Q_INVOKABLE int maxJoypadPlayers(){ return JoypadScriptObject::MAX_JOYPAD_PLAYERS; }
}; };
} // JS } // JS